SilentEye 0.4.1

modulemanager.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye. If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "modulemanager.h"
00017 #include <imagemoduleinterface.h>
00018 #include <audiomoduleinterface.h>
00019 #include <videomoduleinterface.h>
00020 
00021 namespace SilentEye {
00022 
00023     Logger ModuleManager::m_logger("ModuleManager");
00024     QList<ModuleInterface*> ModuleManager::m_modulesList;
00025     QMap< ModuleManager::Category, QMap<QString, ModuleInterface*> > 
00026             ModuleManager::m_modulesMap;
00027     QMap<QString, ModuleInterface*> ModuleManager::m_emptyMap;
00028 
00029     QList<ModuleInterface*>& ModuleManager::getList()
00030     {
00031         return m_modulesList;
00032     }
00033 
00034     QMap<QString, ModuleInterface*>& 
00035             ModuleManager::get(Category cat)
00036     {
00037         if(m_modulesMap.contains(cat))
00038             return m_modulesMap[cat];
00039 
00040         return m_emptyMap;
00041     }
00042 
00043     ModuleInterface* 
00044             ModuleManager::get(Category cat, QString type)
00045     {
00046         if(m_modulesMap.contains(cat) && m_modulesMap[cat].contains(type))
00047             return m_modulesMap[cat][type];
00048 
00049         return NULL;
00050     }
00051 
00052     ModuleManager::Category ModuleManager::getCategory(ModuleInterface* module)
00053     {
00054         QMap<Category, QMap<QString, ModuleInterface*> >::const_iterator moduleMap = m_modulesMap.constBegin();
00055         while( moduleMap != m_modulesMap.constEnd() )
00056         {
00057             QMap<QString,ModuleInterface*>::const_iterator i = moduleMap.value().constBegin();
00058             while( i != moduleMap.value().constEnd() )
00059             {
00060                 if( i.value() == module )
00061                     return moduleMap.key();
00062                 ++i;
00063             }
00064             ++moduleMap;
00065         }
00066 
00067         return C_UNDEF;
00068     }
00069 
00070     int
00071             ModuleManager::count()
00072     {
00073         return m_modulesList.count();
00074     }
00075 
00076     int 
00077             ModuleManager::count(Category cat)
00078     {
00079         if(m_modulesMap.contains(cat))
00080             return m_modulesMap[cat].count();
00081         return 0;
00082     }
00083 
00084     void ModuleManager::load()
00085     {
00086         /* Load static module */
00087         foreach (QObject *plugin, QPluginLoader::staticInstances())
00088         {
00089              loadPlugin(plugin);
00090         }
00091 
00092 
00093         /* Load dynamical module */
00094         QDir pluginsDir = QDir(qApp->applicationDirPath());
00095         pluginsDir.cd("modules");
00096 
00097         /* Load all modules in modules directory */
00098         foreach (QString fileName, pluginsDir.entryList(QDir::Files))
00099         {
00100             if(!QLibrary::isLibrary(fileName))
00101             {
00102                 m_logger.warning(fileName + " is not a loadable library!");
00103                 continue;
00104             }
00105             QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
00106             m_logger.debug("Loading module " + fileName + "...");
00107             QObject *plugin = loader.instance();
00108             loadPlugin(plugin, loader.errorString());
00109         }
00110     }
00111 
00112     void ModuleManager::loadPlugin(QObject* plugin, QString error)
00113     {
00114         if( plugin )
00115         {
00116             ImageModuleInterface* moduleI = qobject_cast<ImageModuleInterface*>(plugin);
00117             if( moduleI )
00118             {
00119                 if (m_modulesMap[IMAGEFORMAT].contains(moduleI->typeSupported()))
00120                 {
00121                     m_logger.debug(moduleI->typeSupported() + " already supported, '" + moduleI->name() + "' ignored.");
00122                     return;
00123                 }
00124                 m_modulesMap[IMAGEFORMAT][moduleI->typeSupported()] = moduleI;
00125                 m_modulesList << moduleI;
00126                 m_logger.debug("Image format module '" + moduleI->name() + "' loaded.");
00127                 return;
00128             }
00129 
00130             //plugin = loader.instance();
00131             AudioModuleInterface* moduleA = qobject_cast<AudioModuleInterface*>(plugin);
00132             if( moduleA )
00133             {
00134                 if (m_modulesMap[AUDIOFORMAT].contains(moduleA->typeSupported()))
00135                 {
00136                     m_logger.debug(moduleA->typeSupported() + " already supported, '" + moduleA->name() + "' ignored.");
00137                     return;
00138                 }
00139                 m_modulesMap[AUDIOFORMAT][moduleA->typeSupported()] = moduleA;
00140                 m_modulesList << moduleA;
00141                 m_logger.debug("Audio format module '" + moduleA->name() + "' loaded.");
00142                 return;
00143             }
00144 
00145             //plugin = loader.instance();
00146             VideoModuleInterface* moduleV = qobject_cast<VideoModuleInterface*>(plugin);
00147             if( moduleV )
00148             {
00149                 if (m_modulesMap[VIDEOFORMAT].contains(moduleV->typeSupported()))
00150                 {
00151                     m_logger.debug(moduleV->typeSupported() + " already supported, '" + moduleV->name() + "' ignored.");
00152                     return;
00153                 }
00154                 m_modulesMap[VIDEOFORMAT][moduleV->typeSupported()] = moduleV;
00155                 m_modulesList << moduleV;
00156                 m_logger.debug("Video format module '" + moduleV->name() + "' loaded.");
00157                 return;
00158             }
00159 
00160             //plugin = loader.instance();
00161             CryptoModuleInterface* moduleC = qobject_cast<CryptoModuleInterface*>(plugin);
00162             if( moduleC )
00163             {
00164                 if (m_modulesMap[CRYPTO].contains(moduleC->typeSupported()))
00165                 {
00166                     m_logger.debug(moduleC->typeSupported() + " already supported, " + moduleC->name() + " ignored.");
00167                     return;
00168                 }
00169                 m_modulesMap[CRYPTO][moduleC->typeSupported()] = moduleC;
00170                 m_modulesList << moduleC;
00171                 m_logger.debug("Cryptography module '" + moduleC->name() + "' loaded.");
00172                 return;
00173             }
00174             m_logger.info("Unreconisgned SilentEye's module type: " + QString::fromLatin1(plugin->metaObject()->className()));
00175         } else {
00176             m_logger.warning(error);
00177         }
00178     }
00179 
00180 }