ctkPluginManager.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 "ctkPluginManager.h"
  16. #include <QServiceManager>
  17. #include <QDirIterator>
  18. #include <QDebug>
  19. #include <QLibrary>
  20. #include <QApplication>
  21. namespace ctk {
  22. class PluginManagerPrivate
  23. {
  24. public:
  25. QList<QString> pluginPaths;
  26. QServiceManager serviceManager;
  27. };
  28. PluginManager::PluginManager()
  29. : d_ptr(new PluginManagerPrivate())
  30. {
  31. Q_D(PluginManager);
  32. QString libName("CTKCore");
  33. QLibrary lib(libName);
  34. QFileInfo fileInfo(libName);
  35. QString libBaseName(fileInfo.baseName());
  36. if (libBaseName.startsWith("lib"))
  37. {
  38. libBaseName.remove(0, 3);
  39. }
  40. qDebug() << libBaseName;
  41. lib.load();
  42. if (lib.isLoaded())
  43. {
  44. QString xyz = QString(":/") + libBaseName + "/servicedescriptor.xml";
  45. qDebug() << "resource string: " << xyz;
  46. QFile serviceDescriptor(xyz);
  47. qDebug() << "file exists: " << serviceDescriptor.exists();
  48. qDebug() << "open returns:" << serviceDescriptor.open(QIODevice::ReadOnly);
  49. qDebug() << "file open: " << serviceDescriptor.isOpen();
  50. qDebug() << "file is readable: " << serviceDescriptor.isReadable();
  51. //QByteArray serviceBA = serviceDescriptor.readAll();
  52. //qDebug() << serviceBA;
  53. qDebug() << "Service for " << libBaseName << " registered:" << d->serviceManager.addService(&serviceDescriptor);
  54. lib.unload();
  55. }
  56. }
  57. PluginManager::~PluginManager()
  58. {
  59. Q_D(PluginManager);
  60. delete d;
  61. }
  62. QServiceManager* PluginManager::serviceManager()
  63. {
  64. Q_D(PluginManager);
  65. return &(d->serviceManager);
  66. }
  67. void PluginManager::addSearchPath(const QString & searchPath)
  68. {
  69. Q_D(PluginManager);
  70. d->pluginPaths.push_back(searchPath);
  71. }
  72. void PluginManager::startAllPlugins()
  73. {
  74. Q_D(PluginManager);
  75. QDirIterator it(d->pluginPaths.front(), QDir::Files);
  76. if (it.hasNext())
  77. {
  78. qApp->addLibraryPath(d->pluginPaths.front());
  79. }
  80. while (it.hasNext()) {
  81. QString libName(it.next());
  82. QLibrary lib(libName);
  83. QFileInfo fileInfo(libName);
  84. QString libBaseName(fileInfo.baseName());
  85. if (libBaseName.startsWith("lib"))
  86. {
  87. libBaseName.remove(0, 3);
  88. }
  89. qDebug() << libBaseName;
  90. lib.load();
  91. if (lib.isLoaded())
  92. {
  93. QString xyz = QString(":/") + libBaseName + "/servicedescriptor.xml";
  94. qDebug() << "resource string: " << xyz;
  95. QFile serviceDescriptor(xyz);
  96. //qDebug() << "file exists: " << serviceDescriptor.exists();
  97. qDebug() << "open returns:" << serviceDescriptor.open(QIODevice::ReadOnly);
  98. qDebug() << "file open: " << serviceDescriptor.isOpen();
  99. qDebug() << "file is readable: " << serviceDescriptor.isReadable();
  100. //QByteArray serviceBA = serviceDescriptor.readAll();
  101. //qDebug() << serviceBA;
  102. qDebug() << "Service for " << libBaseName << " registered:" << d->serviceManager.addService(&serviceDescriptor);
  103. lib.unload();
  104. }
  105. }
  106. }
  107. }