plugininterface.cpp 7.16 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2015-2016 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  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
*/

#include "plugininterface.h"
#include "genericpluginmanager.h"
#include "pimcommon_debug.h"

#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
23
#include <KXMLGUIClient>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QAction>
Laurent Montel's avatar
Laurent Montel committed
25 26 27 28 29 30 31 32

#include "genericplugin.h"

using namespace PimCommon;

class PimCommon::PluginInterfacePrivate
{
public:
33
    PluginInterfacePrivate()
Laurent Montel's avatar
Laurent Montel committed
34
        : mParentWidget(Q_NULLPTR),
35
          mActionCollection(Q_NULLPTR)
Laurent Montel's avatar
Laurent Montel committed
36 37 38 39 40 41 42 43 44 45
    {

    }
    QString mPluginName;
    QString mServiceTypeName;
    QWidget *mParentWidget;
    KActionCollection *mActionCollection;
    QVector<PimCommon::GenericPluginInterface *> mListGenericInterface;
};

46
PluginInterface::PluginInterface(QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
47
    : QObject(parent),
48
      d(new PimCommon::PluginInterfacePrivate)
Laurent Montel's avatar
Laurent Montel committed
49 50 51 52 53 54 55 56
{
}

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

57 58 59 60 61
void PimCommon::PluginInterface::setActionCollection(KActionCollection *ac)
{
    d->mActionCollection = ac;
}

Laurent Montel's avatar
Laurent Montel committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
void PluginInterface::initializePlugins()
{
    PimCommon::GenericPluginManager::self()->setPluginName(d->mPluginName);
    PimCommon::GenericPluginManager::self()->setServiceTypeName(d->mServiceTypeName);
    if (!PimCommon::GenericPluginManager::self()->initializePlugins()) {
        qCDebug(PIMCOMMON_LOG) << " Impossible to initialize plugins";
    }
}

void PluginInterface::setPluginName(const QString &name)
{
    d->mPluginName = name;
}

void PluginInterface::setServiceTypeName(const QString &name)
{
    d->mServiceTypeName = name;
}

void PluginInterface::createPluginInterface()
{
83 84 85 86
    if (!d->mActionCollection) {
        qCWarning(PIMCOMMON_LOG) << "Missing action collection";
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
87
    Q_FOREACH (PimCommon::GenericPlugin *plugin, PimCommon::GenericPluginManager::self()->pluginsList()) {
88 89 90 91 92 93 94
        if (plugin->isEnabled()) {
            PimCommon::GenericPluginInterface *interface = static_cast<PimCommon::GenericPluginInterface *>(plugin->createInterface(d->mActionCollection, this));
            interface->setParentWidget(d->mParentWidget);
            interface->setPlugin(plugin);
            connect(interface, &PimCommon::GenericPluginInterface::emitPluginActivated, this, &PluginInterface::slotPluginActivated);
            d->mListGenericInterface.append(interface);
        }
Laurent Montel's avatar
Laurent Montel committed
95 96 97
    }
}

Laurent Montel's avatar
Laurent Montel committed
98
void PluginInterface::initializeInterfaceRequires(PimCommon::AbstractGenericPluginInterface *interface)
Laurent Montel's avatar
Laurent Montel committed
99 100 101 102
{
    Q_UNUSED(interface)
}

103 104
QString PluginInterface::actionXmlExtension(ActionType::Type type)
{
Laurent Montel's avatar
Laurent Montel committed
105
    switch (type) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    case PimCommon::ActionType::Tools:
        return QStringLiteral("_plugins_tools");
    case PimCommon::ActionType::Edit:
        return QStringLiteral("_plugins_edit");
    case PimCommon::ActionType::File:
        return QStringLiteral("_plugins_file");
    case PimCommon::ActionType::Action:
        return QStringLiteral("_plugins_actions");
    case PimCommon::ActionType::PopupMenu:
        return QStringLiteral("_popupmenu_actions");
    case PimCommon::ActionType::ToolBar:
        return QStringLiteral("_toolbar_actions");
    }
    return {};
}

Laurent Montel's avatar
Laurent Montel committed
122
void PluginInterface::slotPluginActivated(PimCommon::AbstractGenericPluginInterface *interface)
Laurent Montel's avatar
Laurent Montel committed
123 124 125 126 127 128 129 130 131 132 133 134
{
    initializeInterfaceRequires(interface);
    if (interface) {
        interface->exec();
    }
}

void PluginInterface::setParentWidget(QWidget *widget)
{
    d->mParentWidget = widget;
}

Laurent Montel's avatar
Laurent Montel committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void PluginInterface::initializePluginActions(const QString &prefix, KXMLGUIClient *guiClient)
{
    if (guiClient->factory()) {
        QHashIterator<PimCommon::ActionType::Type, QList<QAction *> > localActionsType(actionsType());
        while (localActionsType.hasNext()) {
            localActionsType.next();
            QList<QAction *> lst = localActionsType.value();
            if (!lst.isEmpty()) {
                const QString actionlistname = prefix + PimCommon::PluginInterface::actionXmlExtension(localActionsType.key());
                guiClient->unplugActionList(actionlistname);
                guiClient->plugActionList(actionlistname, lst);
            }
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
151 152 153 154 155 156 157
void PluginInterface::updateActions(int numberOfSelectedItems, int numberOfSelectedCollections)
{
    Q_FOREACH (PimCommon::GenericPluginInterface *interface, d->mListGenericInterface) {
        interface->updateActions(numberOfSelectedItems, numberOfSelectedCollections);
    }
}

Laurent Montel's avatar
Laurent Montel committed
158
QHash<PimCommon::ActionType::Type, QList<QAction *> > PluginInterface::actionsType()
Laurent Montel's avatar
Laurent Montel committed
159 160
{
    QHash<PimCommon::ActionType::Type, QList<QAction *> > listType;
Laurent Montel's avatar
Laurent Montel committed
161
    Q_FOREACH (PimCommon::GenericPluginInterface *interface, d->mListGenericInterface) {
Laurent Montel's avatar
Laurent Montel committed
162
        PimCommon::ActionType actionType = interface->actionType();
Laurent Montel's avatar
Laurent Montel committed
163
        PimCommon::ActionType::Type type = actionType.type();
Laurent Montel's avatar
Laurent Montel committed
164 165
        if (listType.contains(type)) {
            QList<QAction *> lst = listType.value(type);
Laurent Montel's avatar
Laurent Montel committed
166 167 168
            QAction *act = new QAction(this);
            act->setSeparator(true);
            lst << act << actionType.action();
Laurent Montel's avatar
Laurent Montel committed
169 170 171 172
            listType.insert(type, lst);
        } else {
            listType.insert(type, QList<QAction *>() << actionType.action());
        }
173
        if (interface->plugin()->hasPopupMenuSupport()) {
Laurent Montel's avatar
Laurent Montel committed
174 175 176
            type = PimCommon::ActionType::PopupMenu;
            if (listType.contains(type)) {
                QList<QAction *> lst = listType.value(type);
Laurent Montel's avatar
Laurent Montel committed
177 178 179
                QAction *act = new QAction(this);
                act->setSeparator(true);
                lst << act << actionType.action();
Laurent Montel's avatar
Laurent Montel committed
180 181 182 183 184
                listType.insert(type, lst);
            } else {
                listType.insert(type, QList<QAction *>() << actionType.action());
            }
        }
185
        if (interface->plugin()->hasToolBarSupport()) {
186 187 188
            type = PimCommon::ActionType::ToolBar;
            if (listType.contains(type)) {
                QList<QAction *> lst = listType.value(type);
Laurent Montel's avatar
Laurent Montel committed
189 190 191
                QAction *act = new QAction(this);
                act->setSeparator(true);
                lst << act << actionType.action();
192 193 194 195 196
                listType.insert(type, lst);
            } else {
                listType.insert(type, QList<QAction *>() << actionType.action());
            }
        }
Laurent Montel's avatar
Laurent Montel committed
197 198 199 200
    }

    return listType;
}
Laurent Montel's avatar
Laurent Montel committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

QVector<PimCommon::PluginUtilData> PluginInterface::pluginsDataList() const
{
    return GenericPluginManager::self()->pluginsDataList();
}

QString PluginInterface::configGroupName() const
{
    return GenericPluginManager::self()->configGroupName();
}

QString PluginInterface::configPrefixSettingKey() const
{
    return GenericPluginManager::self()->configPrefixSettingKey();
}
Laurent Montel's avatar
Laurent Montel committed
216 217 218 219 220

PimCommon::GenericPlugin *PluginInterface::pluginFromIdentifier(const QString &id)
{
    return GenericPluginManager::self()->pluginFromIdentifier(id);
}