ctkPluginBrowser.cpp 5.3 KB

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