Browse Source

Add update notifier.

Ian Walton 3 years ago
parent
commit
cd6aa66699

+ 2 - 1
.vscode/settings.json

@@ -10,6 +10,7 @@
         "list": "cpp",
         "string": "cpp",
         "unordered_map": "cpp",
-        "vector": "cpp"
+        "vector": "cpp",
+        "qnetworkaccessmanager": "cpp"
     }
 }

+ 38 - 0
native/jmpUpdatePlugin.js

@@ -0,0 +1,38 @@
+class jmpUpdatePlugin {
+    constructor({ confirm, toast }) {
+        this.name = 'JMP Update Plugin';
+        this.type = 'input';
+        this.id = 'jmpUpdatePlugin';
+
+        (async () => {
+            const api = await window.apiPromise;
+
+            const onUpdateNotify = async (url) => {
+                const urlSegments = url.split("/");
+                const version = urlSegments[urlSegments.length - 1].substring(1);
+                const currentVersion = navigator.userAgent.split(" ")[1];
+
+                if (version == currentVersion) return;
+                if (!/^[0-9.-]+$/.test(version)) return;
+
+                try {
+                    await confirm({
+                        title: "Update Available",
+                        text: `Jellyfin Media Player version ${version} is available.`,
+                        cancelText: "Ignore",
+                        confirmText: "Download"
+                    });
+
+                    api.system.openExternalUrl(url);
+                } catch (e) {
+                    // User cancelled update
+                }
+            }
+
+            api.system.updateInfoEmitted.connect(onUpdateNotify);
+            api.system.checkForUpdates();
+        })();
+    }
+}
+
+window._jmpUpdatePlugin = jmpUpdatePlugin;

+ 2 - 1
native/nativeshell.js

@@ -19,7 +19,8 @@ const features = [
 const plugins = [
     'mpvVideoPlayer',
     'mpvAudioPlayer',
-    'jmpInputPlugin'
+    'jmpInputPlugin',
+    'jmpUpdatePlugin'
 ];
 
 function loadScript(src) {

+ 4 - 0
resources/settings/settings_description.json

@@ -115,6 +115,10 @@
         "value": "sdlEnabled",
         "default": true,
         "hidden": true
+      },
+      {
+        "value": "checkForUpdates",
+        "default": true
       }
     ]
   },

+ 30 - 0
src/system/SystemComponent.cpp

@@ -6,6 +6,9 @@
 #include <QDesktopServices>
 #include <QDir>
 #include <QJsonObject>
+#include <QNetworkRequest>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
 
 #include "input/InputComponent.h"
 #include "SystemComponent.h"
@@ -331,6 +334,33 @@ QString SystemComponent::getNativeShellScript()
   return nativeshellString;
 }
 
+/////////////////////////////////////////////////////////////////////////////////////////
+void SystemComponent::checkForUpdates()
+{
+  if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "checkForUpdates").toBool()) {
+    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
+    QString checkUrl = "https://github.com/jellyfin/jellyfin-media-player/releases/latest";
+    QUrl qCheckUrl = QUrl(checkUrl);
+    QLOG_DEBUG() << QString("Checking URL for updates: %1").arg(checkUrl);
+    QNetworkRequest req(qCheckUrl);
+
+    connect(manager, &QNetworkAccessManager::finished, this, &SystemComponent::updateInfoHandler);
+    manager->get(req);
+  }
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+void SystemComponent::updateInfoHandler(QNetworkReply* reply)
+{
+  if (reply->error() == QNetworkReply::NoError) {
+    int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+    if(statusCode == 302) {
+      QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
+      emit updateInfoEmitted(redirectUrl.toString());
+    }
+  }
+}
+
 /////////////////////////////////////////////////////////////////////////////////////////
 #define BASESTR "protocols=shoutcast,http-video;videoDecoders=h264{profile:high&resolution:2160&level:52};audioDecoders=mp3,aac,dts{bitrate:800000&channels:%1},ac3{bitrate:800000&channels:%2}"
 

+ 8 - 0
src/system/SystemComponent.h

@@ -3,6 +3,7 @@
 
 #include "ComponentManager.h"
 #include <QTimer>
+#include <QNetworkReply>
 #include "utils/Utils.h"
 #include "Paths.h"
 #include "Names.h"
@@ -46,6 +47,8 @@ public:
 
   Q_INVOKABLE QString getNativeShellScript();
 
+  Q_INVOKABLE void checkForUpdates();
+
   // called by the web-client when everything is properly inited
   Q_INVOKABLE void hello(const QString& version);
 
@@ -53,6 +56,8 @@ public:
   Q_SIGNAL void capabilitiesChanged(const QString& capabilities);
   Q_SIGNAL void userInfoChanged();
 
+  Q_SIGNAL void updateInfoEmitted(QString url);
+
   // possible os types type enum
   enum PlatformType
   {
@@ -88,6 +93,9 @@ public:
 
   void updateScale(qreal scale);
 
+private Q_SLOTS:
+  void updateInfoHandler(QNetworkReply* reply);
+
 signals:
   void hostMessage(const QString& message);
   void settingsMessage(const QString& setting, const QString& value);