Hi,

The attached patch implements the check for updates feature for windows and mac.


Joshua.
From 23b38aa9d9cce02f80fff8d7019033bf8b7bfe5c Mon Sep 17 00:00:00 2001
From: Joshua Wambua <[email protected]>
Date: Wed, 5 Mar 2014 14:38:56 +0300
Subject: [PATCH 28/28] Add "Check For Updates" Feature

This patch adds a check for updates feature. This feature is disabled on linux.

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 <[email protected]>
---
 qt-ui/mainwindow.cpp    | 11 +++++++++++
 qt-ui/mainwindow.h      |  1 +
 qt-ui/mainwindow.ui     |  8 +++++++-
 qt-ui/updatemanager.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 qt-ui/updatemanager.h   | 21 ++++++++++++++++++++
 subsurface.pro          |  6 ++++--
 6 files changed, 95 insertions(+), 3 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 4e8f463..f762bf6 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -37,6 +37,7 @@
 #include "about.h"
 #include "printdialog.h"
 #include "divelogimportdialog.h"
+#include "updatemanager.h"
 
 MainWindow *MainWindow::m_Instance = NULL;
 
@@ -79,6 +80,10 @@ MainWindow::MainWindow() : QMainWindow(),
 #ifndef ENABLE_PLANNER
 	ui.menuLog->removeAction(ui.actionDivePlanner);
 #endif
+
+#ifdef Q_OS_LINUX
+	ui.actionCheck_For_Updates->setVisible(false);
+#endif
 }
 
 MainWindow::~MainWindow()
@@ -540,6 +545,12 @@ void MainWindow::on_actionUserManual_triggered()
 	helpView->show();
 }
 
+void MainWindow::on_actionCheck_For_Updates_triggered()
+{
+	UpdateManager *checker = new UpdateManager(this);
+	checker->checkForUpdate();
+}
+
 QString MainWindow::filter()
 {
 	QString f;
diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h
index 79aeeec..51cb854 100644
--- a/qt-ui/mainwindow.h
+++ b/qt-ui/mainwindow.h
@@ -118,6 +118,7 @@ slots:
 	void on_actionAboutSubsurface_triggered();
 	void on_actionUserManual_triggered();
 	void on_actionDivePlanner_triggered();
+	void on_actionCheck_For_Updates_triggered();
 
 	/* monitor resize of the info-profile splitter */
 	void on_mainSplitter_splitterMoved(int pos, int idx);
diff --git a/qt-ui/mainwindow.ui b/qt-ui/mainwindow.ui
index 26c727e..91e3eb2 100644
--- a/qt-ui/mainwindow.ui
+++ b/qt-ui/mainwindow.ui
@@ -597,7 +597,7 @@
      <x>0</x>
      <y>0</y>
      <width>1418</width>
-     <height>22</height>
+     <height>19</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
@@ -661,6 +661,7 @@
     </property>
     <addaction name="actionAboutSubsurface"/>
     <addaction name="actionUserManual"/>
+    <addaction name="actionCheck_For_Updates"/>
    </widget>
    <widget class="QMenu" name="menuImport">
     <property name="title">
@@ -964,6 +965,11 @@
     <bool>false</bool>
    </property>
   </action>
+  <action name="actionCheck_For_Updates">
+   <property name="text">
+    <string>Check For &amp;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..e3c7442
--- /dev/null
+++ b/qt-ui/updatemanager.cpp
@@ -0,0 +1,51 @@
+#include "updatemanager.h"
+#include <QtNetwork>
+#include <QMessageBox>
+#include "ssrf-version.h"
+
+UpdateManager::UpdateManager(QObject *parent) :
+	QObject(parent)
+{
+	manager = new QNetworkAccessManager(this);
+	connect (manager, SIGNAL(finished(QNetworkReply*)), SLOT(requestReceived(QNetworkReply*)));
+}
+
+void UpdateManager::checkForUpdate()
+{
+	QString os = "unknown";
+
+#ifdef Q_OS_WIN
+	os = "win";
+#endif
+
+#ifdef Q_OS_MAC
+	os = "osx";
+#endif
+
+	QString version = VERSION_STRING;
+	QString url = tr("http://subsurface.hohndel.org/updatecheck.html?os=%1&ver=%2";).arg(os, version);
+	manager->get(QNetworkRequest(QUrl(url)));
+}
+
+void UpdateManager::requestReceived(QNetworkReply *reply)
+{
+	QString response = reply->readAll();
+	QString responseBody = response.split("\"").at(1);
+	QMessageBox msgbox;
+	QString msgTitle = tr("Check for updates.");
+	QString msgText = tr("Subsurface was unable to check for updates.");
+	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/><a href=\"%1\">Click here</a> 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..41b1ad6
--- /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 checkForUpdate();
+private:
+	QNetworkAccessManager *manager;
+public slots:
+	void requestReceived(QNetworkReply* reply);
+};
+
+#endif // UPDATEMANAGER_H
diff --git a/subsurface.pro b/subsurface.pro
index 81cb906..c2084b7 100644
--- a/subsurface.pro
+++ b/subsurface.pro
@@ -74,7 +74,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
 
 SOURCES =  \
 	deco.c \
@@ -138,7 +139,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
 
 linux*: SOURCES += linux.c
 mac: SOURCES += macos.c
-- 
1.9.0

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to