ctkPluginBrowser.cpp

Go to the documentation of this file.
00001 /*=============================================================================
00002 
00003   Library: CTK
00004 
00005   Copyright (c) 2010 German Cancer Research Center,
00006     Division of Medical and Biological Informatics
00007 
00008   Licensed under the Apache License, Version 2.0 (the "License");
00009   you may not use this file except in compliance with the License.
00010   You may obtain a copy of the License at
00011 
00012     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014   Unless required by applicable law or agreed to in writing, software
00015   distributed under the License is distributed on an "AS IS" BASIS,
00016   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017   See the License for the specific language governing permissions and
00018   limitations under the License.
00019 
00020 =============================================================================*/
00021 
00022 #include "ctkPluginBrowser.h"
00023 
00024 #include "ctkPluginTableModel.h"
00025 #include "ctkPluginResourcesTreeModel.h"
00026 #include "ctkQtResourcesTreeModel.h"
00027 #include "ctkServiceReference.h"
00028 #include "ctkPluginConstants.h"
00029 
00030 #include <ui_ctkPluginBrowserMainWindow.h>
00031 
00032 #include <ctkPluginException.h>
00033 #include <ctkPluginFramework.h>
00034 
00035 #include <QApplication>
00036 #include <QMainWindow>
00037 #include <QStringList>
00038 #include <QDirIterator>
00039 #include <QUrl>
00040 
00041 ctkPluginBrowser::ctkPluginBrowser(ctkPluginFramework* framework)
00042   : framework(framework)
00043 {
00044   framework->getPluginContext()->connectFrameworkListener(this, SLOT(frameworkEvent(ctkPluginFrameworkEvent)));
00045 
00046   QStringList pluginDirs;
00047   pluginDirs << qApp->applicationDirPath() + "/Plugins";
00048 
00049   QStringListIterator dirIt(pluginDirs);
00050   while (dirIt.hasNext())
00051   {
00052     QApplication::addLibraryPath(dirIt.next());
00053   }
00054 
00055   QDirIterator dirIter(pluginDirs.at(0), QDir::Files);
00056   while(dirIter.hasNext())
00057   {
00058     try
00059     {
00060       ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(dirIter.next()).toString());
00061       plugin->start(ctkPlugin::START_ACTIVATION_POLICY);
00062     }
00063     catch (const ctkPluginException& e)
00064     {
00065       qCritical() << e.what();
00066     }
00067   }
00068 
00069   framework->start();
00070 
00071   ui.setupUi(this);
00072 
00073   editors = new ctkPluginBrowserEditors(ui.centralwidget);
00074 
00075   QAbstractItemModel* pluginTableModel = new ctkPluginTableModel(framework->getPluginContext(), this);
00076   ui.pluginsTableView->setModel(pluginTableModel);
00077 
00078   QAbstractItemModel* qtresourcesTreeModel = new ctkQtResourcesTreeModel(this);
00079   ui.qtResourcesTreeView->setModel(qtresourcesTreeModel);
00080 
00081   connect(ui.pluginsTableView, SIGNAL(clicked(QModelIndex)), this, SLOT(pluginSelected(QModelIndex)));
00082   connect(ui.pluginsTableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(pluginDoubleClicked(QModelIndex)));
00083   connect(ui.pluginResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(dbResourceDoubleClicked(QModelIndex)));
00084   connect(ui.qtResourcesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(qtResourceDoubleClicked(QModelIndex)));
00085 }
00086 
00087 void ctkPluginBrowser::pluginSelected(const QModelIndex &index)
00088 {
00089   QVariant v = index.data(Qt::UserRole);
00090 
00091   ctkPlugin* plugin = framework->getPluginContext()->getPlugin(v.toLongLong());
00092 
00093   if (!plugin) return;
00094 
00095   QAbstractItemModel* oldModel = ui.pluginResourcesTreeView->model();
00096   ui.pluginResourcesTreeView->setModel(new ctkPluginResourcesTreeModel(plugin, this));
00097   if (oldModel) oldModel->deleteLater();;
00098 }
00099 
00100 void ctkPluginBrowser::pluginDoubleClicked(const QModelIndex& index)
00101 {
00102   long pluginId = index.data(Qt::UserRole).toLongLong();
00103   ctkPlugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
00104 
00105   QByteArray mfContent = plugin->getResource("/META-INF/MANIFEST.MF");
00106   QString location = QString("/") + plugin->getSymbolicName() + "/META-INF/MANIFEST.MF";
00107   editors->openEditor(location, mfContent, location + " [cached]");
00108 
00109   QList<ctkServiceReference*> serviceRefs = plugin->getPluginContext()->getServiceReferences("");
00110   QListIterator<ctkServiceReference*> it(serviceRefs);
00111   while (it.hasNext())
00112   {
00113     ctkServiceReference* ref = it.next();
00114     qDebug() << "Service from" << ref->getPlugin()->getSymbolicName() << ":" << ref->getPropertyKeys();
00115     qDebug() << "Object Classes:" << ref->getProperty(PluginConstants::OBJECTCLASS).toStringList();
00116   }
00117 
00118   ctkServiceReference* cliRef = plugin->getPluginContext()->getServiceReference("ctkICLIManager");
00119   QObject* cliService = plugin->getPluginContext()->getService(cliRef);
00120   if (cliService)
00121     qDebug() << "Got service object: " << cliService->metaObject()->className();
00122   else qDebug() << "Got null service";
00123 }
00124 
00125 void ctkPluginBrowser::qtResourceDoubleClicked(const QModelIndex& index)
00126 {
00127 
00128 }
00129 
00130 void ctkPluginBrowser::dbResourceDoubleClicked(const QModelIndex& index)
00131 {
00132   QString resPath = index.data(Qt::UserRole).toString();
00133   if (resPath.isEmpty() || resPath.endsWith('/')) return;
00134 
00135   qDebug() << "Trying to open: " << resPath;
00136 
00137   QModelIndex pluginIndex = ui.pluginsTableView->selectionModel()->selectedIndexes().first();
00138   long pluginId = pluginIndex.data(Qt::UserRole).toLongLong();
00139 
00140   ctkPlugin* plugin = framework->getPluginContext()->getPlugin(pluginId);
00141 
00142   QByteArray resContent = plugin->getResource(resPath);
00143   QString location = QString("/") + plugin->getSymbolicName() + resPath;
00144   editors->openEditor(location, resContent, location + " [cached]");
00145 }
00146 
00147 void ctkPluginBrowser::frameworkEvent(const ctkPluginFrameworkEvent& event)
00148 {
00149   qDebug() << "FrameworkEvent: [" << event.getPlugin()->getSymbolicName() << "]" << event.getErrorString();
00150 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1