-
Notifications
You must be signed in to change notification settings - Fork 26
/
autoupdatedialog.cpp
329 lines (268 loc) · 9.23 KB
/
autoupdatedialog.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "autoupdatedialog.h"
#include "mainwindow.h"
#include "ui_autoupdatedialog.h"
extern MainWindow* mw_one;
AutoUpdateDialog::AutoUpdateDialog(QWidget* parent)
: QDialog(parent), ui(new Ui::AutoUpdateDialog) {
ui->setupUi(this);
QFileInfo fi(qAppName());
strLinuxTargetFile = fi.absoluteFilePath();
setWindowTitle("");
ui->progressBar->setTextVisible(false);
Init();
tempDir = QDir::homePath() + "/tempxiasl/";
mw_one->deleteDirfile(tempDir);
ui->label->setVisible(false);
}
AutoUpdateDialog::~AutoUpdateDialog() { delete ui; }
void AutoUpdateDialog::Init() {
//"https://raw.fastgit.org/ic005k/" //HK
strWinUrl =
"https://ghproxy.com/https://raw.githubusercontent.com/ic005k/"
"QtOpenCoreConfigDatabase/main/win.zip";
strMacUrl =
"https://ghproxy.com/https://raw.githubusercontent.com/ic005k/"
"QtOpenCoreConfigDatabase/main/Contents.zip";
strMacClassicalUrl =
"https://ghproxy.com/https://github.com/ic005k/QtOpenCoreConfigDatabase/"
"releases/download/1.0.0/Contents.zip";
strLinuxUrl =
"https://ghproxy.com/https://github.com/ic005k/QtOpenCoreConfigDatabase/"
"releases/download/1.0.0/OCAuxiliaryTools-Linux-x86_64.AppImage";
strDatabaseUrl =
"https://ghproxy.com/https://raw.githubusercontent.com/ic005k/"
"QtOpenCoreConfigDatabase/main/Database.zip";
manager = new QNetworkAccessManager(this);
myfile = new QFile(this);
}
void AutoUpdateDialog::doProcessReadyRead() //读取并写入
{
while (!reply->atEnd()) {
QByteArray ba = reply->readAll();
myfile->write(ba);
}
}
void AutoUpdateDialog::doProcessFinished() { myfile->close(); }
void AutoUpdateDialog::doProcessDownloadProgress(qint64 recv_total,
qint64 all_total) //显示
{
ui->progressBar->setMaximum(all_total);
ui->progressBar->setValue(recv_total);
setWindowTitle(tr("Download Progress") + " : " + GetFileSize(recv_total, 2) +
" -> " + GetFileSize(all_total, 2));
if (recv_total == all_total) {
if (recv_total < 10000) {
return;
}
ui->btnStartUpdate->setEnabled(true);
this->repaint();
if ((mw_one->win) && ui->btnStartUpdate->isVisible()) {
ui->label->setVisible(false);
}
if (mw_one->linuxOS) {
QProcess* p = new QProcess;
p->start("chmod", QStringList() << "+x" << tempDir + filename);
}
}
}
void AutoUpdateDialog::on_btnStartUpdate_clicked() {
ui->btnStartUpdate->setEnabled(false);
startUpdate();
}
void AutoUpdateDialog::startUpdate() {
ui->btnStartUpdate->setEnabled(false);
this->repaint();
QString strZip, strPath, strExec, strUnzip;
QFileInfo appInfo(qApp->applicationDirPath());
strZip = tempDir + filename;
QDir dir;
dir.setCurrent(tempDir);
qApp->exit();
if (mw_one->mac || mw_one->osx1012) {
QString fileName = tempDir + "up.sh";
QTextEdit* txtEdit = new QTextEdit();
QString strTarget = appInfo.path().replace("Contents", "");
strTarget = strTarget + ".";
strTarget = "\"" + strTarget + "\"";
if (mw_one->mac) {
txtEdit->append("hdiutil mount -mountpoint /Volumes/xiasl " + strZip);
txtEdit->append(
"cp -R -p -f "
"/Volumes/xiasl/Xiasl.app/. " +
strTarget);
txtEdit->append("hdiutil eject /Volumes/xiasl");
}
if (mw_one->osx1012) {
txtEdit->append("hdiutil mount -mountpoint /Volumes/xiasl1012 " + strZip);
txtEdit->append(
"cp -R -p -f "
"/Volumes/xiasl1012/Xiasl.app/. " +
strTarget);
txtEdit->append("hdiutil eject /Volumes/xiasl1012");
}
strPath = appInfo.path().replace("Contents", "");
strExec = strPath.mid(0, strPath.length() - 1);
strExec = "\"" + strExec + "\"";
txtEdit->append("open " + strExec);
TextEditToFile(txtEdit, fileName);
QProcess::startDetached("bash", QStringList() << fileName);
}
if (mw_one->win) {
QString fileName = tempDir + "up.bat";
strPath = appInfo.filePath();
QTextEdit* txtEdit = new QTextEdit();
strUnzip = strPath + "/unzip.exe";
strUnzip = "\"" + strUnzip + "\"";
strZip = "\"" + strZip + "\"";
strPath = "\"" + strPath + "\"";
strExec = qApp->applicationFilePath();
strExec = "\"" + strExec + "\"";
QString strCommand1, strCommand2;
QString strx = "\"" + tempDir + "\"";
strCommand1 = strUnzip + " -o " + strZip + " -d " + strx;
QString stry = tempDir + QFileInfo(filename).baseName();
stry = "\"" + stry + "\"";
strCommand2 = "xcopy " + stry + " " + strPath + " /s/y";
txtEdit->append(strCommand1 + " && " + strCommand2 + " && " + strExec);
TextEditToFile(txtEdit, fileName);
QProcess::startDetached("cmd.exe", QStringList() << "/c" << fileName);
}
if (mw_one->linuxOS) {
QString fileName = tempDir + "up.sh";
QTextEdit* txtEdit = new QTextEdit();
strZip = "\"" + strZip + "\"";
strLinuxTargetFile = "\"" + strLinuxTargetFile + "\"";
txtEdit->append("cp -f " + strZip + " " + strLinuxTargetFile);
txtEdit->append(strLinuxTargetFile);
TextEditToFile(txtEdit, fileName);
QProcess::execute("chmod", QStringList() << "+x" << fileName);
QProcess::startDetached("bash", QStringList() << fileName);
}
}
void AutoUpdateDialog::startDownload(bool Database) {
setWindowTitle("");
ui->label->setVisible(false);
ui->btnStartUpdate->setEnabled(false);
this->repaint();
if (!Database) {
ui->btnStartUpdate->setVisible(true);
} else {
strUrl = strDatabaseUrl;
ui->btnStartUpdate->setVisible(false);
}
QString str0, str1;
str0 = "https://download.fastgit.org/"; // 日本东京
str1 = "https://ghproxy.com/https://github.com/"; // 韩国首尔
str0 = mw_one->getProxy().trimmed();
str1 = strUrl;
str1.replace("https://github.com/", str0);
QNetworkRequest request;
request.setUrl(QUrl(str1));
// github redirects the request, so this attribute must be set to true,
// otherwise returns nothing from qt5.6
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
reply = manager->get(request); //发送请求
connect(reply, &QNetworkReply::readyRead, this,
&AutoUpdateDialog::doProcessReadyRead); //可读
connect(reply, &QNetworkReply::finished, this,
&AutoUpdateDialog::doProcessFinished); //结束
connect(reply, &QNetworkReply::downloadProgress, this,
&AutoUpdateDialog::doProcessDownloadProgress); //大小
QStringList list = strUrl.split("/");
filename = list.at(list.length() - 1);
QDir dir;
if (dir.mkpath(tempDir)) {
}
QString file = tempDir + filename;
myfile->setFileName(file);
bool ret =
myfile->open(QIODevice::WriteOnly | QIODevice::Truncate); //创建文件
if (!ret) {
QMessageBox::warning(this, "warning", "Failed to open.");
return;
}
ui->progressBar->setValue(0);
ui->progressBar->setMinimum(0);
}
void AutoUpdateDialog::closeEvent(QCloseEvent* event) {
Q_UNUSED(event);
reply->close();
}
QString AutoUpdateDialog::GetFileSize(qint64 size) {
if (size < 0) return "0";
if (!size) {
return "0 Bytes";
}
static QStringList SizeNames;
if (SizeNames.empty()) {
SizeNames << " Bytes"
<< " KB"
<< " MB"
<< " GB"
<< " TB"
<< " PB"
<< " EB"
<< " ZB"
<< " YB";
}
int i = qFloor(qLn(size) / qLn(1024));
return QString::number(size * 1.0 / qPow(1000, qFloor(i)), 'f',
(i > 1) ? 2 : 0) +
SizeNames.at(i);
}
QString AutoUpdateDialog::GetFileSize(const qint64& size, int precision) {
double sizeAsDouble = size;
static QStringList measures;
if (measures.isEmpty())
measures << QCoreApplication::translate("QInstaller", "bytes")
<< QCoreApplication::translate("QInstaller", "KiB")
<< QCoreApplication::translate("QInstaller", "MiB")
<< QCoreApplication::translate("QInstaller", "GiB")
<< QCoreApplication::translate("QInstaller", "TiB")
<< QCoreApplication::translate("QInstaller", "PiB")
<< QCoreApplication::translate("QInstaller", "EiB")
<< QCoreApplication::translate("QInstaller", "ZiB")
<< QCoreApplication::translate("QInstaller", "YiB");
QStringListIterator it(measures);
QString measure(it.next());
while (sizeAsDouble >= 1024.0 && it.hasNext()) {
measure = it.next();
sizeAsDouble /= 1024.0;
}
return QString::fromLatin1("%1 %2")
.arg(sizeAsDouble, 0, 'f', precision)
.arg(measure);
}
void AutoUpdateDialog::TextEditToFile(QTextEdit* txtEdit, QString fileName) {
QFile* file;
file = new QFile;
file->setFileName(fileName);
bool ok = file->open(QIODevice::WriteOnly);
if (ok) {
QTextStream out(file);
out << txtEdit->toPlainText();
file->close();
delete file;
}
}
void AutoUpdateDialog::keyPressEvent(QKeyEvent* event) {
switch (event->key()) {
case Qt::Key_Escape:
// reply->close();
// close();
break;
case Qt::Key_Return:
break;
case Qt::Key_Backspace:
break;
case Qt::Key_Space:
break;
case Qt::Key_F1:
break;
}
if (event->modifiers() == Qt::ControlModifier) {
if (event->key() == Qt::Key_M) {
this->setWindowState(Qt::WindowMaximized);
}
}
}