Skip to content

Commit

Permalink
Updater: Saving settings persistently; testing version check query
Browse files Browse the repository at this point in the history
The JSON parser is used to parse the reply from the website.
  • Loading branch information
skyjake committed May 24, 2012
1 parent ea60ad8 commit e9bc9d3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
2 changes: 2 additions & 0 deletions doomsday/engine/portable/include/updater.h
Expand Up @@ -34,6 +34,8 @@
*/
class Updater : public QObject
{
Q_OBJECT

public:
explicit Updater(QObject* parent = 0);
~Updater();
Expand Down
89 changes: 79 additions & 10 deletions doomsday/engine/portable/src/updater.cpp
Expand Up @@ -21,59 +21,128 @@
*/

#include "updater.h"
#include "dd_version.h"
#include "dd_types.h"
#include "json.h"
#include <de/Log>
#include <QDateTime>
#include <QDesktopServices>
#include <QNetworkAccessManager>
#include <QSettings>
#include <QDebug>

static Updater* updater = 0;

#define WEBSITE_URL "http://dengine.net/"
#define STK_FREQUENCY "updater/frequency"
#define STK_CHANNEL "updater/channel"
#define STK_LAST_CHECKED "updater/lastChecked"
#define STK_ONLY_MANUAL "updater/onlyManually"
#define STK_DELETE "updater/delete"
#define STK_DOWNLOAD_PATH "updater/downloadPath"

/// @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
{
enum Frequency
{
Daily,
Biweekly, // 3.5 days
Weekly, // 7 days
Monthly // 30 days
Daily = 0,
Biweekly = 1, // 3.5 days
Weekly = 2, // 7 days
Monthly = 3 // 30 days
};
enum Channel
{
Stable,
Unstable
Stable = 0,
Unstable = 1
};

Updater* self;
QNetworkAccessManager* network;

Frequency checkFrequency;
Channel channel; ///< What kind of updates to check for.
QDateTime lastCheckTime;///< Time of last check (automatic or manual).
bool onlyCheckManually; ///< Should only check when manually requested.
bool trashAfterUpdate; ///< Downloaded file is moved to trash afterwards.
bool deleteAfterUpdate; ///< Downloaded file is deleted afterwards.
QString downloadPath; ///< Path where the downloaded file is saved.

Instance(Updater* up) : self(up)
{
// Fetch the current settings.
QSettings st;
checkFrequency = Frequency(st.value(STK_FREQUENCY, Weekly).toInt());
channel = Channel(st.value(STK_CHANNEL, QString(DOOMSDAY_RELEASE_TYPE) == "Stable"? Stable : Unstable).toInt());
lastCheckTime = st.value(STK_LAST_CHECKED).toDateTime();
onlyCheckManually = st.value(STK_ONLY_MANUAL, false).toBool();
deleteAfterUpdate = st.value(STK_DELETE, true).toBool();
downloadPath = st.value(STK_DOWNLOAD_PATH,
QDesktopServices::storageLocation(QDesktopServices::TempLocation)).toString();

network = new QNetworkAccessManager(self);
}

~Instance()
{}
{
// Save settings.
QSettings st;
st.setValue(STK_FREQUENCY, int(checkFrequency));
st.setValue(STK_CHANNEL, int(channel));
st.setValue(STK_LAST_CHECKED, lastCheckTime);
st.setValue(STK_ONLY_MANUAL, onlyCheckManually);
st.setValue(STK_DELETE, deleteAfterUpdate);
st.setValue(STK_DOWNLOAD_PATH, downloadPath);
}

QString composeCheckUri()
{
QString uri = QString(DOOMSDAY_HOMEURL) + "/latestbuild?";
uri += QString("platform=") + PLATFORM_ID;
uri += (channel == Stable? "&stable" : "&unstable");
uri += "&graph";

LOG_DEBUG("Check URI: ") << uri;
return uri;
}

void queryLatestVersion()
{
lastCheckTime = QDateTime::currentDateTime();
network->get(QNetworkRequest(composeCheckUri()));
}

void handleReply(QNetworkReply* reply)
{
reply->deleteLater(); // make sure it gets deleted


QVariant result = parseJSON(QString::fromUtf8(reply->readAll()));
qDebug() << result;
}
};

Updater::Updater(QObject *parent) : QObject(parent)
{
d = new Instance(this);
connect(d->network, SIGNAL(finished(QNetworkReply*)), this, SLOT(gotReply(QNetworkReply*)));

d->queryLatestVersion();
}

Updater::~Updater()
Expand Down

0 comments on commit e9bc9d3

Please sign in to comment.