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
;
28 NFeedProgressDialog::NFeedProgressDialog(QWidget
*parent
/*=NULL*/)
29 : QProgressDialog(parent
), _proc(NULL
), _cancelled(false)
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
);
45 bool NFeedProgressDialog::open(QStringList
& args
)
47 setLabelText(tr("Working..."));
55 _proc
->start(args
.takeFirst(), args
);
57 return _errmsg
.isEmpty();
60 bool NFeedProgressDialog::results(feed::NFeedList
& dst
, QString
& err
)
62 const int n
= _buffer
.indexOf("{");
65 err
= tr("Unexpected data from umph");
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())
79 QScriptValueIterator
i(v
.property("video"));
82 err
= tr("umph did not return any video entries");
91 if (i
.flags() & QScriptValue::SkipInEnumeration
)
94 t
= v
.property("title").toString();
95 u
= v
.property("url").toString();
97 feed::NFeedItem
item(t
,u
);
103 void NFeedProgressDialog::error(QProcess::ProcessError n
)
107 _errmsg
= tr("Error while running command:<p>%1</p>"
108 "Qt error message follows (code #%2):<p>%3</p>")
111 .arg(_proc
->errorString());
116 void NFeedProgressDialog::read()
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)
129 QRegExp
rx("error: (.*)");
130 if (rx
.indexIn(_buffer
) != -1)
131 _errmsg
= rx
.cap(1).simplified();
133 _errmsg
= tr("Unknown error occurred");
139 void NFeedProgressDialog::cleanup()
142 if (_proc
->state() == QProcess::Running
)
146 QString
NFeedProgressDialog::errmsg() const
151 bool NFeedProgressDialog::cancelled() const
158 /* vim: set ts=2 sw=2 tw=72 expandtab: */