Revise HowtoTranslate, add gen-tr.sh
[nomnom.git] / src / ProcProgDiag.cpp
blob3e3bf70e95143d03f84e3bd953141761cd9e86bf
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 "ProcProgDiag.h"
23 ProcessProgressDialog::ProcessProgressDialog (QWidget *parent/*=NULL*/)
24 : QProgressDialog (parent),
25 _rx_error (QRegExp ("error:\\s+(.*)$")),
26 _canceled (false)
28 #define _conn(s,sl) \
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));
41 #undef _conn
43 connect (this, SIGNAL(canceled ()), this, SLOT (onCanceled ()));
45 setWindowModality (Qt::WindowModal);
46 setAutoClose (false);
48 _proc.setProcessChannelMode (QProcess::MergedChannels);
52 void
53 ProcessProgressDialog::setLabelRegExp (const QHash<QString,QRegExp>& h)
55 _rx_label = h;
58 void
59 ProcessProgressDialog::setErrorRegExp (const QRegExp& rx)
61 _rx_error = rx;
64 bool ProcessProgressDialog::canceled() const
66 return _canceled;
69 void
70 ProcessProgressDialog::start (QStringList& args)
72 _buffer.clear ();
73 _error.clear ();
75 _canceled = false;
77 show ();
78 _proc.start (args.takeFirst (), args);
79 exec ();
83 void
84 ProcessProgressDialog::onProcStarted () { }
86 void
87 ProcessProgressDialog::onProcError (QProcess::ProcessError n)
89 if (!_canceled)
91 hide ();
92 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n));
93 emit error ();
95 cancel ();
98 void
99 ProcessProgressDialog::onProcReadyRead ()
101 static char data[1024];
103 while (_proc.readLine (data, sizeof (data)))
105 QString ln = QString::fromLocal8Bit (data);
106 _buffer += ln;
108 QHashIterator<QString, QRegExp> i (_rx_label);
109 while (i.hasNext ())
112 i.next ();
114 if (i.value ().indexIn (ln) != -1)
115 setLabelText (i.key ());
123 void
124 ProcessProgressDialog::onProcFinished (
125 int exitCode,
126 QProcess::ExitStatus exitStatus)
128 if (exitStatus == QProcess::NormalExit && exitCode == 0)
129 emit finished (_buffer);
131 else
134 if (!_canceled)
137 QString err = _buffer;
139 if (_rx_error.indexIn (_buffer) != -1)
140 err = "error: " + _rx_error.cap (1).simplified ();
142 hide ();
143 NomNom::crit (parentWidget (), err);
145 emit error ();
150 cancel ();
153 void
154 ProcessProgressDialog::onCanceled ()
156 _canceled = true;
157 if (_proc.state () == QProcess::Running)
158 _proc.kill ();
161 // vim: set ts=2 sw=2 tw=72 expandtab: