CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxProcessWrapper.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxProcessWrapper.h"
34 #include <QDir>
35 #include <QFileInfo>
36 #include "cxDataLocations.h"
37 #include "cxLogger.h"
38 #include "cxTypeConversions.h"
39 
40 namespace cx
41 {
42 
43 ProcessWrapper::ProcessWrapper(QString name, QObject* parent) :
44  QObject(parent), mName(name), mLastExecutablePath("")
45 {
46  mProcess = new QProcess(this);
47  mReporter = ProcessReporterPtr(new ProcessReporter(mProcess, mName));
48 
49  mProcess->setProcessChannelMode(QProcess::MergedChannels);
50  mProcess->setReadChannel(QProcess::StandardOutput);
51 }
52 
54 {
55  mProcess->close();
56 }
57 
59 {
60  return mProcess;
61 }
62 
63 void ProcessWrapper::launchWithRelativePath(QString executable, QStringList arguments)
64 {
65  QString absolutePathToExe = this->getExecutableInBundlesAbsolutePath(executable);
66 
67  this->launch(absolutePathToExe, arguments);
68 }
69 
70 void ProcessWrapper::launch(QString executable, QStringList arguments)
71 {
72  if (executable.isEmpty() || this->isRunning())
73  return;
74 
75  this->internalLaunch(executable, arguments);
76 }
77 
79 {
80  return mProcess->state() == QProcess::Running;
81 }
82 
83 qint64 ProcessWrapper::write(const char * data)
84 {
85  return mProcess->write(data);
86 }
87 
89 {
90  return mProcess->waitForStarted(msecs);
91 }
92 
94 {
95  return mProcess->waitForFinished(msecs);
96 }
97 
98 QString ProcessWrapper::getExecutableInBundlesAbsolutePath(QString exeInBundle)
99 {
100  QString absolutePathToExe = exeInBundle;
101 
102  if (!QFileInfo(absolutePathToExe).isAbsolute())
103  absolutePathToExe = DataLocations::getBundlePath() + "/" + absolutePathToExe;
104 
105  absolutePathToExe = absolutePathToExe.trimmed();
106  absolutePathToExe = QDir::cleanPath(absolutePathToExe);
107 
108  if (!QFileInfo(absolutePathToExe).exists())
109  reportError(QString("Cannot find %1 [%2]").arg(mName).arg(absolutePathToExe));
110 
111  return absolutePathToExe;
112 }
113 
114 void ProcessWrapper::internalLaunch(QString executable, QStringList arguments)
115 {
116  if(this->isRunning())
117  return;
118 
119  report(QString("Launching %1: [%2 %3]").arg(mName).arg(executable).arg(arguments.join(" ")));
120 
121  if(arguments.isEmpty())
122  mProcess->start(executable);
123  else
124  mProcess->start(executable, arguments);
125 
126  mLastExecutablePath = executable;
127 }
128 }
void reportError(QString msg)
Definition: cxLogger.cpp:92
void launchWithRelativePath(QString executable, QStringList arguments=QStringList())
void launch(QString executable, QStringList argument=QStringList())
ProcessWrapper(QString name="executable", QObject *parent=NULL)
qint64 write(const char *data)
bool waitForStarted(int msecs=30000)
bool waitForFinished(int msecs=30000)
void report(QString msg)
Definition: cxLogger.cpp:90
boost::shared_ptr< class ProcessReporter > ProcessReporterPtr
static QString getBundlePath()
return the folder where the bundle or executable are located.