-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdateform.cpp
More file actions
193 lines (169 loc) · 6.52 KB
/
updateform.cpp
File metadata and controls
193 lines (169 loc) · 6.52 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "updateform.h"
#include "ui_updateform.h"
#include <QFile>
#include <QProcess>
#include <QDesktopServices>
#include <QTimer>
#include <QTextStream>
#include <QDir>
UpdateForm::UpdateForm(QWidget* parent)
: QDialog(parent), ui(new Ui::UpdateForm)
{
ui->setupUi(this);
setWindowFlags(Qt::WindowStaysOnTopHint);
setWindowTitle("UpdateForm");
qDebug() << QSslSocket::sslLibraryBuildVersionString() << QSslSocket::supportsSsl(); //需要安装openssl
resize(DPI(size()));
manager = new QNetworkAccessManager(this);
taskBarBtn = new QWinTaskbarButton(this);
QTimer::singleShot(0, this, [=](){
taskBarBtn->setWindow(windowHandle()); //必须在窗口构造之后
taskBarProgress = taskBarBtn->progress();
taskBarProgress->setRange(0, 100);
});
connect(ui->progressBar, &QProgressBar::valueChanged, this, [=](int value){
taskBarProgress->setValue(value);
});
connect(ui->btn_auto, &QPushButton::clicked, this, [=]() {
taskBarProgress->show();
updateLatestGiteeRelease(releaseUrl, "download.zip");
});
connect(ui->btn_manual, &QPushButton::clicked, this, [=]() {
QDesktopServices::openUrl(QUrl(releaseUrl));
});
}
UpdateForm::~UpdateForm()
{
delete ui;
}
QUrl UpdateForm::getRedirectTarget(QNetworkReply* reply)
{
return reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
}
void UpdateForm::download(const QUrl& url, const QString& path, std::function<void(bool)> todo)
{
if (url.isEmpty()) return;
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); //自动重定向
QNetworkReply* reply = manager->get(request);
static bool isSuccess;
isSuccess = false;
QFile::remove(path);
ui->progressBar->setValue(0);
taskBarProgress->setValue(0);
connect(reply, &QNetworkReply::readyRead, this, [=]() {
QFile file(path);
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
file.write(reply->readAll());
});
connect(reply, &QNetworkReply::finished, [=]() {
qDebug() << "download finished";
reply->deleteLater();
todo(isSuccess);
});
connect(reply, &QNetworkReply::downloadProgress, this, [=](qint64 received, qint64 total) {
isSuccess = (received == total);
qreal percent = 1.0 * received / total * 100;
qDebug() << percent << "%";
ui->progressBar->setValue(percent);
ui->progressBar->setFormat(QString("%1%").arg(QString::number(percent, 'f', 2)));
});
connect(reply, &QNetworkReply::errorOccurred, this, [=](QNetworkReply::NetworkError code) {
qCritical() << code;
setWindowTitle(QString::number(code));
});
}
void UpdateForm::getDownloadUrl(const QString& htmlUrl, std::function<void(QMap<QString, QString>)> todo)
{
QNetworkRequest request { QUrl(htmlUrl) }; //{}否则被误认为函数声明
QNetworkReply* reply = manager->get(request);
connect(reply, &QNetworkReply::finished, this, [=]() {
qDebug() << "html finished";
reply->deleteLater();
QString html(reply->readAll());
QMap<QString, QString> res;
if (html.isEmpty()) {
todo(res);
} else {
res = parseHtml(html);
todo(res);
}
});
}
QMap<QString, QString> UpdateForm::parseHtml(const QString& html)
{
QMap<QString, QString> res;
QRegExp reg("<div class='ui celled list releases-download-list'>\\n<div class='item'>\\n<a href=\"(.*)\"");
reg.setMinimal(true);
reg.indexIn(html);
res["url"] = "https://gitee.com" + reg.cap(1);
reg.setPattern("<a class=\"download-archive\" data-ref=\"(.*)\"");
reg.indexIn(html);
res["version"] = reg.cap(1);
reg.setPattern("<div class='markdown-body'>\\n<p>(.*)</p>");
reg.indexIn(html);
res["desc"] = reg.cap(1).replace(QString("<br>
"), QChar('\n'));
return res;
}
void UpdateForm::updateLatestGiteeRelease(const QString& releaseUrl, const QString& filePath)
{
ui->btn_auto->setEnabled(false);
getDownloadUrl(releaseUrl, [=](QMap<QString, QString> res) {
QString url = res["url"];
QString version = res["version"];
qDebug() << url;
qDebug() << version;
if (url.isEmpty()) {
ui->label->setText("#Network Error");
} else
download(QUrl(url), filePath, [=](bool isSuccess) {
if (isSuccess) {
const QString batPath = "unzip.bat";
writeBat(batPath, filePath, qApp->applicationDirPath());
QDesktopServices::openUrl(QUrl::fromLocalFile(batPath));
qApp->quit();
} else {
qDebug() << "download Failed";
ui->label->setText("!Error: Download Failed");
}
ui->btn_auto->setEnabled(true);
});
});
}
void UpdateForm::writeBat(const QString& batPath, const QString& zipPath, const QString& folder)
{
QFile file(batPath);
if (file.open(QFile::WriteOnly | QFile::Text)) {
QTextStream text(&file);
text << "@timeout /t 2 /NOBREAK" << '\n';
text << "@cd /d %~dp0" << '\n'; //切换到bat目录,否则为qt的exe目录
text << "@echo ##Unziping and replacing the files, please wait......\n";
text << QString("7z x \"%1\" -o\"%2\" -aoa\n").arg(QDir::toNativeSeparators(zipPath)).arg(QDir::toNativeSeparators(folder));
text << "@echo -------------------------------------------------------\n";
text << "@echo ##Update SUCCESSFUL(Maybe)\n";
text << "@echo -------------------------------------------------------\n";
text << QString("@del \"%1\"\n").arg(zipPath);
text << "@pause\n";
text << "@start \"\" Follower.exe\n"; //start
text << "@timeout /t 1 /NOBREAK" << '\n';
text << "@exit";
}
}
void UpdateForm::showEvent(QShowEvent* event)
{
Q_UNUSED(event)
getDownloadUrl(releaseUrl, [=](QMap<QString, QString> res) {
if (res.empty()){
ui->label->setText("ERROR:empty result");
ui->btn_auto->setEnabled(false);
}
else {
qDebug() << res["url"];
ui->label->setText(QString("当前版本:%1\n最新版本:%2\n\n%3").arg(ver, res["version"], res["desc"]));
ui->btn_auto->setEnabled(ver != res["version"]);
}
//ui->label->adjustSize();
ui->verticalLayout->update(); //尝试防止刷新文本后布局紊乱
resize(size());
});
}