ctkCmdLineModuleDefaultPathBuilder.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //----------------------------------------------------------------------------
  21. struct ctkCmdLineModuleDefaultPathBuilderPrivate
  22. {
  23. public:
  24. ctkCmdLineModuleDefaultPathBuilderPrivate();
  25. ~ctkCmdLineModuleDefaultPathBuilderPrivate();
  26. QStringList build() const;
  27. void setLoadFromHomeDir(bool doLoad);
  28. void setLoadFromCurrentDir(bool doLoad);
  29. void setLoadFromApplicationDir(bool doLoad);
  30. void setLoadFromCtkModuleLoadPath(bool doLoad);
  31. bool LoadFromHomeDir;
  32. bool LoadFromCurrentDir;
  33. bool LoadFromApplicationDir;
  34. bool LoadFromCtkModuleLoadPath;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // ctkCmdLineModuleDefaultPathBuilderPrivate methods
  38. //-----------------------------------------------------------------------------
  39. ctkCmdLineModuleDefaultPathBuilderPrivate::ctkCmdLineModuleDefaultPathBuilderPrivate()
  40. : LoadFromHomeDir(false)
  41. , LoadFromCurrentDir(false)
  42. , LoadFromApplicationDir(false)
  43. , LoadFromCtkModuleLoadPath(false)
  44. {
  45. }
  46. //-----------------------------------------------------------------------------
  47. ctkCmdLineModuleDefaultPathBuilderPrivate::~ctkCmdLineModuleDefaultPathBuilderPrivate()
  48. {
  49. }
  50. //-----------------------------------------------------------------------------
  51. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromHomeDir(bool doLoad)
  52. {
  53. LoadFromHomeDir = doLoad;
  54. }
  55. //-----------------------------------------------------------------------------
  56. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromCurrentDir(bool doLoad)
  57. {
  58. LoadFromCurrentDir = doLoad;
  59. }
  60. //-----------------------------------------------------------------------------
  61. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromApplicationDir(bool doLoad)
  62. {
  63. LoadFromApplicationDir = doLoad;
  64. }
  65. //-----------------------------------------------------------------------------
  66. void ctkCmdLineModuleDefaultPathBuilderPrivate::setLoadFromCtkModuleLoadPath(bool doLoad)
  67. {
  68. LoadFromCtkModuleLoadPath = doLoad;
  69. }
  70. //-----------------------------------------------------------------------------
  71. QStringList ctkCmdLineModuleDefaultPathBuilderPrivate::build() const
  72. {
  73. QStringList result;
  74. QString suffix = "cli-modules";
  75. if (LoadFromCtkModuleLoadPath)
  76. {
  77. char *ctkModuleLoadPath = getenv("CTK_MODULE_LOAD_PATH");
  78. if (ctkModuleLoadPath != NULL)
  79. {
  80. QDir dir = QDir(QString(ctkModuleLoadPath));
  81. if (dir.exists())
  82. {
  83. result << dir.canonicalPath();
  84. }
  85. }
  86. }
  87. if (LoadFromHomeDir)
  88. {
  89. if (QDir::home().exists())
  90. {
  91. result << QDir::homePath();
  92. result << QDir::homePath() + QDir::separator() + suffix;
  93. }
  94. }
  95. if (LoadFromCurrentDir)
  96. {
  97. if (QDir::current().exists())
  98. {
  99. result << QDir::currentPath();
  100. result << QDir::currentPath() + QDir::separator() + suffix;
  101. }
  102. }
  103. if (LoadFromApplicationDir)
  104. {
  105. result << QCoreApplication::applicationDirPath();
  106. result << QCoreApplication::applicationDirPath() + QDir::separator() + suffix;
  107. }
  108. return result;
  109. }
  110. //-----------------------------------------------------------------------------
  111. // ctkCmdLineModuleDefaultPathBuilder methods
  112. //-----------------------------------------------------------------------------
  113. ctkCmdLineModuleDefaultPathBuilder::ctkCmdLineModuleDefaultPathBuilder()
  114. : d(new ctkCmdLineModuleDefaultPathBuilderPrivate)
  115. {
  116. }
  117. //-----------------------------------------------------------------------------
  118. ctkCmdLineModuleDefaultPathBuilder::~ctkCmdLineModuleDefaultPathBuilder()
  119. {
  120. }
  121. //-----------------------------------------------------------------------------
  122. QStringList ctkCmdLineModuleDefaultPathBuilder::build() const
  123. {
  124. return d->build();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromHomeDir(bool doLoad)
  128. {
  129. d->setLoadFromHomeDir(doLoad);
  130. }
  131. //-----------------------------------------------------------------------------
  132. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromCurrentDir(bool doLoad)
  133. {
  134. d->setLoadFromCurrentDir(doLoad);
  135. }
  136. //-----------------------------------------------------------------------------
  137. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromApplicationDir(bool doLoad)
  138. {
  139. d->setLoadFromApplicationDir(doLoad);
  140. }
  141. //-----------------------------------------------------------------------------
  142. void ctkCmdLineModuleDefaultPathBuilder::setLoadFromCtkModuleLoadPath(bool doLoad)
  143. {
  144. d->setLoadFromCtkModuleLoadPath(doLoad);
  145. }