summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_kipimanager.cpp
blob: b049e847879c5cfa5b6f03e7b6a377bcd9501d7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
                          sq_kipimanager.cpp  -  description
                             -------------------
    begin                :  Feb 5 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "sq_kipimanager.h"

#ifdef SQ_HAVE_KIPI

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqwidget.h>

#include <kactioncollection.h>
#include <kpopupmenu.h>
#include <klocale.h>
#include <kdebug.h>

#include <libkipi/pluginloader.h>
#include <libkipi/interface.h>

#include "sq_kipiinterface.h"

SQ_ActionMenu::SQ_ActionMenu(const TQString &text, TQObject *parent, const char *name)
    : KActionMenu(text, parent, name)
{}

SQ_ActionMenu::SQ_ActionMenu(const TQString &text, const TQString &icon, TQObject *parent, const char *name)
    : KActionMenu(text, icon, parent, name)
{}

SQ_ActionMenu::~SQ_ActionMenu()
{
    TQValueVector<KAction *>::iterator itEnd = plugged.end();

    for(TQValueVector<KAction *>::iterator it = plugged.begin();it != itEnd;++it)
        remove(*it);
}

void SQ_ActionMenu::insert(KAction *ka, int index)
{
    KActionMenu::insert(ka, index);

    plugged.append(ka);
}

/***********************************************************************/                                        

SQ_KIPIManager::SQ_KIPIManager(TQWidget *_parent, const char *name) : TQObject(_parent, name), parent(_parent)
{
    kdDebug() << "+SQ_KIPIManager" << endl;

    // Create a dummy "no plugin" action
    noPlugin = new KAction(i18n("No Plugins"), 0, 0, 0, (KActionCollection *)0/*actionCollection()*/, "no_plugin");
    noPlugin->setShortcutConfigurable(false);
    noPlugin->setEnabled(false);

    loaded = false;
    p = new KPopupMenu;

    connect(p, TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(slotAboutToShow()));

    SQ_KIPIInterface *interface = new SQ_KIPIInterface(_parent);
    mPluginLoader = new KIPI::PluginLoader(TQStringList(), interface);
    connect(mPluginLoader, TQT_SIGNAL(replug()), this, TQT_SLOT(slotReplug()));
}

SQ_KIPIManager::~SQ_KIPIManager()
{
    kdDebug() << "-SQ_KIPIManager" << endl;

    delete mPluginLoader;

    //unplug all & delete popup menu
    clearMap();
    delete p;

    delete noPlugin;
}

void SQ_KIPIManager::loadPlugins()
{
    kdDebug() << "Loading KIPI plugins..." << endl;

    loaded = true;
    mPluginLoader->loadPlugins();

    kdDebug() << "Done" << endl;
}

void SQ_KIPIManager::clearMap()
{
    // delete menus
    for(CategoryMap::iterator it = cmenus.begin();it != cmenus.end();++it)
        delete it.data(); // destructor will do all things for us

    cmenus.clear();
}

void SQ_KIPIManager::slotReplug()
{
    // Fill the mActions
    KIPI::PluginLoader::PluginList pluginList = mPluginLoader->pluginList();
    KIPI::PluginLoader::PluginList::ConstIterator it(pluginList.begin());
    KIPI::PluginLoader::PluginList::ConstIterator itEnd(pluginList.end());
    KIPI::Plugin *plugin;
    CategoryMap::iterator fnd;

    clearMap();

    SQ_ActionMenu *menuImages  = new SQ_ActionMenu(i18n("Image actions"));
    SQ_ActionMenu *menuEffects = new SQ_ActionMenu(i18n("Effects"));
    SQ_ActionMenu *menuTools   = new SQ_ActionMenu(i18n("Tools"));
    SQ_ActionMenu *menuImport  = new SQ_ActionMenu(i18n("Import"));
    SQ_ActionMenu *menuExport  = new SQ_ActionMenu(i18n("Export"));
    SQ_ActionMenu *menuBatch   = new SQ_ActionMenu(i18n("Batch processing"));
    SQ_ActionMenu *menuColl    = new SQ_ActionMenu(i18n("Collections"));

    cmenus[KIPI::IMAGESPLUGIN]      = menuImages;
    cmenus[KIPI::EFFECTSPLUGIN]     = menuEffects;
    cmenus[KIPI::TOOLSPLUGIN]       = menuTools;
    cmenus[KIPI::IMPORTPLUGIN]      = menuImport;
    cmenus[KIPI::EXPORTPLUGIN]      = menuExport;
    cmenus[KIPI::BATCHPLUGIN]       = menuBatch;
    cmenus[KIPI::COLLECTIONSPLUGIN] = menuColl;

    for( ;it != itEnd;++it)
    {   
        if(!(*it)->shouldLoad())
            continue;

	plugin = (*it)->plugin();

        if (!plugin) continue;

        plugin->setup(parent);

        KActionPtrList actions = plugin->actions();
        KActionPtrList::ConstIterator actionIt = actions.begin(), end = actions.end();
        KIPI::Category category;

        for ( ;actionIt != end;++actionIt)
        {
            category = plugin->category(*actionIt);

            fnd = cmenus.find(category);

            if(fnd == cmenus.end())
                continue;

            fnd.data()->insert(*actionIt);
        }

        plugin->actionCollection()->readShortcutSettings();
    }

    // plug
    for(CategoryMap::iterator it = cmenus.begin();it != cmenus.end();++it)
    {
        if(!it.data()->count())
            it.data()->insert(noPlugin);

        it.data()->plug(p);
    }
}

void SQ_KIPIManager::slotAboutToShow()
{
    // load KIPI plugins on demand
    if(!loaded) loadPlugins();
}

#include "sq_kipimanager.moc"

#endif