Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkProvisioningInfo.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkProvisioningInfo.h"
14 
15 #include <mitkLogMacros.h>
16 
17 #include <QCoreApplication>
18 #include <QFile>
19 #include <QFileInfo>
20 #include <QTextStream>
21 
22 namespace mitk
23 {
24 #ifdef CMAKE_INTDIR
25  const QString ProvisioningInfo::intermediateOutDir = QString(CMAKE_INTDIR);
26 #else
27  const QString ProvisioningInfo::intermediateOutDir = QString();
28 #endif
29 
30  ProvisioningInfo::ProvisioningInfo(const QString &file) { this->readProvisioningFile(file); }
31  QStringList ProvisioningInfo::getPluginDirs() const { return pluginDirs.toList(); }
32  QList<QUrl> ProvisioningInfo::getPluginsToInstall() const { return pluginsToInstall; }
33  QList<QUrl> ProvisioningInfo::getPluginsToStart() const { return pluginsToStart; }
34  void ProvisioningInfo::readProvisioningFile(const QString &filePath)
35  {
36  QFile file(filePath);
37  file.open(QFile::ReadOnly);
38  QTextStream fileStream(&file);
39  QRegExp sep("\\s+");
40  QString line;
41  int count = 1;
42  do
43  {
44  line = fileStream.readLine().trimmed();
45  if (!line.isEmpty() && !line.startsWith('#'))
46  {
47  QString keyword = line.section(sep, 0, 0);
48  QString value = line.mid(keyword.size()).trimmed();
49  value = substituteKeywords(value);
50 
51  if (keyword.isEmpty())
52  {
53  MITK_WARN << "Keyword missing in line " << count << " of provisioning file " << filePath.toStdString();
54  continue;
55  }
56 
57  Keyword key = UNKNOWN;
58  if (keyword.compare("READ", Qt::CaseInsensitive) == 0)
59  {
60  key = READ;
61  }
62  else if (keyword.compare("INSTALL", Qt::CaseInsensitive) == 0)
63  {
64  key = INSTALL;
65  }
66  else if (keyword.compare("START", Qt::CaseInsensitive) == 0)
67  {
68  key = START;
69  }
70  else if (keyword.compare("STOP", Qt::CaseInsensitive) == 0)
71  {
72  key = STOP;
73  }
74 
75  if (key == UNKNOWN)
76  {
77  MITK_WARN << "Keyword " << keyword.toStdString() << " in line " << count << " of provisioning file "
78  << filePath.toStdString() << " unknown";
79  continue;
80  }
81 
82  if (value.isEmpty())
83  {
84  MITK_WARN << "Value after keyword " << keyword.toStdString() << " missing in line " << count
85  << " of provisioning file " << filePath.toStdString();
86  continue;
87  }
88 
89  switch (key)
90  {
91  case READ:
92  {
93  QUrl readFileUrl(value);
94  if (!readFileUrl.isValid())
95  {
96  MITK_WARN << "The READ URL " << value.toStdString()
97  << "is invalid: " << readFileUrl.errorString().toStdString();
98  break;
99  }
100  this->readProvisioningFile(readFileUrl.toLocalFile());
101  break;
102  }
103  case INSTALL:
104  {
105  this->addPluginToInstall(value);
106  break;
107  }
108  case START:
109  {
110  this->addPluginToStart(value);
111  break;
112  }
113  case STOP:
114  {
115  break;
116  }
117  case UNKNOWN:
118  {
119  break; // error handled above
120  }
121  }
122  }
123  ++count;
124  } while (!line.isNull());
125  }
126 
127  QUrl ProvisioningInfo::addPluginToInstall(const QString &file)
128  {
129  QUrl pluginUrl(file);
130  if (!pluginUrl.isValid())
131  {
132  MITK_WARN << "The plugin URL " << file.toStdString() << " is invalid:" << pluginUrl.errorString().toStdString();
133  return QUrl();
134  }
135 
136  QFileInfo fileInfo(pluginUrl.toLocalFile());
137  if (!fileInfo.exists())
138  {
139  QString fileName = fileInfo.fileName();
140  QString filePath = fileInfo.absolutePath();
141  if (!intermediateOutDir.isEmpty())
142  {
143  // search in the intermediate output dir
144  QString filePath2 = filePath + "/" + intermediateOutDir;
145  fileInfo = QFileInfo(filePath2 + "/" + fileName);
146  if (!fileInfo.exists())
147  {
148  MITK_WARN << "The plugin " << fileName.toStdString() << " was not found in " << filePath.toStdString()
149  << " or " << filePath2.toStdString();
150  return QUrl();
151  }
152  pluginUrl = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
153  pluginDirs.insert(fileInfo.canonicalPath());
154  }
155  else
156  {
157  MITK_WARN << "The plugin " << fileName.toStdString() << " was not found in " << filePath.toStdString();
158  return QUrl();
159  }
160  }
161  else
162  {
163  pluginDirs.insert(fileInfo.canonicalPath());
164  }
165 
166  pluginsToInstall.append(pluginUrl);
167  return pluginUrl;
168  }
169 
170  void ProvisioningInfo::addPluginToStart(const QString &file)
171  {
172  QUrl pluginUrl = this->addPluginToInstall(file);
173  if (!pluginUrl.isEmpty())
174  {
175  pluginsToStart.append(pluginUrl);
176  }
177  }
178 
179  QString ProvisioningInfo::substituteKeywords(const QString &value) const
180  {
181  QString appPath = QCoreApplication::applicationDirPath();
182  if (appPath.endsWith('/'))
183  {
184  appPath.chop(1);
185  }
186 
187 #ifdef CMAKE_INTDIR
188  // Strip the intermediate dir from the application path
189  QString intDir(CMAKE_INTDIR);
190  if (appPath.endsWith(intDir))
191  {
192  appPath.chop(intDir.size() + 1);
193  }
194 #endif
195 
196 #ifdef _WIN32
197  if (value.contains("@EXECUTABLE_DIR") && value.contains("blueberry_osgi"))
198  {
199  // special case for org_blueberry_osgi in install trees for Windows
200  return QString(value).replace("@EXECUTABLE_DIR", appPath, Qt::CaseInsensitive).replace("plugins/", "");
201  }
202 #endif
203 
204  return QString(value).replace("@EXECUTABLE_DIR", appPath, Qt::CaseInsensitive);
205  }
206 }
static char * line
Definition: svm.cpp:2870
QStringList getPluginDirs() const
DataCollection - Class to facilitate loading/accessing structured data.
QList< QUrl > getPluginsToStart() const
#define MITK_WARN
Definition: mitkLogMacros.h:19
ProvisioningInfo(const QString &file)
QList< QUrl > getPluginsToInstall() const