File indexing completed on 2024-04-21 16:32:34

0001 /***************************************************************************
0002                         pluginloader.cpp  -  description
0003                              -------------------
0004     begin                : Sun Oct 7 2007
0005     copyright            : (C) 2007 by Dominik Seichter
0006     email                : domseichter@web.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include <QtGlobal>
0019 
0020 #include "pluginloader.h"
0021 
0022 #include "krenameimpl.h"
0023 #include "plugin.h"
0024 
0025 #include "datetimeplugin.h"
0026 #include "dirsortplugin.h"
0027 #include "fileplugin.h"
0028 #include "increasecounterplugin.h"
0029 #include "permissionsplugin.h"
0030 #include "scriptplugin.h"
0031 #include "systemplugin.h"
0032 #include "translitplugin.h"
0033 #include "snumplugin.h"
0034 
0035 #include <kservice.h>
0036 
0037 #include <../config-krename.h>
0038 
0039 #if HAVE_TAGLIB
0040 # include "taglibplugin.h"
0041 #endif // HAVE_TAGLIB
0042 #if HAVE_EXIV2
0043 #include "exiv2plugin.h"
0044 #endif // HAVE_EXIV2
0045 #if HAVE_PODOFO
0046 # include "podofoplugin.h"
0047 #endif // HAVE_PODOFO
0048 #if HAVE_FREETYPE
0049 # include "fontplugin.h"
0050 #endif // HAVE_FREETYPE
0051 
0052 PluginLoader *PluginLoader::s_instance = nullptr;
0053 
0054 PluginLoader *PluginLoader::Instance()
0055 {
0056     if (!s_instance) {
0057         s_instance = new PluginLoader();
0058     }
0059 
0060     return s_instance;
0061 }
0062 
0063 PluginLoader::PluginLoader()
0064 {
0065     this->load();
0066 }
0067 
0068 PluginLoader::~PluginLoader()
0069 {
0070     this->clear();
0071 }
0072 
0073 Plugin *PluginLoader::findPlugin(const QString &token)
0074 {
0075     QString lower = token.toLower();
0076 
0077     // first search in the cache
0078     Plugin *p = m_tokenCache[lower];
0079     if (p) {
0080         return p;
0081     }
0082 
0083     // now search in all tokens
0084     QMap<QString, Plugin *>::const_iterator it = m_tokenMap.constBegin();
0085     while (it != m_tokenMap.constEnd()) {
0086         if (QRegExp(it.key()).exactMatch(lower)) {
0087             // we found a plugin
0088             // put the token into the cache for quick access
0089             p = it.value();
0090             m_tokenCache[lower] = p;
0091             return p;
0092         }
0093 
0094         ++it;
0095     }
0096 
0097     // add typos to the cache, too:
0098     // So that we find immediately that this key is not supported.
0099     m_tokenCache.insert(lower, nullptr);
0100     return nullptr;
0101 }
0102 
0103 Plugin *PluginLoader::findPluginByName(const QString &name)
0104 {
0105     QList<Plugin *>::iterator it = m_plugins.begin();
0106 
0107     while (it != m_plugins.end()) {
0108         if ((*it)->name() == name) {
0109             return *it;
0110         }
0111 
0112         ++it;
0113     }
0114 
0115     return nullptr;
0116 }
0117 
0118 void PluginLoader::clear()
0119 {
0120     QList<Plugin *>::iterator it = m_plugins.begin();
0121 
0122     while (it != m_plugins.end()) {
0123         delete *it;
0124 
0125         ++it;
0126     }
0127 
0128     m_plugins.clear();
0129     m_tokenMap.clear();
0130     m_tokenCache.clear();
0131 }
0132 
0133 void PluginLoader::load()
0134 {
0135 #ifndef Q_OS_WIN
0136     m_plugins.append(new DateTimePlugin(this));
0137 #endif
0138     m_plugins.append(new DirSortPlugin(this));
0139 #if HAVE_EXIV2
0140     m_plugins.append(new Exiv2Plugin(this));
0141 #endif // HAVE_EXIV2
0142 #if HAVE_FREETYPE
0143     m_plugins.append(new FontPlugin(this));
0144 #endif // HAVE_FREETYPE
0145     m_plugins.append(new IncreaseCounterPlugin(this));
0146 #ifndef Q_OS_WIN
0147     m_plugins.append(new PermissionsPlugin(this));
0148 #endif
0149     m_plugins.append(new ScriptPlugin(this));
0150     m_plugins.append(new SystemPlugin(this));
0151 
0152 #if HAVE_TAGLIB
0153     m_plugins.append(new TagLibPlugin(this));
0154 #endif // HAVE_TAGLIB
0155 #if HAVE_PODOFO
0156     m_plugins.append(new PodofoPlugin(this));
0157 #endif // HAVE_PODOFO
0158     m_plugins.append(new TranslitPlugin(this));
0159     m_plugins.append(new SnumPlugin(this));
0160     //this->loadFilePlugins();
0161 
0162     // Fill the token map
0163     QList<Plugin *>::iterator it = m_plugins.begin();
0164     while (it != m_plugins.end()) {
0165         if (((*it)->type() & ePluginType_Token)) {
0166             const QStringList &tokens = (*it)->supportedTokens();
0167             QStringList::const_iterator itList = tokens.begin();
0168             while (itList != tokens.end()) {
0169                 m_tokenMap.insert((*itList).toLower(), *it);
0170                 ++itList;
0171             }
0172         }
0173 
0174         ++it;
0175     }
0176 }
0177 
0178 void PluginLoader::registerForUpdates(KRenameImpl *krename)
0179 {
0180     m_observers.prepend(krename);
0181 }
0182 
0183 void PluginLoader::deregisterForUpdates(KRenameImpl *krename)
0184 {
0185     m_observers.removeOne(krename);
0186 }
0187 
0188 void PluginLoader::sendUpdatePreview()
0189 {
0190     QList<KRenameImpl *>::iterator it = m_observers.begin();
0191 
0192     while (it != m_observers.end()) {
0193         (*it)->slotUpdatePreview();
0194 
0195         ++it;
0196     }
0197 }
0198 
0199 void PluginLoader::loadConfig(KConfigGroup &group)
0200 {
0201     QList<Plugin *>::const_iterator it = m_plugins.constBegin();
0202     while (it != m_plugins.constEnd()) {
0203         (*it)->loadConfig(group);
0204         ++it;
0205     }
0206 
0207 }
0208 
0209 void PluginLoader::saveConfig(KConfigGroup &group)
0210 {
0211     QList<Plugin *>::const_iterator it = m_plugins.constBegin();
0212     while (it != m_plugins.constEnd()) {
0213         (*it)->saveConfig(group);
0214         ++it;
0215     }
0216 
0217 }