ctkPluginFramework.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 "ctkPluginFramework.h"
00023 
00024 #include "ctkPluginFrameworkPrivate_p.h"
00025 #include "ctkPluginPrivate_p.h"
00026 #include "ctkPluginFrameworkContext_p.h"
00027 #include "ctkPluginConstants.h"
00028 #include "ctkPluginArchive_p.h"
00029 
00030 
00031 
00032   ctkPluginFramework::ctkPluginFramework(ctkPluginFrameworkContext* fw)
00033     : ctkPlugin(*new ctkPluginFrameworkPrivate(*this, fw))
00034   {
00035     qRegisterMetaType<ctkPluginFrameworkEvent>("ctkPluginFrameworkEvent");
00036   }
00037 
00038   void ctkPluginFramework::init()
00039   {
00040     Q_D(ctkPluginFramework);
00041 
00042     QMutexLocker sync(&d->lock);
00043     // waitOnActivation(d->lock, "Framework.init", true);
00044     switch (d->state)
00045     {
00046     case ctkPlugin::INSTALLED:
00047     case ctkPlugin::RESOLVED:
00048       break;
00049     case ctkPlugin::STARTING:
00050     case ctkPlugin::ACTIVE:
00051       return;
00052     default:
00053       throw std::logic_error("INTERNAL ERROR, Illegal state");
00054     }
00055     d->init();
00056   }
00057 
00058   void ctkPluginFramework::start(const ctkPlugin::StartOptions& options)
00059   {
00060     Q_UNUSED(options);
00061     Q_D(ctkPluginFramework);
00062 
00063     QStringList pluginsToStart;
00064     {
00065       QMutexLocker sync(&d->lock);
00066       // TODO: parallel start
00067       //waitOnActivation(lock, "ctkPluginFramework::start", true);
00068 
00069       switch (d->state)
00070       {
00071       case INSTALLED:
00072       case RESOLVED:
00073         d->init();
00074       case STARTING:
00075         d->activating = true;
00076         break;
00077       case ACTIVE:
00078         return;
00079       default:
00080         throw std::logic_error("INTERNAL ERROR, Illegal state");
00081       }
00082 
00083       pluginsToStart = d->fwCtx->storage.getStartOnLaunchPlugins();
00084     }
00085 
00086     // Start plugins according to their autostart setting.
00087     QStringListIterator i(pluginsToStart);
00088     while (i.hasNext())
00089     {
00090       ctkPlugin* p = d->fwCtx->plugins->getPlugin(i.next());
00091       try {
00092         const int autostartSetting = p->d_func()->archive->getAutostartSetting();
00093         // Launch must not change the autostart setting of a plugin
00094         StartOptions option = ctkPlugin::START_TRANSIENT;
00095         if (ctkPlugin::START_ACTIVATION_POLICY == autostartSetting)
00096         {
00097           // Transient start according to the plugins activation policy.
00098           option |= ctkPlugin::START_ACTIVATION_POLICY;
00099         }
00100         p->start(option);
00101       }
00102       catch (const ctkPluginException& pe)
00103       {
00104         d->fwCtx->listeners.frameworkError(p, pe);
00105       }
00106     }
00107 
00108     {
00109       QMutexLocker sync(&d->lock);
00110       d->state = ACTIVE;
00111       d->activating = false;
00112       d->fwCtx->listeners.emitFrameworkEvent(
00113           ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::STARTED, this));
00114     }
00115   }
00116 
00117   QStringList ctkPluginFramework::getResourceList(const QString& path) const
00118   {
00119     QString resourcePath = QString(":/") + PluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME;
00120     if (path.startsWith('/'))
00121       resourcePath += path;
00122     else
00123       resourcePath += QString("/") + path;
00124 
00125     QStringList paths;
00126     QFileInfoList entryInfoList = QDir(resourcePath).entryInfoList();
00127     QListIterator<QFileInfo> infoIter(entryInfoList);
00128     while (infoIter.hasNext())
00129     {
00130       const QFileInfo& resInfo = infoIter.next();
00131       QString entry = resInfo.canonicalFilePath().mid(resourcePath.size());
00132       if (resInfo.isDir())
00133         entry += "/";
00134 
00135       paths << entry;
00136     }
00137 
00138     return paths;
00139   }
00140 
00141   QByteArray ctkPluginFramework::getResource(const QString& path) const
00142   {
00143     QString resourcePath = QString(":/") + PluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME;
00144     if (path.startsWith('/'))
00145       resourcePath += path;
00146     else
00147       resourcePath += QString("/") + path;
00148 
00149     QFile resourceFile(resourcePath);
00150     resourceFile.open(QIODevice::ReadOnly);
00151     return resourceFile.readAll();
00152   }
00153 
00154   QHash<QString, QString> ctkPluginFramework::getHeaders()
00155   {
00156     //TODO security
00157     Q_D(ctkPluginFramework);
00158     return d->systemHeaders;
00159 
00160 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines