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/>.
20 #include <QCoreApplication>
27 #include <qjson/parser.h>
29 #include <NFeedProgressDialog>
31 extern QString umph_path
;
36 NFeedProgressDialog::NFeedProgressDialog(QWidget
*parent
/*=NULL*/)
37 : QProgressDialog(parent
), _cancelled(false), _errcode(-1), _proc(NULL
)
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
);
53 bool NFeedProgressDialog::open(QStringList args
)
55 setLabelText(tr("Working..."));
68 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< "args=" << args
;
70 _proc
->start(args
.takeFirst(), args
);
73 return _errmsg
.isEmpty();
76 bool NFeedProgressDialog::results(feed::NFeedList
& dst
, QString
& errmsg
)
78 const int n
= _buffer
.indexOf("{");
81 errmsg
= tr("Unexpected data from umph");
88 QVariantMap r
= p
.parse(_buffer
.mid(n
).toLocal8Bit(), &ok
).toMap();
91 errmsg
= tr("An error occurred while parsing JSON");
97 foreach (const QVariant v
, r
["video"].toList())
99 const QVariantMap m
= v
.toMap();
100 feed::NFeedItem
i(m
["title"].toString(), m
["url"].toString());
106 void NFeedProgressDialog::error(QProcess::ProcessError n
)
110 _errmsg
= _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 QString m
= (rx
.indexIn(_buffer
) != -1)
131 ? rx
.cap(1).simplified()
141 void NFeedProgressDialog::cleanup()
144 if (_proc
->state() == QProcess::Running
)
148 QString
NFeedProgressDialog::errmsg() const
153 int NFeedProgressDialog::errcode() const
158 bool NFeedProgressDialog::cancelled() const
165 /* vim: set ts=2 sw=2 tw=72 expandtab: */