summaryrefslogtreecommitdiffstats
path: root/libkipi/libkipi/pluginloader.cpp
blob: 944c956c3fe9c32f071f0e65ea61be399079d716 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* ============================================================
 * File  : pluginloader.cpp
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Date  : 2004-02-19
 * Description :
 *
 * Copyright 2004 by Renchi Raju

 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Library General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * 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 Library General Public License for more details.
 *
 * ============================================================ */

/** @file pluginloader.cpp */

#include <tqstringlist.h>

#include <ktrader.h>
#include <tdeparts/componentfactory.h>
#include <kdebug.h>
#include <kdialog.h>

#include "plugin.h"
#include "pluginloader.h"
#include "interface.h"
#include <kconfig.h>
#include <tqcheckbox.h>
#include <tqlayout.h>

/**
   \author Renchi Raju
   \class KIPI::PluginLoader
   This is the class that will help host applications to load plugins.

   The host application must create an instance of the plugin loader, and
   call the method loadPlugins() to get the plugins loaded. To ensure that
   plugins are correctly removed from menus and toolbars when loaded and
   unloaded after constructions, the application must connect to either the
   signals plug() / unplug() or the signal replug(). These signals are
   emitted when a plugin is to be inserted into the menus.

   If your application is using XMLGUI, the easiest way to get the plugins
   inserted into the menus is by adding an item in the ui.rc file looking
   list this:
      &lt;ActionList name="image_actions"/&gt;

   Then plugin plugins into menus could be done with code similiar to this from KimDaBa:
   \code
    void slotReplug() {
      unplugActionList( TQString::fromLatin1("file_actions") );
      unplugActionList( TQString::fromLatin1("image_actions") );
      unplugActionList( TQString::fromLatin1("tool_actions") );

      TQPtrList<KAction> fileActions;
      TQPtrList<KAction> imageActions;
      TQPtrList<KAction> toolsActions;

      KIPI::PluginLoader::PluginList list = _pluginLoader->pluginList();
      for( KIPI::PluginLoader::PluginList::Iterator it = list.begin(); it != list.end(); ++it ) {
          KIPI::Plugin* plugin = (*it)->plugin;
          if ( !plugin || !(*it)->shouldLoad )
              continue;

          plugin->setup( this );
          TQPtrList<KAction>* popup = 0;
          if ( plugin->category() == KIPI::IMAGESPLUGIN )
              popup = &imageActions;

          else if ( plugin->category() == KIPI::EXPORTPLUGIN  ||
                    plugin->category() == KIPI::IMPORTPLUGIN )
              popup = &fileActions;

          else if ( plugin->category() == KIPI::TOOLSPLUGIN )
              popup = &toolsActions;

          if ( popup ) {
              KActionPtrList actions = plugin->actions();
              for( KActionPtrList::Iterator it = actions.begin(); it != actions.end(); ++it ) {
                  popup->append( *it );
              }
          }
          else {
              kdDebug() << "No menu found\n";
          }
          plugin->actionCollection()->readShortcutSettings();
      }

      // For this to work I need to pass false as second arg for createGUI
      plugActionList( TQString::fromLatin1("file_actions"), fileActions );
      plugActionList( TQString::fromLatin1("image_actions"), imageActions );
      plugActionList( TQString::fromLatin1("tool_actions"), toolsActions );
  }
  \endcode

  To configure which plugins should be loaded, simply call
  PluginLoader::configWidget(), and insert the widget into your normal
  configuration dialog.
*/
namespace KIPI {

//---------------------------------------------------------------------
//
// PluginLoader::Info
//
//---------------------------------------------------------------------
struct PluginLoader::Info::Private {
    TQString m_name;
    TQString m_comment;
    TQString m_library;
    Plugin* m_plugin;
    bool m_shouldLoad;
};


PluginLoader::Info::Info(const TQString& name, const TQString& comment, const TQString& library, bool shouldLoad)
{
    d=new Private;
    d->m_name=name;
    d->m_comment=comment;
    d->m_library=library;
    d->m_plugin=0;
    d->m_shouldLoad=shouldLoad;
}


PluginLoader::Info::~Info()
{
    delete d;
}


TQString PluginLoader::Info::name() const
{
    return d->m_name;
}


TQString PluginLoader::Info::comment() const
{
    return d->m_comment;
}


TQString PluginLoader::Info::library() const
{
    return d->m_library;
}


Plugin* PluginLoader::Info::plugin() const
{
    return d->m_plugin;
}


void PluginLoader::Info::setPlugin(Plugin* plugin)
{
    d->m_plugin=plugin;
}


bool PluginLoader::Info::shouldLoad() const
{
    return d->m_shouldLoad;
}


void PluginLoader::Info::setShouldLoad(bool value)
{
    d->m_shouldLoad=value;
}


//---------------------------------------------------------------------
//
// PluginLoader
//
//---------------------------------------------------------------------
static PluginLoader* s_instance = 0;


struct PluginLoader::Private
{
    PluginList m_pluginList;
    Interface* m_interface;
    TQStringList m_ignores;
};


PluginLoader::PluginLoader( const TQStringList& ignores, Interface* interface )
{
    Q_ASSERT( s_instance == 0 );
    s_instance = this;

    d=new Private;
    d->m_interface = interface;
    d->m_ignores = ignores;

    KTrader::OfferList offers = KTrader::self()->query("KIPI/Plugin");
    TDEConfig* config = TDEGlobal::config();
    config->setGroup( TQString::fromLatin1( "KIPI/EnabledPlugin" ) );

    KTrader::OfferList::ConstIterator iter;
    for(iter = offers.begin(); iter != offers.end(); ++iter) {

        KService::Ptr service = *iter;
        TQString name    = service->name();
        TQString comment = service->comment();
        TQString library = service->library();
        TQStringList reqFeatures = service->property( TQString::fromLatin1( "X-KIPI-ReqFeatures" ) ).toStringList();

        if (library.isEmpty() || name.isEmpty() ) {
            kdWarning( 51001 ) << "KIPI::PluginLoader: Plugin had an empty name or library file - this should not happen." << endl;
            continue;
        }

        if ( d->m_ignores.contains( name ) ) {
            kdDebug( 51001 ) << "KIPI::PluginLoader: plugin " << name << " is in the ignore list for host application" << endl;
            continue;
        }

        bool appHasAllReqFeatures=true;
        for( TQStringList::Iterator featureIt = reqFeatures.begin(); featureIt != reqFeatures.end(); ++featureIt ) {
            if ( !d->m_interface->hasFeature( *featureIt ) ) {
                kdDebug( 51001 ) << "Plugin " << name << " was not loaded because the host application is missing\n"
                                 << "the feature " << *featureIt << endl;
				appHasAllReqFeatures=false;
				break;
            }
        }

        bool load = config->readBoolEntry( name, true );

        if (!appHasAllReqFeatures)
            continue;

        Info* info = new Info( name, comment, library, load );
        d->m_pluginList.append( info );
    }
}

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

void PluginLoader::loadPlugins()
{
    for( PluginList::Iterator it = d->m_pluginList.begin(); it != d->m_pluginList.end(); ++it ) {
        loadPlugin( *it );
    }
    emit replug();
}

void PluginLoader::loadPlugin( Info* info )
{
    if ( info->plugin() == 0 && info->shouldLoad() ) {
        Plugin *plugin = 0;
        int error;
        plugin =  KParts::ComponentFactory
                  ::createInstanceFromLibrary<Plugin>(info->library().local8Bit().data(),
                                                      d->m_interface, 0, TQStringList(), &error);

        if (plugin)
            kdDebug( 51001 ) << "KIPI::PluginLoader: Loaded plugin " << plugin->name()<< endl;
        else
        {
            kdWarning( 51001 ) << "KIPI::PluginLoader:: createInstanceFromLibrary returned 0 for "
                               << info->name()
                               << " (" << info->library() << ")"
                               << " with error number "
                               << error << endl;
            if (error == KParts::ComponentFactory::ErrNoLibrary)
                kdWarning( 51001 ) << "KLibLoader says: "
                                   << KLibLoader::self()->lastErrorMessage() << endl;
        }
        info->setPlugin(plugin);
    }
    if ( info->plugin() ) // Do not emit if we had trouble loading the plugin.
        emit PluginLoader::instance()->plug( info );
}


const PluginLoader::PluginList& PluginLoader::pluginList()
{
    return d->m_pluginList;
}

PluginLoader* PluginLoader::instance()
{
    Q_ASSERT( s_instance != 0);
    return s_instance;
}


//---------------------------------------------------------------------
//
// ConfigWidget
//
//---------------------------------------------------------------------
ConfigWidget* PluginLoader::configWidget( TQWidget* parent )
{
    return new ConfigWidget( parent );
}


class PluginCheckBox :public TQCheckBox
{
public:
    PluginCheckBox( PluginLoader::Info* info, TQWidget* parent )
        : TQCheckBox( info->comment(), parent ), info( info )
        {
            setChecked( info->shouldLoad() );
        }
    PluginLoader::Info* info;
};


struct ConfigWidget::Private
{
    TQValueList< PluginCheckBox* > _boxes;
};


ConfigWidget::ConfigWidget( TQWidget* parent )
    :TQScrollView( parent, "KIPI::PluginLoader::ConfigWidget" )
{
    d=new Private;
    TQWidget* top = new TQWidget( viewport() );
    addChild( top );
    setResizePolicy( AutoOneFit );

    TQVBoxLayout* lay = new TQVBoxLayout( top, KDialog::marginHint(), KDialog::spacingHint() );

    PluginLoader::PluginList list = PluginLoader::instance()->d->m_pluginList;
    for( PluginLoader::PluginList::Iterator it = list.begin(); it != list.end(); ++it ) {
        PluginCheckBox* cb = new PluginCheckBox( *it, top );
        lay->addWidget( cb );
        d->_boxes.append( cb );
    }

    lay->addStretch(10);
}


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


void ConfigWidget::apply()
{
    TDEConfig* config = TDEGlobal::config();
    config->setGroup( TQString::fromLatin1( "KIPI/EnabledPlugin" ) );
    bool changes = false;

    for( TQValueList<PluginCheckBox*>::Iterator it = d->_boxes.begin(); it != d->_boxes.end(); ++it ) {
        bool orig = (*it)->info->shouldLoad();
        bool load = (*it)->isChecked();
        if ( orig != load ) {
            changes = true;
            config->writeEntry( (*it)->info->name(), load );
            (*it)->info->setShouldLoad(load);
            if ( load ) {
                PluginLoader::instance()->loadPlugin( (*it)->info);
            }
            else {
                if ( (*it)->info->plugin() ) // Do not emit if we had trouble loading plugin.
                    emit PluginLoader::instance()->unplug( (*it)->info);
            }
        }
    }
    emit PluginLoader::instance()->replug();
}


} // namespace

#include "pluginloader.moc"