Reverse line order in Log(View)
[nomnom.git] / src / ProcProgDiag.cpp
blob88bf455f58c6e8934a7f71f735d8d9b6b40dd27d
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)
32 #define _conn(s,sl) \
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));
45 #undef _conn
47 connect (this, SIGNAL(canceled ()), this, SLOT (onCanceled ()));
49 setWindowModality (Qt::WindowModal);
50 setAutoClose (false);
52 _proc.setProcessChannelMode (QProcess::MergedChannels);
56 void
57 ProcessProgressDialog::setLabelRegExp (const QHash<QString,QRegExp>& h)
59 _rx_label = h;
62 void
63 ProcessProgressDialog::setErrorRegExp (const QRegExp& rx)
65 _rx_error = rx;
68 void
69 ProcessProgressDialog::start (QStringList& args)
71 _buffer.clear ();
72 _error.clear ();
74 _canceled = false;
76 log << args.join (" ") + "\n";
78 show ();
79 _proc.start (args.takeFirst (), args);
80 exec ();
84 void
85 ProcessProgressDialog::onProcStarted () { }
87 void
88 ProcessProgressDialog::onProcError (QProcess::ProcessError n)
90 if (!_canceled)
92 hide ();
93 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n));
94 emit error ();
96 cancel ();
99 void
100 ProcessProgressDialog::onProcReadyRead ()
102 static char data[1024];
104 while (_proc.readLine (data, sizeof (data)))
107 QString ln = QString::fromLocal8Bit (data);
109 _buffer += ln;
110 log << ln;
112 QHashIterator<QString, QRegExp> i (_rx_label);
114 while (i.hasNext ())
117 i.next ();
119 if (i.value ().indexIn (ln) != -1)
120 setLabelText (i.key ());
128 void
129 ProcessProgressDialog::onProcFinished (
130 int exitCode,
131 QProcess::ExitStatus exitStatus)
133 if (exitStatus == QProcess::NormalExit && exitCode == 0)
134 emit finished (_buffer);
136 else
139 if (!_canceled)
142 QString err = _buffer;
144 if (_rx_error.indexIn (_buffer) != -1)
145 err = "error: " + _rx_error.cap (1).simplified ();
147 hide ();
148 NomNom::crit (parentWidget (), err);
150 emit error ();
155 cancel ();
158 void
159 ProcessProgressDialog::onCanceled ()
161 _canceled = true;
162 if (_proc.state () == QProcess::Running)
163 _proc.kill ();
166 // vim: set ts=2 sw=2 tw=72 expandtab: