File: irisnetglobal.cpp

package info (click to toggle)
psi 0.11-9
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 11,036 kB
  • ctags: 19,340
  • sloc: cpp: 130,316; ansic: 26,008; xml: 900; sh: 558; makefile: 225; python: 178; ruby: 21
file content (286 lines) | stat: -rw-r--r-- 5,472 bytes parent folder | download
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
/*
 * Copyright (C) 2006  Justin Karneges
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 *
 */

#include "irisnetglobal_p.h"

#include "irisnetplugin.h"

namespace XMPP {

// built-in providers
extern IrisNetProvider *irisnet_createUnixNetProvider();
extern IrisNetProvider *irisnet_createJDnsProvider();

//----------------------------------------------------------------------------
// internal
//----------------------------------------------------------------------------
class PluginInstance
{
private:
	QPluginLoader *_loader;
	QObject *_instance;
	bool _ownInstance;

	PluginInstance()
	{
	}

public:
	static PluginInstance *fromFile(const QString &fname)
	{
		QPluginLoader *loader = new QPluginLoader(fname);
		if(!loader->load())
		{
			delete loader;
			return 0;
		}
		QObject *obj = loader->instance();
		if(!obj)
		{
			loader->unload();
			delete loader;
			return 0;
		}
		PluginInstance *i = new PluginInstance;
		i->_loader = loader;
		i->_instance = obj;
		i->_ownInstance = true;
		return i;
	}

	static PluginInstance *fromStatic(QObject *obj)
	{
		PluginInstance *i = new PluginInstance;
		i->_loader = 0;
		i->_instance = obj;
		i->_ownInstance = false;
		return i;
	}

	static PluginInstance *fromInstance(QObject *obj)
	{
		PluginInstance *i = new PluginInstance;
		i->_loader = 0;
		i->_instance = obj;
		i->_ownInstance = true;
		return i;
	}

	~PluginInstance()
	{
		if(_ownInstance)
			delete _instance;

		if(_loader)
		{
			_loader->unload();
			delete _loader;
		}
	}

	void claim()
	{
		if(_loader)
			_loader->moveToThread(0);
		if(_ownInstance)
			_instance->moveToThread(0);
	}

	QObject *instance()
	{
		return _instance;
	}

	bool sameType(const PluginInstance *other)
	{
		if(!_instance || !other->_instance)
			return false;

		if(qstrcmp(_instance->metaObject()->className(), other->_instance->metaObject()->className()) != 0)
			return false;

		return true;
	}
};

class PluginManager
{
public:
	bool builtin_done;
	QStringList paths;
	QList<PluginInstance*> plugins;
	QList<IrisNetProvider*> providers;

	PluginManager()
	{
		builtin_done = false;
	}

	~PluginManager()
	{
		unload();
	}

	bool tryAdd(PluginInstance *i, bool lowPriority = false)
	{
		// is it the right kind of plugin?
		IrisNetProvider *p = qobject_cast<IrisNetProvider*>(i->instance());
		if(!p)
			return false;

		// make sure we don't have it already
		for(int n = 0; n < plugins.count(); ++n)
		{
			if(i->sameType(plugins[n]))
				return false;
		}

		i->claim();
		plugins += i;
		if(lowPriority)
			providers.append(p);
		else
			providers.prepend(p);
		return true;
	}

	void addBuiltIn(IrisNetProvider *p)
	{
		PluginInstance *i = PluginInstance::fromInstance(p);
		if(!tryAdd(i, true))
			delete i;
	}

	void scan()
	{
		if(!builtin_done)
		{
			addBuiltIn(irisnet_createUnixNetProvider());
			addBuiltIn(irisnet_createJDnsProvider());
			builtin_done = true;
		}

		QObjectList list = QPluginLoader::staticInstances();
		for(int n = 0; n < list.count(); ++n)
		{
			PluginInstance *i = PluginInstance::fromStatic(list[n]);
			if(!tryAdd(i))
				delete i;
		}
		for(int n = 0; n < paths.count(); ++n)
		{
			QDir dir(paths[n]);
			if(!dir.exists())
				continue;

			QStringList entries = dir.entryList();
			for(int k = 0; k < entries.count(); ++k)
			{
				QFileInfo fi(dir.filePath(entries[k]));
				if(!fi.exists())
					continue;
				QString fname = fi.filePath();
				PluginInstance *i = PluginInstance::fromFile(fname);
				if(!i)
					continue;

				if(!tryAdd(i))
					delete i;
			}
		}
	}

	void unload()
	{
		qDeleteAll(plugins);
		plugins.clear();
		providers.clear();
	}
};

class IrisNetGlobal
{
public:
	QMutex m;
	PluginManager pluginManager;
	QList<IrisNetCleanUpFunction> cleanupList;
};

Q_GLOBAL_STATIC(QMutex, global_mutex)
static IrisNetGlobal *global = 0;

static void deinit();

static void init()
{
	QMutexLocker locker(global_mutex());
	if(global)
		return;

	global = new IrisNetGlobal;
	qAddPostRoutine(deinit);
}

void deinit()
{
	if(!global)
		return;

	while(!global->cleanupList.isEmpty())
		(global->cleanupList.takeFirst())();

	delete global;
	global = 0;
}

//----------------------------------------------------------------------------
// Global
//----------------------------------------------------------------------------
void irisNetSetPluginPaths(const QStringList &paths)
{
	init();

	QMutexLocker locker(&global->m);
	global->pluginManager.paths = paths;
}

void irisNetCleanup()
{
	deinit();
}

void irisNetAddPostRoutine(IrisNetCleanUpFunction func)
{
	init();

	QMutexLocker locker(&global->m);
	global->cleanupList.prepend(func);
}

QList<IrisNetProvider*> irisNetProviders()
{
	init();

	QMutexLocker locker(&global->m);
	global->pluginManager.scan();
	return global->pluginManager.providers;
}

}