Use QJson for parsing umph output
[nomnom.git] / src / feed / nfeedprogressdialog.cpp
blob303eabe65ea0f6dc6af6187dc40b520db9b70726
1 /* NomNom
2 * Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
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 "config.h"
20 #include <QCoreApplication>
21 #include <QVariant>
23 #ifdef ENABLE_VERBOSE
24 #include <QDebug>
25 #endif
27 #include <qjson/parser.h>
29 #include <NFeedProgressDialog>
31 extern QString umph_path;
33 namespace nn
36 NFeedProgressDialog::NFeedProgressDialog(QWidget *parent/*=NULL*/)
37 : QProgressDialog(parent), _cancelled(false), _errcode(-1), _proc(NULL)
39 _proc = new QProcess;
40 _proc->setProcessChannelMode(QProcess::MergedChannels);
42 connect(_proc, SIGNAL(error(QProcess::ProcessError)),
43 this, SLOT(error(QProcess::ProcessError)));
44 connect(_proc, SIGNAL(readyRead()), this, SLOT(read()));
45 connect(_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
46 this, SLOT(finished(int, QProcess::ExitStatus)));
47 connect(this, SIGNAL(canceled()), this, SLOT(cleanup()));
49 setWindowModality(Qt::WindowModal);
50 setAutoClose(false);
53 bool NFeedProgressDialog::open(QStringList args)
55 setLabelText(tr("Working..."));
57 _cancelled = false;
58 _args = args;
60 _buffer.clear();
61 _errmsg.clear();
63 setMaximum(0);
64 setMinimum(0);
66 show();
67 #ifdef ENABLE_VERBOSE
68 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "args=" << args;
69 #endif
70 _proc->start(args.takeFirst(), args);
71 exec();
73 return _errmsg.isEmpty();
76 bool NFeedProgressDialog::results(feed::NFeedList& dst, QString& errmsg)
78 const int n = _buffer.indexOf("{");
79 if (n == -1)
81 errmsg = tr("Unexpected data from umph");
82 return false;
85 QJson::Parser p;
86 bool ok;
88 QVariantMap r = p.parse(_buffer.mid(n).toLocal8Bit(), &ok).toMap();
89 if (!ok)
91 errmsg = tr("An error occurred while parsing JSON");
92 return false;
95 dst.clear();
97 foreach (const QVariant v, r["video"].toList())
99 const QVariantMap m = v.toMap();
100 feed::NFeedItem i(m["title"].toString(), m["url"].toString());
101 dst.append(i);
103 return true;
106 void NFeedProgressDialog::error(QProcess::ProcessError n)
108 if (!_cancelled)
110 _errmsg = _proc->errorString();
111 _errcode = n;
113 cancel();
116 void NFeedProgressDialog::read()
118 static char b[1024];
119 while (_proc->readLine(b, sizeof(b)))
120 _buffer += QString::fromLocal8Bit(b);
123 void NFeedProgressDialog::finished(int ec, QProcess::ExitStatus es)
125 if (es != QProcess::NormalExit || ec != 0)
127 if (!_cancelled)
129 QRegExp rx("error: (.*)");
130 QString m = (rx.indexIn(_buffer) != -1)
131 ? rx.cap(1).simplified()
132 : _buffer;
133 _errcode = ec;
134 _errmsg = m;
137 cancel();
141 void NFeedProgressDialog::cleanup()
143 _cancelled = true;
144 if (_proc->state() == QProcess::Running)
145 _proc->kill();
148 QString NFeedProgressDialog::errmsg() const
150 return _errmsg;
153 int NFeedProgressDialog::errcode() const
155 return _errcode;
158 bool NFeedProgressDialog::cancelled() const
160 return _cancelled;
163 } // namespace nn
165 /* vim: set ts=2 sw=2 tw=72 expandtab: */