]> git.sur5r.net Git - minitube/blob - src/jsfunctions.cpp
Imported Upstream version 2.1.3
[minitube] / src / jsfunctions.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "jsfunctions.h"
22 #include "networkaccess.h"
23 #include <QDesktopServices>
24 #include "constants.h"
25
26 namespace The {
27 NetworkAccess* http();
28 }
29
30 JsFunctions* JsFunctions::instance() {
31     static JsFunctions *i = new JsFunctions();
32     return i;
33 }
34
35 JsFunctions::JsFunctions(QObject *parent) : QObject(parent), engine(0) {
36     QFile file(jsPath());
37     if (file.exists()) {
38         if (file.open(QIODevice::ReadOnly | QIODevice::Text))
39             parseJs(QString::fromUtf8(file.readAll()));
40         else
41             qWarning() << file.errorString() << file.fileName();
42         QFileInfo info(file);
43         if (info.lastModified().toTime_t() < QDateTime::currentDateTime().toTime_t() - 3600)
44             loadJs();
45     } else {
46         QFile resFile(QLatin1String(":/") + jsFilename());
47         resFile.open(QIODevice::ReadOnly | QIODevice::Text);
48         parseJs(QString::fromUtf8(resFile.readAll()));
49         loadJs();
50     }
51 }
52
53 void JsFunctions::parseJs(const QString &js) {
54     if (js.isEmpty()) return;
55     if (engine) delete engine;
56     engine = new QScriptEngine();
57     engine->evaluate(js);
58 }
59
60 const QLatin1String & JsFunctions::jsFilename() {
61     static const QLatin1String filename("functions.js");
62     return filename;
63 }
64
65 const QString & JsFunctions::jsPath() {
66     static const QString path(
67                 QDesktopServices::storageLocation(QDesktopServices::DataLocation)
68                 + "/" + jsFilename());
69     return path;
70 }
71
72 void JsFunctions::loadJs() {
73     QUrl url(QLatin1String(Constants::WEBSITE) + "-ws/" + jsFilename());
74     NetworkReply* reply = The::http()->get(url);
75     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotJs(QByteArray)));
76     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorJs(QNetworkReply*)));
77 }
78
79 void JsFunctions::gotJs(QByteArray bytes) {
80     parseJs(QString::fromUtf8(bytes));
81     QFile file(jsPath());
82     if (!file.open(QIODevice::WriteOnly))
83         qWarning() << file.errorString() << file.fileName();
84     QDataStream stream(&file);
85     stream.writeRawData(bytes.constData(), bytes.size());
86 }
87
88 void JsFunctions::errorJs(QNetworkReply *reply) {
89     qWarning() << "Cannot get" << jsFilename() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()
90                   << reply->url().toString() << reply->errorString();
91 }
92
93 QString JsFunctions::evaluateFunction(const QString &function) {
94     if (!engine) return QString();
95     QScriptValue value = engine->evaluate(function);
96     if (value.isUndefined())
97         qWarning() << "Undefined result for" << function;
98     if (value.isError())
99         qWarning() << "Error in" << function << value.toString();
100
101     return value.toString();
102 }
103
104 QString JsFunctions::decryptSignature(const QString &s) {
105     return evaluateFunction("decryptSignature('" + s + "')");
106 }
107
108 QString JsFunctions::decryptAgeSignature(const QString &s) {
109     return evaluateFunction("decryptAgeSignature('" + s + "')");
110 }