ctkPluginContext.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 "ctkPluginContext.h"
00023 
00024 #include "ctkPluginPrivate_p.h"
00025 #include "ctkPluginFrameworkContext_p.h"
00026 #include "ctkServiceRegistration.h"
00027 #include "ctkServiceReference.h"
00028 #include "ctkServiceReferencePrivate.h"
00029 
00030 #include <stdexcept>
00031 
00032 
00033   class ctkPluginContextPrivate
00034   {
00035 
00036   public:
00037 
00038     ctkPluginPrivate* plugin;
00039 
00040     ctkPluginContextPrivate(ctkPluginPrivate* plugin)
00041       : plugin(plugin)
00042     {}
00043 
00047     void isPluginContextValid() const
00048     {
00049       if (!plugin) {
00050         throw std::logic_error("This plugin context is no longer valid");
00051       }
00052     }
00053 
00054     void invalidate()
00055     {
00056       plugin = 0;
00057     }
00058 
00059   };
00060 
00061 
00062   ctkPluginContext::ctkPluginContext(ctkPluginPrivate* plugin)
00063   : d_ptr(new ctkPluginContextPrivate(plugin))
00064   {}
00065 
00066   ctkPluginContext::~ctkPluginContext()
00067   {
00068     Q_D(ctkPluginContext);
00069     delete d;
00070   }
00071 
00072   ctkPlugin* ctkPluginContext::getPlugin() const
00073   {
00074     Q_D(const ctkPluginContext);
00075     d->isPluginContextValid();
00076     return d->plugin->q_func();
00077   }
00078 
00079   ctkPlugin* ctkPluginContext::getPlugin(long id) const
00080   {
00081     Q_D(const ctkPluginContext);
00082     return d->plugin->fwCtx->plugins->getPlugin(id);
00083   }
00084 
00085   QList<ctkPlugin*> ctkPluginContext::getPlugins() const
00086   {
00087     Q_D(const ctkPluginContext);
00088     d->isPluginContextValid();
00089     return d->plugin->fwCtx->plugins->getPlugins();
00090   }
00091 
00092   ctkPlugin* ctkPluginContext::installPlugin(const QUrl& location, QIODevice* in)
00093   {
00094     Q_D(ctkPluginContext);
00095     d->isPluginContextValid();
00096     return d->plugin->fwCtx->plugins->install(location, in);
00097   }
00098 
00099   ctkServiceRegistration* ctkPluginContext::registerService(const QStringList& clazzes, QObject* service, const ServiceProperties& properties)
00100   {
00101     Q_D(ctkPluginContext);
00102     d->isPluginContextValid();
00103     return d->plugin->fwCtx->services.registerService(d->plugin, clazzes, service, properties);
00104   }
00105 
00106   QList<ctkServiceReference*> ctkPluginContext::getServiceReferences(const QString& clazz, const QString& filter)
00107   {
00108     Q_D(ctkPluginContext);
00109     d->isPluginContextValid();
00110     return d->plugin->fwCtx->services.get(clazz, filter);
00111   }
00112 
00113   ctkServiceReference* ctkPluginContext::getServiceReference(const QString& clazz)
00114   {
00115     Q_D(ctkPluginContext);
00116     d->isPluginContextValid();
00117     return d->plugin->fwCtx->services.get(d->plugin, clazz);
00118   }
00119 
00120   QObject* ctkPluginContext::getService(ctkServiceReference* reference)
00121   {
00122     if (reference == 0)
00123     {
00124       throw std::invalid_argument("Null ctkServiceReference is not valid");
00125     }
00126 
00127     Q_D(ctkPluginContext);
00128     d->isPluginContextValid();
00129     return reference->d_func()->getService(d->plugin->q_func());
00130   }
00131 
00132   bool ctkPluginContext::connectPluginListener(const QObject* receiver, const char* method,
00133                                             Qt::ConnectionType type)
00134   {
00135     Q_D(ctkPluginContext);
00136     // TODO check permissions for a direct connection
00137     return receiver->connect(&(d->plugin->fwCtx->listeners), SIGNAL(pluginChanged(ctkPluginEvent)), method, type);
00138   }
00139 
00140   bool ctkPluginContext::connectFrameworkListener(const QObject* receiver, const char* method, Qt::ConnectionType type)
00141   {
00142     Q_D(ctkPluginContext);
00143     return receiver->connect(&(d->plugin->fwCtx->listeners), SIGNAL(frameworkEvent(ctkPluginFrameworkEvent)), method, type);
00144   }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines