viewerplugintoolmanager.cpp 5.23 KB
Newer Older
1
/*
2
   SPDX-FileCopyrightText: 2015-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: LGPL-2.0-or-later
5 6 7 8 9 10
*/

#include "viewerplugintoolmanager.h"
#include "viewerpluginmanager.h"
#include "viewerplugin.h"
#include "viewerplugininterface.h"
11

12 13 14 15 16 17 18 19
#include <QVector>

using namespace MessageViewer;

class MessageViewer::ViewerPluginToolManagerPrivate
{
public:
    ViewerPluginToolManagerPrivate(ViewerPluginToolManager *qq, QWidget *parentWidget)
Laurent Montel's avatar
Laurent Montel committed
20
        : mParentWidget(parentWidget)
Laurent Montel's avatar
Laurent Montel committed
21
        , q(qq)
22 23
    {
    }
Laurent Montel's avatar
Laurent Montel committed
24

25
    void setPluginName(const QString &pluginName);
Laurent Montel's avatar
Laurent Montel committed
26 27
    Q_REQUIRED_RESULT QString pluginDirectory() const;
    Q_REQUIRED_RESULT QString pluginName() const;
28
    void createView();
Laurent Montel's avatar
Laurent Montel committed
29
    void refreshActionList();
30 31 32 33 34
    void closeAllTools();
    void setActionCollection(KActionCollection *ac);
    void updateActions(const Akonadi::Item &messageItem);
    QList<QAction *> actionList(ViewerPluginInterface::SpecificFeatureTypes features) const;
    QList<MessageViewer::ViewerPluginInterface *> mListInterface;
Laurent Montel's avatar
Laurent Montel committed
35 36
    KActionCollection *mActionCollection = nullptr;
    QWidget *mParentWidget = nullptr;
37
    ViewerPluginToolManager *const q;
Laurent Montel's avatar
Laurent Montel committed
38
    void setPluginDirectory(const QString &directory);
39 40
};

Laurent Montel's avatar
Laurent Montel committed
41
void ViewerPluginToolManagerPrivate::setPluginDirectory(const QString &directory)
42
{
Laurent Montel's avatar
Laurent Montel committed
43
    MessageViewer::ViewerPluginManager::self()->setPluginDirectory(directory);
44 45
}

Laurent Montel's avatar
Laurent Montel committed
46
QString ViewerPluginToolManagerPrivate::pluginDirectory() const
47
{
Laurent Montel's avatar
Laurent Montel committed
48
    return MessageViewer::ViewerPluginManager::self()->pluginDirectory();
49 50 51 52 53 54 55 56 57 58 59 60
}

void ViewerPluginToolManagerPrivate::setPluginName(const QString &pluginName)
{
    MessageViewer::ViewerPluginManager::self()->setPluginName(pluginName);
}

QString ViewerPluginToolManagerPrivate::pluginName() const
{
    return MessageViewer::ViewerPluginManager::self()->pluginName();
}

Laurent Montel's avatar
Laurent Montel committed
61 62
void ViewerPluginToolManagerPrivate::refreshActionList()
{
Laurent Montel's avatar
Laurent Montel committed
63
    for (MessageViewer::ViewerPluginInterface *interface : qAsConst(mListInterface)) {
Laurent Montel's avatar
Laurent Montel committed
64 65 66 67
        interface->refreshActionList(mActionCollection);
    }
}

68 69
void ViewerPluginToolManagerPrivate::createView()
{
Laurent Montel's avatar
Laurent Montel committed
70 71
    const QVector<MessageViewer::ViewerPlugin *> listPlugin
        = MessageViewer::ViewerPluginManager::self()->pluginsList();
Laurent Montel's avatar
Laurent Montel committed
72
    for (MessageViewer::ViewerPlugin *plugin : listPlugin) {
73
        if (plugin->isEnabled()) {
Laurent Montel's avatar
Laurent Montel committed
74 75 76 77 78 79
            MessageViewer::ViewerPluginInterface *interface = plugin->createView(mParentWidget,
                                                                                 mActionCollection);
            q->connect(interface, &MessageViewer::ViewerPluginInterface::activatePlugin, q,
                       &ViewerPluginToolManager::activatePlugin);
            q->connect(plugin, &ViewerPlugin::configChanged, q,
                       &ViewerPluginToolManager::refreshActionList);
80 81
            mListInterface.append(interface);
        }
82 83 84 85 86
    }
}

void ViewerPluginToolManagerPrivate::closeAllTools()
{
Laurent Montel's avatar
Laurent Montel committed
87
    for (MessageViewer::ViewerPluginInterface *interface : qAsConst(mListInterface)) {
88 89 90 91 92 93 94 95 96
        interface->closePlugin();
    }
}

void ViewerPluginToolManagerPrivate::setActionCollection(KActionCollection *ac)
{
    mActionCollection = ac;
}

Laurent Montel's avatar
Laurent Montel committed
97 98
QList<QAction *> ViewerPluginToolManagerPrivate::actionList(
    ViewerPluginInterface::SpecificFeatureTypes features) const
99 100
{
    QList<QAction *> lstAction;
Laurent Montel's avatar
Laurent Montel committed
101
    for (MessageViewer::ViewerPluginInterface *interface : qAsConst(mListInterface)) {
102
        if (features & ViewerPluginInterface::All) {
103
            lstAction.append(interface->actions());
104 105
        } else {
            if (interface->featureTypes() & features) {
106
                lstAction.append(interface->actions());
107 108 109 110 111 112 113 114
            }
        }
    }
    return lstAction;
}

void ViewerPluginToolManagerPrivate::updateActions(const Akonadi::Item &messageItem)
{
Laurent Montel's avatar
Laurent Montel committed
115
    for (MessageViewer::ViewerPluginInterface *interface : qAsConst(mListInterface)) {
116 117 118 119 120
        interface->updateAction(messageItem);
    }
}

ViewerPluginToolManager::ViewerPluginToolManager(QWidget *parentWidget, QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
121 122
    : QObject(parent)
    , d(new MessageViewer::ViewerPluginToolManagerPrivate(this, parentWidget))
123 124 125 126 127 128 129 130 131 132 133 134 135
{
}

ViewerPluginToolManager::~ViewerPluginToolManager()
{
    delete d;
}

void ViewerPluginToolManager::closeAllTools()
{
    d->closeAllTools();
}

Laurent Montel's avatar
Laurent Montel committed
136 137 138 139 140
void ViewerPluginToolManager::refreshActionList()
{
    d->refreshActionList();
}

141 142 143 144 145 146 147 148 149 150
void ViewerPluginToolManager::createView()
{
    d->createView();
}

void ViewerPluginToolManager::setActionCollection(KActionCollection *ac)
{
    d->setActionCollection(ac);
}

Laurent Montel's avatar
Laurent Montel committed
151
void ViewerPluginToolManager::setPluginDirectory(const QString &directory)
152
{
Laurent Montel's avatar
Laurent Montel committed
153
    d->setPluginDirectory(directory);
154 155
}

Laurent Montel's avatar
Laurent Montel committed
156
QString ViewerPluginToolManager::pluginDirectory() const
157
{
Laurent Montel's avatar
Laurent Montel committed
158
    return d->pluginDirectory();
159 160 161 162 163 164 165 166 167 168 169 170
}

void ViewerPluginToolManager::setPluginName(const QString &pluginName)
{
    d->setPluginName(pluginName);
}

QString ViewerPluginToolManager::pluginName() const
{
    return d->pluginName();
}

Laurent Montel's avatar
Laurent Montel committed
171 172 173 174 175
bool ViewerPluginToolManager::initializePluginList()
{
    return MessageViewer::ViewerPluginManager::self()->initializePluginList();
}

Laurent Montel's avatar
Laurent Montel committed
176 177
QList<QAction *> ViewerPluginToolManager::viewerPluginActionList(
    ViewerPluginInterface::SpecificFeatureTypes features) const
178 179 180 181 182 183 184 185
{
    return d->actionList(features);
}

void ViewerPluginToolManager::updateActions(const Akonadi::Item &messageItem)
{
    d->updateActions(messageItem);
}