X-Git-Url: https://git.sur5r.net/?p=minitube;a=blobdiff_plain;f=src%2Fytjs%2Fytjs.cpp;fp=src%2Fytjs%2Fytjs.cpp;h=0000000000000000000000000000000000000000;hp=7153ef95b016b2749278e9c458070616297673d0;hb=b4e0b70c51f05c19b9095a381cb73bfb79f128dd;hpb=b76332aa9817cd134abf7e7e89e62456c1116668 diff --git a/src/ytjs/ytjs.cpp b/src/ytjs/ytjs.cpp deleted file mode 100644 index 7153ef9..0000000 --- a/src/ytjs/ytjs.cpp +++ /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; -}