Fraxinus  17.12
An IGT application
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 #include <iostream>
40 
41 namespace cx
42 {
43 
44 ProcessWrapper::ProcessWrapper(QString name, QObject* parent) :
45  QObject(parent), mName(name), mLastExecutablePath("")
46 {
47  mProcess = new QProcess(this);
48  mReporter = ProcessReporterPtr(new ProcessReporter(mProcess, mName));
49 
50  mProcess->setProcessChannelMode(QProcess::MergedChannels);
51  mProcess->setReadChannel(QProcess::StandardOutput);
52 
53  connect(mProcess.data(), &QProcess::stateChanged, this, &ProcessWrapper::stateChanged);
54 }
55 
57 {
58  mProcess->terminate();//terminate gives the process a chance to shutdown
59  this->waitForFinished();
60 }
61 
63 {
64  return mProcess.data();
65 }
66 
67 void ProcessWrapper::launchWithRelativePath(QString executable, QStringList arguments)
68 {
69  QString absolutePathToExe = this->getExecutableInBundlesAbsolutePath(executable);
70 
71  this->launch(absolutePathToExe, arguments);
72 }
73 
74 void ProcessWrapper::launch(QString executable, QStringList arguments)
75 {
76  if (executable.isEmpty() || this->isRunning())
77  return;
78 
79  this->internalLaunch(executable, arguments);
80 }
81 
83 {
84  return mProcess->state() == QProcess::Running;
85 }
86 
87 qint64 ProcessWrapper::write(const char * data)
88 {
89  return mProcess->write(data);
90 }
91 
93 {
94  return mProcess->waitForStarted(msecs);
95 }
96 
98 {
99  return mProcess->waitForFinished(msecs);
100 }
101 
102 QString ProcessWrapper::getExecutableInBundlesAbsolutePath(QString exeInBundle)
103 {
104  QString absolutePathToExe = exeInBundle;
105 
106  if (!QFileInfo(absolutePathToExe).isAbsolute())
107  absolutePathToExe = DataLocations::getBundlePath() + "/" + absolutePathToExe;
108 
109  absolutePathToExe = absolutePathToExe.trimmed();
110  absolutePathToExe = QDir::cleanPath(absolutePathToExe);
111 
112  if (!QFileInfo(absolutePathToExe).exists())
113  reportError(QString("Cannot find %1 [%2]").arg(mName).arg(absolutePathToExe));
114 
115  return absolutePathToExe;
116 }
117 
118 void ProcessWrapper::internalLaunch(QString executable, QStringList arguments)
119 {
120  if(this->isRunning())
121  return;
122 
123  report(QString("Launching %1: [%2 %3]").arg(mName).arg(executable).arg(arguments.join(" ")));
124 
125  if(arguments.isEmpty())
126  mProcess->start(executable);
127  else
128  mProcess->start(executable, arguments);
129 
130  mLastExecutablePath = executable;
131 }
132 }
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.
Namespace for all CustusX production code.