restore if minimized and error occurs
[nomnom.git] / src / ProcProgDiag.cpp
blobf261aabbc4df9b9868bf9ae7728f5744c236aa69
1 /*
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/>.
18 #include <QDebug>
20 #include "util.h"
21 #include "Log.h"
22 #include "ProcProgDiag.h"
24 // main.cpp
25 extern Log log;
27 ProcessProgressDialog::ProcessProgressDialog (QWidget *parent/*=NULL*/)
28 : QProgressDialog (parent),
29 _rx_error (QRegExp ("error:\\s+(.*)$")),
30 _canceled (false)
33 #define _conn(s,sl) \
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));
46 #undef _conn
48 connect (this, SIGNAL(canceled ()), this, SLOT (onCanceled ()));
50 setWindowModality (Qt::WindowModal);
51 setAutoClose (false);
53 _proc.setProcessChannelMode (QProcess::MergedChannels);
57 void
58 ProcessProgressDialog::setLabelRegExp (const QHash<QString,QRegExp>& h)
59 { _rx_label = h; }
61 void
62 ProcessProgressDialog::setErrorRegExp (const QRegExp& rx)
63 { _rx_error = rx; }
65 void
66 ProcessProgressDialog::start (QStringList& args) {
68 _buffer.clear ();
69 _error.clear ();
71 _canceled = false;
73 log << args.join (" ") + "\n";
75 show ();
76 _proc.start (args.takeFirst (), args);
77 exec ();
81 void
82 ProcessProgressDialog::onProcStarted () { }
84 void
85 ProcessProgressDialog::onProcError (QProcess::ProcessError n) {
86 if (!_canceled) {
87 hide ();
88 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n));
89 emit error ();
91 cancel ();
94 void
95 ProcessProgressDialog::onProcReadyRead () {
97 static char data[1024];
99 while (_proc.readLine (data, sizeof (data))) {
101 QString ln = QString::fromLocal8Bit (data);
103 _buffer += ln;
104 log << ln;
106 QHashIterator<QString, QRegExp> i (_rx_label);
108 while (i.hasNext ()) {
110 i.next ();
112 if (i.value ().indexIn (ln) != -1)
113 setLabelText (i.key ());
121 void
122 ProcessProgressDialog::onProcFinished (
123 int exitCode,
124 QProcess::ExitStatus exitStatus)
126 if (exitStatus == QProcess::NormalExit && exitCode == 0)
127 emit finished (_buffer);
129 else {
131 if (!_canceled) {
133 QString err = _buffer;
135 if (_rx_error.indexIn (_buffer) != -1)
136 err = "error: " + _rx_error.cap (1).simplified ();
138 hide ();
139 NomNom::crit (parentWidget (), err);
141 emit error ();
146 cancel ();
149 void
150 ProcessProgressDialog::onCanceled () {
151 _canceled = true;
152 if (_proc.state () == QProcess::Running)
153 _proc.kill ();