#include "lc_global.h" #include "lc_qupdatedialog.h" #include "ui_lc_qupdatedialog.h" #include "lc_application.h" #include "lc_library.h" lcQUpdateDialog::lcQUpdateDialog(QWidget *parent, void *data) : QDialog(parent), ui(new Ui::lcQUpdateDialog) { ui->setupUi(this); if (!data) { 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"))); } else { manager = NULL; updateReply = NULL; parseUpdate((const char*)data); } } 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(); } void lcQUpdateDialog::replyFinished(QNetworkReply *reply) { if (reply->error() == QNetworkReply::NoError) { parseUpdate(reply->readAll()); reply->deleteLater(); } else { ui->status->setText(tr("Error connecting to the update server.")); } updateReply = NULL; } void lcQUpdateDialog::parseUpdate(const char *update) { int majorVersion, minorVersion, patchVersion; int parts; if (sscanf(update, "%d.%d.%d %d", &majorVersion, &minorVersion, &patchVersion, &parts) == 4) { QString status; bool updateAvailable = false; if (majorVersion > LC_VERSION_MAJOR) updateAvailable = true; else if (majorVersion == LC_VERSION_MAJOR) { if (minorVersion > LC_VERSION_MINOR) updateAvailable = true; else if (minorVersion == LC_VERSION_MINOR) { if (patchVersion > LC_VERSION_PATCH) updateAvailable = true; } } if (updateAvailable) status = QString(tr("
There's a newer version of LeoCAD available for download (%1.%2.%3).
")).arg(majorVersion, minorVersion, patchVersion); else status = tr("You are using the latest LeoCAD version.
"); lcPiecesLibrary* library = lcGetPiecesLibrary(); if (library->mNumOfficialPieces) { if (parts > library->mNumOfficialPieces) { status += tr("There are new parts available.
"); updateAvailable = true; } else status += tr("There are no new parts available at this time.
"); } if (updateAvailable) { status += tr("Visit http://www.leocad.org/files/ to download.
"); } ui->status->setText(status); } else ui->status->setText(tr("Error parsing update information.")); ui->buttonBox->setStandardButtons(QDialogButtonBox::Close); }