| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "desktop-widgets/updatemanager.h"
 | 
					
						
							|  |  |  | #include "core/helpers.h"
 | 
					
						
							|  |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | #include <QtNetwork>
 | 
					
						
							|  |  |  | #include <QMessageBox>
 | 
					
						
							| 
									
										
										
										
											2015-01-25 11:27:42 -08:00
										 |  |  | #include <QUuid>
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "desktop-widgets/subsurfacewebservices.h"
 | 
					
						
							|  |  |  | #include "core/version.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/mainwindow.h"
 | 
					
						
							|  |  |  | #include "core/cloudstorage.h"
 | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | #include "core/subsurface-qt/SettingsObjectWrapper.h"
 | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 06:42:02 -07:00
										 |  |  | UpdateManager::UpdateManager(QObject *parent) : | 
					
						
							|  |  |  | 	QObject(parent), | 
					
						
							|  |  |  | 	isAutomaticCheck(false) | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | 	auto update_settings = SettingsObjectWrapper::instance()->update_manager_settings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (update_settings->dontCheckForUpdates()) | 
					
						
							| 
									
										
										
										
											2015-01-03 13:47:05 -08:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (update_settings->lastVersionUsed() == subsurface_git_version() && | 
					
						
							|  |  |  | 	    update_settings->nextCheck() > QDate::currentDate()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	update_settings->setLastVersionUsed(subsurface_git_version()); | 
					
						
							|  |  |  | 	update_settings->setNextCheck(QDate::currentDate().addDays(14)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 13:47:05 -08:00
										 |  |  | 	checkForUpdates(true); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | void UpdateManager::checkForUpdates(bool automatic) | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | { | 
					
						
							|  |  |  | 	QString os; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(Q_OS_WIN)
 | 
					
						
							|  |  |  | 	os = "win"; | 
					
						
							|  |  |  | #elif defined(Q_OS_MAC)
 | 
					
						
							|  |  |  | 	os = "osx"; | 
					
						
							| 
									
										
										
										
											2014-04-02 12:55:33 -07:00
										 |  |  | #elif defined(Q_OS_LINUX)
 | 
					
						
							|  |  |  | 	os = "linux"; | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	os = "unknown"; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 	isAutomaticCheck = automatic; | 
					
						
							| 
									
										
										
										
											2015-02-15 20:25:18 +02:00
										 |  |  | 	QString version = subsurface_canonical_version(); | 
					
						
							| 
									
										
										
										
											2015-01-25 11:27:42 -08:00
										 |  |  | 	QString uuidString = getUUID(); | 
					
						
							|  |  |  | 	QString url = QString("http://subsurface-divelog.org/updatecheck.html?os=%1&version=%2&uuid=%3").arg(os, version, uuidString); | 
					
						
							| 
									
										
										
										
											2014-07-31 11:20:11 -07:00
										 |  |  | 	QNetworkRequest request; | 
					
						
							|  |  |  | 	request.setUrl(url); | 
					
						
							|  |  |  | 	request.setRawHeader("Accept", "text/xml"); | 
					
						
							| 
									
										
										
										
											2015-02-23 09:09:48 -08:00
										 |  |  | 	QString userAgent = getUserAgent(); | 
					
						
							| 
									
										
										
										
											2014-07-31 11:20:11 -07:00
										 |  |  | 	request.setRawHeader("User-Agent", userAgent.toUtf8()); | 
					
						
							| 
									
										
										
										
											2016-01-26 09:49:47 -08:00
										 |  |  | 	connect(manager()->get(request), SIGNAL(finished()), this, SLOT(requestReceived()), Qt::UniqueConnection); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-16 17:20:21 -03:00
										 |  |  | void UpdateManager::requestReceived() | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 	bool haveNewVersion = false; | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 	QMessageBox msgbox; | 
					
						
							|  |  |  | 	QString msgTitle = tr("Check for updates."); | 
					
						
							| 
									
										
										
										
											2014-07-09 10:26:43 -07:00
										 |  |  | 	QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>"; | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-16 17:20:21 -03:00
										 |  |  | 	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 	if (reply->error() != QNetworkReply::NoError) { | 
					
						
							|  |  |  | 		//Network Error
 | 
					
						
							| 
									
										
										
										
											2014-07-09 10:26:43 -07:00
										 |  |  | 		msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString() | 
					
						
							|  |  |  | 				+ "<br/><br/><b>" + tr("Please check your internet connection.") + "</b>"; | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		//No network error
 | 
					
						
							| 
									
										
										
										
											2015-01-25 22:19:05 -08:00
										 |  |  | 		QString responseBody(reply->readAll()); | 
					
						
							|  |  |  | 		QString responseLink; | 
					
						
							|  |  |  | 		if (responseBody.contains('"')) | 
					
						
							|  |  |  | 			responseLink = responseBody.split("\"").at(1); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		msgbox.setIcon(QMessageBox::Information); | 
					
						
							|  |  |  | 		if (responseBody == "OK") { | 
					
						
							| 
									
										
										
										
											2014-11-25 15:47:24 +00:00
										 |  |  | 			msgText = tr("You are using the latest version of Subsurface."); | 
					
						
							| 
									
										
										
										
											2015-01-25 22:19:05 -08:00
										 |  |  | 		} else if (responseBody.startsWith("[\"http")) { | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 			haveNewVersion = true; | 
					
						
							| 
									
										
										
										
											2014-11-25 15:47:24 +00:00
										 |  |  | 			msgText = tr("A new version of Subsurface is available.<br/>Click on:<br/><a href=\"%1\">%1</a><br/> to download it.") | 
					
						
							| 
									
										
										
										
											2015-01-25 22:19:05 -08:00
										 |  |  | 					.arg(responseLink); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 		} else if (responseBody.startsWith("Latest version")) { | 
					
						
							| 
									
										
										
										
											2014-08-26 10:30:41 -07:00
										 |  |  | 			// the webservice backend doesn't localize - but it's easy enough to just replace the
 | 
					
						
							|  |  |  | 			// strings that it is likely to send back
 | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 			haveNewVersion = true; | 
					
						
							| 
									
										
										
										
											2014-11-25 15:47:24 +00:00
										 |  |  | 			msgText = QString("<b>") + tr("A new version of Subsurface is available.") + QString("</b><br/><br/>") + | 
					
						
							| 
									
										
										
										
											2015-01-25 22:19:05 -08:00
										 |  |  | 					tr("Latest version is %1, please check %2 our download page %3 for information in how to update.") | 
					
						
							|  |  |  | 					.arg(responseLink).arg("<a href=\"http://subsurface-divelog.org/download\">").arg("</a>"); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-08-26 10:30:41 -07:00
										 |  |  | 			// the webservice backend doesn't localize - but it's easy enough to just replace the
 | 
					
						
							|  |  |  | 			// strings that it is likely to send back
 | 
					
						
							| 
									
										
										
										
											2015-01-30 22:07:32 -08:00
										 |  |  | 			if (!responseBody.contains("latest development") && | 
					
						
							|  |  |  | 			    !responseBody.contains("newer") && | 
					
						
							|  |  |  | 			    !responseBody.contains("beta", Qt::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2015-01-25 08:13:41 -08:00
										 |  |  | 				haveNewVersion = true; | 
					
						
							| 
									
										
										
										
											2014-08-26 10:30:41 -07:00
										 |  |  | 			if (responseBody.contains("Newest release version is ")) | 
					
						
							|  |  |  | 				responseBody.replace("Newest release version is ", tr("Newest release version is ")); | 
					
						
							| 
									
										
										
										
											2015-01-01 12:13:27 -08:00
										 |  |  | 			msgText = tr("The server returned the following information:").append("<br/><br/>").append(responseBody); | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | 			msgbox.setIcon(QMessageBox::Warning); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-10 13:28:28 -07:00
										 |  |  | #ifndef SUBSURFACE_MOBILE
 | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 	if (haveNewVersion || !isAutomaticCheck) { | 
					
						
							|  |  |  | 		msgbox.setWindowTitle(msgTitle); | 
					
						
							|  |  |  | 		msgbox.setWindowIcon(QIcon(":/subsurface-icon")); | 
					
						
							|  |  |  | 		msgbox.setText(msgText); | 
					
						
							|  |  |  | 		msgbox.setTextFormat(Qt::RichText); | 
					
						
							|  |  |  | 		msgbox.exec(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (isAutomaticCheck) { | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | 		auto update_settings = SettingsObjectWrapper::instance()->update_manager_settings; | 
					
						
							| 
									
										
										
										
											2016-08-27 12:26:13 -07:00
										 |  |  | 		if (!update_settings->dontCheckExists()) { | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 			// we allow an opt out of future checks
 | 
					
						
							|  |  |  | 			QMessageBox response(MainWindow::instance()); | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | 			QString message = tr("Subsurface is checking every two weeks if a new version is available. " | 
					
						
							| 
									
										
										
										
											2017-02-20 08:46:40 +01:00
										 |  |  | 								 "\nIf you don't want Subsurface to continue checking, please click Decline."); | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 			response.addButton(tr("Decline"), QMessageBox::RejectRole); | 
					
						
							|  |  |  | 			response.addButton(tr("Accept"), QMessageBox::AcceptRole); | 
					
						
							|  |  |  | 			response.setText(message); | 
					
						
							|  |  |  | 			response.setWindowTitle(tr("Automatic check for updates")); | 
					
						
							|  |  |  | 			response.setIcon(QMessageBox::Question); | 
					
						
							|  |  |  | 			response.setWindowModality(Qt::WindowModal); | 
					
						
							| 
									
										
										
										
											2016-08-10 16:40:14 -03:00
										 |  |  | 			update_settings->setDontCheckForUpdates(response.exec() != QMessageBox::Accepted); | 
					
						
							| 
									
										
										
										
											2015-01-01 20:49:24 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-10 13:28:28 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-04-02 22:41:39 +03:00
										 |  |  | } |