leocad/qt/lc_qupdatedialog.cpp

156 lines
3.4 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_qupdatedialog.h"
#include "ui_lc_qupdatedialog.h"
#include "lc_application.h"
#include "lc_library.h"
2013-08-12 02:26:52 +02:00
#include "lc_profile.h"
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
void lcDoInitialUpdateCheck()
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
int updateFrequency = lcGetProfileInt(LC_PROFILE_CHECK_UPDATES);
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if (updateFrequency == 0)
return;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
QSettings settings;
QDateTime checkTime = settings.value("LastUpdateCheck", QDateTime()).toDateTime();
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if (!checkTime.isNull())
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
checkTime.addDays(1); // TODO: add option for weekly checks
if (checkTime > QDateTime::currentDateTimeUtc())
return;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
new lcQUpdateDialog(NULL, (void*)1);
}
lcQUpdateDialog::lcQUpdateDialog(QWidget *parent, void *data) :
QDialog(parent),
ui(new Ui::lcQUpdateDialog)
{
ui->setupUi(this);
initialUpdate = (bool)data;
connect(this, SIGNAL(finished(int)), this, SLOT(finished(int)));
ui->status->setText(tr("Connecting to update server..."));
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
updateReply = manager->get(QNetworkRequest(QUrl("http://www.leocad.org/updates.txt")));
2013-08-09 06:57:18 +02:00
}
lcQUpdateDialog::~lcQUpdateDialog()
{
if (updateReply)
{
updateReply->abort();
updateReply->deleteLater();
}
if (manager)
manager->deleteLater();
delete ui;
}
void lcQUpdateDialog::cancel()
{
if (updateReply)
{
updateReply->abort();
updateReply->deleteLater();
updateReply = NULL;
}
QDialog::accept();
}
2013-08-12 02:26:52 +02:00
void lcQUpdateDialog::finished(int result)
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
if (initialUpdate)
deleteLater();
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
void lcQUpdateDialog::replyFinished(QNetworkReply *reply)
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
bool updateAvailable = false;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if (reply->error() == QNetworkReply::NoError)
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
int majorVersion, minorVersion, patchVersion;
int parts;
QByteArray replyBytes = reply->readAll();
const char *update = replyBytes;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if (sscanf(update, "%d.%d.%d %d", &majorVersion, &minorVersion, &patchVersion, &parts) == 4)
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
QString status;
if (majorVersion > LC_VERSION_MAJOR)
2013-08-09 06:57:18 +02:00
updateAvailable = true;
2013-08-12 02:26:52 +02:00
else if (majorVersion == LC_VERSION_MAJOR)
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
if (minorVersion > LC_VERSION_MINOR)
2013-08-09 06:57:18 +02:00
updateAvailable = true;
2013-08-12 02:26:52 +02:00
else if (minorVersion == LC_VERSION_MINOR)
{
if (patchVersion > LC_VERSION_PATCH)
updateAvailable = true;
}
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
if (updateAvailable)
status = QString(tr("<p>There's a newer version of LeoCAD available for download (%1.%2.%3).</p>")).arg(majorVersion, minorVersion, patchVersion);
else
status = tr("<p>You are using the latest LeoCAD version.</p>");
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
lcPiecesLibrary* library = lcGetPiecesLibrary();
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if (library->mNumOfficialPieces)
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
if (parts > library->mNumOfficialPieces)
{
status += tr("<p>There are new parts available.</p>");
updateAvailable = true;
}
else
status += tr("<p>There are no new parts available at this time.</p>");
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
if (updateAvailable)
{
status += tr("<p>Visit <a href=\"http://www.leocad.org/files/\">http://www.leocad.org/files/</a> to download.</p>");
}
ui->status->setText(status);
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
else
ui->status->setText(tr("Error parsing update information."));
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
// TODO: option to ignore a version
QSettings settings;
settings.setValue("LastUpdateCheck", QDateTime::currentDateTimeUtc());
updateReply = NULL;
reply->deleteLater();
2013-08-09 06:57:18 +02:00
}
else
2013-08-12 02:26:52 +02:00
ui->status->setText(tr("Error connecting to the update server."));
if (initialUpdate)
{
if (updateAvailable)
show();
else
deleteLater();
}
2013-08-09 06:57:18 +02:00
ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
}