Debian Codegraph @

HercUtilities/GenericUtility.cpp

raw symbols SCIP
1 /*
2 * File: GenericUtility.h
3 *
4 * Author: Jacob Dekel
5 * Created on: Aug 26, 2010
6 *
7 * Copyright (c) 2010 Jacob Dekel
8 * $Id$
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25 #include "GenericUtility.h"
26 #include "UtilityExecutor.h"
27
28 #include <QMessageBox>
29 #include <QTimer>
30
31 GenericUtility::GenericUtility(QString name, QWidget *parent) :
32 QDialog(parent),
33 mName(name),
34 mExecutor(NULL),
35 mRunner(NULL),
36 mErrorRunner(NULL)
37 {
38
39 }
40
41 GenericUtility::~GenericUtility()
42 {
43 delete mRunner;
44 delete mErrorRunner;
45 delete mExecutor;
46 }
47
48 bool GenericUtility::runOrStopClicked()
49 {
50 if (mExecutor != NULL)
51 {
52 mExecutor->terminate();
53 mExecutor = NULL;
54 QMessageBox::warning(this, mName,
55 mName +" operation was aborted at user's request",
56 QMessageBox::Ok,
57 QMessageBox::NoButton);
58 return false;
59 }
60 else
61 {
62 return true;
63 }
64 }
65
66 int GenericUtility::execute(const std::string & command, const std::string& path, std::vector<std::string>& parameters, const QString &workingDirectory)
67 {
68 mStarted = true;
69 mFinished = false;
70 parameters.push_back("EXTERNALGUI");
71
72 mExecutor = new UtilityExecutor(this);
73 mRunner = new UtilityRunner(mExecutor);
74 mErrorRunner = new UtilityRunner(mExecutor);
75 emit output(getFlatCommand(command,path,parameters).c_str());
76 int ret = mExecutor->run(command, path, parameters, mRunner, mErrorRunner, workingDirectory);
77
78 // do not wait forever
79 //QTimer::singleShot(2000, this, SLOT(timeout()));
80
81 // we should know if finished
82 connect(mExecutor->getQProcess(),SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(finished(int, QProcess::ExitStatus)));
83
84 // connect to default handlers (superclass can override these slots)
85 connect(mRunner, SIGNAL(error(QByteArray)), this, SLOT(runnerError(QByteArray)));
86 connect(mErrorRunner, SIGNAL(error(QByteArray)), this, SLOT(runnerError(QByteArray)));
87
88 // general error status handler
89 connect(this, SIGNAL(error()), this, SLOT(errorSlot()));
90
91 return ret;
92 }
93
94 void GenericUtility::timeout()
95 {
96 }
97
98 void GenericUtility::finished(int result, QProcess::ExitStatus status)
99 {
100 mFinished = true;
101 mRc = result;
102 mFinishedOK = ( (status==QProcess::NormalExit) && (mRc == 0) );
103 QByteArray rcLine = mName.toUtf8() + " ended; rc=";
104 rcLine += QString::number(result);
105 emit output(rcLine);
106 finishedSlot();
107 }
108
109 void GenericUtility::errorSlot()
110 {
111 QMessageBox::warning(this, mName,
112 mName + " failed. Please check the log for details",
113 QMessageBox::Ok,
114 QMessageBox::NoButton);
115 deleteLater();
116 }
117
118 void GenericUtility::runnerError(const QByteArray& line)
119 {
120 emit output(line);
121 }
122
123 std::string GenericUtility::getFlatCommand(const std::string & command, const std::string& path, std::vector<std::string>& parameters)
124 {
125 std::string result;
126 result = path;
127 if (path.size() > 0 ) result += "/";
128 result+=command;
129 for (unsigned int i = 0;
130 i < parameters.size()-1;
131 i++)
132 {
133 result += " ";
134 result+=parameters[i];
135 }
136 return result;
137 }