NorMIT-nav  22.09
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 }
cx::ProcessWrapper::waitForFinished
bool waitForFinished(int msecs=30000)
Definition: cxProcessWrapper.cpp:76
cxLogger.h
cx::ProcessReporter
Definition: cxProcessReporter.h:33
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::ProcessWrapper::waitForStarted
bool waitForStarted(int msecs=30000)
Definition: cxProcessWrapper.cpp:71
cx::report
void report(QString msg)
Definition: cxLogger.cpp:69
cx::ProcessWrapper::ProcessWrapper
ProcessWrapper(QString name="executable", QObject *parent=NULL)
Definition: cxProcessWrapper.cpp:23
cx::ProcessWrapper::write
qint64 write(const char *data)
Definition: cxProcessWrapper.cpp:66
cx::ProcessWrapper::launchWithRelativePath
void launchWithRelativePath(QString executable, QStringList arguments=QStringList())
Definition: cxProcessWrapper.cpp:46
cx::ProcessReporterPtr
boost::shared_ptr< class ProcessReporter > ProcessReporterPtr
Definition: cxProcessReporter.h:24
cxProcessWrapper.h
cx::ProcessWrapper::isRunning
bool isRunning()
Definition: cxProcessWrapper.cpp:61
cxTypeConversions.h
cx::ProcessWrapper::~ProcessWrapper
virtual ~ProcessWrapper()
Definition: cxProcessWrapper.cpp:35
cx::ProcessWrapper::stateChanged
void stateChanged()
cx::ProcessWrapper::turnOffReporting
void turnOffReporting()
Definition: cxProcessWrapper.cpp:81
cxDataLocations.h
cx::ProcessWrapper::launch
bool launch(QString executable, QStringList argument=QStringList())
Definition: cxProcessWrapper.cpp:53
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cx::DataLocations::getBundlePath
static QString getBundlePath()
return the folder where the bundle or executable are located.
Definition: cxDataLocations.cpp:115
cx::ProcessWrapper::getProcess
QProcess * getProcess()
Definition: cxProcessWrapper.cpp:41