ctkCmdLineModuleDefaultPathBuilder.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleDefaultPathBuilder.h"
  16. #include <QDir>
  17. #include <QDebug>
  18. #include <QCoreApplication>
  19. #include <cstdlib>
  20. struct ctkCmdLineModuleDefaultPathBuilderPrivate
  21. {
  22. public:
  23. ctkCmdLineModuleDefaultPathBuilderPrivate();
  24. ~ctkCmdLineModuleDefaultPathBuilderPrivate();
  25. QStringList build() const;
  26. void setLoadFromHomeDir(const bool& doLoad);
  27. void setLoadFromCurrentDir(const bool& doLoad);
  28. void setLoadFromApplicationDir(const bool& doLoad);
  29. void setLoadFromCtkModuleLoadPath(const bool& doLoad);
  30. bool LoadFromHomeDir;
  31. bool LoadFromCurrentDir;
  32. bool LoadFromApplicationDir;
  33. bool LoadFromCtkModuleLoadPath;
  34. };
  35. //-----------------------------------------------------------------------------
  36. // ctkCmdLineModuleDefaultPathBuilderPrivate methods
  37. //-----------------------------------------------------------------------------
  38. ctkCmdLineModuleDefaultPathBuilderPrivate::ctkCmdLineModuleDefaultPathBuilderPrivate()
  39. : LoadFromHomeDir(false)
  40. , LoadFromCurrentDir(false)
  41. , LoadFromApplicationDir(false)
  42. , LoadFromCtkModuleLoadPath(false)
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. ctkCmdLineModuleDefaultPathBuilderPrivate::~ctkCmdLineModuleDefaultPathBuilderPrivate()
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromHomeDir(const bool& doLoad)
  51. {
  52. LoadFromHomeDir = doLoad;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromCurrentDir(const bool& doLoad)
  56. {
  57. LoadFromCurrentDir = doLoad;
  58. }
  59. //-----------------------------------------------------------------------------
  60. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromApplicationDir(const bool& doLoad)
  61. {
  62. LoadFromApplicationDir = doLoad;
  63. }
  64. //-----------------------------------------------------------------------------
  65. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromCtkModuleLoadPath(const bool& doLoad)
  66. {
  67. LoadFromCtkModuleLoadPath = doLoad;
  68. }
  69. //-----------------------------------------------------------------------------
  70. QStringList ctkCmdLineModuleDefaultPathBuilderPrivate::build() const
  71. {
  72. QStringList result;
  73. QString suffix = "cli-modules";
  74. if (LoadFromCtkModuleLoadPath)
  75. {
  76. char *ctkModuleLoadPath = getenv("CTK_MODULE_LOAD_PATH");
  77. if (ctkModuleLoadPath != NULL)
  78. {
  79. QDir dir = QDir(QString(ctkModuleLoadPath));
  80. if (dir.exists())
  81. {
  82. result << dir.canonicalPath();
  83. }
  84. }
  85. }
  86. if (LoadFromHomeDir)
  87. {
  88. if (QDir::home().exists())
  89. {
  90. result << QDir::homePath();
  91. result << QDir::homePath() + QDir::separator() + suffix;
  92. }
  93. }
  94. if (LoadFromCurrentDir)
  95. {
  96. if (QDir::current().exists())
  97. {
  98. result << QDir::currentPath();
  99. result << QDir::currentPath() + QDir::separator() + suffix;
  100. }
  101. }
  102. if (LoadFromApplicationDir)
  103. {
  104. result << QCoreApplication::applicationDirPath();
  105. result << QCoreApplication::applicationDirPath() + QDir::separator() + suffix;
  106. }
  107. return result;
  108. }
  109. //-----------------------------------------------------------------------------
  110. // ctkCmdLineModuleDefaultPathBuilder methods
  111. //-----------------------------------------------------------------------------
  112. ctkCmdLineModuleDefaultPathBuilder::ctkCmdLineModuleDefaultPathBuilder()
  113. : d(new ctkCmdLineModuleDefaultPathBuilderPrivate)
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. ctkCmdLineModuleDefaultPathBuilder::~ctkCmdLineModuleDefaultPathBuilder()
  118. {
  119. }
  120. //-----------------------------------------------------------------------------
  121. QStringList ctkCmdLineModuleDefaultPathBuilder::build() const
  122. {
  123. return d->build();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromHomeDir(const bool& doLoad)
  127. {
  128. d->setLoadFromHomeDir(doLoad);
  129. }
  130. //-----------------------------------------------------------------------------
  131. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromCurrentDir(const bool& doLoad)
  132. {
  133. d->setLoadFromCurrentDir(doLoad);
  134. }
  135. //-----------------------------------------------------------------------------
  136. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromApplicationDir(const bool& doLoad)
  137. {
  138. d->setLoadFromApplicationDir(doLoad);
  139. }
  140. //-----------------------------------------------------------------------------
  141. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromCtkModuleLoadPath(const bool& doLoad)
  142. {
  143. d->setLoadFromCtkModuleLoadPath(doLoad);
  144. }