ctkPluginBrowser.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "ctkPluginBrowser.h"
  16. #include "ctkPluginTableModel.h"
  17. #include "ctkPluginResourcesTreeModel.h"
  18. #include "ctkQtResourcesTreeModel.h"
  19. #include "ctkServiceReference.h"
  20. #include "ctkPluginConstants.h"
  21. #include <ui_ctkPluginBrowserMainWindow.h>
  22. #include <ctkPluginException.h>
  23. #include <ctkPluginFramework.h>
  24. #include <QApplication>
  25. #include <QMainWindow>
  26. #include <QStringList>
  27. #include <QDirIterator>
  28. #include <QUrl>
  29. namespace ctk {
  30. PluginBrowser::PluginBrowser(PluginFramework* framework)
  31. : framework(framework)
  32. {
  33. framework->getPluginContext()->connectFrameworkListener(this, SLOT(frameworkEvent(PluginFrameworkEvent)));
  34. QStringList pluginDirs;
  35. pluginDirs << qApp->applicationDirPath() + "/Plugins";
  36. QStringListIterator dirIt(pluginDirs);
  37. while (dirIt.hasNext())
  38. {
  39. QApplication::addLibraryPath(dirIt.next());
  40. }
  41. QDirIterator dirIter(pluginDirs.at(0), QDir::Files);
  42. while(dirIter.hasNext())
  43. {
  44. try
  45. {
  46. Plugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(dirIter.next()).toString());
  47. plugin->start(Plugin::START_ACTIVATION_POLICY);
  48. }
  49. catch (const PluginException& e)
  50. {
  51. qCritical() << e.what();
  52. }
  53. }
  54. framework->start();
  55. ui.setupUi(this);
  56. editors = new PluginBrowserEditors(ui.centralwidget);
  57. QAbstractItemModel* pluginTableModel = new PluginTableModel(framework->getPluginContext(), this);
  58. ui.pluginsTableView->setModel(pluginTableModel);
  59. QAbstractItemModel* qtresourcesTreeModel = new QtResourcesTreeModel(this);
  60. ui.qtResourcesTreeView->setModel(qtresourcesTreeModel);
  61. connect(ui.pluginsTableView, SIGNAL(clicked(QModelIndex)), this, SLOT(pluginSelected(QModelIndex)));
  62. connect(ui.pluginsTableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(pluginDoubleClicked(QModelIndex)));
  63. connect(ui.pluginResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(dbResourceDoubleClicked(QModelIndex)));
  64. connect(ui.qtResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(qtResourceDoubleClicked(QModelIndex)));
  65. }
  66. void PluginBrowser::pluginSelected(const QModelIndex &index)
  67. {
  68. QVariant v = index.data(Qt::UserRole);
  69. Plugin* plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
  70. if (!plugin) return;
  71. QAbstractItemModel* oldModel = ui.pluginResourcesTreeView->model();
  72. ui.pluginResourcesTreeView->setModel(new PluginResourcesTreeModel(plugin, this));
  73. if (oldModel) oldModel->deleteLater();;
  74. }
  75. void PluginBrowser::pluginDoubleClicked(const QModelIndex& index)
  76. {
  77. long pluginId = index.data(Qt::UserRole).toLongLong();
  78. Plugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
  79. QByteArray mfContent = plugin->getResource("/META-INF/MANIFEST.MF");
  80. QString location = QString("/") + plugin->getSymbolicName() + "/META-INF/MANIFEST.MF";
  81. editors->openEditor(location, mfContent, location + " [cached]");
  82. QList<ServiceReference*> serviceRefs = plugin->getPluginContext()->getServiceReferences("");
  83. QListIterator<ServiceReference*> it(serviceRefs);
  84. while (it.hasNext())
  85. {
  86. ServiceReference* ref = it.next();
  87. qDebug() << "Service from" << ref->getPlugin()->getSymbolicName() << ":" << ref->getPropertyKeys();
  88. qDebug() << "Object Classes:" << ref->getProperty(PluginConstants::OBJECTCLASS).toStringList();
  89. }
  90. ServiceReference* cliRef = plugin->getPluginContext()->getServiceReference("ctk::ICLIManager");
  91. QObject* cliService = plugin->getPluginContext()->getService(cliRef);
  92. if (cliService)
  93. qDebug() << "Got service object: " << cliService->metaObject()->className();
  94. else qDebug() << "Got null service";
  95. }
  96. void PluginBrowser::qtResourceDoubleClicked(const QModelIndex& index)
  97. {
  98. }
  99. void PluginBrowser::dbResourceDoubleClicked(const QModelIndex& index)
  100. {
  101. QString resPath = index.data(Qt::UserRole).toString();
  102. if (resPath.isEmpty() || resPath.endsWith('/')) return;
  103. qDebug() << "Trying to open: " << resPath;
  104. QModelIndex pluginIndex = ui.pluginsTableView->selectionModel()->selectedIndexes().first();
  105. long pluginId = pluginIndex.data(Qt::UserRole).toLongLong();
  106. Plugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
  107. QByteArray resContent = plugin->getResource(resPath);
  108. QString location = QString("/") + plugin->getSymbolicName() + resPath;
  109. editors->openEditor(location, resContent, location + " [cached]");
  110. }
  111. void PluginBrowser::frameworkEvent(const PluginFrameworkEvent& event)
  112. {
  113. qDebug() << "FrameworkEvent: [" << event.getPlugin()->getSymbolicName() << "]" << event.getErrorString();
  114. }
  115. }