@@ -191,6 +191,11 @@ QString Application::getDocumentationPath() const
191191 return QDir::current ().absoluteFilePath (" doc" );
192192}
193193
194+ QString Application::getUpdatePath () const
195+ {
196+ return QDir::temp ().absoluteFilePath (" CEEDUpdate" );
197+ }
198+
194199void Application::checkForUpdates ()
195200{
196201 if (!Utils::isInternetConnected ())
@@ -225,10 +230,25 @@ void Application::checkForUpdates()
225230
226231 QVersionNumber latestVersion = QVersionNumber::fromString (latestVersionStr);
227232 QVersionNumber currentVersion = QVersionNumber::fromString (applicationVersion ());
228- // if (latestVersion.normalized() > currentVersion.normalized())
233+ if (latestVersion.normalized () > currentVersion.normalized ())
229234 {
230- // TODO: check if we failed to update to the latestVersion before, write in a statusbar?
231- // !!!forced update check must clear this value before calling app::checkForUpdate()!
235+ if (_settings->getQSettings ()->value (" update/failed" ).toBool ())
236+ {
237+ auto savedVersion = QVersionNumber::fromString (_settings->getQSettings ()->value (" update/version" ).toString ());
238+ if (latestVersion.normalized () == savedVersion.normalized ())
239+ {
240+ _mainWindow->setStatusMessage (tr (" Auto-update to %1 is blocked because it failed before. "
241+ " Use Help->Check For Updates to try again." ).arg (latestVersionStr));
242+ return ;
243+ }
244+ else
245+ {
246+ _settings->getQSettings ()->remove (" update" );
247+
248+ QDir updateDir (getUpdatePath ());
249+ if (updateDir.exists ()) updateDir.removeRecursively ();
250+ }
251+ }
232252
233253 UpdateDialog dlg (currentVersion, latestVersion, releaseInfo);
234254 dlg.exec ();
@@ -247,10 +267,10 @@ void Application::checkForUpdates()
247267void Application::onUpdateError (const QUrl& url, const QString& errorString)
248268{
249269 _mainWindow->setStatusMessage (" Failed to check for updates" );
250- qCritical () << " Network error '" << errorString << " ' accessing " << url;
270+ qCritical () << " Update error: '" << errorString << " ' accessing " << url;
251271
252- const auto response = QMessageBox::question (_mainWindow, " Update check failed" ,
253- QString (" Update failed with error:\n %1\n\n Open releases web page?" ).arg (errorString),
272+ const auto response = QMessageBox::question (_mainWindow, tr ( " Update check failed" ) ,
273+ tr (" Update failed with error:\n %1\n\n Open releases web page?" ).arg (errorString),
254274 QMessageBox::Yes | QMessageBox::No,
255275 QMessageBox::Yes);
256276
@@ -269,13 +289,75 @@ void Application::checkUpdateResults()
269289 if (!updateLaunched)
270290 QMessageBox::warning (_mainWindow, tr (" Warning" ), tr (" An application was launched by an updater script but no update was scheduled!" ));
271291
292+ int updateResult = -1 ;
293+ QString updateMessage = " <Not launched by updater>" ;
272294 if (startedByUpdater)
273295 {
274- const auto updateResult = _cmdLine->value (" updateResult" ).toInt ();
275- const auto updateMessage = _cmdLine->value (" updateMessage" );
296+ updateResult = _cmdLine->value (" updateResult" ).toInt ();
297+ updateMessage = tr (_cmdLine->value (" updateMessage" ).toUtf8 ());
298+ }
299+
300+ auto currentVersion = QVersionNumber::fromString (applicationVersion ());
301+ auto savedVersion = QVersionNumber::fromString (_settings->getQSettings ()->value (" update/version" ).toString ());
302+ if (currentVersion.normalized () == savedVersion.normalized ())
303+ {
304+ // Updated but couldn't remove a tmp folder, let's try again
305+ if (updateResult == 30 )
306+ {
307+ QDir backupDir (applicationDirPath () + " _old" );
308+ if (!backupDir.exists () || backupDir.removeRecursively ())
309+ {
310+ updateResult = 0 ;
311+ updateMessage = tr (" Updated successfully" );
312+ }
313+ }
276314
277- // !!!DBG TMP!
278- QMessageBox::warning (_mainWindow, " Update results" , tr (" Code: %1\n Msg: %2" ).arg (updateResult).arg (updateMessage));
315+ QDir updateDir (getUpdatePath ());
316+ if (updateDir.exists ()) updateDir.removeRecursively ();
317+
318+ auto releaseWebPage = _settings->getQSettings ()->value (" update/webPage" ).toString ();
319+ if (releaseWebPage.isEmpty ())
320+ releaseWebPage= " https://github.com/cegui/ceed-cpp/releases/tag/v" + currentVersion.toString ();
321+
322+ _settings->getQSettings ()->remove (" update" );
323+
324+ if (updateResult == 0 && startedByUpdater)
325+ {
326+ // NB: \n is replaced with <br/> to indicate Qt that this is a rich text
327+ QMessageBox::information (_mainWindow, tr (" Updated" ),
328+ tr (" Updated to %1.<br/>"
329+ " Visit <a href=\" %2\" >release page</a> for the full release description.<br/><br/>"
330+ " Updater result code: %3<br/>"
331+ " Updater message: %4" )
332+ .arg (currentVersion.toString ())
333+ .arg (releaseWebPage)
334+ .arg (updateResult)
335+ .arg (updateMessage));
336+ }
337+ else
338+ {
339+ QMessageBox::warning (_mainWindow, tr (" Updated with problems" ),
340+ tr (" Application executable is updated to %1, yet something gone wrong. "
341+ " Please check updater results and reinstall manually if you encounter problems.\n\n "
342+ " Updater result code: %2\n "
343+ " Updater message: %3" )
344+ .arg (currentVersion.toString ())
345+ .arg (updateResult)
346+ .arg (updateMessage));
347+ }
348+ }
349+ else
350+ {
351+ QMessageBox::critical (_mainWindow, tr (" Update failed" ),
352+ tr (" Failed to update to %1.\n "
353+ " Automatic update will be blocked for this version.\n "
354+ " Use Help->Check For Updates to remove the block.\n\n "
355+ " Updater result code: %2\n "
356+ " Updater message: %3" )
357+ .arg (savedVersion.toString ())
358+ .arg (updateResult)
359+ .arg (updateMessage));
360+ _settings->getQSettings ()->setValue (" update/failed" , true );
279361 }
280362}
281363
0 commit comments