UniversalIndentGUI 1.2.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2006-2012 by Thomas Schweitzer * 00003 * thomas-schweitzer(at)arcor.de * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License version 2.0 as * 00007 * published by the Free Software Foundation. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU General Public License * 00015 * along with this program in the file LICENSE.GPL; if not, write to the * 00016 * Free Software Foundation, Inc., * 00017 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00018 ***************************************************************************/ 00019 00020 #include "UpdateCheckDialog.h" 00021 #include "ui_UpdateCheckDialog.h" 00022 00023 #include "UiGuiSettings.h" 00024 #include "UiGuiVersion.h" 00025 00026 #include <QMessageBox> 00027 #include <QDesktopServices> 00028 #include <QNetworkAccessManager> 00029 #include <QTimer> 00030 #include <QDate> 00031 #include <QUrl> 00032 #include <QRegExpValidator> 00033 #include <QNetworkRequest> 00034 #include <QNetworkReply> 00035 #include <QNetworkProxy> 00036 00047 UpdateCheckDialog::UpdateCheckDialog(QSharedPointer<UiGuiSettings> settings, QWidget *parent) : QDialog(parent), 00048 _manualUpdateRequested(false), 00049 _currentNetworkReply(NULL), 00050 _roleOfClickedButton(QDialogButtonBox::InvalidRole) 00051 { 00052 _updateCheckDialogForm = new Ui::UpdateCheckDialog(); 00053 _updateCheckDialogForm->setupUi(this); 00054 00055 // Create object for _networkAccessManager request and connect it with the request return handler. 00056 _networkAccessManager = new QNetworkAccessManager(this); 00057 connect( _networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkResultsOfFetchedPadXMLFile(QNetworkReply*)) ); 00058 00059 // Create a timer object used for the progress bar. 00060 _updateCheckProgressTimer = new QTimer(this); 00061 _updateCheckProgressTimer->setInterval(5); 00062 connect( _updateCheckProgressTimer, SIGNAL(timeout()), this, SLOT(updateUpdateCheckProgressBar()) ); 00063 _updateCheckProgressCounter = 0; 00064 00065 // Connect the dialogs buttonbox with a button click handler. 00066 connect( _updateCheckDialogForm->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(handleUpdateCheckDialogButtonClicked(QAbstractButton*)) ); 00067 00068 settings->registerObjectSlot(this, "initProxySettings()", "ProxyEnabled"); 00069 settings->registerObjectSlot(this, "initProxySettings()", "ProxyHostName"); 00070 settings->registerObjectSlot(this, "initProxySettings()", "ProxyPort"); 00071 settings->registerObjectSlot(this, "initProxySettings()", "ProxyUserName"); 00072 settings->registerObjectSlot(this, "initProxySettings()", "ProxyPassword"); 00073 00074 _settings = settings; 00075 00076 initProxySettings(); 00077 00078 // This dialog is always modal. 00079 setModal(true); 00080 } 00081 00082 00086 UpdateCheckDialog::~UpdateCheckDialog() { 00087 disconnect( _networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkResultsOfFetchedPadXMLFile(QNetworkReply*)) ); 00088 if (_currentNetworkReply != NULL) 00089 _currentNetworkReply->abort(); 00090 } 00091 00092 00099 void UpdateCheckDialog::checkForUpdateAndShowDialog() { 00100 _manualUpdateRequested = true; 00101 getPadXMLFile(); 00102 showCheckingForUpdateDialog(); 00103 } 00104 00105 00112 void UpdateCheckDialog::checkForUpdate() { 00113 _manualUpdateRequested = false; 00114 getPadXMLFile(); 00115 } 00116 00117 00121 void UpdateCheckDialog::getPadXMLFile() { 00122 //_networkAccessManager->setHost("universalindent.sourceforge.net"); 00123 //_networkAccessManager->get("/universalindentgui_pad.xml"); 00124 _currentNetworkReply = _networkAccessManager->get(QNetworkRequest(QUrl("http://universalindent.sourceforge.net/universalindentgui_pad.xml"))); 00125 } 00126 00127 00136 void UpdateCheckDialog::checkResultsOfFetchedPadXMLFile(QNetworkReply *networkReply) { 00137 Q_ASSERT(_currentNetworkReply == networkReply); 00138 00139 // Stop the progress bar timer. 00140 _updateCheckProgressTimer->stop(); 00141 00142 if ( networkReply->error() == QNetworkReply::NoError ) { 00143 // Try to find the version string. 00144 QString returnedString = networkReply->readAll(); 00145 int leftPosition = returnedString.indexOf("<Program_Version>"); 00146 int rightPosition = returnedString.indexOf("</Program_Version>"); 00147 00148 // If the version string could be found in the returned string, show an update dialog and set last update check date. 00149 if ( leftPosition != -1 && rightPosition != -1 ) { 00150 // Get the pure version string from returned string. 00151 returnedString = returnedString.mid( leftPosition+17, rightPosition-(leftPosition+17) ); 00152 00153 // Create integer values from the version strings. 00154 int versionOnServerInt = convertVersionStringToNumber( returnedString ); 00155 int currentVersionInt = convertVersionStringToNumber( PROGRAM_VERSION_STRING ); 00156 00157 // Only show update dialog, if the current version number is lower than the one received from the server. 00158 if ( versionOnServerInt > currentVersionInt && currentVersionInt >= 0 && versionOnServerInt >= 0 ) { 00159 // Show message box whether to download the new version. 00160 showNewVersionAvailableDialog(returnedString); 00161 00162 // If yes clicked, open the download url in the default browser. 00163 if ( _roleOfClickedButton == QDialogButtonBox::YesRole ) { 00164 QDesktopServices::openUrl( QUrl("_networkAccessManager://sourceforge.net/project/showfiles.php?group_id=167482") ); 00165 } 00166 } 00167 else if ( _manualUpdateRequested ) { 00168 showNoNewVersionAvailableDialog(); 00169 } 00170 // Set last update check date. 00171 _settings->setValueByName("LastUpdateCheck", QDate::currentDate()); 00172 } 00173 // In the returned string, the version string could not be found. 00174 else { 00175 QMessageBox::warning(this, tr("Update check error"), tr("There was an error while trying to check for an update! The retrieved file did not contain expected content.") ); 00176 } 00177 } 00178 // If there was some error while trying to retrieve the update info from server and not cancel was pressed. 00179 else if ( _roleOfClickedButton != QDialogButtonBox::RejectRole ) { 00180 QMessageBox::warning(this, tr("Update check error"), tr("There was an error while trying to check for an update! Error was : %1").arg(networkReply->errorString()) ); 00181 hide(); 00182 } 00183 _manualUpdateRequested = false; 00184 networkReply->deleteLater(); 00185 _currentNetworkReply = NULL; 00186 } 00187 00188 00196 void UpdateCheckDialog::showCheckingForUpdateDialog() { 00197 // Reset the progress bar. 00198 _updateCheckProgressCounter = 0; 00199 _updateCheckDialogForm->progressBar->setValue(_updateCheckProgressCounter); 00200 _updateCheckDialogForm->progressBar->setInvertedAppearance( false ); 00201 00202 _updateCheckProgressTimer->start(); 00203 _updateCheckDialogForm->progressBar->show(); 00204 setWindowTitle( tr("Checking for update...") ); 00205 _updateCheckDialogForm->label->setText( tr("Checking whether a newer version is available") ); 00206 _updateCheckDialogForm->buttonBox->setStandardButtons(QDialogButtonBox::Cancel); 00207 show(); 00208 } 00209 00210 00214 void UpdateCheckDialog::showNewVersionAvailableDialog(QString newVersion) { 00215 _updateCheckDialogForm->progressBar->hide(); 00216 setWindowTitle( tr("Update available") ); 00217 _updateCheckDialogForm->label->setText( tr("A newer version of UniversalIndentGUI is available.\nYour version is %1. New version is %2.\nDo you want to go to the download website?").arg(PROGRAM_VERSION_STRING).arg(newVersion) ); 00218 _updateCheckDialogForm->buttonBox->setStandardButtons(QDialogButtonBox::No|QDialogButtonBox::NoButton|QDialogButtonBox::Yes); 00219 exec(); 00220 } 00221 00222 00226 void UpdateCheckDialog::showNoNewVersionAvailableDialog() { 00227 _updateCheckDialogForm->progressBar->hide(); 00228 setWindowTitle( tr("No new update available") ); 00229 _updateCheckDialogForm->label->setText( tr("You already have the latest version of UniversalIndentGUI.") ); 00230 _updateCheckDialogForm->buttonBox->setStandardButtons(QDialogButtonBox::Ok); 00231 exec(); 00232 } 00233 00234 00244 void UpdateCheckDialog::handleUpdateCheckDialogButtonClicked(QAbstractButton *clickedButton) { 00245 _roleOfClickedButton = _updateCheckDialogForm->buttonBox->buttonRole(clickedButton); 00246 00247 if ( _roleOfClickedButton == QDialogButtonBox::RejectRole ) { 00248 // Abort the _networkAccessManager request. 00249 _currentNetworkReply->abort(); 00250 // Stop the progress bar timer. 00251 _updateCheckProgressTimer->stop(); 00252 } 00253 00254 accept(); 00255 } 00256 00257 00261 void UpdateCheckDialog::updateUpdateCheckProgressBar() { 00262 // Depending on the progress bar direction, decrease or increase the progressbar value. 00263 if ( _updateCheckDialogForm->progressBar->invertedAppearance() ) { 00264 _updateCheckProgressCounter--; 00265 } 00266 else { 00267 _updateCheckProgressCounter++; 00268 } 00269 00270 // If the progress bar reaches 0 or 100 as value, swap the animation direction. 00271 if ( _updateCheckProgressCounter == 0 || _updateCheckProgressCounter == 100 ) { 00272 _updateCheckDialogForm->progressBar->setInvertedAppearance( !_updateCheckDialogForm->progressBar->invertedAppearance() ); 00273 } 00274 00275 // Update the progress bar value. 00276 _updateCheckDialogForm->progressBar->setValue(_updateCheckProgressCounter); 00277 } 00278 00279 00290 int UpdateCheckDialog::convertVersionStringToNumber(QString versionString) { 00291 int versionInteger = 0; 00292 int pos = 0; 00293 00294 QRegExp regEx("\\d{1,3}.\\d{1,3}.\\d{1,3}"); 00295 QRegExpValidator validator(regEx, NULL); 00296 00297 if ( validator.validate(versionString, pos) == QValidator::Acceptable ) { 00298 QStringList versionNumberStringList = versionString.split("."); 00299 versionInteger = versionNumberStringList.at(0).toInt() * 1000000; 00300 versionInteger += versionNumberStringList.at(1).toInt() * 1000; 00301 versionInteger += versionNumberStringList.at(2).toInt(); 00302 } 00303 else { 00304 versionInteger = -1; 00305 } 00306 00307 return versionInteger; 00308 } 00309 00310 void UpdateCheckDialog::initProxySettings() 00311 { 00312 if ( _settings->getValueByName("ProxyEnabled") == true ) { 00313 QString proxyHostName = _settings->getValueByName("ProxyHostName").toString(); 00314 int proxyPort = _settings->getValueByName("ProxyPort").toInt(); 00315 QString proxyUserName = _settings->getValueByName("ProxyUserName").toString(); 00316 QString proxyPassword = _settings->getValueByName("ProxyPassword").toString(); 00317 _networkAccessManager->setProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyHostName, proxyPort, proxyUserName, proxyPassword)); 00318 } 00319 else { 00320 _networkAccessManager->setProxy(QNetworkProxy()); 00321 } 00322 }