2 * Copyright (C) 2010 Toni Gundogdu.
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/>.
22 #include "ProcProgDiag.h"
27 ProcessProgressDialog::ProcessProgressDialog (QWidget
*parent
/*=NULL*/)
28 : QProgressDialog (parent
),
29 _rx_error (QRegExp ("error:\\s+(.*)$")),
34 do { connect (&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
36 _conn (started (), onProcStarted ());
38 _conn (error (QProcess::ProcessError
),
39 onProcError (QProcess::ProcessError
));
41 _conn (readyRead (), onProcReadyRead ());
43 _conn (finished (int, QProcess::ExitStatus
),
44 onProcFinished (int, QProcess::ExitStatus
));
48 connect (this, SIGNAL(canceled ()), this, SLOT (onCanceled ()));
50 setWindowModality (Qt::WindowModal
);
53 _proc
.setProcessChannelMode (QProcess::MergedChannels
);
58 ProcessProgressDialog::setLabelRegExp (const QHash
<QString
,QRegExp
>& h
)
62 ProcessProgressDialog::setErrorRegExp (const QRegExp
& rx
)
66 ProcessProgressDialog::start (QStringList
& args
) {
73 log
<< args
.join (" ") + "\n";
76 _proc
.start (args
.takeFirst (), args
);
82 ProcessProgressDialog::onProcStarted () { }
85 ProcessProgressDialog::onProcError (QProcess::ProcessError n
) {
88 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n
));
95 ProcessProgressDialog::onProcReadyRead () {
97 static char data
[1024];
99 while (_proc
.readLine (data
, sizeof (data
))) {
101 QString ln
= QString::fromLocal8Bit (data
);
106 QHashIterator
<QString
, QRegExp
> i (_rx_label
);
108 while (i
.hasNext ()) {
112 if (i
.value ().indexIn (ln
) != -1)
113 setLabelText (i
.key ());
122 ProcessProgressDialog::onProcFinished (
124 QProcess::ExitStatus exitStatus
)
126 if (exitStatus
== QProcess::NormalExit
&& exitCode
== 0)
127 emit
finished (_buffer
);
133 QString err
= _buffer
;
135 if (_rx_error
.indexIn (_buffer
) != -1)
136 err
= "error: " + _rx_error
.cap (1).simplified ();
139 NomNom::crit (parentWidget (), err
);
150 ProcessProgressDialog::onCanceled () {
152 if (_proc
.state () == QProcess::Running
)