ctkPluginManager.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 "ctkPluginManager.h"
00023 
00024 #include <QServiceManager>
00025 #include <QDirIterator>
00026 #include <QDebug>
00027 #include <QLibrary>
00028 #include <QApplication>
00029 
00030 
00031 class ctkPluginManagerPrivate
00032 {
00033 public:
00034 
00035   QList<QString> pluginPaths;
00036   QServiceManager serviceManager;
00037 };
00038 
00039 ctkPluginManager::ctkPluginManager()
00040 : d_ptr(new ctkPluginManagerPrivate())
00041 {
00042   Q_D(ctkPluginManager);
00043 
00044   QString libName("CTKCore");
00045   QLibrary lib(libName);
00046   QFileInfo fileInfo(libName);
00047   QString libBaseName(fileInfo.baseName());
00048   if (libBaseName.startsWith("lib"))
00049   {
00050     libBaseName.remove(0, 3);
00051   }
00052   qDebug() << libBaseName;
00053   lib.load();
00054   if (lib.isLoaded())
00055   {
00056     QString xyz = QString(":/") + libBaseName + "/servicedescriptor.xml";
00057     qDebug() << "resource string: " << xyz;
00058     QFile serviceDescriptor(xyz);
00059     qDebug() << "file exists: " << serviceDescriptor.exists();
00060     qDebug() << "open returns:" << serviceDescriptor.open(QIODevice::ReadOnly);
00061     qDebug() << "file open: " << serviceDescriptor.isOpen();
00062     qDebug() << "file is readable: " << serviceDescriptor.isReadable();
00063     //QByteArray serviceBA = serviceDescriptor.readAll();
00064     //qDebug() << serviceBA;
00065     qDebug() << "Service for " << libBaseName << " registered:" << d->serviceManager.addService(&serviceDescriptor);
00066     lib.unload();
00067   }
00068 }
00069 
00070 ctkPluginManager::~ctkPluginManager()
00071 {
00072   Q_D(ctkPluginManager);
00073   delete d;
00074 }
00075 
00076 QServiceManager* ctkPluginManager::serviceManager()
00077 {
00078   Q_D(ctkPluginManager);
00079   return &(d->serviceManager);
00080 }
00081 
00082 void ctkPluginManager::addSearchPath(const QString & searchPath)
00083 {
00084   Q_D(ctkPluginManager);
00085   d->pluginPaths.push_back(searchPath);
00086 }
00087 
00088 void ctkPluginManager::startAllPlugins()
00089 {
00090   Q_D(ctkPluginManager);
00091   QDirIterator it(d->pluginPaths.front(), QDir::Files);
00092   if (it.hasNext())
00093   {
00094     qApp->addLibraryPath(d->pluginPaths.front());
00095   }
00096   while (it.hasNext()) {
00097        QString libName(it.next());
00098        QLibrary lib(libName);
00099        QFileInfo fileInfo(libName);
00100        QString libBaseName(fileInfo.baseName());
00101        if (libBaseName.startsWith("lib"))
00102        {
00103          libBaseName.remove(0, 3);
00104        }
00105        qDebug() << libBaseName;
00106        lib.load();
00107        if (lib.isLoaded())
00108        {
00109          QString xyz = QString(":/") + libBaseName + "/servicedescriptor.xml";
00110          qDebug() << "resource string: " << xyz;
00111          QFile serviceDescriptor(xyz);
00112          //qDebug() << "file exists: " << serviceDescriptor.exists();
00113          qDebug() << "open returns:" << serviceDescriptor.open(QIODevice::ReadOnly);
00114          qDebug() << "file open: " << serviceDescriptor.isOpen();
00115          qDebug() << "file is readable: " << serviceDescriptor.isReadable();
00116          //QByteArray serviceBA = serviceDescriptor.readAll();
00117          //qDebug() << serviceBA;
00118          qDebug() << "Service for " << libBaseName << " registered:" << d->serviceManager.addService(&serviceDescriptor);
00119          lib.unload();
00120        }
00121   }
00122 
00123 
00124 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines