KVIrc  4.9.2
DeveloperAPIs
KviModule.h
Go to the documentation of this file.
1 #ifndef _KVI_MODULE_H_
2 #define _KVI_MODULE_H_
3 
4 //=============================================================================
5 //
6 // File : KviModule.h
7 // Creation date : Sat Aug 12 2000 18:34:22 by Szymon Stefanek
8 //
9 // This file is part of the KVIrc IRC client distribution
10 // Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
11 //
12 // This program is FREE software. You can redistribute it and/or
13 // modify it under the terms of the GNU General Public License
14 // as published by the Free Software Foundation; either version 2
15 // of the License, or (at your option) any later version.
16 //
17 // This program is distributed in the HOPE that it will be USEFUL,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 // See the GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program. If not, write to the Free Software Foundation,
24 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 //=============================================================================
27 
28 #include "kvi_settings.h"
29 
30 #include "KviModuleExtension.h"
31 #include "KviKvsModuleInterface.h"
32 
33 template <typename Key, typename T>
35 
36 #ifdef COMPILE_CRYPT_SUPPORT
37 class KviCryptEngine;
38 class KviCryptEngineDescription;
39 class KviCryptEngineManager;
40 #endif
41 
42 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
43 #if defined(COMPILE_ON_MINGW)
44 #define KVIMODULEEXPORT extern "C" __declspec(dllexport) __attribute__((visibility("default")))
45 #else
46 #define KVIMODULEEXPORT extern "C" __declspec(dllexport)
47 #endif
48 #define KVIMODULEEXPORTDATA KVIMODULEEXPORT
49 #define KVIMODULEEXPORTFUNC KVIMODULEEXPORT
50 #else
51 #define KVIMODULEEXPORT extern "C" __attribute__((visibility("default")))
52 #define KVIMODULEEXPORTDATA __attribute__((visibility("default")))
53 #define KVIMODULEEXPORTFUNC KVIMODULEEXPORT
54 #endif
55 
56 class KviModule;
57 class KviCString;
58 class QLibrary;
59 
60 typedef bool (*KviModuleSystemRoutine)(KviModule *);
61 typedef bool (*KviModuleCtrlRoutine)(KviModule *, const char *, void *);
62 
63 typedef struct _KviModuleInfo
64 {
65  const char * szKVIrcVersion; // must match KVI_VERSION if module version checking is in force
66  const char * szModuleName; // module name
67  const char * szModuleContext; // name of the translation catalogue (if any)
68  const char * szVersion;
69  const char * szCopyright;
70  const char * szDescription;
71  /*
72  * This routine is called when the module is loaded
73  */
75  /*
76  * This should return true if the module is actually
77  * not providing any service and can be unloaded from memory.
78  * Please note that this is not a mandatory lock: KVIrc may still unload
79  * the module even when this function returns false.
80  * The only assumption you can make is that KVIrc will not try
81  * to unload the module spontaneously: it will do it only if forced to
82  * (actually only by the user, but maybe later also by constrained resources).
83  * If this pointer is zero, KVIrc will assume that the module
84  * does not provide any service and will unload the module at the
85  * first spontaneous cleanup.
86  * There is a yet stronger locking method in KviModule::lock()
87  */
89  /*
90  * This is a generic control routine with prototype
91  * bool <name>(KviModule * m,const char * operation,void * param)
92  * KVIrc uses it to comunicate with bundled modules
93  * in most user-build modules this will be 0
94  */
96  /*
97  * This routine is called when the module is being unloaded
98  * Note that the module can be unloaded even if can_unload returns false:
99  * that's the user choice, KVIrc can only forcibly unload the module,
100  * so better cleanup everything here :)
101  */
102  KviModuleSystemRoutine cleanup_routine; // WARNING : g_pApp may be in the destructor and may have no frames open!
103 } KviModuleInfo;
104 
105 // NOTE: The init and cleanup routines should NEVER rely on g_pApp existing!
106 // so only "initialization and cleanup INTERNAL to the module" goes there!
107 
108 // A module should be prepared to be unloaded at ANY time, even if it is locked
109 // or if can_unload returns false; locking is only a "suggestion" to the KVIrc core.
110 
111 // When unloaded, a module must ensure the destruction of all the resources that depend
112 // on the module core code
113 
114 #define KVIRC_MODULE_STRUCTURE_SYMBOL "KVIrc_module_info"
115 
116 #define KVIRC_MODULE(_szModuleName, _szVersion, _szCopyright, _szDescription, _init_routine, _can_unload, _ctrl_routine, _cleanup_routine, _szModuleContext) \
117  \
118  KVIMODULEEXPORTDATA KviModuleInfo KVIrc_module_info = { \
119  KVI_VERSION, \
120  _szModuleName, \
121  _szModuleContext, \
122  _szVersion, \
123  _szCopyright, \
124  _szDescription, \
125  _init_routine, \
126  _can_unload, \
127  _ctrl_routine, \
128  _cleanup_routine \
129  };
130 
132 {
133  friend class KviPointerHashTable<const char *, KviModule>;
134  friend class KviModuleManager;
135  friend class KviUserParser;
136 
137 protected:
138  KviModule(QLibrary * handle, KviModuleInfo * info, const QString & name, const QString & filename);
139 
140 public:
141  ~KviModule(); // must be public for KviPointerList
142 private:
143  QString m_szName;
144  QString m_szFileName;
146  QLibrary * m_pLibrary;
147  unsigned int m_uLock;
149 
150 protected:
151  void updateAccessTime();
152  unsigned int secondsSinceLastAccess();
153 
154 public:
155  // name of this module: always low case, single word
156  const QString & name() { return m_szName; };
157  // filename of this module (with NO path): formatted as "libkvi%s.so",name()
158  const QString & filename() { return m_szFileName; };
159  QLibrary * handle() { return m_pLibrary; };
160  KviModuleInfo * moduleInfo() { return m_pModuleInfo; };
161 
162  //
163  // This is a locking method a bit stronger than the can_unload routine
164  // in the descriptor. It will behave in the same way plus
165  // the user will be unable to unload the module unless he will specify
166  // the -f switch to the <module>.unload command. Without the -f switch
167  // he will be just warned that the module is locked in memory and
168  // don't want to be unloaded.
169  // The usage of this method is encouraged
170  // only when you have blocking dialogs inside the module code, like the
171  // QMessageBox or QFileDialog static methods.
172  // In this case you're entering a modal event loop that you can't control
173  // and if some script will attempt to forcibly unload the module
174  // it will surely lead to a crash when the static method returns (jumping into no mans land).
175  // <module>.unload -f is in fact undocumented so people will substantially
176  // not use it (unless they are developers and they are reading this comment).
177  //
178  void lock() { m_uLock++; };
179  void unlock()
180  {
181  if(m_uLock > 0)
182  m_uLock--;
183  };
184  bool isLocked() { return (m_uLock > 0); };
185 
186  void * getSymbol(const char * symname);
187  bool ctrl(const char * operation, void * param);
188 
189  void getDefaultConfigFileName(QString & szBuffer);
190 
191  static void unregisterMetaObject(const char * metaObjName);
192 
193 #ifdef COMPILE_CRYPT_SUPPORT
194  void registerCryptEngine(KviCryptEngineDescription * d);
195  void unregisterCryptEngine(const char * szName);
196  void unregisterCryptEngines();
197 #endif
198 
199  KviModuleExtensionDescriptor * registerExtension(const KviCString & szType, const KviCString & szName, const QString & szVisibleName, KviModuleExtensionAllocRoutine r);
200  KviModuleExtensionDescriptor * registerExtension(const KviCString & szType, const KviCString & szName, const QString & szVisibleName, KviModuleExtensionAllocRoutine r, const QPixmap & icon);
201  KviModuleExtensionDescriptor * findExtensionDescriptor(const KviCString & szType, const KviCString & szName);
202  void unregisterAllExtensions();
203 };
204 
205 #endif //_KVI_MODULE_H_
const char * szModuleName
Definition: KviModule.h:66
unsigned int m_uLock
Definition: KviModule.h:147
KviModuleSystemRoutine can_unload
Definition: KviModule.h:88
Definition: KviCString.h:105
#define KVIRC_API
Definition: kvi_settings.h:128
const QString & name()
Definition: KviModule.h:156
KviModuleExtension *(* KviModuleExtensionAllocRoutine)(KviModuleExtensionAllocStruct *)
Definition: KviModuleExtension.h:49
QLibrary * m_pLibrary
Definition: KviModule.h:146
const char * szModuleContext
Definition: KviModule.h:67
QString m_szName
Definition: KviModule.h:143
KviModuleInfo * m_pModuleInfo
Definition: KviModule.h:145
QString m_szFileName
Definition: KviModule.h:144
QLibrary * handle()
Definition: KviModule.h:159
const char * szCopyright
Definition: KviModule.h:69
bool(* KviModuleCtrlRoutine)(KviModule *, const char *, void *)
Definition: KviModule.h:61
KviModuleSystemRoutine init_routine
Definition: KviModule.h:74
void unlock()
Definition: KviModule.h:179
Definition: KviModuleManager.h:36
struct _KviModuleInfo KviModuleInfo
const char * szVersion
Definition: KviModule.h:68
char s d
Definition: KviIrcNumericCodes.h:391
const QString & filename()
Definition: KviModule.h:158
Definition: KviCryptEngine.h:54
bool isLocked()
Definition: KviModule.h:184
long int m_lastAccessTime
Definition: KviModule.h:148
#define r
Definition: detector.cpp:82
Definition: KviModuleExtension.h:51
Definition: KviModule.h:131
const char * szDescription
Definition: KviModule.h:70
KviModuleInfo * moduleInfo()
Definition: KviModule.h:160
QString name()
Definition: KviRuntimeInfo.cpp:655
char szBuffer[4096]
Definition: winamp.cpp:77
A fast pointer hash table implementation.
Definition: KviPointerHashTable.h:391
This file contains compile time settings.
Definition: KviKvsModuleInterface.h:134
bool(* KviModuleSystemRoutine)(KviModule *)
Definition: KviModule.h:60
void lock()
Definition: KviModule.h:178
KviModuleSystemRoutine cleanup_routine
Definition: KviModule.h:102
KviModuleCtrlRoutine ctrl_routine
Definition: KviModule.h:95
Definition: KviModule.h:63
const char * szKVIrcVersion
Definition: KviModule.h:65