-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdatechecker.cpp
137 lines (105 loc) · 4.7 KB
/
updatechecker.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
/*
*
Copyright (C) 2017-2019 Fábio Bento (fabiobento512)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "updatechecker.h"
UpdateChecker::UpdateChecker(const ConfigFileFRequest::Settings &settings)
:settings(settings)
{
this->networkRequest.setUrl(QUrl(this->updateCheckApiUrl));
}
void UpdateChecker::startNewInstance(const ConfigFileFRequest::Settings &settings){
UpdateChecker *newInstance = new UpdateChecker(settings);
newInstance->checkForUpdates(); // this deletes itself once finished
}
void UpdateChecker::checkForUpdates(){
// Apply proxy type
ProxySetup::setupProxyForNetworkManager(this->settings, &this->networkAccessManager);
connect(&this->networkAccessManager, &QNetworkAccessManager::finished, this, &UpdateChecker::replyFinished);
// do the request and also check for timeout
checkForQNetworkAccessManagerTimeout(this->networkAccessManager.get(this->networkRequest));
this->deleteLater(); // delete when signal / slots are finished
}
void UpdateChecker::replyFinished(QNetworkReply *reply){
try{
if(!this->replyHasFinished){
if (reply->error()) {
throw std::runtime_error(QSTR_TO_TEMPORARY_CSTR(reply->errorString()));
}
QString answer = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(answer.toUtf8());
if(!doc.isObject()){
throw std::runtime_error("(http api error) json is not an object.");
}
QJsonObject jsonObj = doc.object();
QJsonValue jsonProgramLastVersion = jsonObj.value("tag_name");
if(jsonProgramLastVersion.isUndefined()){
throw std::runtime_error("(http api error) json tag_name doesn't exist.");
}
QString newVersion = jsonProgramLastVersion.toString();
// remove v (from v1.0 for example) if it exists
if(newVersion.startsWith("v")){
newVersion.remove(0,1); // remove first character
}
if(newVersion != GlobalVars::AppVersion){
Util::Dialogs::showInfo
(
"There's a new version of " + GlobalVars::AppName + "! (v" + newVersion + ")<br/><br/>"+
"You can download it <a href='" + this->releasesUrl + "'>here</a>.", true
);
}
else{
Util::Dialogs::showInfo("You are using last version.");
}
}
}
catch(const std::exception& e){
QString errorMessage = "An error ocurred while checking for updates: " + QString(e.what());
LOG_ERROR << errorMessage;
Util::Dialogs::showError(errorMessage);
}
this->replyHasFinished = true;
}
// Since QNetworkReply doesn't have a way to set a timeout we need implement it by ourselves
// http://stackoverflow.com/a/13229926
void UpdateChecker::checkForQNetworkAccessManagerTimeout(QNetworkReply *reply)
{
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
timer.start(10 * 1000); // 10 seconds hardcoded timeout
loop.exec();
if(timer.isActive()) // request didn't timeout
{
timer.stop();
}
else if(!this->replyHasFinished)
{
// timeout
// TODO this class is a bit confusing,
// especially when we are using a boolean to check if the reply has already finished
// (we are doing that because if user doesn't close success dialog, a second call is emitted to replyFinished function
// after 10 secs (timeout)
// If possible this should be refactored to a simpler method
this->replyHasFinished = true;
disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
reply->abort();
QString errorMessage = "Timeout while checking for updates.<br/><br/>You still can check manually, by clicking <a href='" + this->releasesUrl + "'>here</a>.";
Util::Dialogs::showError(errorMessage, true);
LOG_ERROR << errorMessage;
}
reply->deleteLater();
}