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+(.*)$")),
33 do { connect (&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
35 _conn (started (), onProcStarted ());
37 _conn (error (QProcess::ProcessError
),
38 onProcError (QProcess::ProcessError
));
40 _conn (readyRead (), onProcReadyRead ());
42 _conn (finished (int, QProcess::ExitStatus
),
43 onProcFinished (int, QProcess::ExitStatus
));
47 connect (this, SIGNAL(canceled ()), this, SLOT (onCanceled ()));
49 setWindowModality (Qt::WindowModal
);
52 _proc
.setProcessChannelMode (QProcess::MergedChannels
);
57 ProcessProgressDialog::setLabelRegExp (const QHash
<QString
,QRegExp
>& h
)
63 ProcessProgressDialog::setErrorRegExp (const QRegExp
& rx
)
69 ProcessProgressDialog::start (QStringList
& args
)
76 log
<< args
.join (" ") + "\n";
79 _proc
.start (args
.takeFirst (), args
);
85 ProcessProgressDialog::onProcStarted () { }
88 ProcessProgressDialog::onProcError (QProcess::ProcessError n
)
93 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n
));
100 ProcessProgressDialog::onProcReadyRead ()
102 static char data
[1024];
104 while (_proc
.readLine (data
, sizeof (data
)))
107 QString ln
= QString::fromLocal8Bit (data
);
112 QHashIterator
<QString
, QRegExp
> i (_rx_label
);
119 if (i
.value ().indexIn (ln
) != -1)
120 setLabelText (i
.key ());
129 ProcessProgressDialog::onProcFinished (
131 QProcess::ExitStatus exitStatus
)
133 if (exitStatus
== QProcess::NormalExit
&& exitCode
== 0)
134 emit
finished (_buffer
);
142 QString err
= _buffer
;
144 if (_rx_error
.indexIn (_buffer
) != -1)
145 err
= "error: " + _rx_error
.cap (1).simplified ();
148 NomNom::crit (parentWidget (), err
);
159 ProcessProgressDialog::onCanceled ()
162 if (_proc
.state () == QProcess::Running
)
166 // vim: set ts=2 sw=2 tw=72 expandtab: