Edit updater_service.h

This commit is contained in:
collecting
2025-10-25 02:13:00 +00:00
parent 8050dc0cd2
commit 1175bf35b7

View File

@@ -10,88 +10,91 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
// Forward declare to keep headers clean #include <QString>
class QNetworkAccessManager;
#include <QNetworkAccessManager>
namespace Updater { namespace Updater {
std::string ExtractCommitHash(const std::string& version_string); // Declaration for the helper function.
QString FormatDateTimeString(const std::string& iso_string);
std::string ExtractCommitHash(const std::string& version_string);
struct DownloadOption { struct DownloadOption {
std::string name; std::string name;
std::string url; std::string url;
}; };
struct UpdateInfo { struct UpdateInfo {
std::string version; std::string version;
std::vector<DownloadOption> download_options; // Used for both Windows .zip and Linux .AppImage std::vector<DownloadOption> download_options;
std::string changelog; std::string changelog;
std::string release_date; std::string release_date;
bool is_newer_version = false; bool is_newer_version = false;
}; };
class UpdaterService : public QObject { class UpdaterService : public QObject {
Q_OBJECT Q_OBJECT
public: public:
enum class UpdateResult { Success, Failed, Cancelled, NetworkError, ExtractionError, PermissionError, InvalidArchive, NoUpdateAvailable }; enum class UpdateResult { Success, Failed, Cancelled, NetworkError, ExtractionError, PermissionError, InvalidArchive, NoUpdateAvailable };
explicit UpdaterService(QObject* parent = nullptr); explicit UpdaterService(QObject* parent = nullptr);
~UpdaterService() override; ~UpdaterService() override;
void CheckForUpdates(const std::string& update_url); void CheckForUpdates(const std::string& update_url);
void DownloadAndInstallUpdate(const std::string& download_url); void DownloadAndInstallUpdate(const std::string& download_url);
void CancelUpdate(); void CancelUpdate();
std::string GetCurrentVersion() const; std::string GetCurrentVersion() const;
bool IsUpdateInProgress() const; bool IsUpdateInProgress() const;
static bool HasStagedUpdate(const std::filesystem::path& app_directory); static bool HasStagedUpdate(const std::filesystem::path& app_directory);
static bool ApplyStagedUpdate(const std::filesystem::path& app_directory); static bool ApplyStagedUpdate(const std::filesystem::path& app_directory);
signals: signals:
void UpdateCheckCompleted(bool has_update, const UpdateInfo& update_info); void UpdateCheckCompleted(bool has_update, const UpdateInfo& update_info);
void UpdateDownloadProgress(int percentage, qint64 bytes_received, qint64 bytes_total); void UpdateDownloadProgress(int percentage, qint64 bytes_received, qint64 bytes_total);
void UpdateInstallProgress(int percentage, const QString& current_file); void UpdateInstallProgress(int percentage, const QString& current_file);
void UpdateCompleted(UpdateResult result, const QString& message); void UpdateCompleted(UpdateResult result, const QString& message);
void UpdateError(const QString& error_message); void UpdateError(const QString& error_message);
private slots: private slots:
void OnDownloadFinished(); void OnDownloadFinished();
void OnDownloadProgress(qint64 bytes_received, qint64 bytes_total); void OnDownloadProgress(qint64 bytes_received, qint64 bytes_total);
void OnDownloadError(QNetworkReply::NetworkError error); void OnDownloadError(QNetworkReply::NetworkError error);
private: private:
void InitializeSSL(); void InitializeSSL();
void ConfigureSSLForRequest(QNetworkRequest& request); void ConfigureSSLForRequest(QNetworkRequest& request);
void ParseUpdateResponse(const QByteArray& response); void ParseUpdateResponse(const QByteArray& response);
bool CompareVersions(const std::string& current, const std::string& latest) const; bool CompareVersions(const std::string& current, const std::string& latest) const;
#ifdef _WIN32 #ifdef _WIN32
bool ExtractArchive(const std::filesystem::path& archive_path, const std::filesystem::path& extract_path); bool ExtractArchive(const std::filesystem::path& archive_path, const std::filesystem::path& extract_path);
#ifndef CITRON_ENABLE_LIBARCHIVE #ifndef CITRON_ENABLE_LIBARCHIVE
bool ExtractArchiveWindows(const std::filesystem::path& archive_path, const std::filesystem::path& extract_path); bool ExtractArchiveWindows(const std::filesystem::path& archive_path, const std::filesystem::path& extract_path);
#endif #endif
bool InstallUpdate(const std::filesystem::path& update_path); bool InstallUpdate(const std::filesystem::path& update_path);
bool CreateBackup(); bool CreateBackup();
bool RestoreBackup(); bool RestoreBackup();
#endif #endif
bool CleanupFiles(); bool CleanupFiles();
std::filesystem::path GetTempDirectory() const; std::filesystem::path GetTempDirectory() const;
std::filesystem::path GetApplicationDirectory() const; std::filesystem::path GetApplicationDirectory() const;
std::filesystem::path GetBackupDirectory() const; std::filesystem::path GetBackupDirectory() const;
bool EnsureDirectoryExists(const std::filesystem::path& path) const; bool EnsureDirectoryExists(const std::filesystem::path& path) const;
std::unique_ptr<QNetworkAccessManager> network_manager; std::unique_ptr<QNetworkAccessManager> network_manager;
QNetworkReply* current_reply = nullptr; QNetworkReply* current_reply = nullptr;
std::atomic<bool> update_in_progress{false}; std::atomic<bool> update_in_progress{false};
std::atomic<bool> cancel_requested{false}; std::atomic<bool> cancel_requested{false};
UpdateInfo current_update_info; UpdateInfo current_update_info;
std::filesystem::path app_directory; std::filesystem::path app_directory;
std::filesystem::path temp_download_path; std::filesystem::path temp_download_path;
std::filesystem::path backup_path; std::filesystem::path backup_path;
static constexpr const char* CITRON_VERSION_FILE = "version.txt"; static constexpr const char* CITRON_VERSION_FILE = "version.txt";
static constexpr const char* BACKUP_DIRECTORY = "backup"; static constexpr const char* BACKUP_DIRECTORY = "backup";
}; };
} // namespace Updater } // namespace Updater