additional umph specifiers
[nomnom.git] / src / i / YoutubeFeed.cpp
blob36e870856a29b0174d497dbcc3e7ca58890f4583
1 /*
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/>.
18 #include <QSettings>
19 #include <QScriptEngine>
20 #include <QScriptValueIterator>
21 #include <QDebug>
23 #include "util.h"
24 #include "Log.h"
25 #include "Preferences.h"
26 #include "YoutubeFeed.h"
28 #define QSETTINGS_GROUP "YoutubeFeed"
30 // main.cpp
31 extern SharedPreferences shPrefs;
33 // Ctor.
35 YoutubeFeed::YoutubeFeed (QWidget *parent)
36 : QDialog(parent), _got_items (false)
38 setupUi (this);
40 QSettings s;
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)));
46 #ifdef _0
47 connect (_prog, SIGNAL (error ()), this, SLOT (onError ()));
48 #endif
51 bool YoutubeFeed::gotItems () const { return _got_items; }
53 // main.cpp
54 extern QHash<QString,QString> feed;
56 bool
57 YoutubeFeed::fromJSON (const QString& data, QString& error) {
59 QScriptEngine e;
61 QScriptValue v = e.evaluate ("(" +data+ ")");
63 if (e.hasUncaughtException ()) {
64 error = tr ("Uncaught exception at line %1: %2")
65 .arg (e.uncaughtExceptionLineNumber ())
66 .arg (v.toString ());
67 return false;
70 QScriptValueIterator i (v.property ("video"));
72 if (!i.hasNext ()) {
73 error = tr ("Expected at least one video from umph(1), got none.");
74 return false;
77 feed.clear ();
79 QString t,u;
81 while (i.hasNext ()) {
83 i.next ();
85 if (i.flags() & QScriptValue::SkipInEnumeration)
86 continue;
88 v = i.value ();
89 t = v.property ("title").toString ();
90 u = v.property ("url").toString ();
92 feed[t] = u;
96 return true;
99 // Slot: type changed.
101 void
102 YoutubeFeed::onTypeChanged (int n) {
104 static const QString s[] =
105 { tr ("&Playlist ID:"), tr ("&Username:") };
107 static const int m =
108 static_cast<int>(sizeof (s) / sizeof (QString));
110 if (n >= m)
111 n = 1;
113 idLabel->setText (s[n]);
117 // Slot: Umph: finished.
119 void
120 YoutubeFeed::onFinished (QString s) {
122 QString error = tr ("Could not find the JSON data in umph output.");
123 const int n = s.indexOf ("{");
125 if (n != -1) {
126 _got_items = fromJSON (s.mid (n), error);
127 if (_got_items)
128 return;
131 NomNom::crit (this, error);
134 #ifdef _0
135 // Slot: Umph: error.
137 void
138 YoutubeFeed::onError ()
139 { NomNom::crit (this, s); }
141 #endif
143 // Done. QDialog and closeEvent design glitch workaround.
145 void
146 YoutubeFeed::done (int r) {
148 if (r == QDialog::Accepted) {
150 const QString id = idEdit->text ();
152 if (id.isEmpty ())
153 return;
155 QStringList args =
156 shPrefs.get (SharedPreferences::UmphPath).toString ().split (" ");
158 QString type;
160 switch (typeCombo->currentIndex ()) {
161 default:
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);
174 _prog->start (args);
176 if (!_got_items)
177 return;
180 QSettings s;
181 NomNom::save_size (s, this, QSETTINGS_GROUP);
183 QDialog::done (r);
184 close ();
187 // Close.
189 void
190 YoutubeFeed::closeEvent (QCloseEvent *e)
191 { QDialog::closeEvent (e); }