File indexing completed on 2024-05-12 04:45:34

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2013-2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 
0019 #ifndef PLUGINCONTAINER_H
0020 #define PLUGINCONTAINER_H
0021 #include "libsnore/snore_exports.h"
0022 #include "libsnore/snore_p.h"
0023 
0024 #include <QPluginLoader>
0025 #include <QDir>
0026 
0027 namespace Snore
0028 {
0029 class PluginContainer;
0030 
0031 typedef  QHash<QString, PluginContainer *> PluginContaienrHash;
0032 
0033 class PluginContainer
0034 {
0035 public:
0036     static const QHash<QString, PluginContainer *> pluginCache(SnorePlugin::PluginTypes type);
0037 
0038     PluginContainer(const QString &pluginName, SnorePlugin::PluginTypes type, SnorePlugin *plugin);
0039     PluginContainer(const QString &fileName, const QString &pluginName, SnorePlugin::PluginTypes type);
0040     ~PluginContainer();
0041     SnorePlugin *load();
0042     const QString &file();
0043     const QString &name();
0044     SnorePlugin::PluginTypes type();
0045 
0046     bool isLoaded() const;
0047 
0048 private:
0049     Q_DISABLE_COPY(PluginContainer)
0050     static QHash<SnorePlugin::PluginTypes, PluginContaienrHash > s_pluginCache;
0051 
0052     void static updatePluginCache();
0053     static const QDir &pluginDir();
0054     static inline const QStringList pluginExtentions()
0055     {
0056         QStringList out;
0057 #if defined(Q_OS_UNIX)
0058         out << QStringLiteral("so");
0059 #endif
0060 #if defined(Q_OS_WIN)
0061         out << QStringLiteral("dll");
0062 #endif
0063 #if defined(Q_OS_MAC)
0064         out << QStringLiteral("dylib");
0065 #endif
0066         return out;
0067     }
0068 
0069     static inline const QStringList pluginFileFilters()
0070     {
0071         QStringList out;
0072         for (const QString &extention : pluginExtentions()) {
0073             out << QLatin1String("libsnore_*.") + extention;
0074         }
0075         return out;
0076     }
0077 
0078     static inline const QStringList pluginFileFilters(Snore::SnorePlugin::PluginTypes type)
0079     {
0080         QStringList out;
0081         QString typeName = SnorePlugin::typeToString(type).toLower();
0082         for (const QString &extention : pluginExtentions()) {
0083             out << QLatin1String("libsnore_") + typeName + QLatin1String("_*.") + extention;
0084         }
0085         return out;
0086     }
0087 
0088     QString m_pluginFile;
0089     QString m_pluginName;
0090     SnorePlugin::PluginTypes m_pluginType;
0091     QPluginLoader m_loader;
0092     SnorePlugin *m_plugin = nullptr;
0093 };
0094 }
0095 
0096 #endif//PLUGINCONTAINER_H