From 50723c9ae30ce5626a6554f4b1da7210e48a031e Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Thu, 25 May 2017 05:38:15 +0900
Subject: [PATCH] System handler

---
 app/app.pri                        |  3 +--
 app/src/handlers/systemhandler.cpp | 41 ++++++++++++++++++++++++++++++
 app/src/handlers/systemhandler.h   |  2 ++
 ocs-manager.pro                    |  6 +++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/app/app.pri b/app/app.pri
index 737e3eb..5495172 100644
--- a/app/app.pri
+++ b/app/app.pri
@@ -1,10 +1,9 @@
 QT += \
     core \
+    gui \
     network \
     websockets
 
-QT -= gui
-
 HEADERS += \
     $${PWD}/src/websockets/websocketserver.h \
     $${PWD}/src/handlers/confighandler.h \
diff --git a/app/src/handlers/systemhandler.cpp b/app/src/handlers/systemhandler.cpp
index b7b95ae..2230d8d 100644
--- a/app/src/handlers/systemhandler.cpp
+++ b/app/src/handlers/systemhandler.cpp
@@ -1,5 +1,8 @@
 #include "systemhandler.h"
 
+#include <QUrl>
+#include <QDesktopServices>
+
 #ifdef QTLIB_UNIX
 #include <QFileInfo>
 #include <QProcess>
@@ -9,6 +12,10 @@
 #include <QDebug>
 #endif
 
+#ifdef Q_OS_ANDROID
+#include "qtlib_package.h"
+#endif
+
 SystemHandler::SystemHandler(QObject *parent)
     : QObject(parent)
 {}
@@ -21,6 +28,40 @@ bool SystemHandler::isUnix()
     return false;
 }
 
+bool SystemHandler::isMobileDevice()
+{
+#if defined(APP_MOBILE)
+    return true;
+#elif defined(Q_OS_IOS) || defined(Q_OS_ANDROID) || defined(Q_OS_WINPHONE)
+    return true;
+#elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_ARM) // Ubuntu Phone, Plasma Phone
+    return true;
+#endif
+    return false;
+}
+
+bool SystemHandler::openUrl(const QString &url)
+{
+    QString path = url;
+    path.replace("file://localhost", "", Qt::CaseInsensitive);
+    path.replace("file://", "", Qt::CaseInsensitive);
+
+#ifdef QTLIB_UNIX
+    if ((path.endsWith(".appimage", Qt::CaseInsensitive) || path.endsWith(".exe", Qt::CaseInsensitive))
+            && QFileInfo(path).isExecutable()) {
+        return QProcess::startDetached(path);
+    }
+#endif
+
+#ifdef Q_OS_ANDROID
+    if (path.endsWith(".apk", Qt::CaseInsensitive)) {
+        return qtlib::Package(path).installAsApk();
+    }
+#endif
+
+    return QDesktopServices::openUrl(QUrl(url));
+}
+
 QString SystemHandler::desktopEnvironment()
 {
     QString desktop = "unknown";
diff --git a/app/src/handlers/systemhandler.h b/app/src/handlers/systemhandler.h
index a49f9a6..f291c8a 100644
--- a/app/src/handlers/systemhandler.h
+++ b/app/src/handlers/systemhandler.h
@@ -11,6 +11,8 @@ public:
 
 public slots:
     bool isUnix();
+    bool isMobileDevice();
+    bool openUrl(const QString &url);
 
     QString desktopEnvironment();
     bool isApplicableType(const QString &installType);
diff --git a/ocs-manager.pro b/ocs-manager.pro
index f1df8fa..c2e4aab 100644
--- a/ocs-manager.pro
+++ b/ocs-manager.pro
@@ -1,5 +1,11 @@
 message("Please execute scripts/import.sh for build dependencies")
 
+ios|android|!isEmpty(APP_MOBILE) {
+    DEFINES += APP_MOBILE
+} else {
+    DEFINES += APP_DESKTOP
+}
+
 TARGET = ocs-manager
 
 TEMPLATE = app
-- 
GitLab