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()));
// 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<QSslError>)));
}
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("<b>" +
tr("To apply the update(s), you must first quit %1")
.arg(qApp->applicationName()) +
"</b>");
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 ("<b>" +
tr ("To apply the update(s), you must first quit %1")
.arg (qApp->applicationName()) +
"</b>");
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..."));

View File

@ -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<QSslError>& 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<QSslError>& 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

View File

@ -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<QSslError>)),
this, SLOT (ignoreSslErrors (QNetworkReply *, QList<QSslError>)));
}
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<QSslError>)),
this, SLOT (ignoreSslErrors (QNetworkReply *, QList<QSslError>)));
_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);
}

View File

@ -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<QSslError>& error);
private slots:
void checkDownloadedVersion (QNetworkReply *reply);
void processDownloadedChangelog (QNetworkReply *reply);
void ignoreSslErrors (QNetworkReply *reply, const QList<QSslError>& 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