| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
|
|---|
| 3 |
|
|---|
| 4 | This program is free software; you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program; if not, write to the Free Software
|
|---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | #include "ytsig.h"
|
|---|
| 20 |
|
|---|
| 21 | #ifdef YT_USE_SCRIPT
|
|---|
| 22 | #include <QtScript>
|
|---|
| 23 |
|
|---|
| 24 | QString YTSig::aclara(const QString & text, const QString & player, const QString & function_name) {
|
|---|
| 25 | int dot = text.indexOf('.');
|
|---|
| 26 | qDebug("YTSig::aclara: length: %d (%d.%d) p: %d", text.size(), dot, text.size()-dot-1, !player.isEmpty());
|
|---|
| 27 |
|
|---|
| 28 | if (script.isEmpty()) script = default_script;
|
|---|
| 29 |
|
|---|
| 30 | QScriptEngine engine;
|
|---|
| 31 |
|
|---|
| 32 | //QScriptSyntaxCheckResult r = engine.checkSyntax(script);
|
|---|
| 33 | //qDebug() << (int) r.state();
|
|---|
| 34 |
|
|---|
| 35 | engine.evaluate(script);
|
|---|
| 36 |
|
|---|
| 37 | QScriptValueList args;
|
|---|
| 38 | QString fname;
|
|---|
| 39 |
|
|---|
| 40 | if (!function_name.isEmpty()) {
|
|---|
| 41 | fname = function_name;
|
|---|
| 42 | args << text;
|
|---|
| 43 | }
|
|---|
| 44 | else
|
|---|
| 45 | if (player.isEmpty()) {
|
|---|
| 46 | fname = "aclara";
|
|---|
| 47 | args << text;
|
|---|
| 48 | }
|
|---|
| 49 | else {
|
|---|
| 50 | fname = "aclara_p";
|
|---|
| 51 | args << text << player;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | //qDebug("YTSig::aclara: function_name: %s", function_name.toLatin1().constData());
|
|---|
| 55 |
|
|---|
| 56 | QScriptValue aclarar = engine.globalObject().property(fname);
|
|---|
| 57 | QString res = aclarar.call(QScriptValue(), args).toString();
|
|---|
| 58 |
|
|---|
| 59 | //qDebug() << res;
|
|---|
| 60 |
|
|---|
| 61 | if (res.isEmpty()) {
|
|---|
| 62 | qDebug("YTSig::aclara: signature length not supported: %d: %s", text.size(), text.toLatin1().constData());
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | return res;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | void YTSig::reloadScriptFile() {
|
|---|
| 69 | qDebug("YTSig::reloadScriptFile: %s", script_file.toUtf8().constData());
|
|---|
| 70 |
|
|---|
| 71 | if (!QFile::exists(script_file)) {
|
|---|
| 72 | qDebug("YTSig::reloadScriptFile: file doesn't exist.");
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | QFile f(script_file);
|
|---|
| 77 | f.open(QIODevice::ReadOnly);
|
|---|
| 78 | QByteArray bytes = f.readAll();
|
|---|
| 79 | f.close();
|
|---|
| 80 |
|
|---|
| 81 | parsed_ts = "";
|
|---|
| 82 |
|
|---|
| 83 | if (!bytes.isEmpty()) {
|
|---|
| 84 | script = bytes;
|
|---|
| 85 |
|
|---|
| 86 | QRegExp rx("D: ([\\d,a-z,A-Z-]+)");
|
|---|
| 87 | if (rx.indexIn(bytes)) {
|
|---|
| 88 | QByteArray d = rx.cap(1).toLatin1();
|
|---|
| 89 | //qDebug("YTSig::reloadScriptFile: d: %s", d.constData());
|
|---|
| 90 | parsed_ts = QByteArray::fromBase64(d);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | QString YTSig::script;
|
|---|
| 97 | QString YTSig::script_file;
|
|---|
| 98 |
|
|---|
| 99 | QString YTSig::default_script;
|
|---|
| 100 |
|
|---|
| 101 | #else // YT_USE_SCRIPT
|
|---|
| 102 |
|
|---|
| 103 | #ifdef YTSIG_STATIC
|
|---|
| 104 | #include "ytsig_priv.cpp"
|
|---|
| 105 | #else
|
|---|
| 106 | QString YTSig::aclara(const QString & text, const QString & player, const QString & function_name) {
|
|---|
| 107 | QString res;
|
|---|
| 108 |
|
|---|
| 109 | int dot = text.indexOf('.');
|
|---|
| 110 | qDebug("YTSig::aclara (2): length: %d (%d.%d)", text.size(), dot, text.size()-dot-1);
|
|---|
| 111 |
|
|---|
| 112 | /*
|
|---|
| 113 | qDebug("%d: orig: %s", text.size(), text.toLatin1().constData());
|
|---|
| 114 | qDebug("%d: conv: %s", text.size(), res.toLatin1().constData());
|
|---|
| 115 | */
|
|---|
| 116 |
|
|---|
| 117 | return res;
|
|---|
| 118 | }
|
|---|
| 119 | #endif
|
|---|
| 120 |
|
|---|
| 121 | #endif // YT_USE_SCRIPT
|
|---|
| 122 |
|
|---|
| 123 | void YTSig::check(QString & u) {
|
|---|
| 124 | if (!parsed_ts.isEmpty()) {
|
|---|
| 125 | u.append(QString("&%1").arg(parsed_ts));
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | QString YTSig::parsed_ts;
|
|---|