[win-linux] update manager: add package size determination

This commit is contained in:
SimplestStudio
2023-10-23 13:17:50 +03:00
parent 364c3d5f53
commit c4e9759663
9 changed files with 100 additions and 5 deletions

View File

@ -67,7 +67,8 @@ enum MsgCommands {
MSG_ClearTempFiles,
MSG_Progress,
MSG_StopDownload,
MSG_OtherError
MSG_OtherError,
MSG_RequestContentLenght
};
class CSocket

View File

@ -202,6 +202,9 @@ void CSvcManager::aboutToQuit(FnVoidVoid callback)
void CSvcManager::init()
{
m_pDownloader->onQueryResponse([=](int error, int lenght) {
onQueryResponse(error, lenght);
});
m_pDownloader->onComplete([=](int error) {
onCompleteSlot(error, m_pDownloader->GetFilePath());
});
@ -235,6 +238,13 @@ void CSvcManager::init()
NS_Logger::WriteLog(_T("Received MSG_LoadUpdates, URL: ") + params[1]);
break;
}
case MSG_RequestContentLenght: {
__GLOBAL_LOCK
if (m_pDownloader)
m_pDownloader->queryContentLenght(params[1]);
NS_Logger::WriteLog(_T("Received MSG_RequestContentLenght, URL: ") + params[1]);
break;
}
case MSG_StopDownload: {
m_downloadMode = Mode::CHECK_UPDATES;
if (m_pDownloader)
@ -273,6 +283,12 @@ void CSvcManager::init()
});
}
void CSvcManager::onQueryResponse(const int error, const int lenght)
{
__UNLOCK
m_socket->sendMessage(MSG_RequestContentLenght, (error == 0) ? to_tstring(lenght) : _T(""));
}
void CSvcManager::onCompleteUnzip(const int error)
{
__UNLOCK

View File

@ -58,6 +58,7 @@ public:
private:
void init();
void onQueryResponse(const int error, const int lenght);
void onCompleteUnzip(const int error);
void onCompleteSlot(const int error, const tstring &filePath);
void onProgressSlot(const int percent);

View File

@ -43,6 +43,7 @@ public:
~CDownloaderPrivate()
{}
FnVoidIntInt m_query_callback = nullptr;
FnVoidInt m_complete_callback = nullptr,
m_progress_callback = nullptr;
string m_url,
@ -85,6 +86,32 @@ CDownloader::~CDownloader()
delete pimpl, pimpl = nullptr;
}
void CDownloader::queryContentLenght(const string &url)
{
if (url.empty() || pimpl->m_lock)
return;
pimpl->m_lock = true;
pimpl->m_future = std::async(std::launch::async, [=]() {
double fileSize = 0;
CURLcode res = CURLE_FAILED_INIT;
if (CURL *curl = curl_easy_init()) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
res = curl_easy_perform(curl);
if (res == CURLE_OK)
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fileSize);
curl_easy_cleanup(curl);
}
int error = (res == CURLE_OK) ? 0 :
(res == CURLE_HTTP_RETURNED_ERROR) ? -2 : -3;
if (pimpl->m_query_callback)
pimpl->m_query_callback(error, (int)fileSize);
pimpl->m_lock = false;
});
}
void CDownloader::downloadFile(const string &url, const string &filePath)
{
pimpl->m_url.clear();
@ -150,6 +177,11 @@ string CDownloader::GetFilePath()
return pimpl->m_filePath;
}
void CDownloader::onQueryResponse(FnVoidIntInt callback)
{
pimpl->m_query_callback = callback;
}
void CDownloader::onComplete(FnVoidInt callback)
{
pimpl->m_complete_callback = callback;

View File

@ -37,6 +37,7 @@
#include <functional>
typedef std::function<void(int)> FnVoidInt;
typedef std::function<void(int,int)> FnVoidIntInt;
using std::string;
@ -49,6 +50,7 @@ public:
CDownloader();
~CDownloader();
void queryContentLenght(const string &url);
void downloadFile(const string &url, const string &filePath);
void start();
void pause();
@ -56,6 +58,7 @@ public:
string GetFilePath();
/* callback */
void onQueryResponse(FnVoidIntInt callback);
void onComplete(FnVoidInt callback);
void onProgress(FnVoidInt callback);

View File

@ -164,6 +164,7 @@ public:
DeleteFile(m_filePath.c_str());
return result;
}
FnVoidIntInt m_query_callback = nullptr;
FnVoidInt m_complete_callback = nullptr,
m_progress_callback = nullptr;
wstring m_url,
@ -188,6 +189,26 @@ CDownloader::~CDownloader()
delete pimpl, pimpl = nullptr;
}
void CDownloader::queryContentLenght(const wstring &url)
{
if (url.empty() || pimpl->m_lock)
return;
pimpl->m_lock = true;
pimpl->m_future = std::async(std::launch::async, [=]() {
DWORD dwFileSize = 0;
Connection conn;
int hr = initConnection(url, dwFileSize, conn);
int error = (hr == DNL_OK) ? 0 :
(hr == DNL_CONN_ERR) ? -2 :
(hr == DNL_URL_ERR) ? -3 : -5;
if (pimpl->m_query_callback)
pimpl->m_query_callback(error, dwFileSize);
pimpl->m_lock = false;
});
}
void CDownloader::downloadFile(const std::wstring &url, const std::wstring &filePath)
{
pimpl->m_url.clear();
@ -232,6 +253,11 @@ wstring CDownloader::GetFilePath()
return pimpl->m_filePath;
}
void CDownloader::onQueryResponse(FnVoidIntInt callback)
{
pimpl->m_query_callback = callback;
}
void CDownloader::onComplete(FnVoidInt callback)
{
pimpl->m_complete_callback = callback;

View File

@ -37,6 +37,7 @@
#include <functional>
typedef std::function<void(int)> FnVoidInt;
typedef std::function<void(int,int)> FnVoidIntInt;
using std::wstring;
@ -49,12 +50,14 @@ public:
CDownloader();
~CDownloader();
void queryContentLenght(const wstring &url);
void downloadFile(const wstring &url, const wstring &filePath);
void start();
void stop();
wstring GetFilePath();
/* callback */
void onQueryResponse(FnVoidIntInt callback);
void onComplete(FnVoidInt callback);
void onProgress(FnVoidInt callback);

View File

@ -219,6 +219,7 @@ auto runProcess(const tstring &fileName, const tstring &args, bool runAsAdmin =
struct CUpdateManager::PackageData {
QString fileName,
fileType,
fileSize,
hash,
version;
wstring packageUrl,
@ -226,6 +227,7 @@ struct CUpdateManager::PackageData {
void clear() {
fileName.clear();
fileType.clear();
fileSize.clear();
hash.clear();
version.clear();
packageUrl.clear();
@ -353,6 +355,14 @@ void CUpdateManager::init()
QMetaObject::invokeMethod(this, "onProgressSlot", Qt::QueuedConnection, Q_ARG(int, std::stoi(params[1])));
break;
case MSG_RequestContentLenght: {
double fileSize = std::stod(params[1])/1024/1024;
m_packageData->fileSize = (fileSize == 0) ? "--" : QString::number(fileSize, 'f', 1);
QMetaObject::invokeMethod(this, "onCheckFinished", Qt::QueuedConnection, Q_ARG(bool, false), Q_ARG(bool, true),
Q_ARG(QString, m_packageData->version), Q_ARG(QString, ""));
break;
}
case MSG_OtherError:
QMetaObject::invokeMethod(this, "onError", Qt::QueuedConnection, Q_ARG(QString, TStrToQStr(params[1])));
break;
@ -762,7 +772,10 @@ void CUpdateManager::onLoadCheckFinished(const QString &filePath)
QJsonValue changelog = release_notes.value(lang);
clearTempFiles(isSavedPackageValid() ? m_savedPackageData->fileName : "");
onCheckFinished(false, true, m_packageData->version, changelog.toString());
if (m_packageData->packageUrl.empty() || !m_socket->sendMessage(MSG_RequestContentLenght, WStrToTStr(m_packageData->packageUrl))) {
m_packageData->fileSize = "--";
onCheckFinished(false, true, m_packageData->version, "");
}
} else {
clearTempFiles();
onCheckFinished(false, false, "", "");
@ -806,9 +819,9 @@ void CUpdateManager::onCheckFinished(bool error, bool updateExist, const QString
void CUpdateManager::showUpdateMessage(QWidget *parent) {
int result = WinDlg::showDialog(parent, tr("Update is available"),
QString("%1\n%2: %3\n%4: %5\n%6").arg(QString(WINDOW_NAME), tr("Current version"),
QString("%1\n%2: %3\n%4: %5\n%6 (%7 MB)").arg(QString(WINDOW_NAME), tr("Current version"),
QString(VER_FILEVERSION_STR), tr("Update version"), getVersion(),
tr("Would you like to download update now?")),
tr("Would you like to download update now?"), m_packageData->fileSize),
WinDlg::DlgBtns::mbSkipRemindDownload);
__UNLOCK
switch (result) {

View File

@ -102,7 +102,6 @@ private:
void init();
void clearTempFiles(const QString &except = QString());
void updateNeededCheking();
void onCheckFinished(bool error, bool updateExist, const QString &version, const QString &changelog);
void unzipIfNeeded();
void savePackageData(const QString &version = QString(), const QString &fileName = QString(), const QString &fileType = QString());
QString ignoredVersion();
@ -135,6 +134,7 @@ private:
CSocket *m_socket = nullptr;
private slots:
void onCheckFinished(bool error, bool updateExist, const QString &version, const QString &changelog);
void onLoadCheckFinished(const QString &filePath);
void showUpdateMessage(QWidget *parent);
void onLoadUpdateFinished(const QString &filePath);