-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdater.cpp
More file actions
157 lines (134 loc) · 4.63 KB
/
Copy pathupdater.cpp
File metadata and controls
157 lines (134 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "updater.h"
#include <QDebug>
updater::updater(QObject *parent)
: QObject(parent),
m_updater(new QSimpleUpdater(this)),
m_currentVersion("1.0.0"),
m_updateAvailable(false),
m_isDownloading(false),
m_downloadProgress(0)
{
// Configure QSimpleUpdater
m_updater->setModuleVersion(m_currentVersion);
m_updater->setNotifyOnFinish(false); // On gère nous-mêmes les notifications
m_updater->setNotifyOnUpdateAvailable(false);
m_updater->setDownloaderEnabled(true);
// URL du fichier version.json
m_updater->setUrl("https://raw.githubusercontent.com/ton-utilisateur/MuseStream/main/version.json");
// Connecte les signaux de QSimpleUpdater
connect(m_updater, &QSimpleUpdater::updateAvailable, this, &updater::handleUpdateAvailable);
connect(m_updater, &QSimpleUpdater::updateDownloaded, this, &updater::handleUpdateDownloaded);
connect(m_updater, &QSimpleUpdater::downloadProgress, this, &updater::handleDownloadProgress);
connect(m_updater, &QSimpleUpdater::errorOccurred, this, &updater::handleUpdateError);
}
updater::~updater()
{
delete m_updater;
}
// Méthodes exposées à QML
void updater::checkForUpdates()
{
m_isDownloading = false;
emit isDownloadingChanged();
m_updater->checkForUpdates();
}
void updater::downloadUpdate()
{
if (m_updateAvailable) {
m_isDownloading = true;
emit isDownloadingChanged();
m_updater->downloadUpdate();
}
}
void updater::installUpdate()
{
// Logique pour installer la mise à jour (extraire, remplacer les fichiers, etc.)
QString downloadPath = m_updater->getDownloadedFilePath();
qDebug() << "Installation de la mise à jour depuis :" << downloadPath;
// Exemple : Extraire et remplacer les fichiers (à adapter)
QProcess *extractProcess = new QProcess(this);
QString extractDir = QCoreApplication::applicationDirPath() + "/update_temp";
QDir().mkdir(extractDir);
// Utilise unzip (Linux/macOS) ou 7z (Windows)
extractProcess->start("unzip", {downloadPath, "-d", extractDir});
connect(extractProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[this, extractDir, downloadPath](int exitCode, QProcess::ExitStatus exitStatus) {
if (exitCode == 0) {
// Copie les fichiers extraits
QDir dir(extractDir);
for (const QString &file : dir.entryList(QDir::Files)) {
QString destPath = QCoreApplication::applicationDirPath() + "/" + file;
if (QFile::exists(destPath)) {
QFile::remove(destPath);
}
QFile::copy(extractDir + "/" + file, destPath);
QFile::setPermissions(destPath, QFile::Permissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner));
}
// Nettoyage
QDir().rmdir(extractDir);
QFile::remove(downloadPath);
emit changelogChanged(); // Notifie QML que l'installation est terminée
} else {
emit updateError("Échec de l'extraction de l'archive.");
}
});
}
// Getters/Setters
QString updater::currentVersion() const
{
return m_currentVersion;
}
void updater::setCurrentVersion(const QString &version)
{
if (m_currentVersion != version) {
m_currentVersion = version;
emit currentVersionChanged();
}
}
bool updater::updateAvailable() const
{
return m_updateAvailable;
}
bool updater::isDownloading() const
{
return m_isDownloading;
}
int updater::downloadProgress() const
{
return m_downloadProgress;
}
QString updater::latestVersion() const
{
return m_latestVersion;
}
QString updater::changelog() const
{
return m_changelog;
}
// Handlers pour QSimpleUpdater
void updater::handleUpdateAvailable()
{
m_latestVersion = m_updater->getLatestVersion();
m_changelog = m_updater->getChangelog();
m_updateAvailable = true;
emit updateAvailableChanged();
emit latestVersionChanged();
emit changelogChanged();
}
void updater::handleUpdateDownloaded()
{
m_isDownloading = false;
emit isDownloadingChanged();
emit changelogChanged(); // Notifie que le téléchargement est terminé
}
void updater::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
m_downloadProgress = (bytesReceived * 100) / bytesTotal;
emit downloadProgressChanged();
}
void updater::handleUpdateError(const QString &error)
{
m_isDownloading = false;
emit isDownloadingChanged();
emit updateError(error);
}