2 * Copyright (C) 2010 Toni Gundogdu.
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 3 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <QScriptEngine>
20 #include <QScriptValueIterator>
25 #include "Preferences.h"
26 #include "YoutubeFeed.h"
28 #define QSETTINGS_GROUP "YoutubeFeed"
31 extern SharedPreferences shPrefs
;
35 YoutubeFeed::YoutubeFeed (QWidget
*parent
)
36 : QDialog(parent
), _got_items (false)
41 NomNom::restore_size (s
, this, QSETTINGS_GROUP
, QSize (215,165));
43 _prog
= new ProcessProgressDialog (this);
45 connect (_prog
, SIGNAL (finished (QString
)), this, SLOT (onFinished (QString
)));
47 connect (_prog
, SIGNAL (error ()), this, SLOT (onError ()));
51 bool YoutubeFeed::gotItems () const { return _got_items
; }
54 extern QHash
<QString
,QString
> feed
;
57 YoutubeFeed::fromJSON (const QString
& data
, QString
& error
) {
61 QScriptValue v
= e
.evaluate ("(" +data
+ ")");
63 if (e
.hasUncaughtException ()) {
64 error
= tr ("Uncaught exception at line %1: %2")
65 .arg (e
.uncaughtExceptionLineNumber ())
70 QScriptValueIterator
i (v
.property ("video"));
73 error
= tr ("Expected at least one video from umph(1), got none.");
81 while (i
.hasNext ()) {
85 if (i
.flags() & QScriptValue::SkipInEnumeration
)
89 t
= v
.property ("title").toString ();
90 u
= v
.property ("url").toString ();
99 // Slot: type changed.
102 YoutubeFeed::onTypeChanged (int n
) {
104 static const QString s
[] =
105 { tr ("&Playlist ID:"), tr ("&Username:") };
108 static_cast<int>(sizeof (s
) / sizeof (QString
));
113 idLabel
->setText (s
[n
]);
117 // Slot: Umph: finished.
120 YoutubeFeed::onFinished (QString s
) {
122 QString error
= tr ("Could not find the JSON data in umph output.");
123 const int n
= s
.indexOf ("{");
126 _got_items
= fromJSON (s
.mid (n
), error
);
131 NomNom::crit (this, error
);
135 // Slot: Umph: error.
138 YoutubeFeed::onError ()
139 { NomNom::crit (this, s
); }
143 // Done. QDialog and closeEvent design glitch workaround.
146 YoutubeFeed::done (int r
) {
148 if (r
== QDialog::Accepted
) {
150 const QString id
= idEdit
->text ();
156 shPrefs
.get (SharedPreferences::UmphPath
).toString ().split (" ");
160 switch (typeCombo
->currentIndex ()) {
162 case 0: type
= "p"; break;
163 case 1: type
= "f"; break;
164 case 2: type
= "u"; break;
167 args
.replaceInStrings ("%t", type
);
168 args
.replaceInStrings ("%s", startIndexSpin
->text ());
169 args
.replaceInStrings ("%m", maxResultsSpin
->text ());
170 args
.replaceInStrings ("%i", id
);
172 _prog
->setLabelText (tr ("Checking ..."));
173 _prog
->setMaximum (0);
181 NomNom::save_size (s
, this, QSETTINGS_GROUP
);
190 YoutubeFeed::closeEvent (QCloseEvent
*e
)
191 { QDialog::closeEvent (e
); }