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/>.
21 #include "ProcProgDiag.h"
23 ProcessProgressDialog::ProcessProgressDialog (QWidget
*parent
/*=NULL*/)
24 : QProgressDialog (parent
),
25 _rx_error (QRegExp ("error:\\s+(.*)$")),
29 do { connect (&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
31 _conn (started (), onProcStarted ());
33 _conn (error (QProcess::ProcessError
),
34 onProcError (QProcess::ProcessError
));
36 _conn (readyRead (), onProcReadyRead ());
38 _conn (finished (int, QProcess::ExitStatus
),
39 onProcFinished (int, QProcess::ExitStatus
));
43 connect (this, SIGNAL(canceled ()), this, SLOT (onCanceled ()));
45 setWindowModality (Qt::WindowModal
);
48 _proc
.setProcessChannelMode (QProcess::MergedChannels
);
53 ProcessProgressDialog::setLabelRegExp (const QHash
<QString
,QRegExp
>& h
)
59 ProcessProgressDialog::setErrorRegExp (const QRegExp
& rx
)
64 bool ProcessProgressDialog::canceled() const
70 ProcessProgressDialog::start (QStringList
& args
)
78 _proc
.start (args
.takeFirst (), args
);
84 ProcessProgressDialog::onProcStarted () { }
87 ProcessProgressDialog::onProcError (QProcess::ProcessError n
)
92 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n
));
99 ProcessProgressDialog::onProcReadyRead ()
101 static char data
[1024];
103 while (_proc
.readLine (data
, sizeof (data
)))
105 QString ln
= QString::fromLocal8Bit (data
);
108 QHashIterator
<QString
, QRegExp
> i (_rx_label
);
114 if (i
.value ().indexIn (ln
) != -1)
115 setLabelText (i
.key ());
124 ProcessProgressDialog::onProcFinished (
126 QProcess::ExitStatus exitStatus
)
128 if (exitStatus
== QProcess::NormalExit
&& exitCode
== 0)
129 emit
finished (_buffer
);
137 QString err
= _buffer
;
139 if (_rx_error
.indexIn (_buffer
) != -1)
140 err
= "error: " + _rx_error
.cap (1).simplified ();
143 NomNom::crit (parentWidget (), err
);
154 ProcessProgressDialog::onCanceled ()
157 if (_proc
.state () == QProcess::Running
)
161 // vim: set ts=2 sw=2 tw=72 expandtab: