CustusX  18.04
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxProcessWrapper.h"
13 #include <QDir>
14 #include <QFileInfo>
15 #include "cxDataLocations.h"
16 #include "cxLogger.h"
17 #include "cxTypeConversions.h"
18 #include <iostream>
19 
20 namespace cx
21 {
22 
23 ProcessWrapper::ProcessWrapper(QString name, QObject* parent) :
24  QObject(parent), mName(name), mLastExecutablePath("")
25 {
26  mProcess = new QProcess(this);
27  mReporter = ProcessReporterPtr(new ProcessReporter(mProcess, mName));
28 
29  mProcess->setProcessChannelMode(QProcess::MergedChannels);
30  mProcess->setReadChannel(QProcess::StandardOutput);
31 
32  connect(mProcess.data(), &QProcess::stateChanged, this, &ProcessWrapper::stateChanged);
33 }
34 
36 {
37  mProcess->terminate();//terminate gives the process a chance to shutdown
38  this->waitForFinished();
39 }
40 
42 {
43  return mProcess.data();
44 }
45 
46 void ProcessWrapper::launchWithRelativePath(QString executable, QStringList arguments)
47 {
48  QString absolutePathToExe = this->getExecutableInBundlesAbsolutePath(executable);
49 
50  this->launch(absolutePathToExe, arguments);
51 }
52 
53 void ProcessWrapper::launch(QString executable, QStringList arguments)
54 {
55  if (executable.isEmpty() || this->isRunning())
56  return;
57 
58  this->internalLaunch(executable, arguments);
59 }
60 
62 {
63  return mProcess->state() == QProcess::Running;
64 }
65 
66 qint64 ProcessWrapper::write(const char * data)
67 {
68  return mProcess->write(data);
69 }
70 
72 {
73  return mProcess->waitForStarted(msecs);
74 }
75 
77 {
78  return mProcess->waitForFinished(msecs);
79 }
80 
81 QString ProcessWrapper::getExecutableInBundlesAbsolutePath(QString exeInBundle)
82 {
83  QString absolutePathToExe = exeInBundle;
84 
85  if (!QFileInfo(absolutePathToExe).isAbsolute())
86  absolutePathToExe = DataLocations::getBundlePath() + "/" + absolutePathToExe;
87 
88  absolutePathToExe = absolutePathToExe.trimmed();
89  absolutePathToExe = QDir::cleanPath(absolutePathToExe);
90 
91  if (!QFileInfo(absolutePathToExe).exists())
92  reportError(QString("Cannot find %1 [%2]").arg(mName).arg(absolutePathToExe));
93 
94  return absolutePathToExe;
95 }
96 
97 void ProcessWrapper::internalLaunch(QString executable, QStringList arguments)
98 {
99  if(this->isRunning())
100  return;
101 
102  report(QString("Launching %1: [%2 %3]").arg(mName).arg(executable).arg(arguments.join(" ")));
103 
104  if(arguments.isEmpty())
105  mProcess->start(executable);
106  else
107  mProcess->start(executable, arguments);
108 
109  mLastExecutablePath = executable;
110 }
111 }
void reportError(QString msg)
Definition: cxLogger.cpp:71
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:69
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.