File indexing completed on 2024-04-28 09:47:12

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