leocad/qt/lc_qupdatedialog.cpp

153 lines
3.7 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"
2018-04-14 20:44:39 +02:00
#include "lc_http.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-05-08 04:16:52 +02:00
if (NextCheckTime > QDateTime::currentDateTimeUtc())
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
new lcQUpdateDialog(nullptr, 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..."));
2018-04-14 20:44:39 +02:00
mHttpManager = new lcHttpManager(this);
connect(mHttpManager, SIGNAL(DownloadFinished(lcHttpReply*)), this, SLOT(DownloadFinished(lcHttpReply*)));
2013-08-12 02:26:52 +02:00
2018-04-14 20:44:39 +02:00
mHttpManager->DownloadFile(QLatin1String("https://www.leocad.org/updates.txt"));
2013-08-09 06:57:18 +02:00
}
lcQUpdateDialog::~lcQUpdateDialog()
{
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
{
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
}
2018-04-14 20:44:39 +02:00
void lcQUpdateDialog::DownloadFinished(lcHttpReply *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
settings.setValue("Updates/LastCheck", QDateTime::currentDateTimeUtc());
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
}