Bump version to 0.3.1
[nomnom.git] / src / ProcProgDiag.cpp
blob5943460fa8b85cd9f61d03553b22430217fe25be
1 /*
2 * NomNom
3 * Copyright (C) 2010-2011 Toni Gundogdu <legatvs@gmail.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #ifdef ENABLE_VERBOSE
22 #include <QDebug>
23 #endif
25 #include "ProcProgDiag.h"
27 static QHash<QString,QRegExp> rx_labels;
29 ProcessProgressDialog::ProcessProgressDialog(QWidget *parent/*=NULL*/)
30 : QProgressDialog(parent), _canceled(false), _errcode(-1)
32 _proc.setProcessChannelMode(QProcess::MergedChannels);
34 #define _conn(s,sl) \
35 do { connect(&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
36 _conn(started(), onProcStarted());
37 _conn(error(QProcess::ProcessError), onProcError(QProcess::ProcessError));
38 _conn(readyRead(), onProcReadyRead());
39 _conn(finished(int, QProcess::ExitStatus),
40 onProcFinished(int, QProcess::ExitStatus));
41 #undef _conn
43 connect(this, SIGNAL(canceled()), this, SLOT(onCanceled()));
45 setWindowModality(Qt::WindowModal);
46 setAutoClose(false);
48 #define _wrap(s,r) do { rx_labels[s] = QRegExp(r); } while (0)
49 _wrap(tr("Checking..."), "^:: Check");
50 _wrap(tr("Fetching..."), "^:: Fetch");
51 _wrap(tr("Verifying..."), "^:: Verify");
52 #undef _wrap
55 bool ProcessProgressDialog::canceled() const
57 return _canceled;
60 bool ProcessProgressDialog::failed() const
62 return !_errmsg.isEmpty();
65 QString ProcessProgressDialog::errmsg() const
67 return _errmsg;
70 int ProcessProgressDialog::errcode() const
72 return _errcode;
75 void ProcessProgressDialog::start(QStringList& args)
77 #ifdef ENABLE_VERBOSE
78 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "args=" << args;
79 #endif
80 _canceled = false;
82 _buffer.clear();
83 _errmsg.clear();
84 _args = args;
86 show();
87 #ifdef _1
88 _proc.setProcessEnvironment(NomNom::proc_env());
89 #endif
90 _proc.start(args.takeFirst(), args);
91 exec();
94 void ProcessProgressDialog::onProcStarted()
96 #ifdef ENABLE_VERBOSE
97 qDebug() << __PRETTY_FUNCTION__ << __LINE__;
98 #endif
101 void ProcessProgressDialog::onProcError(QProcess::ProcessError n)
103 #ifdef ENABLE_VERBOSE
104 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "code=" << n;
105 #endif
106 if (!_canceled)
108 hide();
109 _errcode = static_cast<int>(n);
110 _errmsg = _proc.errorString();
111 emit error();
113 cancel();
116 static void update_label(QProgressDialog *d,
117 const QString& ln)
119 QHashIterator<QString, QRegExp> i(rx_labels);
120 while (i.hasNext())
122 i.next();
123 if (i.value().indexIn(ln) != -1)
124 d->setLabelText(i.key());
128 void ProcessProgressDialog::onProcReadyRead()
130 static char data[1024];
131 while (_proc.readLine(data, sizeof(data)))
133 const QString ln = QString::fromLocal8Bit(data);
134 update_label(this, ln);
135 _buffer += ln;
136 #ifdef ENABLE_VERBOSE
137 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "ln=" << ln;
138 #endif
142 static QRegExp rx_error("error:\\s+(.*)$");
144 void ProcessProgressDialog::onProcFinished(int ec, QProcess::ExitStatus es)
146 if (es == QProcess::NormalExit && ec == 0)
147 emit finished(_buffer);
148 else
150 if (!_canceled)
152 hide();
153 _errmsg = _buffer;
154 if (rx_error.indexIn(_buffer) != -1)
156 _errmsg = rx_error.cap(1).simplified();
157 _errcode = static_cast<int>(ec);
159 #ifdef ENABLE_VERBOSE
160 qDebug() << __PRETTY_FUNCTION__ << __LINE__ << "errmsg=" << _errmsg;
161 #endif
162 emit error();
165 cancel();
168 void ProcessProgressDialog::onCanceled()
170 _canceled = true;
171 if (_proc.state() == QProcess::Running)
172 _proc.kill();
175 // vim: set ts=2 sw=2 tw=72 expandtab: