On Thu, Mar 6, 2014 at 10:03 PM, Dirk Hohndel <d...@hohndel.org> wrote:
>
> BTW: is there a QT function that gives us a nice "OS Version"?
> I looked at QSysInfo and that's not all that useful.
>
> I was thinking of something like a little more versatile :-)
>

Hi Dirk,

See the attached updated patch, I hope it is OK. I am still working on the
OS detection feature.


-- 
*With Kind Regards,*
*Joseph W Joshua*
*+254 724 276 654*
From 1b8f2a187ecba70f9c11d9bf071188eb56557c4d Mon Sep 17 00:00:00 2001
From: Joshua Wambua <jos...@megvel.me.ke>
Date: Wed, 2 Apr 2014 09:49:03 +0300
Subject: [PATCH] Add "Check for updates" Feature

This patch adds a check for updates feature.

On windows and mac, it connects to http://subsurface.hohndel.org/updatecheck.html
to check for any new versions. It then prompts the user with a download link if
an update is available

Signed-off-by: Joshua Wambua <joejo...@gmail.com>
---
 qt-ui/mainwindow.cpp    | 11 +++++++++
 qt-ui/mainwindow.h      |  1 +
 qt-ui/mainwindow.ui     |  6 +++++
 qt-ui/updatemanager.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
 qt-ui/updatemanager.h   | 21 +++++++++++++++++
 subsurface.pro          |  6 +++--
 6 files changed, 106 insertions(+), 2 deletions(-)
 create mode 100644 qt-ui/updatemanager.cpp
 create mode 100644 qt-ui/updatemanager.h

diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index b632252..b2f9947 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -37,6 +37,7 @@
 #include "diveplanner.h"
 #include "about.h"
 #include "worldmap-save.h"
+#include "updatemanager.h"
 #ifndef NO_PRINTING
 #include "printdialog.h"
 #endif
@@ -97,6 +98,10 @@ MainWindow::MainWindow() : QMainWindow(),
 #ifdef NO_PRINTING
 	ui.menuFile->removeAction(ui.actionPrint);
 #endif
+
+#ifdef Q_OS_LINUX
+	ui.menuHelp->removeAction(ui.action_Check_for_Updates);
+#endif
 }
 
 MainWindow::~MainWindow()
@@ -575,6 +580,12 @@ void MainWindow::on_actionAboutSubsurface_triggered()
 	dlg.exec();
 }
 
+void MainWindow::on_action_Check_for_Updates_triggered()
+{
+	UpdateManager *updater = new UpdateManager(this);
+	updater->checkForUpdates();
+}
+
 void MainWindow::on_actionUserManual_triggered()
 {
 #ifndef NO_USERMANUAL
diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h
index a0c5be3..f18bd90 100644
--- a/qt-ui/mainwindow.h
+++ b/qt-ui/mainwindow.h
@@ -121,6 +121,7 @@ slots:
 	void on_actionAboutSubsurface_triggered();
 	void on_actionUserManual_triggered();
 	void on_actionDivePlanner_triggered();
+	void on_action_Check_for_Updates_triggered();
 
 	void current_dive_changed(int divenr);
 	void initialUiSetup();
diff --git a/qt-ui/mainwindow.ui b/qt-ui/mainwindow.ui
index 2bf2496..1eadec7 100644
--- a/qt-ui/mainwindow.ui
+++ b/qt-ui/mainwindow.ui
@@ -601,6 +601,7 @@
      <string>&amp;Help</string>
     </property>
     <addaction name="actionAboutSubsurface"/>
+    <addaction name="action_Check_for_Updates"/>
     <addaction name="actionUserManual"/>
    </widget>
    <widget class="QMenu" name="menuImport">
@@ -910,6 +911,11 @@
     <bool>false</bool>
    </property>
   </action>
+  <action name="action_Check_for_Updates">
+   <property name="text">
+    <string>&amp;Check for Updates</string>
+   </property>
+  </action>
  </widget>
  <customwidgets>
   <customwidget>
diff --git a/qt-ui/updatemanager.cpp b/qt-ui/updatemanager.cpp
new file mode 100644
index 0000000..f89d385
--- /dev/null
+++ b/qt-ui/updatemanager.cpp
@@ -0,0 +1,63 @@
+#include "updatemanager.h"
+#include <QtNetwork>
+#include <QMessageBox>
+#include "subsurfacewebservices.h"
+#include "ssrf-version.h"
+
+UpdateManager::UpdateManager(QObject *parent) :
+	QObject(parent)
+{
+	manager = SubsurfaceWebServices::manager();
+	connect (manager, SIGNAL(finished(QNetworkReply*)), SLOT(requestReceived(QNetworkReply*)));
+}
+
+void UpdateManager::checkForUpdates()
+{
+	QString os;
+
+#if defined(Q_OS_WIN)
+	os = "win";
+#elif defined(Q_OS_MAC)
+	os = "osx";
+#else
+	os = "unknown";
+#endif
+
+	QString version = VERSION_STRING;
+	QString url = QString("http://subsurface.hohndel.org/updatecheck.html?os=%1&ver=%2";).arg(os, version);
+	manager->get(QNetworkRequest(QUrl(url)));
+}
+
+void UpdateManager::requestReceived(QNetworkReply *reply)
+{
+	QMessageBox msgbox;
+	QString msgTitle = tr("Check for updates.");
+	QString msgText = tr("<h3>Subsurface was unable to check for updates.</h3>");
+
+
+	if (reply->error() != QNetworkReply::NoError) {
+		//Network Error
+		msgText = msgText + tr("<br/><b>The following error occurred:</b><br/>") + reply->errorString()
+				+ tr("<br/><br/><b>Please check your internet connection.</b>");
+	} else {
+		//No network error
+		QString response(reply->readAll());
+		QString responseBody = response.split("\"").at(1);
+
+		msgbox.setIcon(QMessageBox::Information);
+
+		if (responseBody == "OK") {
+			msgText = tr("You are using the latest version of subsurface.");
+		} else if (responseBody.startsWith("http")) {
+			msgText = tr("A new version of subsurface is available.<br/>Click on:<br/><a href=\"%1\">%1</a><br/> to download it.")
+					.arg(responseBody);
+		} else {
+			msgbox.setIcon(QMessageBox::Warning);
+		}
+	}
+
+	msgbox.setWindowTitle(msgTitle);
+	msgbox.setText(msgText);
+	msgbox.setTextFormat(Qt::RichText);
+	msgbox.exec();
+}
diff --git a/qt-ui/updatemanager.h b/qt-ui/updatemanager.h
new file mode 100644
index 0000000..18b47cf
--- /dev/null
+++ b/qt-ui/updatemanager.h
@@ -0,0 +1,21 @@
+#ifndef UPDATEMANAGER_H
+#define UPDATEMANAGER_H
+
+#include <QObject>
+
+class QNetworkAccessManager;
+class QNetworkReply;
+
+class UpdateManager : public QObject
+{
+	Q_OBJECT
+public:
+	explicit UpdateManager(QObject *parent = 0);
+	void checkForUpdates();
+private:
+	QNetworkAccessManager *manager;
+public slots:
+	void requestReceived(QNetworkReply* reply);
+};
+
+#endif // UPDATEMANAGER_H
diff --git a/subsurface.pro b/subsurface.pro
index 3944da0..6d13c1d 100644
--- a/subsurface.pro
+++ b/subsurface.pro
@@ -76,7 +76,8 @@ HEADERS = \
 	qt-ui/profile/diveprofileitem.h \
 	qt-ui/profile/diveeventitem.h \
 	qt-ui/profile/divetooltipitem.h \
-	qt-ui/profile/ruleritem.h
+	qt-ui/profile/ruleritem.h \
+	qt-ui/updatemanager.h
 
 android: HEADERS -= \
 	qt-ui/usermanual.h \
@@ -148,7 +149,8 @@ SOURCES =  \
 	qt-ui/profile/diveprofileitem.cpp \
 	qt-ui/profile/diveeventitem.cpp \
 	qt-ui/profile/divetooltipitem.cpp \
-	qt-ui/profile/ruleritem.cpp
+	qt-ui/profile/ruleritem.cpp \
+	qt-ui/updatemanager.cpp
 
 android: SOURCES += android.cpp
 else: linux*: SOURCES += linux.c
-- 
1.9.1

_______________________________________________
subsurface mailing list
subsurface@hohndel.org
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to