Fraxinus  16.5.0-fx-rc1
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 #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->close();
59 }
60 
62 {
63  return mProcess.data();
64 }
65 
66 void ProcessWrapper::launchWithRelativePath(QString executable, QStringList arguments)
67 {
68  QString absolutePathToExe = this->getExecutableInBundlesAbsolutePath(executable);
69 
70  this->launch(absolutePathToExe, arguments);
71 }
72 
73 void ProcessWrapper::launch(QString executable, QStringList arguments)
74 {
75  if (executable.isEmpty() || this->isRunning())
76  return;
77 
78  this->internalLaunch(executable, arguments);
79 }
80 
82 {
83  return mProcess->state() == QProcess::Running;
84 }
85 
86 qint64 ProcessWrapper::write(const char * data)
87 {
88  return mProcess->write(data);
89 }
90 
92 {
93  return mProcess->waitForStarted(msecs);
94 }
95 
97 {
98  return mProcess->waitForFinished(msecs);
99 }
100 
101 QString ProcessWrapper::getExecutableInBundlesAbsolutePath(QString exeInBundle)
102 {
103  QString absolutePathToExe = exeInBundle;
104 
105  if (!QFileInfo(absolutePathToExe).isAbsolute())
106  absolutePathToExe = DataLocations::getBundlePath() + "/" + absolutePathToExe;
107 
108  absolutePathToExe = absolutePathToExe.trimmed();
109  absolutePathToExe = QDir::cleanPath(absolutePathToExe);
110 
111  if (!QFileInfo(absolutePathToExe).exists())
112  reportError(QString("Cannot find %1 [%2]").arg(mName).arg(absolutePathToExe));
113 
114  return absolutePathToExe;
115 }
116 
117 void ProcessWrapper::internalLaunch(QString executable, QStringList arguments)
118 {
119  if(this->isRunning())
120  return;
121 
122  report(QString("Launching %1: [%2 %3]").arg(mName).arg(executable).arg(arguments.join(" ")));
123 
124  if(arguments.isEmpty())
125  mProcess->start(executable);
126  else
127  mProcess->start(executable, arguments);
128 
129  mLastExecutablePath = executable;
130 }
131 }
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.