/Modules/AppUtil/src/mitkProvisioningInfo.cpp

https://github.com/NifTK/MITK · C++ · 232 lines · 189 code · 25 blank · 18 comment · 29 complexity · f309a60a1beebee8d0fc23ccb56fa5fd MD5 · raw file

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