CustusX  2021.04.21-dev+develop.337f9
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 bool ProcessWrapper::launch(QString executable, QStringList arguments)
54 {
55  if (executable.isEmpty() || this->isRunning())
56  return false;
57 
58  return 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 
82 {
83  mReporter.reset();
84 }
85 
86 QString ProcessWrapper::getExecutableInBundlesAbsolutePath(QString exeInBundle)
87 {
88  QString absolutePathToExe = exeInBundle;
89 
90  if (!QFileInfo(absolutePathToExe).isAbsolute())
91  absolutePathToExe = DataLocations::getBundlePath() + "/" + absolutePathToExe;
92 
93  absolutePathToExe = absolutePathToExe.trimmed();
94  absolutePathToExe = QDir::cleanPath(absolutePathToExe);
95 
96  if (!QFileInfo(absolutePathToExe).exists())
97  reportError(QString("Cannot find %1 [%2]").arg(mName).arg(absolutePathToExe));
98 
99  return absolutePathToExe;
100 }
101 
102 bool ProcessWrapper::internalLaunch(QString executable, QStringList arguments)
103 {
104  if(this->isRunning())
105  return false;
106 
107  report(QString("Launching %1: [%2 %3]").arg(mName).arg(executable).arg(arguments.join(" ")));
108 
109  if(arguments.isEmpty())
110  mProcess->start(executable);
111  else
112  mProcess->start(executable, arguments);
113 
114  mLastExecutablePath = executable;
115  return true;
116 }
117 }
void reportError(QString msg)
Definition: cxLogger.cpp:71
void launchWithRelativePath(QString executable, QStringList arguments=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
bool launch(QString executable, QStringList argument=QStringList())
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.