Browse Source

Revert "Just do it all in JS."

This reverts commit ddccc58bb6c649a494166ee756039dfab9004ec3.
Ian Walton 3 năm trước cách đây
mục cha
commit
1a152270c9
3 tập tin đã thay đổi với 71 bổ sung27 xóa
  1. 30 27
      native/jmpUpdatePlugin.js
  2. 34 0
      src/system/SystemComponent.cpp
  3. 7 0
      src/system/SystemComponent.h

+ 30 - 27
native/jmpUpdatePlugin.js

@@ -7,34 +7,37 @@ class jmpUpdatePlugin {
         (async () => {
             const api = await window.apiPromise;
 
-            const checkForUpdates = await new Promise(resolve => {
-                api.settings.value("main", "checkForUpdates", resolve);
-            });
-
-            if (!checkForUpdates) return;
-
-            const checkUrl = "https://github.com/jellyfin/jellyfin-media-player/releases/latest";
-            const url = (await fetch(checkUrl)).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
+            const onUpdateNotify = async (url) => {
+                if (url == "SSL_UNAVAILABLE") {
+                    // Windows (and possibly macOS) don't ship with SSL in QT......
+                    // So we get to do a full request to GitHub here :(
+                    const checkUrl = "https://github.com/jellyfin/jellyfin-media-player/releases/latest";
+                    url = (await fetch(checkUrl)).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();
         })();
     }
 }

+ 34 - 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,37 @@ QString SystemComponent::getNativeShellScript()
   return nativeshellString;
 }
 
+/////////////////////////////////////////////////////////////////////////////////////////
+void SystemComponent::checkForUpdates()
+{
+  if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "checkForUpdates").toBool()) {
+#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
+    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);
+#else
+    emit updateInfoEmitted("SSL_UNAVAILABLE");
+#endif
+  }
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+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}"
 

+ 7 - 0
src/system/SystemComponent.h

@@ -47,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);
 
@@ -54,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
   {
@@ -89,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);