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