comparison src/UpdateCheckDialog.cpp @ 751:ac165b6ae67e

Done some refactoring: - Moved includes into the cpp files where possible and using class pre-declarations if possible - Made class member variable names begin with an underscore - Made by uic created header files be used as class members instead of inherting them - Renamed some variables to reflect their purpose better - Added some NULL initializations and added some comments - Rearranged some include and declaration code parts to be consistent and better readable - Updated for QScintilla 2.4.5 - Made UiGuiSettings be accessed via a shared pointer only git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@1028 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Thu, 14 Oct 2010 19:52:47 +0000
parents aae5a8d04f70
children f3631db17328
comparison
equal deleted inserted replaced
750:a884b5861e93 751:ac165b6ae67e
16 * Free Software Foundation, Inc., * 16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/ 18 ***************************************************************************/
19 19
20 #include "UpdateCheckDialog.h" 20 #include "UpdateCheckDialog.h"
21 21 #include "ui_UpdateCheckDialog.h"
22
23 #include "UiGuiSettings.h"
22 #include "UiGuiVersion.h" 24 #include "UiGuiVersion.h"
23 25
26 #include <QMessageBox>
24 #include <QDesktopServices> 27 #include <QDesktopServices>
25 #include <QNetworkAccessManager> 28 #include <QNetworkAccessManager>
26 #include <QTimer> 29 #include <QTimer>
30 #include <QDate>
27 #include <QUrl> 31 #include <QUrl>
28 #include <QRegExpValidator> 32 #include <QRegExpValidator>
29 #include <QNetworkRequest> 33 #include <QNetworkRequest>
30 #include <QNetworkReply> 34 #include <QNetworkReply>
31 35
35 \brief UpdateCheckDialog is a dialog widget that contains functions 39 \brief UpdateCheckDialog is a dialog widget that contains functions
36 for online checking for a new version of UniversalIndentGUI. 40 for online checking for a new version of UniversalIndentGUI.
37 */ 41 */
38 42
39 /*! 43 /*!
40 \brief Initializes member variables and stores the version of UiGui and a pointer to the settings object. 44 \brief Initializes member variables and stores the version of UiGui and a pointer to the _settings object.
41 */ 45 */
42 UpdateCheckDialog::UpdateCheckDialog(UiGuiSettings *settings, QWidget *parent) : QDialog(parent), 46 UpdateCheckDialog::UpdateCheckDialog(QSharedPointer<UiGuiSettings> settings, QWidget *parent) : QDialog(parent),
43 manualUpdateRequested(false), 47 _manualUpdateRequested(false),
44 currentNetworkReply(NULL), 48 _currentNetworkReply(NULL),
45 roleOfClickedButton(QDialogButtonBox::InvalidRole) 49 _roleOfClickedButton(QDialogButtonBox::InvalidRole)
46 { 50 {
47 setupUi(this); 51 _updateCheckDialogForm = new Ui::UpdateCheckDialog();
48 52 _updateCheckDialogForm->setupUi(this);
49 // Create object for networkAccessManager request and connect it with the request return handler. 53
50 networkAccessManager = new QNetworkAccessManager(this); 54 // Create object for _networkAccessManager request and connect it with the request return handler.
51 connect( networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkResultsOfFetchedPadXMLFile(QNetworkReply*)) ); 55 _networkAccessManager = new QNetworkAccessManager(this);
56 connect( _networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkResultsOfFetchedPadXMLFile(QNetworkReply*)) );
52 57
53 // Create a timer object used for the progress bar. 58 // Create a timer object used for the progress bar.
54 updateCheckProgressTimer = new QTimer(this); 59 _updateCheckProgressTimer = new QTimer(this);
55 updateCheckProgressTimer->setInterval(5); 60 _updateCheckProgressTimer->setInterval(5);
56 connect( updateCheckProgressTimer, SIGNAL(timeout()), this, SLOT(updateUpdateCheckProgressBar()) ); 61 connect( _updateCheckProgressTimer, SIGNAL(timeout()), this, SLOT(updateUpdateCheckProgressBar()) );
57 updateCheckProgressCounter = 0; 62 _updateCheckProgressCounter = 0;
58 63
59 // Connect the dialogs buttonbox with a button click handler. 64 // Connect the dialogs buttonbox with a button click handler.
60 connect( buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(handleUpdateCheckDialogButtonClicked(QAbstractButton*)) ); 65 connect( _updateCheckDialogForm->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(handleUpdateCheckDialogButtonClicked(QAbstractButton*)) );
61 66
62 this->settings = settings; 67 _settings = settings;
63 68
64 // This dialog is always modal. 69 // This dialog is always modal.
65 setModal(true); 70 setModal(true);
66 } 71 }
67 72
68 73
69 /*! 74 /*!
70 \brief On destroy cancels any currently running network request. 75 \brief On destroy cancels any currently running network request.
71 */ 76 */
72 UpdateCheckDialog::~UpdateCheckDialog() { 77 UpdateCheckDialog::~UpdateCheckDialog() {
73 disconnect( networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkResultsOfFetchedPadXMLFile(QNetworkReply*)) ); 78 disconnect( _networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(checkResultsOfFetchedPadXMLFile(QNetworkReply*)) );
74 if (currentNetworkReply != NULL) 79 if (_currentNetworkReply != NULL)
75 currentNetworkReply->abort(); 80 _currentNetworkReply->abort();
76 } 81 }
77 82
78 83
79 /*! 84 /*!
80 \brief This slot should be called, if an update check is manually invoked. 85 \brief This slot should be called, if an update check is manually invoked.
81 86
82 In difference to the automatic update check, during manual update check request 87 In difference to the automatic update check, during manual update check request
83 a modal progress indicator dialog will be shown. 88 a modal progress indicator dialog will be shown.
84 */ 89 */
85 void UpdateCheckDialog::checkForUpdateAndShowDialog() { 90 void UpdateCheckDialog::checkForUpdateAndShowDialog() {
86 manualUpdateRequested = true; 91 _manualUpdateRequested = true;
87 getPadXMLFile(); 92 getPadXMLFile();
88 showCheckingForUpdateDialog(); 93 showCheckingForUpdateDialog();
89 } 94 }
90 95
91 96
94 99
95 An automatic invoked update check should run in background, so the user 100 An automatic invoked update check should run in background, so the user
96 gets not interrupted by a dialog box. 101 gets not interrupted by a dialog box.
97 */ 102 */
98 void UpdateCheckDialog::checkForUpdate() { 103 void UpdateCheckDialog::checkForUpdate() {
99 manualUpdateRequested = false; 104 _manualUpdateRequested = false;
100 getPadXMLFile(); 105 getPadXMLFile();
101 } 106 }
102 107
103 108
104 /*! 109 /*!
105 \brief This function tries to download the UniversalIndentGui pad file from the SourceForge server. 110 \brief This function tries to download the UniversalIndentGui pad file from the SourceForge server.
106 */ 111 */
107 void UpdateCheckDialog::getPadXMLFile() { 112 void UpdateCheckDialog::getPadXMLFile() {
108 //networkAccessManager->setHost("universalindent.sourceforge.net"); 113 //_networkAccessManager->setHost("universalindent.sourceforge.net");
109 //networkAccessManager->get("/universalindentgui_pad.xml"); 114 //_networkAccessManager->get("/universalindentgui_pad.xml");
110 currentNetworkReply = networkAccessManager->get(QNetworkRequest(QUrl("http://universalindent.sourceforge.net/universalindentgui_pad.xml"))); 115 _currentNetworkReply = _networkAccessManager->get(QNetworkRequest(QUrl("http://universalindent.sourceforge.net/universalindentgui_pad.xml")));
111 } 116 }
112 117
113 118
114 /*! 119 /*!
115 \brief This slot is called after the update check has returned, either by successfully 120 \brief This slot is called after the update check has returned, either by successfully
118 Shows a message if check was successful or not. Offers the user to go to the 123 Shows a message if check was successful or not. Offers the user to go to the
119 download page if a newer version exists. In case of an error during update 124 download page if a newer version exists. In case of an error during update
120 check, a message box with the error will be displayed. 125 check, a message box with the error will be displayed.
121 */ 126 */
122 void UpdateCheckDialog::checkResultsOfFetchedPadXMLFile(QNetworkReply *networkReply) { 127 void UpdateCheckDialog::checkResultsOfFetchedPadXMLFile(QNetworkReply *networkReply) {
123 Q_ASSERT(currentNetworkReply == networkReply); 128 Q_ASSERT(_currentNetworkReply == networkReply);
124 129
125 // Stop the progress bar timer. 130 // Stop the progress bar timer.
126 updateCheckProgressTimer->stop(); 131 _updateCheckProgressTimer->stop();
127 132
128 if ( networkReply->error() == QNetworkReply::NoError ) { 133 if ( networkReply->error() == QNetworkReply::NoError ) {
129 // Try to find the version string. 134 // Try to find the version string.
130 QString returnedString = networkReply->readAll(); 135 QString returnedString = networkReply->readAll();
131 int leftPosition = returnedString.indexOf("<Program_Version>"); 136 int leftPosition = returnedString.indexOf("<Program_Version>");
144 if ( versionOnServerInt > currentVersionInt && currentVersionInt >= 0 && versionOnServerInt >= 0 ) { 149 if ( versionOnServerInt > currentVersionInt && currentVersionInt >= 0 && versionOnServerInt >= 0 ) {
145 // Show message box whether to download the new version. 150 // Show message box whether to download the new version.
146 showNewVersionAvailableDialog(returnedString); 151 showNewVersionAvailableDialog(returnedString);
147 152
148 // If yes clicked, open the download url in the default browser. 153 // If yes clicked, open the download url in the default browser.
149 if ( roleOfClickedButton == QDialogButtonBox::YesRole ) { 154 if ( _roleOfClickedButton == QDialogButtonBox::YesRole ) {
150 QDesktopServices::openUrl( QUrl("networkAccessManager://sourceforge.net/project/showfiles.php?group_id=167482") ); 155 QDesktopServices::openUrl( QUrl("_networkAccessManager://sourceforge.net/project/showfiles.php?group_id=167482") );
151 } 156 }
152 } 157 }
153 else if ( manualUpdateRequested ) { 158 else if ( _manualUpdateRequested ) {
154 showNoNewVersionAvailableDialog(); 159 showNoNewVersionAvailableDialog();
155 } 160 }
156 // Set last update check date. 161 // Set last update check date.
157 settings->setValueByName("LastUpdateCheck", QDate::currentDate()); 162 _settings->setValueByName("LastUpdateCheck", QDate::currentDate());
158 } 163 }
159 // In the returned string, the version string could not be found. 164 // In the returned string, the version string could not be found.
160 else { 165 else {
161 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.") ); 166 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.") );
162 } 167 }
163 } 168 }
164 // If there was some error while trying to retrieve the update info from server and not cancel was pressed. 169 // If there was some error while trying to retrieve the update info from server and not cancel was pressed.
165 else if ( roleOfClickedButton != QDialogButtonBox::RejectRole ) { 170 else if ( _roleOfClickedButton != QDialogButtonBox::RejectRole ) {
166 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()) ); 171 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()) );
167 } 172 }
168 manualUpdateRequested = false; 173 _manualUpdateRequested = false;
169 networkReply->deleteLater(); 174 networkReply->deleteLater();
170 currentNetworkReply = NULL; 175 _currentNetworkReply = NULL;
171 } 176 }
172 177
173 178
174 /*! 179 /*!
175 \brief Displays the progress bar during update check. 180 \brief Displays the progress bar during update check.
178 updated the progress bar. The user can press a cancel button to 183 updated the progress bar. The user can press a cancel button to
179 stop the update check. 184 stop the update check.
180 */ 185 */
181 void UpdateCheckDialog::showCheckingForUpdateDialog() { 186 void UpdateCheckDialog::showCheckingForUpdateDialog() {
182 // Reset the progress bar. 187 // Reset the progress bar.
183 updateCheckProgressCounter = 0; 188 _updateCheckProgressCounter = 0;
184 progressBar->setValue(updateCheckProgressCounter); 189 _updateCheckDialogForm->progressBar->setValue(_updateCheckProgressCounter);
185 progressBar->setInvertedAppearance( false ); 190 _updateCheckDialogForm->progressBar->setInvertedAppearance( false );
186 191
187 updateCheckProgressTimer->start(); 192 _updateCheckProgressTimer->start();
188 progressBar->show(); 193 _updateCheckDialogForm->progressBar->show();
189 setWindowTitle( tr("Checking for update...") ); 194 setWindowTitle( tr("Checking for update...") );
190 label->setText( tr("Checking whether a newer version is available") ); 195 _updateCheckDialogForm->label->setText( tr("Checking whether a newer version is available") );
191 buttonBox->setStandardButtons(QDialogButtonBox::Cancel); 196 _updateCheckDialogForm->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
192 show(); 197 show();
193 } 198 }
194 199
195 200
196 /*! 201 /*!
197 \brief Displays the dialog with info about the new available version. 202 \brief Displays the dialog with info about the new available version.
198 */ 203 */
199 void UpdateCheckDialog::showNewVersionAvailableDialog(QString newVersion) { 204 void UpdateCheckDialog::showNewVersionAvailableDialog(QString newVersion) {
200 progressBar->hide(); 205 _updateCheckDialogForm->progressBar->hide();
201 setWindowTitle( tr("Update available") ); 206 setWindowTitle( tr("Update available") );
202 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) ); 207 _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) );
203 buttonBox->setStandardButtons(QDialogButtonBox::No|QDialogButtonBox::NoButton|QDialogButtonBox::Yes); 208 _updateCheckDialogForm->buttonBox->setStandardButtons(QDialogButtonBox::No|QDialogButtonBox::NoButton|QDialogButtonBox::Yes);
204 exec(); 209 exec();
205 } 210 }
206 211
207 212
208 /*! 213 /*!
209 \brief Displays the dialog, that no new version is available. 214 \brief Displays the dialog, that no new version is available.
210 */ 215 */
211 void UpdateCheckDialog::showNoNewVersionAvailableDialog() { 216 void UpdateCheckDialog::showNoNewVersionAvailableDialog() {
212 progressBar->hide(); 217 _updateCheckDialogForm->progressBar->hide();
213 setWindowTitle( tr("No new update available") ); 218 setWindowTitle( tr("No new update available") );
214 label->setText( tr("You already have the latest version of UniversalIndentGUI.") ); 219 _updateCheckDialogForm->label->setText( tr("You already have the latest version of UniversalIndentGUI.") );
215 buttonBox->setStandardButtons(QDialogButtonBox::Ok); 220 _updateCheckDialogForm->buttonBox->setStandardButtons(QDialogButtonBox::Ok);
216 exec(); 221 exec();
217 } 222 }
218 223
219 224
220 /*! 225 /*!
221 \brief This slot is called, when a button in the dialog is clicked. 226 \brief This slot is called, when a button in the dialog is clicked.
222 227
223 If the clicked button was the cancel button, the user wants to cancel 228 If the clicked button was the cancel button, the user wants to cancel
224 the update check. So the networkAccessManager request is aborted and the timer for the 229 the update check. So the _networkAccessManager request is aborted and the timer for the
225 progress bar animation is stopped. 230 progress bar animation is stopped.
226 231
227 In any case if a button is clicked, the dialog box will be closed. 232 In any case if a button is clicked, the dialog box will be closed.
228 */ 233 */
229 void UpdateCheckDialog::handleUpdateCheckDialogButtonClicked(QAbstractButton *clickedButton) { 234 void UpdateCheckDialog::handleUpdateCheckDialogButtonClicked(QAbstractButton *clickedButton) {
230 roleOfClickedButton = buttonBox->buttonRole(clickedButton); 235 _roleOfClickedButton = _updateCheckDialogForm->buttonBox->buttonRole(clickedButton);
231 236
232 if ( roleOfClickedButton == QDialogButtonBox::RejectRole ) { 237 if ( _roleOfClickedButton == QDialogButtonBox::RejectRole ) {
233 // Abort the networkAccessManager request. 238 // Abort the _networkAccessManager request.
234 currentNetworkReply->abort(); 239 _currentNetworkReply->abort();
235 // Stop the progress bar timer. 240 // Stop the progress bar timer.
236 updateCheckProgressTimer->stop(); 241 _updateCheckProgressTimer->stop();
237 } 242 }
238 243
239 accept(); 244 accept();
240 } 245 }
241 246
243 /*! 248 /*!
244 \brief This slot is responsible for the animation of the update check progress bar. 249 \brief This slot is responsible for the animation of the update check progress bar.
245 */ 250 */
246 void UpdateCheckDialog::updateUpdateCheckProgressBar() { 251 void UpdateCheckDialog::updateUpdateCheckProgressBar() {
247 // Depending on the progress bar direction, decrease or increase the progressbar value. 252 // Depending on the progress bar direction, decrease or increase the progressbar value.
248 if ( progressBar->invertedAppearance() ) { 253 if ( _updateCheckDialogForm->progressBar->invertedAppearance() ) {
249 updateCheckProgressCounter--; 254 _updateCheckProgressCounter--;
250 } 255 }
251 else { 256 else {
252 updateCheckProgressCounter++; 257 _updateCheckProgressCounter++;
253 } 258 }
254 259
255 // If the progress bar reaches 0 or 100 as value, swap the animation direction. 260 // If the progress bar reaches 0 or 100 as value, swap the animation direction.
256 if ( updateCheckProgressCounter == 0 || updateCheckProgressCounter == 100 ) { 261 if ( _updateCheckProgressCounter == 0 || _updateCheckProgressCounter == 100 ) {
257 progressBar->setInvertedAppearance( !progressBar->invertedAppearance() ); 262 _updateCheckDialogForm->progressBar->setInvertedAppearance( !_updateCheckDialogForm->progressBar->invertedAppearance() );
258 } 263 }
259 264
260 // Update the progress bar value. 265 // Update the progress bar value.
261 progressBar->setValue(updateCheckProgressCounter); 266 _updateCheckDialogForm->progressBar->setValue(_updateCheckProgressCounter);
262 } 267 }
263 268
264 269
265 /*! 270 /*!
266 \brief Converts the as string given version \a versionString to an integer number. 271 \brief Converts the as string given version \a versionString to an integer number.