networkurlinterceptorpluginmanager.cpp 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
  Copyright (c) 2016 Montel Laurent <montel@kde.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

Laurent Montel's avatar
Laurent Montel committed
18 19
#include "networkpluginurlinterceptor.h"
#include "networkurlinterceptorpluginmanager.h"
20

Laurent Montel's avatar
Laurent Montel committed
21 22 23 24 25
#include <KPluginLoader>
#include <KPluginFactory>
#include <QFileInfo>
#include <QVariant>
#include <QSet>
26
#include <QDebug>
Laurent Montel's avatar
Laurent Montel committed
27 28
#include <kpluginmetadata.h>

29 30
using namespace MessageViewer;

Laurent Montel's avatar
Laurent Montel committed
31
class MailNetworkUrlInterceptorPluginManagerInstancePrivate
32 33
{
public:
Laurent Montel's avatar
Laurent Montel committed
34
    MailNetworkUrlInterceptorPluginManagerInstancePrivate()
Laurent Montel's avatar
Rename  
Laurent Montel committed
35
        : mailNetworkUrlInterceptorPluginManager(new NetworkUrlInterceptorPluginManager)
36 37 38
    {
    }

Laurent Montel's avatar
Laurent Montel committed
39
    ~MailNetworkUrlInterceptorPluginManagerInstancePrivate()
40 41 42 43
    {
        delete mailNetworkUrlInterceptorPluginManager;
    }

Laurent Montel's avatar
Rename  
Laurent Montel committed
44
    NetworkUrlInterceptorPluginManager *mailNetworkUrlInterceptorPluginManager;
45 46
};

Laurent Montel's avatar
Laurent Montel committed
47
Q_GLOBAL_STATIC(MailNetworkUrlInterceptorPluginManagerInstancePrivate, sInstance)
48

Laurent Montel's avatar
Laurent Montel committed
49
class MailNetworkUrlInterceptorPluginInfo
Laurent Montel's avatar
Laurent Montel committed
50 51
{
public:
Laurent Montel's avatar
Laurent Montel committed
52
    MailNetworkUrlInterceptorPluginInfo()
Laurent Montel's avatar
Laurent Montel committed
53 54 55 56 57 58 59
        : plugin(Q_NULLPTR)
    {

    }
    QString saveName() const;

    KPluginMetaData metaData;
Laurent Montel's avatar
Rename  
Laurent Montel committed
60
    MessageViewer::NetworkPluginUrlInterceptor *plugin;
Laurent Montel's avatar
Laurent Montel committed
61 62
};

Laurent Montel's avatar
Laurent Montel committed
63
QString MailNetworkUrlInterceptorPluginInfo::saveName() const
Laurent Montel's avatar
Laurent Montel committed
64 65 66 67 68 69 70 71 72 73 74
{
    return QFileInfo(metaData.fileName()).baseName();
}

namespace
{
QString pluginVersion()
{
    return QStringLiteral("1.0");
}
}
75

Laurent Montel's avatar
Rename  
Laurent Montel committed
76
class MessageViewer::NetworkUrlInterceptorPluginManagerPrivate
77 78
{
public:
Laurent Montel's avatar
Rename  
Laurent Montel committed
79
    NetworkUrlInterceptorPluginManagerPrivate(NetworkUrlInterceptorPluginManager *qq)
Laurent Montel's avatar
Laurent Montel committed
80
        : q(qq)
81 82 83
    {

    }
Laurent Montel's avatar
Laurent Montel committed
84 85
    void initializePluginList();

Laurent Montel's avatar
Laurent Montel committed
86
    void loadPlugin(MailNetworkUrlInterceptorPluginInfo *item);
Laurent Montel's avatar
Rename  
Laurent Montel committed
87
    QVector<MessageViewer::NetworkPluginUrlInterceptor *> pluginsList() const;
Laurent Montel's avatar
Laurent Montel committed
88

Laurent Montel's avatar
Laurent Montel committed
89
    QVector<MailNetworkUrlInterceptorPluginInfo> mPluginList;
Laurent Montel's avatar
Rename  
Laurent Montel committed
90
    NetworkUrlInterceptorPluginManager *q;
91 92
};

Laurent Montel's avatar
Rename  
Laurent Montel committed
93
void NetworkUrlInterceptorPluginManagerPrivate::initializePluginList()
Laurent Montel's avatar
Laurent Montel committed
94
{
Laurent Montel's avatar
Laurent Montel committed
95 96 97 98 99 100 101 102
    const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("messageviewer"), [](const KPluginMetaData & md) {
        return md.serviceTypes().contains(QStringLiteral("MessageViewer/UrlInterceptor"));
    });

    QVectorIterator<KPluginMetaData> i(plugins);
    i.toBack();
    QSet<QString> unique;
    while (i.hasPrevious()) {
Laurent Montel's avatar
Laurent Montel committed
103
        MailNetworkUrlInterceptorPluginInfo info;
Laurent Montel's avatar
Laurent Montel committed
104 105 106 107 108 109 110 111 112 113 114 115
        info.metaData = i.previous();

        const QString version = info.metaData.version();
        if (pluginVersion() == version) {

            // only load plugins once, even if found multiple times!
            if (unique.contains(info.saveName())) {
                continue;
            }
            info.plugin = Q_NULLPTR;
            mPluginList.append(info);
            unique.insert(info.saveName());
116 117
        } else {
            qWarning() << "Plugin " << info.metaData.name() << " doesn't have correction plugin version. It will not be loaded.";
Laurent Montel's avatar
Laurent Montel committed
118 119
        }
    }
Laurent Montel's avatar
Laurent Montel committed
120 121
    QVector<MailNetworkUrlInterceptorPluginInfo>::iterator end(mPluginList.end());
    for (QVector<MailNetworkUrlInterceptorPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
122 123 124 125
        loadPlugin(&(*it));
    }
}

Laurent Montel's avatar
Rename  
Laurent Montel committed
126
QVector<MessageViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManagerPrivate::pluginsList() const
Laurent Montel's avatar
Laurent Montel committed
127
{
Laurent Montel's avatar
Rename  
Laurent Montel committed
128
    QVector<MessageViewer::NetworkPluginUrlInterceptor *> lst;
Laurent Montel's avatar
Laurent Montel committed
129 130
    QVector<MailNetworkUrlInterceptorPluginInfo>::ConstIterator end(mPluginList.constEnd());
    for (QVector<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
131 132 133 134 135 136 137
        if ((*it).plugin) {
            lst << (*it).plugin;
        }
    }
    return lst;
}

Laurent Montel's avatar
Rename  
Laurent Montel committed
138
void NetworkUrlInterceptorPluginManagerPrivate::loadPlugin(MailNetworkUrlInterceptorPluginInfo *item)
Laurent Montel's avatar
Laurent Montel committed
139
{
Laurent Montel's avatar
Rename  
Laurent Montel committed
140
    item->plugin = KPluginLoader(item->metaData.fileName()).factory()->create<MessageViewer::NetworkPluginUrlInterceptor>(q, QVariantList() << item->saveName());
Laurent Montel's avatar
Laurent Montel committed
141 142
}

Laurent Montel's avatar
Rename  
Laurent Montel committed
143
NetworkUrlInterceptorPluginManager *NetworkUrlInterceptorPluginManager::self()
144 145 146 147
{
    return sInstance->mailNetworkUrlInterceptorPluginManager;
}

Laurent Montel's avatar
Rename  
Laurent Montel committed
148
NetworkUrlInterceptorPluginManager::NetworkUrlInterceptorPluginManager(QObject *parent)
149
    : QObject(parent),
Laurent Montel's avatar
Rename  
Laurent Montel committed
150
      d(new NetworkUrlInterceptorPluginManagerPrivate(this))
151
{
Laurent Montel's avatar
Laurent Montel committed
152
    d->initializePluginList();
153 154
}

Laurent Montel's avatar
Rename  
Laurent Montel committed
155
NetworkUrlInterceptorPluginManager::~NetworkUrlInterceptorPluginManager()
156 157 158
{
    delete d;
}
Laurent Montel's avatar
Laurent Montel committed
159

Laurent Montel's avatar
Rename  
Laurent Montel committed
160
QVector<MessageViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManager::pluginsList() const
Laurent Montel's avatar
Laurent Montel committed
161 162 163
{
    return d->pluginsList();
}