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/>.
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
);
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
));
43 connect(this, SIGNAL(canceled()), this, SLOT(onCanceled()));
45 setWindowModality(Qt::WindowModal
);
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");
55 bool ProcessProgressDialog::canceled() const
60 bool ProcessProgressDialog::failed() const
62 return !_errmsg
.isEmpty();
65 QString
ProcessProgressDialog::errmsg() const
70 int ProcessProgressDialog::errcode() const
75 void ProcessProgressDialog::start(QStringList
& args
)
78 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< "args=" << args
;
88 _proc
.setProcessEnvironment(NomNom::proc_env());
90 _proc
.start(args
.takeFirst(), args
);
94 void ProcessProgressDialog::onProcStarted()
97 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
;
101 void ProcessProgressDialog::onProcError(QProcess::ProcessError n
)
103 #ifdef ENABLE_VERBOSE
104 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< "code=" << n
;
109 _errcode
= static_cast<int>(n
);
110 _errmsg
= _proc
.errorString();
116 static void update_label(QProgressDialog
*d
,
119 QHashIterator
<QString
, QRegExp
> i(rx_labels
);
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
);
136 #ifdef ENABLE_VERBOSE
137 qDebug() << __PRETTY_FUNCTION__
<< __LINE__
<< "ln=" << ln
;
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
);
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
;
168 void ProcessProgressDialog::onCanceled()
171 if (_proc
.state() == QProcess::Running
)
175 // vim: set ts=2 sw=2 tw=72 expandtab: