ctkAbstractFileBasedFactory.tpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  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.txt
  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. #ifndef __ctkAbstractFileBasedFactory_tpp
  15. #define __ctkAbstractFileBasedFactory_tpp
  16. // Qt includes
  17. #include <QDirIterator>
  18. // CTK includes
  19. #include "ctkAbstractFileBasedFactory.h"
  20. //----------------------------------------------------------------------------
  21. // ctkFactoryFileBasedItem methods
  22. //----------------------------------------------------------------------------
  23. template<typename BaseClassType>
  24. void ctkAbstractFactoryFileBasedItem<BaseClassType>::setPath(const QString& path)
  25. {
  26. this->Path = path;
  27. }
  28. //----------------------------------------------------------------------------
  29. template<typename BaseClassType>
  30. QString ctkAbstractFactoryFileBasedItem<BaseClassType>::path()const
  31. {
  32. return this->Path;
  33. }
  34. //----------------------------------------------------------------------------
  35. // ctkAbstractFileBasedFactory methods
  36. //----------------------------------------------------------------------------
  37. template<typename BaseClassType>
  38. QString ctkAbstractFileBasedFactory<BaseClassType>::path(const QString& key)
  39. {
  40. ctkAbstractFactoryFileBasedItem<BaseClassType>* _item =
  41. dynamic_cast<ctkAbstractFactoryFileBasedItem<BaseClassType>*>(this->item(key));
  42. Q_ASSERT(_item);
  43. return _item->path();
  44. }
  45. //-----------------------------------------------------------------------------
  46. template<typename BaseClassType>
  47. void ctkAbstractFileBasedFactory<BaseClassType>::registerAllFileItems(const QStringList& directories)
  48. {
  49. // Process one path at a time
  50. foreach (QString path, directories)
  51. {
  52. QDirIterator it(path);
  53. while (it.hasNext())
  54. {
  55. it.next();
  56. QFileInfo fileInfo = it.fileInfo();
  57. if (fileInfo.isSymLink())
  58. {
  59. // symLinkTarget() handles links pointing to symlinks.
  60. // How about a symlink pointing to a symlink ?
  61. fileInfo = QFileInfo(fileInfo.symLinkTarget());
  62. }
  63. // Skip if item isn't a file
  64. if (!this->isValidFile(fileInfo))
  65. {
  66. continue;
  67. }
  68. this->registerFileItem(fileInfo);
  69. }
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. template<typename BaseClassType>
  74. QString ctkAbstractFileBasedFactory<BaseClassType>
  75. ::itemKey(const QFileInfo& fileInfo)const
  76. {
  77. return this->fileNameToKey(fileInfo.filePath());
  78. }
  79. //-----------------------------------------------------------------------------
  80. template<typename BaseClassType>
  81. QString ctkAbstractFileBasedFactory<BaseClassType>
  82. ::registerFileItem(const QFileInfo& fileInfo)
  83. {
  84. QString key = this->itemKey(fileInfo);
  85. bool registered = this->registerFileItem(key, fileInfo);
  86. return registered ? key : QString();
  87. }
  88. //-----------------------------------------------------------------------------
  89. template<typename BaseClassType>
  90. bool ctkAbstractFileBasedFactory<BaseClassType>
  91. ::registerFileItem(const QString& key, const QFileInfo& fileInfo)
  92. {
  93. QString description = QString("Attempt to register \"%1\"").arg(key);
  94. if (this->item(key))
  95. {
  96. this->displayStatusMessage(QtWarningMsg, description, "Already registered", this->verbose());
  97. return true;
  98. }
  99. if (this->sharedItem(key))
  100. {
  101. this->displayStatusMessage(QtDebugMsg, description,
  102. "Already registered in other factory", this->verbose());
  103. return false;
  104. }
  105. QSharedPointer<ctkAbstractFactoryItem<BaseClassType> >
  106. itemToRegister(this->createFactoryFileBasedItem());
  107. if (itemToRegister.isNull())
  108. {
  109. this->displayStatusMessage(QtWarningMsg, description,
  110. "Failed to create FileBasedItem", this->verbose());
  111. return false;
  112. }
  113. dynamic_cast<ctkAbstractFactoryFileBasedItem<BaseClassType>*>(itemToRegister.data())
  114. ->setPath(fileInfo.filePath());
  115. this->initItem(itemToRegister.data());
  116. return this->registerItem(key, itemToRegister);
  117. }
  118. //-----------------------------------------------------------------------------
  119. template<typename BaseClassType>
  120. bool ctkAbstractFileBasedFactory<BaseClassType>
  121. ::isValidFile(const QFileInfo& file)const
  122. {
  123. return file.isFile();
  124. }
  125. //-----------------------------------------------------------------------------
  126. template<typename BaseClassType>
  127. ctkAbstractFactoryItem<BaseClassType>* ctkAbstractFileBasedFactory<BaseClassType>
  128. ::createFactoryFileBasedItem()
  129. {
  130. return 0;
  131. }
  132. //-----------------------------------------------------------------------------
  133. template<typename BaseClassType>
  134. void ctkAbstractFileBasedFactory<BaseClassType>::
  135. initItem(ctkAbstractFactoryItem<BaseClassType>* item)
  136. {
  137. item->setVerbose(this->verbose());
  138. }
  139. //-----------------------------------------------------------------------------
  140. template<typename BaseClassType>
  141. QString ctkAbstractFileBasedFactory<BaseClassType>
  142. ::fileNameToKey(const QString& fileName)const
  143. {
  144. return QFileInfo(fileName).baseName();
  145. }
  146. #endif