From 56c132a21e292fefd2ccad53d39c7dcacdb05c16 Mon Sep 17 00:00:00 2001 From: Alex Spataru Date: Fri, 9 Jan 2015 11:06:41 -0600 Subject: [PATCH] Improved code performance Fixed a "bug" in the checkForUpdates() function which caused the program to delay about 400 milliseconds. --- .../src/dialogs/download_dialog.cpp | 32 ++--- QSimpleUpdater/src/dialogs/download_dialog.h | 40 +++--- QSimpleUpdater/src/qsimpleupdater.cpp | 27 ++-- QSimpleUpdater/src/qsimpleupdater.h | 123 +++++++++--------- 4 files changed, 110 insertions(+), 112 deletions(-) diff --git a/QSimpleUpdater/src/dialogs/download_dialog.cpp b/QSimpleUpdater/src/dialogs/download_dialog.cpp index 3e98067..61e30aa 100644 --- a/QSimpleUpdater/src/dialogs/download_dialog.cpp +++ b/QSimpleUpdater/src/dialogs/download_dialog.cpp @@ -27,8 +27,8 @@ DownloadDialog::DownloadDialog (QWidget *parent) connect (ui->openButton, SIGNAL (clicked()), this, SLOT (installUpdate())); // Configure open button - ui->openButton->setEnabled(false); - ui->openButton->setVisible(false); + ui->openButton->setEnabled (false); + ui->openButton->setVisible (false); // Initialize the network access manager m_manager = new QNetworkAccessManager (this); @@ -38,7 +38,7 @@ DownloadDialog::DownloadDialog (QWidget *parent) SLOT (ignoreSslErrors (QNetworkReply *, QList))); } -DownloadDialog::~DownloadDialog(void) { +DownloadDialog::~DownloadDialog (void) { delete ui; } @@ -66,15 +66,15 @@ void DownloadDialog::beginDownload (const QUrl& url) { showNormal(); } -void DownloadDialog::installUpdate(void) { +void DownloadDialog::installUpdate (void) { QMessageBox msg; - msg.setIcon(QMessageBox::Question); - msg.setText("" + - tr("To apply the update(s), you must first quit %1") - .arg(qApp->applicationName()) + - ""); - msg.setInformativeText(tr("Do you want to quit %1 now?").arg(qApp->applicationName())); - msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + msg.setIcon (QMessageBox::Question); + msg.setText ("" + + tr ("To apply the update(s), you must first quit %1") + .arg (qApp->applicationName()) + + ""); + msg.setInformativeText (tr ("Do you want to quit %1 now?").arg (qApp->applicationName())); + msg.setStandardButtons (QMessageBox::Yes | QMessageBox::No); if (msg.exec() == QMessageBox::Yes) { openDownload(); @@ -82,13 +82,13 @@ void DownloadDialog::installUpdate(void) { } else { - ui->openButton->setEnabled(true); - ui->openButton->setVisible(true); + ui->openButton->setEnabled (true); + ui->openButton->setVisible (true); ui->timeLabel->setText (tr ("Click the \"Open\" button to apply the update")); } } -void DownloadDialog::openDownload(void) { +void DownloadDialog::openDownload (void) { if (!m_path.isEmpty()) { QString url = m_path; @@ -102,7 +102,7 @@ void DownloadDialog::openDownload(void) { } } -void DownloadDialog::cancelDownload(void) { +void DownloadDialog::cancelDownload (void) { if (!m_reply->isFinished()) { QMessageBox _message; _message.setWindowTitle (tr ("Updater")); @@ -120,7 +120,7 @@ void DownloadDialog::cancelDownload(void) { hide(); } -void DownloadDialog::downloadFinished(void) { +void DownloadDialog::downloadFinished (void) { ui->stopButton->setText (tr ("Close")); ui->downloadLabel->setText (tr ("Download complete!")); ui->timeLabel->setText (tr ("The installer will open in a separate window...")); diff --git a/QSimpleUpdater/src/dialogs/download_dialog.h b/QSimpleUpdater/src/dialogs/download_dialog.h index a5d5d0f..282a90e 100644 --- a/QSimpleUpdater/src/dialogs/download_dialog.h +++ b/QSimpleUpdater/src/dialogs/download_dialog.h @@ -31,34 +31,34 @@ class DownloadDialog; } class DownloadDialog : public QWidget { - Q_OBJECT + Q_OBJECT -public: - explicit DownloadDialog (QWidget *parent = 0); - ~DownloadDialog(void); + public: + explicit DownloadDialog (QWidget *parent = 0); + ~DownloadDialog (void); - void beginDownload (const QUrl& url); + void beginDownload (const QUrl& url); -private slots: - void openDownload(void); - void installUpdate(void); - void cancelDownload(void); - void downloadFinished(void); - void updateProgress (qint64 received, qint64 total); - void ignoreSslErrors (QNetworkReply *reply, const QList& error); + private slots: + void openDownload (void); + void installUpdate (void); + void cancelDownload (void); + void downloadFinished (void); + void updateProgress (qint64 received, qint64 total); + void ignoreSslErrors (QNetworkReply *reply, const QList& error); -private: - Ui::DownloadDialog *ui; + private: + Ui::DownloadDialog *ui; - QString m_path; - bool m_download_paused; + QString m_path; + bool m_download_paused; - QNetworkReply *m_reply; - QNetworkAccessManager *m_manager; + QNetworkReply *m_reply; + QNetworkAccessManager *m_manager; - uint m_start_time; + uint m_start_time; - float roundNumber (const float& input); + float roundNumber (const float& input); }; #endif diff --git a/QSimpleUpdater/src/qsimpleupdater.cpp b/QSimpleUpdater/src/qsimpleupdater.cpp index a2e8c17..b4283fb 100644 --- a/QSimpleUpdater/src/qsimpleupdater.cpp +++ b/QSimpleUpdater/src/qsimpleupdater.cpp @@ -20,30 +20,27 @@ QSimpleUpdater::QSimpleUpdater (QObject *parent) , m_version_check_finished (false) , m_new_version_available (false) { m_downloadDialog = new DownloadDialog(); + + m_manager = new QNetworkAccessManager (this); + connect (m_manager, SIGNAL (finished (QNetworkReply *)), this, + SLOT (checkDownloadedVersion (QNetworkReply *))); + connect (m_manager, SIGNAL (sslErrors (QNetworkReply *, QList)), + this, SLOT (ignoreSslErrors (QNetworkReply *, QList))); } -QString QSimpleUpdater::changeLog() const { +QString QSimpleUpdater::changeLog() const { return m_changelog; } -void QSimpleUpdater::checkForUpdates(void) { - if (!m_reference_url.isEmpty()) { - QNetworkAccessManager *_manager = new QNetworkAccessManager (this); - - connect (_manager, SIGNAL (finished (QNetworkReply *)), this, - SLOT (checkDownloadedVersion (QNetworkReply *))); - - connect (_manager, SIGNAL (sslErrors (QNetworkReply *, QList)), - this, SLOT (ignoreSslErrors (QNetworkReply *, QList))); - - _manager->get (QNetworkRequest (m_reference_url)); - } +void QSimpleUpdater::checkForUpdates (void) { + if (!m_reference_url.isEmpty()) + m_manager->get (QNetworkRequest (m_reference_url)); else qDebug() << "QSimpleUpdater: Invalid reference URL"; } -void QSimpleUpdater::openDownloadLink(void) { +void QSimpleUpdater::openDownloadLink (void) { if (!m_download_url.isEmpty()) QDesktopServices::openUrl (m_download_url); } @@ -56,7 +53,7 @@ QString QSimpleUpdater::installedVersion() const { return m_installed_version; } -void QSimpleUpdater::downloadLatestVersion(void) { +void QSimpleUpdater::downloadLatestVersion (void) { if (!m_download_url.isEmpty()) m_downloadDialog->beginDownload (m_download_url); } diff --git a/QSimpleUpdater/src/qsimpleupdater.h b/QSimpleUpdater/src/qsimpleupdater.h index cc99c98..86a8301 100644 --- a/QSimpleUpdater/src/qsimpleupdater.h +++ b/QSimpleUpdater/src/qsimpleupdater.h @@ -33,87 +33,88 @@ #include "dialogs/download_dialog.h" class QSimpleUpdater : public QObject { - Q_OBJECT + Q_OBJECT -public: - QSimpleUpdater (QObject *parent = 0); + public: + QSimpleUpdater (QObject *parent = 0); - /// Returns the downloaded change log - QString changeLog() const; + /// Returns the downloaded change log + QString changeLog() const; - /// Returns the downloaded version string - QString latestVersion() const; + /// Returns the downloaded version string + QString latestVersion() const; - /// Returns the local version, referenced by - /// the setApplicationVersion() function - QString installedVersion() const; + /// Returns the local version, referenced by + /// the setApplicationVersion() function + QString installedVersion() const; - /// Returns \c true if there's a newer version available - bool newerVersionAvailable() const; + /// Returns \c true if there's a newer version available + bool newerVersionAvailable() const; - /// Checks for updates and calls the appropriate - /// signals when finished - void checkForUpdates(void); + /// Checks for updates and calls the appropriate + /// signals when finished + void checkForUpdates (void); - /// Opens the download URL in a a web browser. - /// The URL is referenced by the \c setDownloadUrl() function - void openDownloadLink(void); + /// Opens the download URL in a a web browser. + /// The URL is referenced by the \c setDownloadUrl() function + void openDownloadLink (void); - /// Shows a dialog that downloads the file in the - /// URL referenced by the \c setDownloadUrl() function - void downloadLatestVersion(void); + /// Shows a dialog that downloads the file in the + /// URL referenced by the \c setDownloadUrl() function + void downloadLatestVersion (void); -public slots: + public slots: - /// Changes the URL that we can open in a web browser or - /// download. Its recommended to use fixed URLs if you - /// want to automatically download and install your updates - void setDownloadUrl (const QString& url); + /// Changes the URL that we can open in a web browser or + /// download. Its recommended to use fixed URLs if you + /// want to automatically download and install your updates + void setDownloadUrl (const QString& url); - /// Changes the reference URL, which contains ONLY the latest - /// version of your application as a plain text file. - /// Examples include: - /// - 1.2.3 - /// - 5.4.0 - /// - 0.1.2 - /// - etc. - void setReferenceUrl (const QString& url); + /// Changes the reference URL, which contains ONLY the latest + /// version of your application as a plain text file. + /// Examples include: + /// - 1.2.3 + /// - 5.4.0 + /// - 0.1.2 + /// - etc. + void setReferenceUrl (const QString& url); - /// Changes the change log URL, which contains the change log - /// of your application. The change log can be any file you - /// like, however, its recommended to write it in plain text, - /// such as TXT, HTML and RTF files. - void setChangelogUrl (const QString& url); + /// Changes the change log URL, which contains the change log + /// of your application. The change log can be any file you + /// like, however, its recommended to write it in plain text, + /// such as TXT, HTML and RTF files. + void setChangelogUrl (const QString& url); - /// Tells the updater the version of the installed - /// copy of your application. - void setApplicationVersion (const QString& version); + /// Tells the updater the version of the installed + /// copy of your application. + void setApplicationVersion (const QString& version); -private slots: - void checkDownloadedVersion (QNetworkReply *reply); - void processDownloadedChangelog (QNetworkReply *reply); - void ignoreSslErrors (QNetworkReply *reply, const QList& error); + private slots: + void checkDownloadedVersion (QNetworkReply *reply); + void processDownloadedChangelog (QNetworkReply *reply); + void ignoreSslErrors (QNetworkReply *reply, const QList& error); -signals: - void checkingFinished(void); - void versionCheckFinished(void); - void changelogDownloadFinished(void); + signals: + void checkingFinished (void); + void versionCheckFinished (void); + void changelogDownloadFinished (void); -private: - QString m_changelog; - QString m_latest_version; - QString m_installed_version; + private: + QString m_changelog; + QString m_latest_version; + QString m_installed_version; + QNetworkAccessManager *m_manager; - QUrl m_download_url; - QUrl m_reference_url; - QUrl m_changelog_url; + QUrl m_download_url; + QUrl m_reference_url; + QUrl m_changelog_url; - bool m_changelog_downloaded; - bool m_version_check_finished; + bool m_changelog_downloaded; + bool m_version_check_finished; - bool m_new_version_available; + bool m_new_version_available; - DownloadDialog *m_downloadDialog; + DownloadDialog *m_downloadDialog; }; #endif