00001 /*************************************************************************** 00002 * OmgScraperPluginRegistry.cpp - Singleton class for tracking scraper 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: omgscraperpluginregistry.cpp 3110 2007-05-29 14:29:49Z timlinux $ */ 00018 00019 #include "omgscraperpluginregistry.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 OmgScraperPluginRegistry *OmgScraperPluginRegistry::mpInstance = 0; 00032 OmgScraperPluginRegistry *OmgScraperPluginRegistry::instance() 00033 { 00034 if (mpInstance == 0) 00035 { 00036 mpInstance = new OmgScraperPluginRegistry(); 00037 } 00038 return mpInstance; 00039 } 00040 00041 // 00042 // Main class begins now... 00043 // 00044 00045 OmgScraperPluginRegistry::OmgScraperPluginRegistry(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() + "scraper"; 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() + "scraperplugins"; 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 scraper 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 scraper plugin 00069 OmgScraperPluginInterface * mypScraperPlugin = qobject_cast<OmgScraperPluginInterface *>(mypPlugin); 00070 //qDebug("Plugin to registry: cast to scraperplugin : " + myFileName.toLocal8Bit()); 00071 if (!mypScraperPlugin->getName().isEmpty()) 00072 { 00073 mPluginsMap.insert(mypScraperPlugin->getName(),mypScraperPlugin); 00074 //qDebug("Added scraper plugin to registry : " + mypScraperPlugin->getName().toLocal8Bit()); 00075 } 00076 else 00077 { 00078 myLoader.unload(); 00079 delete mypScraperPlugin; 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("OmgScraperPluginRegistry created!"); 00091 } 00092 const int OmgScraperPluginRegistry::count() 00093 { 00094 return mPluginsMap.count(); 00095 } 00096 00097 OmgScraperPluginInterface * OmgScraperPluginRegistry::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("locScraper/scraperPluginName", "GBIF REST Web Service 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 (OmgScraperPluginInterface*) mPluginsMap.value(thePluginName,0); 00110 } 00111 00112 QStringList OmgScraperPluginRegistry::names() 00113 { 00114 QStringList myList = (QStringList) mPluginsMap.keys(); 00115 return myList; 00116 } 00117 00118 QMap<QString,QString> OmgScraperPluginRegistry::licenses() 00119 { 00120 QMap<QString,QString> myMap; 00121 00122 foreach (QString myName, names()) 00123 { 00124 myMap[myName] = mPluginsMap.value(myName)->getLicense(); 00125 } 00126 return myMap; 00127 }