/
updater.cpp
152 lines (126 loc) · 4.01 KB
/
updater.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
/**
* @file updater.cpp
* Automatic updater that works with dengine.net. @ingroup base
*
* @authors Copyright © 2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2012 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 2 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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "updater.h"
#include "dd_version.h"
#include "dd_types.h"
#include "json.h"
#include "updater/updateavailabledialog.h"
#include "updater/updatersettings.h"
#include "updater/versioninfo.h"
#include <de/Time>
#include <de/Log>
#include <QStringList>
#include <QDateTime>
#include <QDesktopServices>
#include <QNetworkAccessManager>
#include <QSettings>
#include <QDebug>
static Updater* updater = 0;
/// @todo The platform ID should come from the Builder.
#if defined(WIN32)
# define PLATFORM_ID "win-x86"
#elif defined(MACOSX)
# if defined(__64BIT__)
# define PLATFORM_ID "mac10_6-x86-x86_64"
# else
# define PLATFORM_ID "mac10_4-x86-ppc"
# endif
#else
# if defined(__64BIT__)
# define PLATFORM_ID "linux-x86_64"
# else
# define PLATFORM_ID "linux-x86"
# endif
#endif
struct Updater::Instance
{
Updater* self;
QNetworkAccessManager* network;
VersionInfo latestVersion;
QString latestPackageUri;
QString latestLogUri;
Instance(Updater* up) : self(up)
{
network = new QNetworkAccessManager(self);
}
~Instance()
{
}
QString composeCheckUri()
{
UpdaterSettings st;
QString uri = QString(DOOMSDAY_HOMEURL) + "/latestbuild?";
uri += QString("platform=") + PLATFORM_ID;
uri += (st.channel() == UpdaterSettings::Stable? "&stable" : "&unstable");
uri += "&graph";
LOG_DEBUG("Check URI: ") << uri;
return uri;
}
void queryLatestVersion()
{
UpdaterSettings().setLastCheckTime(de::Time());
network->get(QNetworkRequest(composeCheckUri()));
}
void handleReply(QNetworkReply* reply)
{
reply->deleteLater(); // make sure it gets deleted
QVariant result = parseJSON(QString::fromUtf8(reply->readAll()));
if(!result.isValid()) return;
QVariantMap map = result.toMap();
latestPackageUri = map["direct_download_uri"].toString();
latestLogUri = map["release_changeloguri"].toString();
latestVersion = VersionInfo(map["version"].toString(), map["build_uniqueid"].toInt());
LOG_VERBOSE("Received latest version information:\n"
" - version: %s (running %s)\n"
" - package: %s\n"
" - change log: %s")
<< latestVersion.asText()
<< VersionInfo().asText()
<< latestPackageUri << latestLogUri;
// Is this newer than what we're running?
UpdateAvailableDialog* dlg = new UpdateAvailableDialog(latestVersion, latestLogUri);
dlg->exec();
delete dlg;
}
};
Updater::Updater(QObject *parent) : QObject(parent)
{
d = new Instance(this);
connect(d->network, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotReply(QNetworkReply*)));
d->queryLatestVersion();
}
Updater::~Updater()
{
delete d;
}
void Updater::gotReply(QNetworkReply* reply)
{
d->handleReply(reply);
}
void Updater_Init(void)
{
updater = new Updater;
}
void Updater_Shutdown(void)
{
delete updater;
}