Use QProcess::errorString instead
[nomnom.git] / src / feed / nfeedprogressdialog.cpp
blob1774c496109e36e3cb4bbef52217af5c73161c34
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 <QScriptValueIterator>
19 #include <QScriptEngine>
21 #include <NFeedProgressDialog>
23 extern QString umph_path;
25 namespace nn
28 NFeedProgressDialog::NFeedProgressDialog(QWidget *parent/*=NULL*/)
29 : QProgressDialog(parent), _proc(NULL), _cancelled(false)
31 _proc = new QProcess;
32 _proc->setProcessChannelMode(QProcess::MergedChannels);
34 connect(_proc, SIGNAL(error(QProcess::ProcessError)),
35 this, SLOT(error(QProcess::ProcessError)));
36 connect(_proc, SIGNAL(readyRead()), this, SLOT(read()));
37 connect(_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
38 this, SLOT(finished(int, QProcess::ExitStatus)));
39 connect(this, SIGNAL(canceled()), this, SLOT(cleanup()));
41 setWindowModality(Qt::WindowModal);
42 setAutoClose(false);
45 bool NFeedProgressDialog::open(QStringList& args)
47 setLabelText(tr("Working..."));
48 _cancelled = false;
49 _buffer.clear();
50 _errmsg.clear();
51 setMaximum(0);
52 setMinimum(0);
53 _args = args;
54 show();
55 _proc->start(args.takeFirst(), args);
56 exec();
57 return _errmsg.isEmpty();
60 bool NFeedProgressDialog::results(feed::NFeedList& dst, QString& err)
62 const int n = _buffer.indexOf("{");
63 if (n == -1)
65 err = tr("Unexpected data from umph");
66 return false;
69 QScriptEngine e;
70 QScriptValue v = e.evaluate("("+_buffer.mid(n)+")");
71 if (e.hasUncaughtException())
73 err = tr("Uncaught exception at line %1: %2")
74 .arg(e.uncaughtExceptionLineNumber())
75 .arg(v.toString());
76 return false;
79 QScriptValueIterator i(v.property("video"));
80 if (!i.hasNext())
82 err = tr("umph did not return any video entries");
83 return false;
85 dst.clear();
87 QString t,u;
88 while (i.hasNext())
90 i.next();
91 if (i.flags() & QScriptValue::SkipInEnumeration)
92 continue;
93 v = i.value();
94 t = v.property("title").toString();
95 u = v.property("url").toString();
97 feed::NFeedItem item(t,u);
98 dst.append(item);
100 return true;
103 void NFeedProgressDialog::error(QProcess::ProcessError n)
105 if (!_cancelled)
107 _errmsg = tr("Error while running command:<p>%1</p>"
108 "Qt error message follows (code #%2):<p>%3</p>")
109 .arg(_args.first())
110 .arg(n)
111 .arg(_proc->errorString());
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 if (rx.indexIn(_buffer) != -1)
131 _errmsg = rx.cap(1).simplified();
132 else
133 _errmsg = tr("Unknown error occurred");
136 cancel();
139 void NFeedProgressDialog::cleanup()
141 _cancelled = true;
142 if (_proc->state() == QProcess::Running)
143 _proc->kill();
146 QString NFeedProgressDialog::errmsg() const
148 return _errmsg;
151 bool NFeedProgressDialog::cancelled() const
153 return _cancelled;
156 } // namespace nn
158 /* vim: set ts=2 sw=2 tw=72 expandtab: */