This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateform.cpp
190 lines (142 loc) · 4.5 KB
/
updateform.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
/*
Copyright 2011 Gene Chen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "updateform.h"
#include "dcapifetcher.h"
#include "xdcc_version.h"
#include <QDir>
UpdateForm::UpdateForm(QWidget *parent, QFlags<Qt::WindowType> flags)
: QDialog(parent, flags)
{
ui.setupUi(this);
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(updateNow()));
m_Version = QSettings("DotaCash", "DCClient X").value("version", XDCC_VERSION).toString();
// m_Downloader = new DownloaderForm(this);
// connect(m_Downloader, SIGNAL(downloadsComplete()), this, SLOT(beginUpdate()));
}
void UpdateForm::updateNow()
{
// QDir updateFolder("./updates/");
// QStringList list = updateFolder.entryList(QDir::Files);
// for (int i = 0; i < list.size(); ++i)
// updateFolder.remove(list.at(i));
// m_Downloader->addFile("http://www.dotacash.com/xdcc/file_list.xml");
// m_Downloader->addFile(m_Latest["url"]);
// m_Downloader->beginDownloads();
}
void UpdateForm::beginUpdate()
{
emit updateFromURL(m_Latest["url"]);
}
void UpdateForm::checkForUpdates(QString appCastURL, bool alwaysShow)
{
m_AlwaysShow = alwaysShow;
ApiFetcher* updateFetcher = new ApiFetcher(this);
connect(updateFetcher, SIGNAL(fetchComplete(QString&)), this, SLOT(parseUpdateData(QString&)));
updateFetcher->fetch(appCastURL);
}
void UpdateForm::parseUpdateData(QString& data)
{
QXmlStreamReader xml(data);
while(!xml.atEnd() && !xml.hasError())
{
xml.readNext();
if(xml.isStartDocument())
continue;
if(xml.isStartElement())
{
if(xml.name() == "channel")
continue;
if(xml.name() == "item")
{
m_Latest = parseUpdateItem(xml);
break;
}
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError)
{
qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
}
bool isNewer = isUpdateRequired(m_Latest["version"]);
if(isNewer)
{
ui.label->setText("<span style=\" font-size:14pt; color:#17069c;\">" + m_Latest["title"] + "</span>");
ui.lblChangelog->setText(m_Latest["description"].replace("</br>", "<br>"));
ui.cancelButton->setText(tr("Later"));
ui.okButton->show();
}
else
{
ui.label->setText("<span style=\" font-size:14pt; color:#17069c;\">" + tr("You're up to date!") + "</span>");
ui.lblChangelog->setText(tr("DCClient v%1 is the newest version currently available.").arg(m_Version));
ui.cancelButton->setText(tr("OK"));
ui.okButton->hide();
}
if(isNewer || m_AlwaysShow)
this->show();
}
bool UpdateForm::isUpdateRequired(QString& latestVer)
{
QStringList vals1 = m_Version.split('.');
QStringList vals2 = latestVer.split('.');
int i=0;
while(i < vals1.length() && i < vals2.length() && vals1[i] == vals2[i]) {
i++;
}
if (i < vals1.length() && i < vals2.length())
{
if(vals1[i].toInt() < vals2[i].toInt())
return true;
}
return false;
}
QMap<QString, QString> UpdateForm::parseUpdateItem(QXmlStreamReader& xml)
{
QMap<QString, QString> mapData;
if(!xml.isStartElement() || xml.name() != "item")
return mapData;
xml.readNext();
while(!(xml.isEndElement() && xml.name() == "item"))
{
if(xml.isStartElement())
{
if(xml.name() == "channel")
continue;
if(xml.name() == "title")
addElementDataToMap(xml, mapData);
else if(xml.name() == "description")
addElementDataToMap(xml, mapData);
else if(xml.name() == "enclosure")
{
QXmlStreamAttributes attrs = xml.attributes();
mapData["url"] = attrs.value("url").toString();
mapData["version"] = attrs.value("sparkle:version").toString();
mapData["os"] = attrs.value("sparkle:os").toString();
mapData["type"] = attrs.value("type").toString();
mapData["size"] = attrs.value("size").toString();
}
}
xml.readNext();
}
return mapData;
}
void UpdateForm::addElementDataToMap(QXmlStreamReader& xml, QMap<QString, QString>& map)
{
if(!xml.isStartElement())
return;
QString elementName = xml.name().toString();
xml.readNext();
if(!xml.isCharacters() || xml.isWhitespace())
return;
map.insert(elementName, xml.text().toString());
}