]> git.sur5r.net Git - minitube/blobdiff - src/ytjs/ytjs.cpp
Update upstream source from tag 'upstream/3.8'
[minitube] / src / ytjs / ytjs.cpp
diff --git a/src/ytjs/ytjs.cpp b/src/ytjs/ytjs.cpp
deleted file mode 100644 (file)
index 7153ef9..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-#include "ytjs.h"
-
-#include "ytjsnamfactory.h"
-
-#include "cachedhttp.h"
-#include "http.h"
-#include "httputils.h"
-
-namespace {
-
-QString wsBase = "https://flavio.tordini.org/minitube-ws/ytjs/";
-QString ytJs = wsBase + "yt.js";
-
-} // namespace
-
-YTJS &YTJS::instance() {
-    static YTJS i;
-    return i;
-}
-
-Http &YTJS::http() {
-    static Http *h = [] {
-        Http *http = new Http;
-        http->addRequestHeader("User-Agent", HttpUtils::userAgent());
-        return http;
-    }();
-    return *h;
-}
-
-Http &YTJS::cachedHttp() {
-    static Http *h = [] {
-        CachedHttp *cachedHttp = new CachedHttp(http(), "ytjs");
-        cachedHttp->setMaxSeconds(3600 * 6);
-        // Avoid expiring the cached js
-        cachedHttp->setMaxSize(0);
-        cachedHttp->setIgnoreHostname(true);
-
-        cachedHttp->getValidators().insert("application/javascript", [](const auto &reply) -> bool {
-            return !reply.body().isEmpty();
-        });
-
-        return cachedHttp;
-    }();
-    return *h;
-}
-
-YTJS::YTJS(QObject *parent) : QObject(parent), engine(nullptr) {
-    initialize();
-}
-
-bool YTJS::checkError(const QJSValue &value) {
-    if (value.isError()) {
-        qWarning() << "Error" << value.toString();
-        qDebug() << value.property("stack").toString().splitRef('\n');
-        return true;
-    }
-    return false;
-}
-
-bool YTJS::isInitialized() {
-    if (ready) return true;
-    initialize();
-    return false;
-}
-
-void YTJS::initialize() {
-    if (initializing) return;
-    initializing = true;
-
-    if (engine) engine->deleteLater();
-    engine = new QQmlEngine(this);
-    engine->setNetworkAccessManagerFactory(new YTJSNAMFactory);
-
-    engine->globalObject().setProperty("global", engine->globalObject());
-
-    QJSValue timer = engine->newQObject(new JsTimer(engine));
-    engine->globalObject().setProperty("clearTimeout", timer.property("clearTimeout"));
-    engine->globalObject().setProperty("setTimeout", timer.property("setTimeout"));
-
-    connect(cachedHttp().get(ytJs), &HttpReply::finished, this, [this](auto &reply) {
-        if (!reply.isSuccessful()) {
-            emit initFailed("Cannot load JS");
-            qDebug() << "Cannot load JS";
-            return;
-        }
-        evaluate(reply.body());
-        ready = true;
-        initializing = false;
-        qDebug() << "Initialized";
-        emit initialized();
-    });
-}
-
-QJSValue YTJS::evaluate(const QString &js) {
-    auto value = engine->evaluate(js);
-    checkError(value);
-    return value;
-}