Improved code performance

Fixed a "bug" in the checkForUpdates() function which caused the program
to delay about 400 milliseconds.
This commit is contained in:
Alex Spataru 2015-01-09 11:06:41 -06:00
parent 7b0444263e
commit 56c132a21e
4 changed files with 110 additions and 112 deletions

View File

@ -27,8 +27,8 @@ DownloadDialog::DownloadDialog (QWidget *parent)
connect (ui->openButton, SIGNAL (clicked()), this, SLOT (installUpdate())); connect (ui->openButton, SIGNAL (clicked()), this, SLOT (installUpdate()));
// Configure open button // Configure open button
ui->openButton->setEnabled(false); ui->openButton->setEnabled (false);
ui->openButton->setVisible(false); ui->openButton->setVisible (false);
// Initialize the network access manager // Initialize the network access manager
m_manager = new QNetworkAccessManager (this); m_manager = new QNetworkAccessManager (this);
@ -38,7 +38,7 @@ DownloadDialog::DownloadDialog (QWidget *parent)
SLOT (ignoreSslErrors (QNetworkReply *, QList<QSslError>))); SLOT (ignoreSslErrors (QNetworkReply *, QList<QSslError>)));
} }
DownloadDialog::~DownloadDialog(void) { DownloadDialog::~DownloadDialog (void) {
delete ui; delete ui;
} }
@ -66,15 +66,15 @@ void DownloadDialog::beginDownload (const QUrl& url) {
showNormal(); showNormal();
} }
void DownloadDialog::installUpdate(void) { void DownloadDialog::installUpdate (void) {
QMessageBox msg; QMessageBox msg;
msg.setIcon(QMessageBox::Question); msg.setIcon (QMessageBox::Question);
msg.setText("<b>" + msg.setText ("<b>" +
tr("To apply the update(s), you must first quit %1") tr ("To apply the update(s), you must first quit %1")
.arg(qApp->applicationName()) + .arg (qApp->applicationName()) +
"</b>"); "</b>");
msg.setInformativeText(tr("Do you want to quit %1 now?").arg(qApp->applicationName())); msg.setInformativeText (tr ("Do you want to quit %1 now?").arg (qApp->applicationName()));
msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msg.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
if (msg.exec() == QMessageBox::Yes) { if (msg.exec() == QMessageBox::Yes) {
openDownload(); openDownload();
@ -82,13 +82,13 @@ void DownloadDialog::installUpdate(void) {
} }
else { else {
ui->openButton->setEnabled(true); ui->openButton->setEnabled (true);
ui->openButton->setVisible(true); ui->openButton->setVisible (true);
ui->timeLabel->setText (tr ("Click the \"Open\" button to apply the update")); ui->timeLabel->setText (tr ("Click the \"Open\" button to apply the update"));
} }
} }
void DownloadDialog::openDownload(void) { void DownloadDialog::openDownload (void) {
if (!m_path.isEmpty()) { if (!m_path.isEmpty()) {
QString url = m_path; QString url = m_path;
@ -102,7 +102,7 @@ void DownloadDialog::openDownload(void) {
} }
} }
void DownloadDialog::cancelDownload(void) { void DownloadDialog::cancelDownload (void) {
if (!m_reply->isFinished()) { if (!m_reply->isFinished()) {
QMessageBox _message; QMessageBox _message;
_message.setWindowTitle (tr ("Updater")); _message.setWindowTitle (tr ("Updater"));
@ -120,7 +120,7 @@ void DownloadDialog::cancelDownload(void) {
hide(); hide();
} }
void DownloadDialog::downloadFinished(void) { void DownloadDialog::downloadFinished (void) {
ui->stopButton->setText (tr ("Close")); ui->stopButton->setText (tr ("Close"));
ui->downloadLabel->setText (tr ("Download complete!")); ui->downloadLabel->setText (tr ("Download complete!"));
ui->timeLabel->setText (tr ("The installer will open in a separate window...")); ui->timeLabel->setText (tr ("The installer will open in a separate window..."));

View File

@ -31,34 +31,34 @@ class DownloadDialog;
} }
class DownloadDialog : public QWidget { class DownloadDialog : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit DownloadDialog (QWidget *parent = 0); explicit DownloadDialog (QWidget *parent = 0);
~DownloadDialog(void); ~DownloadDialog (void);
void beginDownload (const QUrl& url); void beginDownload (const QUrl& url);
private slots: private slots:
void openDownload(void); void openDownload (void);
void installUpdate(void); void installUpdate (void);
void cancelDownload(void); void cancelDownload (void);
void downloadFinished(void); void downloadFinished (void);
void updateProgress (qint64 received, qint64 total); void updateProgress (qint64 received, qint64 total);
void ignoreSslErrors (QNetworkReply *reply, const QList<QSslError>& error); void ignoreSslErrors (QNetworkReply *reply, const QList<QSslError>& error);
private: private:
Ui::DownloadDialog *ui; Ui::DownloadDialog *ui;
QString m_path; QString m_path;
bool m_download_paused; bool m_download_paused;
QNetworkReply *m_reply; QNetworkReply *m_reply;
QNetworkAccessManager *m_manager; QNetworkAccessManager *m_manager;
uint m_start_time; uint m_start_time;
float roundNumber (const float& input); float roundNumber (const float& input);
}; };
#endif #endif

View File

@ -20,30 +20,27 @@ QSimpleUpdater::QSimpleUpdater (QObject *parent)
, m_version_check_finished (false) , m_version_check_finished (false)
, m_new_version_available (false) { , m_new_version_available (false) {
m_downloadDialog = new DownloadDialog(); 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<QSslError>)),
this, SLOT (ignoreSslErrors (QNetworkReply *, QList<QSslError>)));
} }
QString QSimpleUpdater::changeLog() const { QString QSimpleUpdater::changeLog() const {
return m_changelog; return m_changelog;
} }
void QSimpleUpdater::checkForUpdates(void) { void QSimpleUpdater::checkForUpdates (void) {
if (!m_reference_url.isEmpty()) { if (!m_reference_url.isEmpty())
QNetworkAccessManager *_manager = new QNetworkAccessManager (this); m_manager->get (QNetworkRequest (m_reference_url));
connect (_manager, SIGNAL (finished (QNetworkReply *)), this,
SLOT (checkDownloadedVersion (QNetworkReply *)));
connect (_manager, SIGNAL (sslErrors (QNetworkReply *, QList<QSslError>)),
this, SLOT (ignoreSslErrors (QNetworkReply *, QList<QSslError>)));
_manager->get (QNetworkRequest (m_reference_url));
}
else else
qDebug() << "QSimpleUpdater: Invalid reference URL"; qDebug() << "QSimpleUpdater: Invalid reference URL";
} }
void QSimpleUpdater::openDownloadLink(void) { void QSimpleUpdater::openDownloadLink (void) {
if (!m_download_url.isEmpty()) if (!m_download_url.isEmpty())
QDesktopServices::openUrl (m_download_url); QDesktopServices::openUrl (m_download_url);
} }
@ -56,7 +53,7 @@ QString QSimpleUpdater::installedVersion() const {
return m_installed_version; return m_installed_version;
} }
void QSimpleUpdater::downloadLatestVersion(void) { void QSimpleUpdater::downloadLatestVersion (void) {
if (!m_download_url.isEmpty()) if (!m_download_url.isEmpty())
m_downloadDialog->beginDownload (m_download_url); m_downloadDialog->beginDownload (m_download_url);
} }

View File

@ -33,87 +33,88 @@
#include "dialogs/download_dialog.h" #include "dialogs/download_dialog.h"
class QSimpleUpdater : public QObject { class QSimpleUpdater : public QObject {
Q_OBJECT Q_OBJECT
public: public:
QSimpleUpdater (QObject *parent = 0); QSimpleUpdater (QObject *parent = 0);
/// Returns the downloaded change log /// Returns the downloaded change log
QString changeLog() const; QString changeLog() const;
/// Returns the downloaded version string /// Returns the downloaded version string
QString latestVersion() const; QString latestVersion() const;
/// Returns the local version, referenced by /// Returns the local version, referenced by
/// the setApplicationVersion() function /// the setApplicationVersion() function
QString installedVersion() const; QString installedVersion() const;
/// Returns \c true if there's a newer version available /// Returns \c true if there's a newer version available
bool newerVersionAvailable() const; bool newerVersionAvailable() const;
/// Checks for updates and calls the appropriate /// Checks for updates and calls the appropriate
/// signals when finished /// signals when finished
void checkForUpdates(void); void checkForUpdates (void);
/// Opens the download URL in a a web browser. /// Opens the download URL in a a web browser.
/// The URL is referenced by the \c setDownloadUrl() function /// The URL is referenced by the \c setDownloadUrl() function
void openDownloadLink(void); void openDownloadLink (void);
/// Shows a dialog that downloads the file in the /// Shows a dialog that downloads the file in the
/// URL referenced by the \c setDownloadUrl() function /// URL referenced by the \c setDownloadUrl() function
void downloadLatestVersion(void); void downloadLatestVersion (void);
public slots: public slots:
/// Changes the URL that we can open in a web browser or /// Changes the URL that we can open in a web browser or
/// download. Its recommended to use fixed URLs if you /// download. Its recommended to use fixed URLs if you
/// want to automatically download and install your updates /// want to automatically download and install your updates
void setDownloadUrl (const QString& url); void setDownloadUrl (const QString& url);
/// Changes the reference URL, which contains ONLY the latest /// Changes the reference URL, which contains ONLY the latest
/// version of your application as a plain text file. /// version of your application as a plain text file.
/// Examples include: /// Examples include:
/// - 1.2.3 /// - 1.2.3
/// - 5.4.0 /// - 5.4.0
/// - 0.1.2 /// - 0.1.2
/// - etc. /// - etc.
void setReferenceUrl (const QString& url); void setReferenceUrl (const QString& url);
/// Changes the change log URL, which contains the change log /// Changes the change log URL, which contains the change log
/// of your application. The change log can be any file you /// of your application. The change log can be any file you
/// like, however, its recommended to write it in plain text, /// like, however, its recommended to write it in plain text,
/// such as TXT, HTML and RTF files. /// such as TXT, HTML and RTF files.
void setChangelogUrl (const QString& url); void setChangelogUrl (const QString& url);
/// Tells the updater the version of the installed /// Tells the updater the version of the installed
/// copy of your application. /// copy of your application.
void setApplicationVersion (const QString& version); void setApplicationVersion (const QString& version);
private slots: private slots:
void checkDownloadedVersion (QNetworkReply *reply); void checkDownloadedVersion (QNetworkReply *reply);
void processDownloadedChangelog (QNetworkReply *reply); void processDownloadedChangelog (QNetworkReply *reply);
void ignoreSslErrors (QNetworkReply *reply, const QList<QSslError>& error); void ignoreSslErrors (QNetworkReply *reply, const QList<QSslError>& error);
signals: signals:
void checkingFinished(void); void checkingFinished (void);
void versionCheckFinished(void); void versionCheckFinished (void);
void changelogDownloadFinished(void); void changelogDownloadFinished (void);
private: private:
QString m_changelog; QString m_changelog;
QString m_latest_version; QString m_latest_version;
QString m_installed_version; QString m_installed_version;
QNetworkAccessManager *m_manager;
QUrl m_download_url; QUrl m_download_url;
QUrl m_reference_url; QUrl m_reference_url;
QUrl m_changelog_url; QUrl m_changelog_url;
bool m_changelog_downloaded; bool m_changelog_downloaded;
bool m_version_check_finished; bool m_version_check_finished;
bool m_new_version_available; bool m_new_version_available;
DownloadDialog *m_downloadDialog; DownloadDialog *m_downloadDialog;
}; };
#endif #endif