ctkCmdLineModuleDefaultPathBuilder.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) University College London
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =============================================================================*/
  14. #include "ctkCmdLineModuleDefaultPathBuilder.h"
  15. #include <QDir>
  16. #include <QDebug>
  17. #include <QCoreApplication>
  18. #include <cstdlib>
  19. #include <QStringList>
  20. //----------------------------------------------------------------------------
  21. struct ctkCmdLineModuleDefaultPathBuilderPrivate
  22. {
  23. public:
  24. ctkCmdLineModuleDefaultPathBuilderPrivate();
  25. ~ctkCmdLineModuleDefaultPathBuilderPrivate();
  26. void clear();
  27. void setStrictMode(const bool& strict);
  28. bool strictMode() const;
  29. void addHomeDir(const QString& subFolder = QString());
  30. void addCurrentDir(const QString& subFolder = QString());
  31. void addApplicationDir(const QString& subFolder = QString());
  32. void addCtkModuleLoadPath();
  33. QStringList getDirectoryList() const;
  34. bool isStrictMode;
  35. QStringList directoryList;
  36. private:
  37. QString addSubFolder(const QString& folder, const QString& subFolder);
  38. };
  39. //-----------------------------------------------------------------------------
  40. // ctkCmdLineModuleDefaultPathBuilderPrivate methods
  41. //-----------------------------------------------------------------------------
  42. ctkCmdLineModuleDefaultPathBuilderPrivate::ctkCmdLineModuleDefaultPathBuilderPrivate()
  43. : isStrictMode(false)
  44. {
  45. }
  46. //-----------------------------------------------------------------------------
  47. ctkCmdLineModuleDefaultPathBuilderPrivate::~ctkCmdLineModuleDefaultPathBuilderPrivate()
  48. {
  49. }
  50. //-----------------------------------------------------------------------------
  51. void ctkCmdLineModuleDefaultPathBuilderPrivate::clear()
  52. {
  53. directoryList.clear();
  54. }
  55. //-----------------------------------------------------------------------------
  56. void ctkCmdLineModuleDefaultPathBuilderPrivate::setStrictMode(const bool& strict)
  57. {
  58. isStrictMode = strict;
  59. }
  60. //-----------------------------------------------------------------------------
  61. bool ctkCmdLineModuleDefaultPathBuilderPrivate::strictMode() const
  62. {
  63. return isStrictMode;
  64. }
  65. //-----------------------------------------------------------------------------
  66. QString ctkCmdLineModuleDefaultPathBuilderPrivate::addSubFolder(
  67. const QString& folder, const QString& subFolder)
  68. {
  69. if (subFolder.length() > 0)
  70. {
  71. return folder + QDir::separator() + subFolder;
  72. }
  73. else
  74. {
  75. return folder;
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. void ctkCmdLineModuleDefaultPathBuilderPrivate::addHomeDir(const QString& subFolder)
  80. {
  81. if (QDir::home().exists())
  82. {
  83. QString result = addSubFolder(QDir::homePath(), subFolder);
  84. directoryList << result;
  85. }
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ctkCmdLineModuleDefaultPathBuilderPrivate::addCurrentDir(const QString& subFolder)
  89. {
  90. if (QDir::current().exists())
  91. {
  92. QString result = addSubFolder(QDir::currentPath(), subFolder);
  93. directoryList << result;
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. void ctkCmdLineModuleDefaultPathBuilderPrivate::addApplicationDir(const QString& subFolder)
  98. {
  99. QString result = addSubFolder(QCoreApplication::applicationDirPath(), subFolder);
  100. directoryList << result;
  101. }
  102. //-----------------------------------------------------------------------------
  103. void ctkCmdLineModuleDefaultPathBuilderPrivate::addCtkModuleLoadPath()
  104. {
  105. char *ctkModuleLoadPath = getenv("CTK_MODULE_LOAD_PATH");
  106. if (ctkModuleLoadPath != NULL)
  107. {
  108. // The load path may in fact be a semi-colon or colon separated list of directories, not just one.
  109. QString paths(ctkModuleLoadPath);
  110. #ifdef Q_OS_WIN32
  111. QString pathSeparator(";");
  112. #else
  113. QString pathSeparator(":");
  114. #endif
  115. QStringList splitPath = paths.split(pathSeparator, QString::SkipEmptyParts);
  116. foreach (QString path, splitPath)
  117. {
  118. QDir dir = QDir(path);
  119. directoryList << dir.absolutePath();
  120. }
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. QStringList ctkCmdLineModuleDefaultPathBuilderPrivate::getDirectoryList() const
  125. {
  126. if (!isStrictMode)
  127. {
  128. return directoryList;
  129. }
  130. else
  131. {
  132. QStringList filteredList;
  133. foreach (QString directory, directoryList)
  134. {
  135. QDir dir(directory);
  136. if (dir.exists())
  137. {
  138. filteredList << directory;
  139. }
  140. }
  141. qDebug() << "Filtered directory list " << directoryList << " to " << filteredList;
  142. return filteredList;
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. // ctkCmdLineModuleDefaultPathBuilder methods
  147. //-----------------------------------------------------------------------------
  148. ctkCmdLineModuleDefaultPathBuilder::ctkCmdLineModuleDefaultPathBuilder()
  149. : d(new ctkCmdLineModuleDefaultPathBuilderPrivate)
  150. {
  151. }
  152. //-----------------------------------------------------------------------------
  153. ctkCmdLineModuleDefaultPathBuilder::~ctkCmdLineModuleDefaultPathBuilder()
  154. {
  155. }
  156. //-----------------------------------------------------------------------------
  157. void ctkCmdLineModuleDefaultPathBuilder::clear()
  158. {
  159. d->clear();
  160. }
  161. //-----------------------------------------------------------------------------
  162. void ctkCmdLineModuleDefaultPathBuilder::setStrictMode(const bool& strict)
  163. {
  164. d->setStrictMode(strict);
  165. }
  166. //-----------------------------------------------------------------------------
  167. bool ctkCmdLineModuleDefaultPathBuilder::strictMode() const
  168. {
  169. return d->strictMode();
  170. }
  171. //-----------------------------------------------------------------------------
  172. void ctkCmdLineModuleDefaultPathBuilder::addHomeDir(const QString& subFolder)
  173. {
  174. d->addHomeDir(subFolder);
  175. }
  176. //-----------------------------------------------------------------------------
  177. void ctkCmdLineModuleDefaultPathBuilder::addCurrentDir(const QString& subFolder)
  178. {
  179. d->addCurrentDir(subFolder);
  180. }
  181. //-----------------------------------------------------------------------------
  182. void ctkCmdLineModuleDefaultPathBuilder::addApplicationDir(const QString& subFolder)
  183. {
  184. d->addApplicationDir(subFolder);
  185. }
  186. //-----------------------------------------------------------------------------
  187. void ctkCmdLineModuleDefaultPathBuilder::addCtkModuleLoadPath()
  188. {
  189. d->addCtkModuleLoadPath();
  190. }
  191. //-----------------------------------------------------------------------------
  192. QStringList ctkCmdLineModuleDefaultPathBuilder::getDirectoryList() const
  193. {
  194. return d->getDirectoryList();
  195. }