~ubuntu-branches/ubuntu/hoary/kvirc/hoary

« back to all changes in this revision

Viewing changes to src/kvirc/kvi_plugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2004-12-14 15:32:19 UTC
  • mfrom: (0.2.1 upstream) (1.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041214153219-fdink3gyp2s20b6g
Tags: 2:2.1.3.1-2
* Change Recommends on xmms to a Suggests.
* Rebuild against KDE 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//   File : kvi_plugin.cpp (/usr/build/KVIrc/kvirc/src/kvilib/kvi_plugin.cpp)
3
 
//   Last major modification : Wed Jul 21 1999 16:41:14 by Szymon Stefanek
4
 
//
5
 
//   This file is part of the KVirc irc client distribution
 
1
// =============================================================================
 
2
//
 
3
//      --- kvi_plugin.cpp ---
 
4
//
 
5
//   This file is part of the KVIrc IRC client distribution
6
6
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
7
7
//
8
8
//   This program is FREE software. You can redistribute it and/or
17
17
//
18
18
//   You should have received a copy of the GNU General Public License
19
19
//   along with this program. If not, write to the Free Software Foundation,
20
 
//   Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
//   Inc, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
21
//
 
22
// =============================================================================
 
23
 
22
24
#define _KVI_DEBUG_CHECK_RANGE_
23
 
#include "kvi_debug.h"
 
25
#define _KVI_DEBUG_CLASS_NAME_ "KviPlugin"
24
26
 
25
 
#include "kvi_plugin.h"
 
27
#include "kvi_settings.h"
26
28
 
27
29
#ifdef COMPILE_PLUGIN_SUPPORT
28
30
 
29
 
#include "kvi_locale.h"
 
31
#include <dlfcn.h>
 
32
 
 
33
#include <qobjectlist.h>
 
34
 
 
35
#include "kvi_app.h"
30
36
#include "kvi_config.h"
 
37
#include "kvi_debug.h"
 
38
#include "kvi_defines.h"
 
39
#include "kvi_dockablewidget.h"
31
40
#include "kvi_event.h"
32
 
#include "kvi_app.h"
33
41
#include "kvi_frame.h"
 
42
#include "kvi_locale.h"
 
43
#include "kvi_plugin.h"
 
44
#include "kvi_statusbar.h"
 
45
#include "kvi_systray.h"
 
46
#include "kvi_systray_bar.h"
 
47
#include "kvi_systray_widget.h"
34
48
#include "kvi_window.h"
35
 
#include "kvi_style.h"
36
 
#include "kvi_statusbar.h"
37
 
#include "kvi_systray.h"
38
 
 
39
 
extern KviEventManager * g_pEventManager;
40
 
 
41
 
#include <dlfcn.h>
42
 
 
43
 
#include <qobjcoll.h>
44
 
#include <qobjectdict.h>
45
 
#include <qmetaobject.h>
46
 
 
47
 
 
48
 
#include "kvi_statusbar.h"
 
49
 
 
50
// From kvi_app.cpp
 
51
extern KviEventManager *g_pEventManager;
49
52
 
50
53
KviPluginManager::KviPluginManager()
51
54
{
52
 
        m_pPluginList = new QList<KviPluginData>;
 
55
        m_pPluginList   = new QPtrList<KviPluginData>;
53
56
        m_pPluginList->setAutoDelete(true);
54
 
        m_pCommandList = new QList<KviPluginCommandHandler>;
 
57
        m_pCommandList  = new QPtrList<KviPluginCommandHandler>;
55
58
        m_pCommandList->setAutoDelete(true);
56
 
        m_pFunctionList = new QList<KviPluginFunctionHandler>;
 
59
        m_pFunctionList = new QPtrList<KviPluginFunctionHandler>;
57
60
        m_pFunctionList->setAutoDelete(true);
58
61
}
59
62
 
60
63
KviPluginManager::~KviPluginManager()
61
64
{
62
 
        while(m_pPluginList->first())unloadPlugin(m_pPluginList->first()->filename.ptr());
 
65
        while( m_pPluginList->first() )
 
66
                unloadPlugin(m_pPluginList->first()->filename.ptr());
63
67
        delete m_pPluginList;
 
68
        m_pPluginList = 0;
64
69
        __range_valid(m_pCommandList->count() == 0);
65
70
        delete m_pCommandList;
 
71
        m_pCommandList = 0;
66
72
        __range_valid(m_pFunctionList->count() == 0);
67
73
        delete m_pFunctionList;
68
 
}
69
 
 
70
 
//#include <qasciidict.h>
71
 
 
72
 
void KviPluginManager::unregisterMetaObject(const char * metaObjName)
73
 
{
74
 
        if(!objectDict)return;
75
 
        QMetaObject * ob = objectDict->find(metaObjName);
76
 
        if(ob)
77
 
        {
78
 
//              debug("QMetaObject(\"%s\") found...destroying",metaObjName);
79
 
                // WARNING: HACK for a Qt bug
80
 
                // ob->d->slotAccess is never zeroed , and we get sigsegvs here..
81
 
 
82
 
// mmmmmh...some versions of qt seem to not have it ? (set_slot_access)
83
 
 
84
 
                ob->set_slot_access(0);
85
 
                objectDict->remove(metaObjName);
86
 
//              debug("QMetaObject(\"%s\") destroyed",metaObjName);
87
 
 
88
 
        }
89
 
}
90
 
 
 
74
        m_pFunctionList = 0;
 
75
}
91
76
 
92
77
void KviPluginManager::getLastError(KviStr &buffer)
93
78
{
94
79
        buffer = dlerror();
95
80
}
96
81
 
97
 
KviPluginData * KviPluginManager::loadPlugin(const char * fName,bool bAutoload,KviStr * pErrorRet)
 
82
KviPluginData *KviPluginManager::loadPlugin(const char *fName, bool bAutoload, KviStr *pErrorRet)
98
83
{
99
 
//      unloadPlugin(fName); //unload the file with the same name...
100
 
        KviPluginData * d = findPlugin(fName);
101
 
        if(d)return d; //already there
102
 
 
103
 
        void * handle = dlopen(fName,RTLD_NOW);
104
 
 
105
 
        if(!handle){
106
 
                if(pErrorRet)pErrorRet->sprintf(__tr("dlopen error: %s"),dlerror());
 
84
        KviPluginData *d = findPlugin(fName);
 
85
        if( d )
 
86
                return d; // Already there
 
87
 
 
88
        void *handle = dlopen(fName, RTLD_NOW);
 
89
        if( !handle ) {
 
90
                if( pErrorRet )
 
91
                        pErrorRet->sprintf(_i18n_("dlopen() error: %s"), dlerror());
107
92
                return 0;
108
93
        }
109
94
 
110
 
        KviPlugin * description = (KviPlugin *)dlsym(handle,"kvirc_plugin");
111
 
        if(!description){
112
 
                // not a kvirc plugin
113
 
                if(pErrorRet)*pErrorRet = __tr("Plugin loaded but exports no 'kvirc_plugin' symbol");
 
95
        KviPlugin *description = (KviPlugin *) dlsym(handle, "kvirc_plugin");
 
96
        if( !description ) {
 
97
                // Not a KVIrc plugin
 
98
                if( pErrorRet )
 
99
                        *pErrorRet = _i18n_("Plugin loaded but exports no 'kvirc_plugin' symbol");
114
100
                dlclose(handle);
115
101
                return 0;
116
102
        }
117
103
 
118
 
        d = new KviPluginData;
119
 
        d->filename = fName;
120
 
        d->handle = handle;
121
 
        d->description = description;
122
 
        d->bAutoload = bAutoload;
 
104
        d = new KviPluginData();
 
105
        d->filename      = fName;
 
106
        d->handle        = handle;
 
107
        d->description   = description;
 
108
        d->bAutoload     = bAutoload;
123
109
        d->bInitExecuted = false;
124
110
 
125
111
        m_pPluginList->append(d);
127
113
        return d;
128
114
}
129
115
 
130
 
KviPluginData * KviPluginManager::findPlugin(const char * fName)
131
 
{
132
 
        for(KviPluginData *d=m_pPluginList->first();d;d=m_pPluginList->next()){
133
 
                if(kvi_strEqualCI(fName,d->filename.ptr()))return d;
134
 
        }
135
 
        return 0;
136
 
}
137
 
 
138
 
KviPluginData * KviPluginManager::findPluginByModuleName(const char * name)
139
 
{
140
 
        for(KviPluginData *d=m_pPluginList->first();d;d=m_pPluginList->next()){
141
 
                if(kvi_strEqualCI(name,d->description->module_name))return d;
142
 
        }
143
 
        return 0;
144
 
}
145
 
 
146
 
KviPluginData * KviPluginManager::findPlugin(void * plugin_handle)
147
 
{
148
 
        for(KviPluginData *d=m_pPluginList->first();d;d=m_pPluginList->next()){
149
 
                if(d->handle == plugin_handle)return d;
150
 
        }
151
 
        return 0;
152
 
}
153
 
 
154
 
bool KviPluginManager::executeInitRoutine(KviPluginData * data,KviPluginCommandStruct * cmd)
 
116
KviPluginData *KviPluginManager::findPlugin(const char *fName)
 
117
{
 
118
        for( KviPluginData *d = m_pPluginList->first(); d; d = m_pPluginList->next() ) {
 
119
                if( kvi_strEqualCI(fName, d->filename.ptr()) )
 
120
                        return d;
 
121
        }
 
122
        return 0;
 
123
}
 
124
 
 
125
KviPluginData *KviPluginManager::findPluginByModuleName(const char *name)
 
126
{
 
127
        for( KviPluginData *d = m_pPluginList->first(); d; d = m_pPluginList->next() ) {
 
128
                if( kvi_strEqualCI(name, d->description->module_name) )
 
129
                        return d;
 
130
        }
 
131
        return 0;
 
132
}
 
133
 
 
134
KviPluginData *KviPluginManager::findPlugin(void *plugin_handle)
 
135
{
 
136
        for( KviPluginData *d = m_pPluginList->first(); d; d = m_pPluginList->next() ) {
 
137
                if( d->handle == plugin_handle )
 
138
                        return d;
 
139
        }
 
140
        return 0;
 
141
}
 
142
 
 
143
bool KviPluginManager::executeInitRoutine(KviPluginData *data, KviPluginCommandStruct *cmd)
155
144
{
156
145
        __range_valid(data);
157
146
        __range_valid(findPlugin(data->filename.ptr()));
158
 
        if(data->bInitExecuted)return true; //already done with success!
159
 
        data->bInitExecuted = (data->description->init_routine)(cmd);
 
147
        if( data->bInitExecuted )
 
148
                return true; // Already successfully done!
 
149
        data->bInitExecuted = (data->description->init_routine) (cmd);
160
150
        return data->bInitExecuted;
161
151
}
162
152
 
163
 
bool KviPluginManager::unloadPlugin(const char * fName)
164
 
{
165
 
        KviPluginData * d = findPlugin(fName);
166
 
        if(!d)return false;
167
 
        unloadPluginInternal(d);
168
 
        return true;
169
 
}
170
 
 
171
 
bool KviPluginManager::unloadPluginByModuleName(const char * mName)
172
 
{
173
 
        KviPluginData * d = findPluginByModuleName(mName);
174
 
        if(!d)return false;
175
 
        unloadPluginInternal(d);
176
 
        return true;    
177
 
}
178
 
/*
179
 
bool KviPluginManager::unloadPlugin(void * plugin_handle)
180
 
{
181
 
        KviPluginData * d = findPlugin(plugin_handle);
182
 
        if(!d)return false;
183
 
        unloadPluginInternal(d);
184
 
        return true;
185
 
}
186
 
*/
 
153
bool KviPluginManager::unloadPlugin(const char *fName)
 
154
{
 
155
        KviPluginData *d = findPlugin(fName);
 
156
        if( !d ) return false;
 
157
 
 
158
        unloadPluginInternal(d);
 
159
        return true;
 
160
}
 
161
 
 
162
bool KviPluginManager::unloadPluginByModuleName(const char *mName)
 
163
{
 
164
        KviPluginData *d = findPluginByModuleName(mName);
 
165
        if( !d )return false;
 
166
 
 
167
        unloadPluginInternal(d);
 
168
        return true;
 
169
}
 
170
 
187
171
void KviPluginManager::unloadPluginInternal(KviPluginData *d)
188
172
{
189
173
        __range_valid(d);
190
 
//      debug("Unloading plugin %s",d->filename.ptr());
191
 
        if(d->bInitExecuted)
192
 
        {
193
 
                if(d->description->cleanup_routine){
194
 
                        // call the cleanup routine
 
174
        if( d->bInitExecuted ) {
 
175
                if( d->description->cleanup_routine ) {
 
176
                        // Call the cleanup routine
195
177
                        (d->description->cleanup_routine)();
196
178
                }
197
179
                g_pApp->unregisterStylesFor(d->handle);
206
188
        m_pPluginList->removeRef(d);
207
189
}
208
190
 
209
 
void KviPluginManager::unregisterCommandsFor(void * plugin_handle)
 
191
void KviPluginManager::unregisterCommandsFor(void *plugin_handle)
210
192
{
211
 
        QList<KviPluginCommandHandler> l;
 
193
        QPtrList<KviPluginCommandHandler> l;
212
194
        l.setAutoDelete(false);
213
 
        // find all the handlers owned by this plugin
214
 
        for(KviPluginCommandHandler * c=m_pCommandList->first();c;c=m_pCommandList->next()){
215
 
                if(c->plugin_handle == plugin_handle)l.append(c);
 
195
        // Find all the handlers owned by this plugin
 
196
        for( KviPluginCommandHandler *c = m_pCommandList->first(); c; c = m_pCommandList->next() ) {
 
197
                if( c->plugin_handle == plugin_handle )
 
198
                        l.append(c);
216
199
        }
217
 
        // now kill it
218
 
        for(KviPluginCommandHandler * h=l.first();h;h=l.next())m_pCommandList->removeRef(h);
 
200
        // Now remove them
 
201
        for( KviPluginCommandHandler *h = l.first(); h; h = l.next() )
 
202
                m_pCommandList->removeRef(h);
219
203
}
220
204
 
221
 
void KviPluginManager::registerCommand(void * plugin_handle,const char * cmdname,
222
 
                        bool (*handler_routine)(KviPluginCommandStruct *))
 
205
void KviPluginManager::registerCommand(
 
206
        void *plugin_handle, const char *cmdname, bool (*handler_routine) (KviPluginCommandStruct *))
223
207
{
224
208
        __range_valid(findPlugin(plugin_handle));
225
 
        // kill any previous handler with this name
226
 
        KviPluginCommandHandler * d = findCommandHandler(cmdname);
227
 
        if(d)m_pCommandList->removeRef(d);
228
 
        // now add it
229
 
        d = new KviPluginCommandHandler;
 
209
        // Remove any previous handler with this name
 
210
        KviPluginCommandHandler *d = findCommandHandler(cmdname);
 
211
        if( d )
 
212
                m_pCommandList->removeRef(d);
 
213
        // Now add it
 
214
        d = new KviPluginCommandHandler();
230
215
        d->plugin_handle   = plugin_handle;
231
216
        d->handler_routine = handler_routine;
232
217
        d->cmd_name        = cmdname;
233
218
        m_pCommandList->append(d);
234
219
}
235
220
 
236
 
KviPluginCommandHandler * KviPluginManager::findCommandHandler(const char *cmdname)
 
221
KviPluginCommandHandler *KviPluginManager::findCommandHandler(const char *cmdname)
237
222
{
238
 
        for(KviPluginCommandHandler * c=m_pCommandList->first();c;c=m_pCommandList->next()){
239
 
                if(kvi_strEqualCI(c->cmd_name.ptr(),cmdname))return c;
 
223
        for( KviPluginCommandHandler *c = m_pCommandList->first(); c; c = m_pCommandList->next() ) {
 
224
                if( kvi_strEqualCI(c->cmd_name.ptr(), cmdname) )
 
225
                        return c;
240
226
        }
241
227
        return 0;
242
228
}
243
229
 
244
 
void KviPluginManager::unregisterCommand(void * plugin_handle,const char *cmdname)
 
230
void KviPluginManager::unregisterCommand(void *plugin_handle, const char *cmdname)
245
231
{
246
 
        KviPluginCommandHandler * c = findCommandHandler(cmdname);
247
 
        if(c){
 
232
        KviPluginCommandHandler *c = findCommandHandler(cmdname);
 
233
        if( c ) {
248
234
                __range_valid(c->plugin_handle = plugin_handle);
249
235
                m_pCommandList->removeRef(c);
250
236
        }
251
237
}
252
238
 
253
 
void KviPluginManager::registerFunction(void * plugin_handle,const char * fncname,
254
 
                        bool (*handler_routine)(KviPluginCommandStruct *,KviStr *))
 
239
void KviPluginManager::registerFunction(
 
240
        void *plugin_handle, const char *fncname, bool (*handler_routine) (KviPluginCommandStruct *, KviStr *))
255
241
{
256
242
        __range_valid(findPlugin(plugin_handle));
257
 
        // kill any previous handler with this name
258
 
        KviPluginFunctionHandler * d = findFunctionHandler(fncname);
259
 
        if(d)m_pFunctionList->removeRef(d);
260
 
        // now add it
261
 
        d = new KviPluginFunctionHandler;
 
243
        // Remove any previous handler with this name
 
244
        KviPluginFunctionHandler *d = findFunctionHandler(fncname);
 
245
        if( d )
 
246
                m_pFunctionList->removeRef(d);
 
247
        // Now add it
 
248
        d = new KviPluginFunctionHandler();
262
249
        d->plugin_handle   = plugin_handle;
263
250
        d->handler_routine = handler_routine;
264
251
        d->fnc_name        = fncname;
265
252
        m_pFunctionList->append(d);
266
253
}
267
254
 
268
 
void KviPluginManager::unregisterFunction(void * plugin_handle,const char *fncname)
 
255
void KviPluginManager::unregisterFunction(void *plugin_handle, const char *fncname)
269
256
{
270
 
        KviPluginFunctionHandler * c = findFunctionHandler(fncname);
271
 
        if(c){
 
257
        KviPluginFunctionHandler *c = findFunctionHandler(fncname);
 
258
        if( c ) {
272
259
                __range_valid(c->plugin_handle = plugin_handle);
273
260
                m_pFunctionList->removeRef(c);
274
261
        }
275
262
}
276
263
 
277
 
void KviPluginManager::unregisterFunctionsFor(void * plugin_handle)
 
264
void KviPluginManager::unregisterFunctionsFor(void *plugin_handle)
278
265
{
279
 
        QList<KviPluginFunctionHandler> l;
 
266
        QPtrList<KviPluginFunctionHandler> l;
280
267
        l.setAutoDelete(false);
281
 
        // find all the handlers owned by this plugin
282
 
        for(KviPluginFunctionHandler * c=m_pFunctionList->first();c;c=m_pFunctionList->next()){
283
 
                if(c->plugin_handle == plugin_handle)l.append(c);
 
268
        // Find all the handlers owned by this plugin
 
269
        for( KviPluginFunctionHandler *c = m_pFunctionList->first(); c; c = m_pFunctionList->next() ) {
 
270
                if( c->plugin_handle == plugin_handle )
 
271
                        l.append(c);
284
272
        }
285
 
        // now kill it
286
 
        for(KviPluginFunctionHandler * h=l.first();h;h=l.next())m_pFunctionList->removeRef(h);
 
273
        // Now remove them
 
274
        for( KviPluginFunctionHandler *h = l.first(); h; h = l.next() )
 
275
                m_pFunctionList->removeRef(h);
287
276
}
288
277
 
289
 
KviPluginFunctionHandler * KviPluginManager::findFunctionHandler(const char *fncname)
 
278
KviPluginFunctionHandler *KviPluginManager::findFunctionHandler(const char *fncname)
290
279
{
291
 
        for(KviPluginFunctionHandler * c=m_pFunctionList->first();c;c=m_pFunctionList->next()){
292
 
                if(kvi_strEqualCI(c->fnc_name.ptr(),fncname))return c;
 
280
        for( KviPluginFunctionHandler *c = m_pFunctionList->first(); c; c = m_pFunctionList->next() ) {
 
281
                if( kvi_strEqualCI(c->fnc_name.ptr(), fncname) )
 
282
                        return c;
293
283
        }
294
284
        return 0;
295
285
}
296
286
 
297
 
bool KviPluginManager::load(const char *configName,KviStr &error)
 
287
bool KviPluginManager::load(const char *configName, KviStr &error)
298
288
{
299
289
        error = "";
300
290
 
301
291
        KviConfig cfg(configName);
302
 
        int nPlugins = cfg.readIntEntry("nPlugins",0);
303
 
        for(int i=0;i<nPlugins;i++){
304
 
                KviStr tmp(KviStr::Format,"plugin%d",i);
305
 
                KviStr fName = cfg.readEntry(tmp.ptr(),"");
306
 
                if(fName.hasData()){
307
 
                        KviPluginData * d = loadPlugin(fName.ptr());
308
 
                        if(!d){
 
292
        int nPlugins = cfg.readIntEntry("nPlugins", 0);
 
293
        for( int i = 0; i < nPlugins; i++ ) {
 
294
                KviStr tmp(KviStr::Format, "plugin%d", i);
 
295
                KviStr fName = cfg.readEntry(tmp.ptr(), "");
 
296
                if( fName.hasData() ) {
 
297
                        KviPluginData *d = loadPlugin(fName.ptr());
 
298
                        if( !d ) {
309
299
                                error.append("Could not load plugin ");
310
300
                                error.append(fName);
311
301
                                error.append('\n');
321
311
                                plgcmd.sock    = 0;
322
312
                                plgcmd.console = 0;
323
313
 
324
 
                                if(!executeInitRoutine(d,&plgcmd)){
 
314
                                if( !executeInitRoutine(d, &plgcmd) ) {
325
315
                                        error.append("Could not initialize plugin ");
326
316
                                        error.append(fName);
327
317
                                        error.append('\n');
337
327
{
338
328
        KviConfig cfg(configName);
339
329
        cfg.clear();
 
330
        cfg.writeEntry("nPlugins", m_pPluginList->count());
340
331
        int nPlugins = 0;
341
 
        for(KviPluginData *d=m_pPluginList->first();d;d=m_pPluginList->next()){
342
 
                if(d->bAutoload){
343
 
                        KviStr tmp(KviStr::Format,"plugin%d",nPlugins);
344
 
                        cfg.writeEntry(tmp.ptr(),d->filename.ptr());
 
332
        for( KviPluginData *d = m_pPluginList->first(); d; d = m_pPluginList->next() ) {
 
333
                if( d->bAutoload ) {
 
334
                        KviStr tmp(KviStr::Format, "plugin%d", nPlugins);
 
335
                        cfg.writeEntry(tmp.ptr(), d->filename.ptr());
345
336
                        nPlugins++;
346
337
                }
347
338
        }
348
 
        cfg.writeEntry("nPlugins",nPlugins);
349
339
        return cfg.sync();
350
340
}
351
341
 
352
 
void KviPluginManager::registerHook(void * plugin_handle,int eventindex,
353
 
                        bool (*handler_routine)(KviPluginCommandStruct *))
 
342
void KviPluginManager::registerHook(
 
343
        void *plugin_handle, int eventindex, bool (*handler_routine) (KviPluginCommandStruct *))
354
344
{
355
345
        __range_valid(findPlugin(plugin_handle));
356
 
        if(isHookRegistered(plugin_handle,eventindex)){
357
 
                unregisterHook(plugin_handle,eventindex); // kill the previous hook that belongs to this plugin
358
 
        }
 
346
        if( isHookRegistered(plugin_handle, eventindex) )
 
347
                unregisterHook(plugin_handle, eventindex); // Remove the previous hook that belongs to this plugin
359
348
 
360
 
        KviPluginEventHandlerStruct * s= new KviPluginEventHandlerStruct;
 
349
        KviPluginEventHandlerStruct *s = new KviPluginEventHandlerStruct();
361
350
        s->handler_routine = handler_routine;
362
 
        s->plugin_handle = plugin_handle;
 
351
        s->plugin_handle   = plugin_handle;
363
352
 
364
 
        if(!g_pEventManager->m_pEventHandlerList[eventindex]){
365
 
                g_pEventManager->m_pEventHandlerList[eventindex] = new QList<KviPluginEventHandlerStruct>;
 
353
        if( !g_pEventManager->m_pEventHandlerList[eventindex] ) {
 
354
                g_pEventManager->m_pEventHandlerList[eventindex] = new QPtrList<KviPluginEventHandlerStruct>;
366
355
                g_pEventManager->m_pEventHandlerList[eventindex]->setAutoDelete(true);
367
356
        }
368
357
        g_pEventManager->m_pEventHandlerList[eventindex]->append(s);
369
358
}
370
359
 
371
 
bool KviPluginManager::isHookRegistered(void * plugin_handle,int eventindex)
 
360
bool KviPluginManager::isHookRegistered(void *plugin_handle, int eventindex)
372
361
{
373
 
        if(!g_pEventManager->m_pEventHandlerList[eventindex])return false; //no hooks registered        
374
 
        for(KviPluginEventHandlerStruct *s=g_pEventManager->m_pEventHandlerList[eventindex]->first();s;s=g_pEventManager->m_pEventHandlerList[eventindex]->next()){
375
 
                if(s->plugin_handle == plugin_handle)return true;
 
362
        if( !g_pEventManager->m_pEventHandlerList[eventindex] )
 
363
                return false; // No hooks registered
 
364
 
 
365
        for( KviPluginEventHandlerStruct *s = g_pEventManager->m_pEventHandlerList[eventindex]->first()
 
366
           ; s
 
367
           ; s = g_pEventManager->m_pEventHandlerList[eventindex]->next()
 
368
        ) {
 
369
                if( s->plugin_handle == plugin_handle )
 
370
                        return true;
376
371
        }
377
372
        return false;
378
373
}
379
374
 
380
 
void KviPluginManager::unregisterHook(void * plugin_handle,int eventindex)
 
375
void KviPluginManager::unregisterHook(void *plugin_handle, int eventindex)
381
376
{
382
377
        __range_valid(findPlugin(plugin_handle));
383
 
 
384
 
        if(!g_pEventManager->m_pEventHandlerList[eventindex])return; //no hooks registered
385
 
 
386
 
        KviPluginEventHandlerStruct *s=g_pEventManager->m_pEventHandlerList[eventindex]->first();
387
 
        while(s){
388
 
                if(s->plugin_handle == plugin_handle){
 
378
        if( !g_pEventManager->m_pEventHandlerList[eventindex] )
 
379
                return; // No hooks registered
 
380
 
 
381
        KviPluginEventHandlerStruct *s = g_pEventManager->m_pEventHandlerList[eventindex]->first();
 
382
        while( s ) {
 
383
                if( s->plugin_handle == plugin_handle ) {
389
384
                        g_pEventManager->m_pEventHandlerList[eventindex]->removeRef(s);
390
385
                        s = 0;
391
 
                } else s = g_pEventManager->m_pEventHandlerList[eventindex]->next();
 
386
                } else
 
387
                        s = g_pEventManager->m_pEventHandlerList[eventindex]->next();
392
388
        }
393
389
 
394
 
        if(g_pEventManager->m_pEventHandlerList[eventindex]->count() == 0){
395
 
                // and cleanup if the list is empty
 
390
        if( g_pEventManager->m_pEventHandlerList[eventindex]->count() == 0 ) {
 
391
                // And cleanup if the list is empty
396
392
                delete g_pEventManager->m_pEventHandlerList[eventindex];
397
393
                g_pEventManager->m_pEventHandlerList[eventindex] = 0;
398
394
        }
399
395
}
400
396
 
401
 
void KviPluginManager::unregisterHooksFor(void * plugin_handle)
 
397
void KviPluginManager::unregisterHooksFor(void *plugin_handle)
402
398
{
403
 
        for(int i=0;i<KVI_NUM_EVENTS;i++)unregisterHook(plugin_handle,i);
 
399
        for( int i = 0; i < KVI_NUM_EVENTS; i++ )
 
400
                unregisterHook(plugin_handle, i);
404
401
}
405
402
 
406
 
void KviPluginManager::addPluginWindow(void * plugin_handle,KviFrame * frame,KviWindow *wnd,bool bShow)
 
403
void KviPluginManager::addPluginWindow(void *plugin_handle, KviFrame *frame, KviWindow *wnd, bool bShow)
407
404
{
408
 
        if(wnd->type() != KVI_WND_TYPE_PLUGIN){
409
 
                wnd->m_type = KVI_WND_TYPE_PLUGIN; //fix silently
 
405
        if( wnd->type() != KVI_WND_TYPE_PLUGIN ) {
 
406
                wnd->m_type = KVI_WND_TYPE_PLUGIN; // Fix silently
410
407
#ifdef _KVI_DEBUG_CHECK_RANGE_
411
 
                debug("The window exported by the plugin is not of type KVI_WND_TYPE_PLUGIN : fixing");
 
408
                debug("The window exported by the plugin is not of type KVI_WND_TYPE_PLUGIN: fixing");
412
409
#endif
413
410
        }
414
411
        KviStr tmp = wnd->caption();
415
 
        if(!kvi_strEqualCIN("!",tmp.ptr(),1)){
 
412
        if( !kvi_strEqualCIN("!", tmp.ptr(), 1) ) {
416
413
                tmp.prepend("!");
417
414
                wnd->setWindowCaption(tmp.ptr());
418
415
                wnd->setName(tmp.ptr());
419
416
#ifdef _KVI_DEBUG_CHECK_RANGE_
420
 
                debug("The window exported by the plugn has a wrong name : fixing");
 
417
                debug("The window exported by the plugin has a wrong name: fixing");
421
418
#endif
422
419
        }
423
420
        wnd->m_pluginHandle = plugin_handle;
424
 
        frame->addWindow(wnd,bShow);
 
421
        frame->addWindow(wnd, bShow);
425
422
}
426
423
 
427
 
void KviPluginManager::removeAllPluginWindows(void * plugin_handle)
 
424
void KviPluginManager::removeAllPluginWindows(void *plugin_handle)
428
425
{
429
 
        if(!g_pApp->m_pFrameList)return; //shutdown...no more windows here.
430
 
        for(KviFrame * f=g_pApp->m_pFrameList->first();f;f=g_pApp->m_pFrameList->next()){
431
 
                QList<KviWindow> l;
 
426
        if( !g_pApp->m_pFrameList )
 
427
                return; // Shut down... no more windows here.
 
428
 
 
429
        for( KviFrame *f = g_pApp->m_pFrameList->first(); f; f = g_pApp->m_pFrameList->next() ) {
 
430
                QPtrList<KviWindow> l;
432
431
                l.setAutoDelete(false);
433
 
                for(KviWindow *wnd=f->m_pWinList->first();wnd;wnd=f->m_pWinList->next()){
434
 
                        if(wnd->type() == KVI_WND_TYPE_PLUGIN){
435
 
                                if(wnd->m_pluginHandle == plugin_handle)l.append(wnd);
 
432
                for( KviWindow *wnd = f->m_pWinList->first(); wnd; wnd = f->m_pWinList->next() ) {
 
433
                        if( wnd->type() == KVI_WND_TYPE_PLUGIN ) {
 
434
                                if( wnd->m_pluginHandle == plugin_handle )
 
435
                                        l.append(wnd);
436
436
                        }
437
437
                }
438
 
                for(KviWindow *w=l.first();w;w=l.next())f->closeWindow(w);      
 
438
                for( KviWindow *w = l.first(); w; w = l.next() )
 
439
                        f->closeWindow(w);
439
440
        }
440
441
}
441
442
 
442
 
void KviPluginManager::addPluginDockedWidget(void * plugin_handle,KviFrame * frame,KviDockableWidget * w)
 
443
void KviPluginManager::addPluginDockedWidget(void *plugin_handle, KviFrame *frame, KviDockableWidget *w)
443
444
{
444
445
        w->m_plugin_handle = plugin_handle;
445
 
        frame->m_pStatusBar->addWidget(w,0,true);
446
 
/*
447
 
        const QObjectList * l = frame->m_pStatusBar->children();
448
 
        QObjectListIt it(*l);
449
 
        QObject *o;
450
 
        while((o=it.current())){
451
 
                KviStr tmp =o->className();
452
 
                if(kvi_strEqualCI(tmp.ptr(),"QSizeGrip")){
453
 
                        ((QWidget *)o)->setMaximumWidth(((QWidget *)o)->sizeHint().width());
454
 
                }
455
 
                ++it;
456
 
        }
457
 
        delete l;
458
 
*/
 
446
        frame->m_pStatusBar->addWidget(w, 0, true);
459
447
}
460
448
 
461
 
void KviPluginManager::removeAllPluginDockedWidgets(void * plugin_handle)
 
449
void KviPluginManager::removeAllPluginDockedWidgets(void *plugin_handle)
462
450
{
463
 
        if(!g_pApp->m_pFrameList)return; //shutdown...no more windows here.
464
 
        QList<KviDockableWidget> * list = new QList<KviDockableWidget>;
 
451
        if( !g_pApp->m_pFrameList )
 
452
                return; // Shut down... no more windows here.
 
453
 
 
454
        QPtrList<KviDockableWidget> *list = new QPtrList<KviDockableWidget>;
465
455
        list->setAutoDelete(true);
466
 
        for(KviFrame * f=g_pApp->m_pFrameList->first();f;f=g_pApp->m_pFrameList->next()){
467
 
        //      f->m_pStatusBar->killDockedWidgetsByPluginHandle(plugin_handle);
468
 
                QObjectList * l = f->m_pStatusBar->queryList("KviDockableWidget");
469
 
                l->setAutoDelete(false); //!!!
 
456
        for( KviFrame *f = g_pApp->m_pFrameList->first(); f; f = g_pApp->m_pFrameList->next() ) {
 
457
                QObjectList *l = f->m_pStatusBar->queryList("KviDockableWidget");
 
458
                l->setAutoDelete(false);
470
459
                QObjectListIt it(*l);
471
460
                QObject *o;
472
 
                while((o=it.current())){
473
 
                        if(((KviDockableWidget *)o)->m_plugin_handle == plugin_handle){
474
 
                                f->m_pStatusBar->removeWidget((QWidget *)o);
475
 
                                list->append((KviDockableWidget *)o);
 
461
                while( (o = it.current()) ) {
 
462
                        if( ((KviDockableWidget *) o)->m_plugin_handle == plugin_handle ) {
 
463
                                f->m_pStatusBar->removeWidget((QWidget *) o);
 
464
                                list->append((KviDockableWidget *) o);
476
465
                        }
477
466
                        ++it;
478
467
                }
479
468
                delete l;
480
 
 
481
469
        }
482
470
        delete list;
483
471
}
484
472
 
485
 
void KviPluginManager::addPluginSysTrayWidget(void * plugin_handle,KviFrame * frame,KviSysTrayWidget * w, bool _show)
 
473
void KviPluginManager::addPluginSysTrayWidget(void *plugin_handle, KviFrame *frame, KviSysTrayWidget *w, bool show)
486
474
{
487
475
        w->m_plugin_handle = plugin_handle;
488
 
        frame->m_pSysTrayBar->m_pSysTray->addPluginWidget(w, _show);
 
476
        frame->m_pSysTrayBar->m_pSysTray->addPluginWidget(w, show);
489
477
}
490
478
 
491
 
void KviPluginManager::removeAllPluginSysTrayWidgets(void * plugin_handle)
 
479
void KviPluginManager::removeAllPluginSysTrayWidgets(void *plugin_handle)
492
480
{
493
 
        if(!g_pApp->m_pFrameList)return; //shutdown...no more windows here.
494
 
        QList<KviSysTrayWidget> * list = new QList<KviSysTrayWidget>;
 
481
        if( !g_pApp->m_pFrameList )
 
482
                return; // Shut down... no more windows here.
 
483
 
 
484
        QPtrList<KviSysTrayWidget> *list = new QPtrList<KviSysTrayWidget>;
495
485
        list->setAutoDelete(true);
496
 
        for(KviFrame * f=g_pApp->m_pFrameList->first();f;f=g_pApp->m_pFrameList->next()){
497
 
        //      f->m_pStatusBar->killDockedWidgetsByPluginHandle(plugin_handle);
 
486
        for( KviFrame *f = g_pApp->m_pFrameList->first(); f; f = g_pApp->m_pFrameList->next() ) {
498
487
                f->m_pSysTrayBar->m_pSysTray->killPluginWidgets(plugin_handle);
499
488
        }
500
489
        delete list;
501
490
}
502
 
 
503
 
#else //!COMPILE_PLUGIN_SUPPORT
504
 
 
 
491
#else // COMPILE_PLUGIN_SUPPORT
505
492
        #warning "Plugin support is disabled"
506
 
 
507
493
#endif