ctkAbstractFileBasedFactory.tpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.commontk.org/LICENSE
  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. ctkAbstractFactoryFileBasedItem<BaseClassType>::ctkAbstractFactoryFileBasedItem(const QString& _path)
  25. :ctkAbstractFactoryItem<BaseClassType>()
  26. ,Path(_path)
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. template<typename BaseClassType>
  31. QString ctkAbstractFactoryFileBasedItem<BaseClassType>::path()const
  32. {
  33. return this->Path;
  34. }
  35. //----------------------------------------------------------------------------
  36. // ctkAbstractFileBasedFactory methods
  37. //-----------------------------------------------------------------------------
  38. template<typename BaseClassType>
  39. ctkAbstractFileBasedFactory<BaseClassType>::ctkAbstractFileBasedFactory()
  40. :ctkAbstractFactory<BaseClassType>()
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. template<typename BaseClassType>
  45. ctkAbstractFileBasedFactory<BaseClassType>::~ctkAbstractFileBasedFactory()
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. template<typename BaseClassType>
  50. QString ctkAbstractFileBasedFactory<BaseClassType>::path(const QString& key)
  51. {
  52. ctkAbstractFactoryFileBasedItem<BaseClassType>* _item =
  53. dynamic_cast<ctkAbstractFactoryFileBasedItem<BaseClassType>*>(this->item(key));
  54. Q_ASSERT(_item);
  55. return _item->path();
  56. }
  57. //-----------------------------------------------------------------------------
  58. template<typename BaseClassType>
  59. void ctkAbstractFileBasedFactory<BaseClassType>::registerAllFileItems(const QStringList& directories)
  60. {
  61. // Process one path at a time
  62. foreach (QString path, directories)
  63. {
  64. QDirIterator it(path);
  65. while (it.hasNext())
  66. {
  67. it.next();
  68. QFileInfo fileInfo = it.fileInfo();
  69. if (fileInfo.isSymLink())
  70. {
  71. // symLinkTarget() handles links pointing to symlinks.
  72. // How about a symlink pointing to a symlink ?
  73. fileInfo = QFileInfo(fileInfo.symLinkTarget());
  74. }
  75. // Skip if item isn't a file
  76. if (!fileInfo.isFile())
  77. {
  78. continue;
  79. }
  80. if (this->verbose())
  81. {
  82. qDebug() << "Attempt to register command line module:" << fileInfo.fileName();
  83. }
  84. QString key = this->fileNameToKey(fileInfo.filePath());
  85. QSharedPointer<ctkAbstractFactoryItem<BaseClassType> >
  86. itemToRegister = QSharedPointer<ctkAbstractFactoryItem<BaseClassType> >(
  87. this->createFactoryFileBasedItem(fileInfo));
  88. if (itemToRegister.isNull())
  89. {
  90. continue;
  91. }
  92. itemToRegister->setVerbose(this->verbose());
  93. if (!this->registerItem(key, itemToRegister))
  94. {
  95. if (this->verbose())
  96. {
  97. qWarning() << "Failed to register module: " << key;
  98. }
  99. continue;
  100. }
  101. }
  102. }
  103. }
  104. //-----------------------------------------------------------------------------
  105. template<typename BaseClassType>
  106. ctkAbstractFactoryItem<BaseClassType>* ctkAbstractFileBasedFactory<BaseClassType>
  107. ::createFactoryFileBasedItem(const QFileInfo& file)
  108. {
  109. Q_UNUSED(file);
  110. return 0;
  111. }
  112. //-----------------------------------------------------------------------------
  113. template<typename BaseClassType>
  114. QString ctkAbstractFileBasedFactory<BaseClassType>
  115. ::fileNameToKey(const QString& fileName)const
  116. {
  117. return QFileInfo(fileName).baseName().toLower();
  118. }
  119. #endif