00001 /*************************************************************************** 00002 * OmgModellerPluginRegistry.cpp - Singleton class for tracking modeller plugins. 00003 * ------------------- 00004 * begin : Tues 26 Dec 2007 00005 * copyright : (C) 2007 by Tim Sutton 00006 * email : tim@linfiniti.com 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 /* $Id: omgmodellerpluginregistry.cpp 4024 2008-02-11 19:19:34Z timlinux $ */ 00018 00019 #include "omgmodellerpluginregistry.h" 00020 #include "omgui.h" 00021 00022 #include <QPluginLoader> 00023 #include <QDir> 00024 #include <QFile> 00025 #include <QStringList> 00026 #include <QSettings> 00027 00028 // 00029 // Static calls to enforce singleton behaviour 00030 // 00031 OmgModellerPluginRegistry *OmgModellerPluginRegistry::mpInstance = 0; 00032 OmgModellerPluginRegistry *OmgModellerPluginRegistry::instance() 00033 { 00034 if (mpInstance == 0) 00035 { 00036 mpInstance = new OmgModellerPluginRegistry(); 00037 } 00038 return mpInstance; 00039 } 00040 00041 // 00042 // Main class begins now... 00043 // 00044 00045 OmgModellerPluginRegistry::OmgModellerPluginRegistry(QObject *parent) : QObject(parent) 00046 { 00047 //now populate the registry since this is the first time its being used 00048 QString myPluginsPath=Omgui::pluginDirPath() + QDir::separator() + "modeller"; 00049 //if we are running unit tests we need to be able to load directly 00050 //from the build tree so we check the above path exists and if 00051 //it doesnt assume we are running in the build tree... 00052 if (!QFile::exists(myPluginsPath)) 00053 { 00054 myPluginsPath=Omgui::pluginDirPath() + QDir::separator() + "modellerplugins"; 00055 } 00056 //qDebug("Searching for plugins in : " + myPluginsPath.toLocal8Bit()); 00057 QDir myPluginsDir(myPluginsPath); 00058 foreach (QString myFileName, myPluginsDir.entryList(QDir::Files)) 00059 { 00060 //check if the filename matches the users chose modeller plugin 00061 //qDebug("Plugin registry trying to load : " + myFileName.toLocal8Bit()); 00062 QPluginLoader myLoader(myPluginsDir.absoluteFilePath(myFileName)); 00063 //qDebug("Plugin registry: loaded : " + myFileName.toLocal8Bit()); 00064 QObject *mypPlugin = myLoader.instance(); 00065 //qDebug("Plugin to registry: cast to qobject : " + myFileName.toLocal8Bit()); 00066 if (mypPlugin) 00067 { 00068 //try to cast to a modeller plugin 00069 OmgModellerPluginInterface * mypModellerPlugin = qobject_cast<OmgModellerPluginInterface *>(mypPlugin); 00070 //qDebug("Plugin to registry: cast to modellerplugin : " + myFileName.toLocal8Bit()); 00071 if (!mypModellerPlugin->getName().isEmpty()) 00072 { 00073 mPluginsMap.insert(mypModellerPlugin->getName(),mypModellerPlugin); 00074 //qDebug("Added modeller plugin to registry : " + mypModellerPlugin->getName().toLocal8Bit()); 00075 } 00076 else 00077 { 00078 myLoader.unload(); 00079 delete mypModellerPlugin; 00080 //qDebug("...plugin name was invalid"); 00081 } 00082 } 00083 else 00084 { 00085 myLoader.unload(); 00086 delete mypPlugin; 00087 //qDebug("...plugin could not be loaded"); 00088 } 00089 } 00090 //qDebug("OmgModellerPluginRegistry created!"); 00091 } 00092 const int OmgModellerPluginRegistry::count() 00093 { 00094 return mPluginsMap.count(); 00095 } 00096 00097 OmgModellerPluginInterface * OmgModellerPluginRegistry::getPlugin(QString thePluginName) 00098 { 00099 //if no explicit plugin name was given return the default one 00100 if (thePluginName.isEmpty()) 00101 { 00102 //find out which plugin should be active 00103 QSettings mySettings; 00104 thePluginName= 00105 mySettings.value("openModeller/modellerPluginType", "Local Modeller Plugin" ).toString(); 00106 } 00107 //qDebug("Getting the instance of: " + thePluginName.toLocal8Bit()); 00108 //look for the named plugin - if its not registered return 0 00109 return mPluginsMap.value(thePluginName,0); 00110 } 00111 00112 QStringList OmgModellerPluginRegistry::names() 00113 { 00114 QStringList myList = (QStringList) mPluginsMap.keys(); 00115 return myList; 00116 } 00117