summaryrefslogtreecommitdiffstats
path: root/src/kvirc/kvs/kvi_kvs_scriptaddonmanager.cpp
blob: 23f6ade352af08189b121798fc61d0202c70d1fa (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
//=============================================================================
//
//   File : kvi_kvs_scriptaddonmanager.cpp
//   Created on Thu 31 Mar 2005 01:21:23 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2005 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 opinion) 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 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.
//
//=============================================================================

#define __KVIRC__

#include "kvi_kvs_scriptaddonmanager.h"
#include "kvi_kvs_script.h"
#include "kvi_config.h"
#include "kvi_window.h"
#include "kvi_iconmanager.h"

KviKvsScriptAddonManager * KviKvsScriptAddonManager::m_pInstance = 0;




KviKvsScriptAddon::KviKvsScriptAddon(
			const TQString &szName,
			const TQString &szVersion,
			const TQString &szVisibleNameCode,
			const TQString &szDescriptionCode,
			const TQString &szUninstallCallbackCode,
			const TQString &szIconId
) : KviHeapObject(), m_szName(szName), m_szVersion(szVersion), m_szIconId(szIconId)
{
	allocateScripts(szVisibleNameCode,szDescriptionCode,szUninstallCallbackCode);
	m_pConfigureCallback = 0;
	m_pHelpCallback = 0;
}

KviKvsScriptAddon::KviKvsScriptAddon(const KviKvsScriptAddon &a)
: KviHeapObject()
{
	m_szName = a.m_szName;
	m_szVersion = a.m_szVersion;
	m_szIconId = a.m_szIconId;
	allocateScripts(a.m_pVisibleNameScript->code(),a.m_pDescriptionScript->code(),a.m_pUninstallCallback ? a.m_pUninstallCallback->code() : TQString());
	m_pConfigureCallback = 0;
	setConfigureCallback(a.m_pConfigureCallback ? a.m_pConfigureCallback->code() : TQString());
	m_pHelpCallback = 0;
	setHelpCallback(a.m_pHelpCallback ? a.m_pHelpCallback->code() : TQString());
}

KviKvsScriptAddon::KviKvsScriptAddon()
: KviHeapObject()
{
	m_pVisibleNameScript = 0;
	m_pDescriptionScript = 0;
	m_pUninstallCallback = 0;
	m_pConfigureCallback = 0;
	m_pHelpCallback = 0;
}

KviKvsScriptAddon::~KviKvsScriptAddon()
{
	if(m_pVisibleNameScript)delete m_pVisibleNameScript;
	if(m_pDescriptionScript)delete m_pDescriptionScript;
	if(m_pUninstallCallback)delete m_pUninstallCallback;
	if(m_pConfigureCallback)delete m_pConfigureCallback;
	if(m_pHelpCallback)delete m_pHelpCallback;
}

TQPixmap * KviKvsScriptAddon::icon()
{
	return g_pIconManager->getBigIcon(m_szIconId.isEmpty() ? TQString(KVI_BIGICON_KVS) : m_szIconId);
}

const TQString & KviKvsScriptAddon::visibleName()
{
	if(!m_pVisibleNameScript)return m_szVisibleName;
	if(!m_pVisibleNameScript->run(g_pActiveWindow,0,m_szVisibleName))m_szVisibleName = m_pVisibleNameScript->code();
	return m_szVisibleName;
}

const TQString & KviKvsScriptAddon::description()
{
	if(!m_pDescriptionScript)return m_szDescription;
	if(!m_pDescriptionScript->run(g_pActiveWindow,0,m_szDescription))m_szDescription = m_pDescriptionScript->code();
	return m_szDescription;
}

const TQString & KviKvsScriptAddon::visibleNameCode()
{
	return m_pVisibleNameScript->code();
}

const TQString & KviKvsScriptAddon::descriptionCode()
{
	return m_pDescriptionScript->code();
}

const TQString & KviKvsScriptAddon::uninstallCallbackCode()
{
	return m_pUninstallCallback->code();
}

const TQString & KviKvsScriptAddon::configureCallbackCode()
{
	if(m_pConfigureCallback)return m_pConfigureCallback->code();
	return KviTQString::empty;
}

const TQString & KviKvsScriptAddon::helpCallbackCode()
{
	if(m_pHelpCallback)return m_pHelpCallback->code();
	return KviTQString::empty;
}

bool KviKvsScriptAddon::load(KviConfig * cfg,const TQString &szName)
{
	m_szName = szName;
	cfg->setGroup(m_szName);
	m_szVersion = cfg->readTQStringEntry("Version");
	m_szIconId = cfg->readTQStringEntry("IconId");
	if(m_szVersion.isEmpty())return false;
	TQString tmp1,tmp2,tmp3;
	tmp1 = cfg->readTQStringEntry("VisibleNameCode");
	tmp2 = cfg->readTQStringEntry("DescriptionCode");
	tmp3 = cfg->readTQStringEntry("UninstallCallback");
	if(tmp1.isEmpty())return false;
	allocateScripts(tmp1,tmp2,tmp3);
	tmp1 = cfg->readTQStringEntry("ConfigureCallback");
	if(!tmp1.isEmpty())
		setConfigureCallback(tmp1);
	tmp1 = cfg->readTQStringEntry("HelpCallback");
	if(!tmp1.isEmpty())
		setHelpCallback(tmp1);
	return true;
}

void KviKvsScriptAddon::setConfigureCallback(const TQString &szConfigureCallbackCode)
{
	if(m_pConfigureCallback)delete m_pConfigureCallback;
	if(szConfigureCallbackCode.isEmpty())
	{
		m_pConfigureCallback = 0;
		return;
	}

	TQString szKvsName = "addon::";
	szKvsName += m_szName;

	TQString szTmp;

	szTmp = szKvsName;
	szTmp += "::configure";
	m_pConfigureCallback = new KviKvsScript(szTmp,szConfigureCallbackCode,KviKvsScript::InstructionList);
}

void KviKvsScriptAddon::setHelpCallback(const TQString &szHelpCallbackCode)
{
	if(m_pHelpCallback)delete m_pHelpCallback;
	if(szHelpCallbackCode.isEmpty())
	{
		m_pHelpCallback = 0;
		return;
	}

	TQString szKvsName = "addon::";
	szKvsName += m_szName;

	TQString szTmp;

	szTmp = szKvsName;
	szTmp += "::help";
	m_pHelpCallback = new KviKvsScript(szTmp,szHelpCallbackCode,KviKvsScript::InstructionList);
}

void KviKvsScriptAddon::allocateScripts(const TQString &szVisibleNameCode,const TQString &szDescriptionCode,const TQString &szUninstallCallbackCode)
{
	TQString szKvsName = "addon::";
	szKvsName += m_szName;

	TQString szTmp;

	szTmp = szKvsName;
	szTmp += "::name";
	m_pVisibleNameScript = new KviKvsScript(szTmp,szVisibleNameCode,KviKvsScript::Parameter);
	szTmp = szKvsName;
	szTmp += "::description";
	m_pDescriptionScript = new KviKvsScript(szTmp,szDescriptionCode,KviKvsScript::Parameter);
	szTmp = szKvsName;
	szTmp += "::uninstall";
	m_pUninstallCallback = new KviKvsScript(szTmp,szUninstallCallbackCode,KviKvsScript::InstructionList);
}

void KviKvsScriptAddon::save(KviConfig * cfg)
{
	cfg->setGroup(m_szName);
	cfg->writeEntry("Version",m_szVersion);
	cfg->writeEntry("VisibleNameCode",visibleNameCode());
	cfg->writeEntry("DescriptionCode",descriptionCode());
	cfg->writeEntry("UninstallCallback",uninstallCallbackCode());
	cfg->writeEntry("ConfigureCallback",configureCallbackCode());
	cfg->writeEntry("HelpCallback",helpCallbackCode());
	cfg->writeEntry("IconId",m_szIconId);
}

void KviKvsScriptAddon::executeUninstallCallback(KviWindow * pWnd)
{
	if(!m_pUninstallCallback)return;
	m_pUninstallCallback->run(pWnd);
}

void KviKvsScriptAddon::executeConfigureCallback(KviWindow * pWnd)
{
	if(!m_pConfigureCallback)return;
	m_pConfigureCallback->run(pWnd);
}

void KviKvsScriptAddon::executeHelpCallback(KviWindow * pWnd)
{
	if(!m_pHelpCallback)return;
	m_pHelpCallback->run(pWnd);
}





KviKvsScriptAddonManager::KviKvsScriptAddonManager()
{
	m_pInstance = this;
	m_bLoaded = false;
	m_pAddonDict = new KviPointerHashTable<TQString,KviKvsScriptAddon>(17,false);
	m_pAddonDict->setAutoDelete(true);
}

KviKvsScriptAddonManager::~KviKvsScriptAddonManager()
{
	delete m_pAddonDict;
}

void KviKvsScriptAddonManager::init()
{
	if(KviKvsScriptAddonManager::instance())
	{
		debug("WARNING: Trying to create the KviKvsScriptAddonManager twice!");
		return;
	}
	(void)new KviKvsScriptAddonManager();
}

void KviKvsScriptAddonManager::done()
{
	if(!KviKvsScriptAddonManager::instance())
	{
		debug("WARNING: Trying to destroy the KviKvsScriptAddonManager twice!");
		return;
	}
	delete KviKvsScriptAddonManager::instance();
}

void KviKvsScriptAddonManager::load(const TQString &szFileName)
{
	// in fact we implement delayed loading
	// so this function only stores the filename
	// from which we will load at the first request
	m_szFileName = szFileName;
	// this to make sure that we reload the addons
	// if someone explicitly requests a load after we have already loaded
	// (this does not happen in kvirc tough at the moment)
	m_bLoaded = false;
}

void KviKvsScriptAddonManager::save(const TQString &szFileName)
{
	if(!m_bLoaded)return; // nothing to store anyway
	// we're stored here from now on...
	m_szFileName = szFileName;

	KviConfig cfg(szFileName,KviConfig::Write);

	cfg.clear();

	KviPointerHashTableIterator<TQString,KviKvsScriptAddon> it(*m_pAddonDict);
	
	while(KviKvsScriptAddon * a = it.current())
	{
		cfg.setGroup(a->name());
		a->save(&cfg);
		++it;
	}
}

void KviKvsScriptAddonManager::delayedLoad()
{
	if(m_bLoaded)return; // already loaded
	m_bLoaded = true;
	// ::load() might be never called if we don't have
	// a scriptaddons.kvc file on disk, KviApp checks that.
	// So finally m_szFileName may be empty here
	if(m_szFileName.isEmpty())return;

	KviConfig cfg(m_szFileName,KviConfig::Read);

	KviPointerHashTable<TQString,KviConfigGroup> * d = cfg.dict();
	if(!d)return;
	
	KviPointerHashTableIterator<TQString,KviConfigGroup> it(*d);
	while(it.current())
	{
		TQString szName = it.currentKey();
		KviKvsScriptAddon * a = new KviKvsScriptAddon();
		if(a->load(&cfg,szName))
			m_pAddonDict->tqreplace(szName,a);
		else
			delete a;
		++it;
	}
}

KviPointerHashTable<TQString,KviKvsScriptAddon> * KviKvsScriptAddonManager::addonDict()
{
	if(!m_bLoaded)delayedLoad();
	return m_pAddonDict;
}





bool KviKvsScriptAddonManager::registerAddon(KviKvsScriptAddonRegistrationData * d)
{
	if(findAddon(d->szName))return false;
	KviKvsScriptAddon * a = new KviKvsScriptAddon(
			d->szName,
			d->szVersion,
			d->szVisibleNameScript,
			d->szDescriptionScript,
			d->szUninstallCallbackScript,
			d->szIconId);
	m_pAddonDict->tqreplace(d->szName,a);
	return true;
}

KviKvsScriptAddon * KviKvsScriptAddonManager::findAddon(const TQString &szName)
{
	if(!m_bLoaded)delayedLoad();
	return m_pAddonDict->tqfind(szName);
}

bool KviKvsScriptAddonManager::unregisterAddon(const TQString &szName,KviWindow * pWnd,bool bExecuteUninstallCallback)
{
	KviKvsScriptAddon * a = findAddon(szName);
	if(!a)return false;

	// remove the addon before executing the uninstall callback
	// so the user effectively can't call addon.unregister on itself in the uninstall callback code :D
	m_pAddonDict->setAutoDelete(false);
	m_pAddonDict->remove(szName);
	m_pAddonDict->setAutoDelete(true);

	if(bExecuteUninstallCallback)
		a->executeUninstallCallback(pWnd);

	delete a;
	return true;
}

void KviKvsScriptAddonManager::clear()
{
	if(!m_bLoaded)delayedLoad();
	m_pAddonDict->clear();
}