leocad/qt/lc_qupdatedialog.cpp

179 lines
4.2 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;
2016-05-08 04:16:52 +02:00
QDateTime CheckTime = settings.value("Updates/LastCheck", QDateTime()).toDateTime();
2013-08-09 06:57:18 +02:00
2016-05-08 04:16:52 +02:00
if (!CheckTime.isNull())
2013-08-09 06:57:18 +02:00
{
2016-05-08 04:16:52 +02:00
QDateTime NextCheckTime = CheckTime.addDays(updateFrequency == 1 ? 1 : 7);
2013-08-12 02:26:52 +02:00
2016-09-22 17:04:51 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 0))
2016-05-08 04:16:52 +02:00
if (NextCheckTime > QDateTime::currentDateTimeUtc())
2016-09-22 17:04:51 +02:00
#else
if (NextCheckTime > QDateTime::currentDateTime())
#endif
2013-08-12 02:26:52 +02:00
return;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
2016-08-01 05:44:15 +02:00
new lcQUpdateDialog(NULL, true);
2013-08-12 02:26:52 +02:00
}
2016-08-01 05:44:15 +02:00
lcQUpdateDialog::lcQUpdateDialog(QWidget* Parent, bool InitialUpdate)
: QDialog(Parent), ui(new Ui::lcQUpdateDialog), mInitialUpdate(InitialUpdate)
2013-08-12 02:26:52 +02:00
{
ui->setupUi(this);
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::accept()
{
QSettings settings;
settings.setValue("Updates/IgnoreVersion", versionData);
QDialog::accept();
}
void lcQUpdateDialog::reject()
2013-08-09 06:57:18 +02:00
{
if (updateReply)
{
updateReply->abort();
updateReply->deleteLater();
updateReply = NULL;
}
QDialog::reject();
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
void lcQUpdateDialog::finished(int result)
2013-08-09 06:57:18 +02:00
{
2016-02-17 00:11:52 +01:00
Q_UNUSED(result);
2016-08-01 05:44:15 +02:00
if (mInitialUpdate)
2013-08-12 02:26:52 +02:00
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;
versionData = reply->readAll();
const char *update = versionData;
2013-08-09 06:57:18 +02:00
QSettings settings;
QByteArray ignoreUpdate = settings.value("Updates/IgnoreVersion", QByteArray()).toByteArray();
2016-08-01 05:44:15 +02:00
if (mInitialUpdate && ignoreUpdate == versionData)
{
updateAvailable = false;
}
else 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)
2013-10-21 23:49:26 +02:00
status = QString(tr("<p>There's a newer version of LeoCAD available for download (%1.%2.%3).</p>")).arg(QString::number(majorVersion), QString::number(minorVersion), QString::number(patchVersion));
2013-08-12 02:26:52 +02:00
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)
{
2017-02-09 02:58:49 +01:00
status += tr("<p>Visit <a href=\"https://github.com/leozide/leocad/releases\">https://github.com/leozide/leocad/releases</a> to download.</p>");
2013-08-12 02:26:52 +02:00
}
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
2016-09-22 17:04:51 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 0))
settings.setValue("Updates/LastCheck", QDateTime::currentDateTimeUtc());
2016-09-22 17:04:51 +02:00
#else
settings.setValue("Updates/LastCheck", QDateTime::currentDateTime());
#endif
2013-08-12 02:26:52 +02:00
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."));
2016-08-01 05:44:15 +02:00
if (mInitialUpdate)
2013-08-12 02:26:52 +02:00
{
if (updateAvailable)
show();
else
deleteLater();
}
2013-08-09 06:57:18 +02:00
if (updateAvailable)
ui->buttonBox->setStandardButtons(QDialogButtonBox::Close | QDialogButtonBox::Ignore);
else
ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
2013-08-09 06:57:18 +02:00
}