ctkPluginBrowser.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "ctkServiceException.h"
  21. #include "ctkPluginConstants.h"
  22. #include <ui_ctkPluginBrowserMainWindow.h>
  23. #include <ctkPluginException.h>
  24. #include <ctkPluginFramework.h>
  25. #include <QApplication>
  26. #include <QMainWindow>
  27. #include <QStringList>
  28. #include <QDirIterator>
  29. #include <QUrl>
  30. ctkPluginBrowser::ctkPluginBrowser(ctkPluginFramework* framework)
  31. : framework(framework)
  32. {
  33. pluginEventTypeToString[ctkPluginEvent::INSTALLED] = "Installed";
  34. pluginEventTypeToString[ctkPluginEvent::LAZY_ACTIVATION] = "Lazy Activation";
  35. pluginEventTypeToString[ctkPluginEvent::RESOLVED] = "Resolved";
  36. pluginEventTypeToString[ctkPluginEvent::STARTED] = "Started";
  37. pluginEventTypeToString[ctkPluginEvent::STARTING] = "Starting";
  38. pluginEventTypeToString[ctkPluginEvent::STOPPED] = "Stopped";
  39. pluginEventTypeToString[ctkPluginEvent::STOPPING] = "Stopping";
  40. pluginEventTypeToString[ctkPluginEvent::UNINSTALLED] = "Uninstalled";
  41. pluginEventTypeToString[ctkPluginEvent::UNRESOLVED] = "Unresolved";
  42. pluginEventTypeToString[ctkPluginEvent::UPDATED] = "Updated";
  43. framework->getPluginContext()->connectFrameworkListener(this, SLOT(frameworkEvent(ctkPluginFrameworkEvent)));
  44. framework->getPluginContext()->connectPluginListener(this, SLOT(pluginEvent(ctkPluginEvent)));
  45. QStringList pluginDirs;
  46. #ifdef CMAKE_INTDIR
  47. pluginDirs << qApp->applicationDirPath() + "/../plugins/" CMAKE_INTDIR "/";
  48. #else
  49. pluginDirs << qApp->applicationDirPath() + "/plugins/";
  50. #endif
  51. QStringListIterator dirIt(pluginDirs);
  52. while (dirIt.hasNext())
  53. {
  54. QApplication::addLibraryPath(dirIt.next());
  55. }
  56. QStringList libFilter;
  57. libFilter << "*.dll" << "*.so" << "*.dylib";
  58. QDirIterator dirIter(pluginDirs.at(0), libFilter, QDir::Files);
  59. while(dirIter.hasNext())
  60. {
  61. try
  62. {
  63. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(dirIter.next()).toString());
  64. //plugin->start(ctkPlugin::START_ACTIVATION_POLICY);
  65. }
  66. catch (const ctkPluginException& e)
  67. {
  68. qCritical() << e.what();
  69. }
  70. }
  71. framework->start();
  72. ui.setupUi(this);
  73. editors = new ctkPluginBrowserEditors(ui.centralwidget);
  74. QAbstractItemModel* pluginTableModel = new ctkPluginTableModel(framework->getPluginContext(), this);
  75. ui.pluginsTableView->setModel(pluginTableModel);
  76. QAbstractItemModel* qtresourcesTreeModel = new ctkQtResourcesTreeModel(this);
  77. ui.qtResourcesTreeView->setModel(qtresourcesTreeModel);
  78. connect(ui.pluginsTableView, SIGNAL(clicked(QModelIndex)), this, SLOT(pluginSelected(QModelIndex)));
  79. connect(ui.pluginsTableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(pluginDoubleClicked(QModelIndex)));
  80. connect(ui.pluginResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(dbResourceDoubleClicked(QModelIndex)));
  81. connect(ui.qtResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(qtResourceDoubleClicked(QModelIndex)));
  82. startPluginNowAction = new QAction(QIcon(":/pluginbrowser/images/run-now.png"), "Start Plugin (ignore activation policy)", this);
  83. startPluginAction = new QAction(QIcon(":/pluginbrowser/images/run.png"), "Start Plugin", this);
  84. stopPluginAction = new QAction(QIcon(":/pluginbrowser/images/stop.png"), "Stop Plugin", this);
  85. connect(startPluginNowAction, SIGNAL(triggered()), this, SLOT(startPluginNow()));
  86. connect(startPluginAction, SIGNAL(triggered()), this, SLOT(startPlugin()));
  87. connect(stopPluginAction, SIGNAL(triggered()), this, SLOT(stopPlugin()));
  88. startPluginNowAction->setEnabled(false);
  89. startPluginAction->setEnabled(false);
  90. stopPluginAction->setEnabled(false);
  91. ui.pluginToolBar->addAction(startPluginNowAction);
  92. ui.pluginToolBar->addAction(startPluginAction);
  93. ui.pluginToolBar->addAction(stopPluginAction);
  94. }
  95. void ctkPluginBrowser::pluginSelected(const QModelIndex &index)
  96. {
  97. QVariant v = index.data(Qt::UserRole);
  98. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
  99. if (!plugin) return;
  100. updatePluginToolbar(plugin);
  101. QAbstractItemModel* oldModel = ui.pluginResourcesTreeView->model();
  102. ui.pluginResourcesTreeView->setModel(new ctkPluginResourcesTreeModel(plugin, this));
  103. if (oldModel) oldModel->deleteLater();;
  104. }
  105. void ctkPluginBrowser::updatePluginToolbar(ctkPlugin* plugin)
  106. {
  107. startPluginNowAction->setEnabled(false);
  108. startPluginAction->setEnabled(false);
  109. stopPluginAction->setEnabled(false);
  110. if (!plugin) return;
  111. const ctkPlugin::States startStates = ctkPlugin::INSTALLED | ctkPlugin::RESOLVED | ctkPlugin::STOPPING;
  112. const ctkPlugin::States stopStates = ctkPlugin::STARTING | ctkPlugin::ACTIVE;
  113. if (startStates.testFlag(plugin->getState()))
  114. {
  115. startPluginNowAction->setEnabled(true);
  116. startPluginAction->setEnabled(true);
  117. }
  118. if (stopStates.testFlag(plugin->getState()))
  119. {
  120. stopPluginAction->setEnabled(true);
  121. }
  122. }
  123. void ctkPluginBrowser::pluginDoubleClicked(const QModelIndex& index)
  124. {
  125. long pluginId = index.data(Qt::UserRole).toLongLong();
  126. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
  127. QByteArray mfContent = plugin->getResource("/META-INF/MANIFEST.MF");
  128. QString location = QString("/") + plugin->getSymbolicName() + "/META-INF/MANIFEST.MF";
  129. editors->openEditor(location, mfContent, location + " [cached]");
  130. QList<ctkServiceReference> serviceRefs = plugin->getPluginContext()->getServiceReferences("");
  131. QListIterator<ctkServiceReference> it(serviceRefs);
  132. while (it.hasNext())
  133. {
  134. ctkServiceReference ref(it.next());
  135. qDebug() << "Service from" << ref.getPlugin()->getSymbolicName() << ":" << ref.getPropertyKeys();
  136. qDebug() << "Object Classes:" << ref.getProperty(ctkPluginConstants::OBJECTCLASS).toStringList();
  137. }
  138. try
  139. {
  140. ctkServiceReference cliRef(plugin->getPluginContext()->getServiceReference("ctkICLIManager"));
  141. QObject* cliService = plugin->getPluginContext()->getService(cliRef);
  142. if (cliService)
  143. qDebug() << "Got service object: " << cliService->metaObject()->className();
  144. else qDebug() << "Got null service";
  145. }
  146. catch (const ctkServiceException& e)
  147. {
  148. qDebug() << e;
  149. }
  150. }
  151. void ctkPluginBrowser::qtResourceDoubleClicked(const QModelIndex& index)
  152. {
  153. Q_UNUSED(index)
  154. }
  155. void ctkPluginBrowser::dbResourceDoubleClicked(const QModelIndex& index)
  156. {
  157. QString resPath = index.data(Qt::UserRole).toString();
  158. if (resPath.isEmpty() || resPath.endsWith('/')) return;
  159. qDebug() << "Trying to open: " << resPath;
  160. QModelIndex pluginIndex = ui.pluginsTableView->selectionModel()->selectedIndexes().first();
  161. long pluginId = pluginIndex.data(Qt::UserRole).toLongLong();
  162. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
  163. QByteArray resContent = plugin->getResource(resPath);
  164. QString location = QString("/") + plugin->getSymbolicName() + resPath;
  165. editors->openEditor(location, resContent, location + " [cached]");
  166. }
  167. void ctkPluginBrowser::frameworkEvent(const ctkPluginFrameworkEvent& event)
  168. {
  169. qDebug() << "FrameworkEvent: [" << event.getPlugin()->getSymbolicName() << "]" << event.getErrorString();
  170. }
  171. void ctkPluginBrowser::pluginEvent(const ctkPluginEvent& event)
  172. {
  173. qDebug() << "PluginEvent: [" << event.getPlugin()->getSymbolicName() << "]" << pluginEventTypeToString[event.getType()];
  174. ctkPlugin* plugin = event.getPlugin();
  175. QModelIndexList selection = ui.pluginsTableView->selectionModel()->selectedIndexes();
  176. if (!selection.isEmpty() && selection.first().data(Qt::UserRole).toLongLong() == plugin->getPluginId())
  177. {
  178. updatePluginToolbar(plugin);
  179. }
  180. }
  181. void ctkPluginBrowser::startPlugin()
  182. {
  183. startPlugin(ctkPlugin::START_TRANSIENT | ctkPlugin::START_ACTIVATION_POLICY);
  184. }
  185. void ctkPluginBrowser::startPluginNow()
  186. {
  187. startPlugin(ctkPlugin::START_TRANSIENT);
  188. }
  189. void ctkPluginBrowser::startPlugin(ctkPlugin::StartOptions options)
  190. {
  191. QModelIndex selection = ui.pluginsTableView->selectionModel()->currentIndex();
  192. QVariant v = selection.data(Qt::UserRole);
  193. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
  194. plugin->start(options);
  195. }
  196. void ctkPluginBrowser::stopPlugin()
  197. {
  198. QModelIndex selection = ui.pluginsTableView->selectionModel()->currentIndex();
  199. QVariant v = selection.data(Qt::UserRole);
  200. ctkPlugin* plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
  201. plugin->stop();
  202. }