PageRenderTime 628ms CodeModel.GetById 262ms app.highlight 167ms RepoModel.GetById 195ms app.codeStats 0ms

/src/scim-python.cpp

http://scim-python.googlecode.com/
C++ | 447 lines | 321 code | 99 blank | 27 comment | 56 complexity | c3bd9a8cc1ab821cbb1a7ca0c4bc61e9 MD5 | raw file
  1/* vim:set noet ts=4: */
  2/** 
  3 * scim-python
  4 * 
  5 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6 *
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this program; if not, write to the
 20 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 21 * Boston, MA  02111-1307  USA
 22 *
 23 * $Id: $
 24 */
 25#include "../config.h"
 26#include "scim-python.h"
 27#include <assert.h>
 28
 29using namespace scim;
 30using namespace std;
 31
 32static vector <IMEngineFactoryPointer> _factorys;
 33static vector <HelperInfo> _helpers;
 34static int _use_count = 0;
 35
 36static PyObject *
 37scim_get_version (PyObject *self, PyObject *args)
 38{
 39	return PyString_FromString (PACKAGE_VERSION);
 40}
 41
 42static PyMethodDef _methods[] = {
 43	{"get_version", scim_get_version, METH_NOARGS, "get version string of scim-python" },
 44	{0, }
 45};
 46
 47PyMODINIT_FUNC
 48init_scim (void) 
 49{
 50	PyObject* m;
 51
 52	m = Py_InitModule3("_scim", _methods,
 53						"SCIM.");
 54
 55	if (m == NULL) {
 56		PyErr_Print ();
 57		return;
 58	}
 59
 60	init_event (m);
 61	init_property (m);
 62	init_config (m);
 63	init_engine (m);
 64	init_factory (m);
 65	init_helper (m);
 66	init_attribute (m);
 67	init_lookup_table (m);
 68}
 69
 70static PyObject *
 71Py_CallFunction (const char *module, const char *function, PyObject *args)
 72{
 73	PyObject *pName = NULL;
 74	PyObject *pModule = NULL;
 75	PyObject *pFunc = NULL;
 76	PyObject *pValue = NULL;
 77
 78	pName = PyString_FromString (module);
 79	if (pName == NULL)
 80		goto _failed_out;
 81
 82	pModule = PyImport_Import (pName);
 83
 84	if (pModule == NULL)
 85		goto _failed_out;
 86
 87	pFunc = PyObject_GetAttrString (pModule, function);
 88
 89	if (pFunc == NULL)
 90		goto _failed_out;
 91
 92	pValue = PyObject_CallObject (pFunc, args);
 93
 94	if (pValue == NULL)
 95		goto _failed_out;
 96
 97	goto _success_out;;
 98
 99_failed_out:
100	PyErr_Print ();
101_success_out:
102	Py_XDECREF (pName);
103	Py_XDECREF (pFunc);
104	Py_XDECREF (pModule);
105
106	return pValue;
107
108}
109
110static PyObject *
111Py_CallModuleFunction (PyObject *module, const char *function, PyObject *args)
112{
113	PyObject *pFunc = NULL;
114	PyObject *pValue = NULL;
115
116	pFunc = PyObject_GetAttrString (module, function);
117
118	if (pFunc == NULL)
119		goto _failed_out;
120
121	pValue = PyObject_CallObject (pFunc, args);
122
123	if (pValue == NULL)
124		goto _failed_out;
125
126	goto _success_out;;
127
128_failed_out:
129	PyErr_Print ();
130_success_out:
131	Py_XDECREF (pFunc);
132
133	return pValue;
134
135}
136
137
138extern "C" void
139scim_module_init (void)
140{
141
142	char * argv[] = { 
143		(char *)"scim-python", 
144		NULL
145	};
146
147	bindtextdomain (GETTEXT_PACKAGE, SCIM_PYTHON_LOCALEDIR);
148	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
149
150	if (_use_count > 0) {
151		goto out;
152	}
153
154	if (Py_IsInitialized ()) {
155		goto out;
156	}
157
158	static int add_path = 0;
159
160	if (add_path == 0) {
161		char *pythonpath = NULL;
162		char *old_pythonpath = getenv ("PYTHONPATH");
163
164		if (old_pythonpath != NULL) {
165			asprintf (&pythonpath, 
166				"PYTHONPATH=" SCIM_PYTHON_DATADIR ":%s", old_pythonpath);
167		}
168		else {
169			asprintf (&pythonpath, 
170				"PYTHONPATH=" SCIM_PYTHON_DATADIR);
171		}
172		putenv (pythonpath);
173		add_path = 1;
174	}
175
176	Py_Initialize ();
177	PySys_SetArgv (1, argv);
178
179out:
180	_use_count ++;
181}
182
183extern "C" void
184scim_module_exit (void)
185{
186	if (_use_count == 0)
187		return;
188
189	_use_count --;
190	if (_use_count == 0) {
191		_factorys.clear ();
192		Py_Finalize ();
193	}
194}
195
196
197#ifndef DISABLE_IMENGINE
198extern "C" unsigned int 
199scim_imengine_module_init (const ConfigPointer &config)
200{
201	PyObject *pValue = NULL;
202	PyObject *pArgs = NULL;
203	PyObject *pyconfig = NULL;
204	int size = 0;
205
206	pyconfig = PyConfig_New (config);
207	Py_DECREF (pyconfig);
208	pyconfig = PyConfig_New (config);
209
210	pArgs = Py_BuildValue ("(O)", pyconfig);
211	pValue = Py_CallFunction ("engine", "query_engines", pArgs);
212
213	Py_DECREF (pArgs);
214	Py_DECREF (pyconfig);
215	if (PyList_Check (pValue)) {
216		PyObject * pTuple = PyList_AsTuple (pValue);
217		Py_DECREF (pValue);
218		pValue = pTuple;
219	}
220	if (!PyTuple_Check (pValue))
221		return 0;
222
223	size = PyTuple_Size (pValue);
224
225	for (int i = 0; i < size; i++) {
226		IMEngineFactoryPointer p = PyIMEngineFactory::from_pyobject (PyTuple_GetItem (pValue, i));
227		_factorys.push_back (p);
228	}
229
230	Py_DECREF (pValue);
231	return size;
232}
233
234extern "C" IMEngineFactoryPointer 
235scim_imengine_module_create_factory (uint32 engine)
236{
237	if ( engine < 0 || engine >= _factorys.size ())
238		return IMEngineFactoryPointer (0);
239
240	return _factorys[engine];
241}
242#endif //DISABLE_IMENGINE
243
244#ifndef DISABLE_SETUPUI
245#include <gtk/gtk.h>
246#include <pygobject.h>
247
248extern "C" GtkWidget * 
249scim_setup_module_create_ui (void)
250{
251	static GtkWidget *window = NULL;
252	PyObject *pValue = NULL;
253
254	if (window != NULL)
255		return window;
256
257
258	pValue = Py_CallFunction ("setupui", "create_ui", NULL);
259
260	if (pValue != Py_None) {
261		window = GTK_WIDGET (pygobject_get (pValue));
262		gtk_object_ref (GTK_OBJECT (window));
263	}
264
265	Py_DECREF (pValue);
266	return window;
267}
268
269extern "C" String
270scim_setup_module_get_category (void)
271{
272	return String ("IMEngine");
273}
274
275extern "C" String
276scim_setup_module_get_name (void)
277{
278	return String (_("Python"));
279}
280
281extern "C" String
282scim_setup_module_get_description (void)
283{
284	return String (_("Setup UI for Python IM engines."));
285}
286
287extern "C" void
288scim_setup_module_load_config (const ConfigPointer &config)
289{
290	PyObject *pValue = NULL;
291	PyObject *pArgs = NULL;
292
293	pArgs = Py_BuildValue ("(O)", PyConfig_New (config));
294	pValue = Py_CallFunction ("setupui", "load_config", pArgs);
295
296	if ( pValue == NULL) {
297		PyErr_Clear ();
298	}
299
300	Py_XDECREF (pArgs);
301	Py_XDECREF (pValue);
302}
303
304extern "C" void
305scim_setup_module_save_config (const ConfigPointer &config)
306{
307	PyObject *pValue = NULL;
308	PyObject *pArgs = NULL;
309
310	pArgs = Py_BuildValue ("(O)", PyConfig_New (config));
311
312	pValue = Py_CallFunction ("setupui", "save_config", pArgs);
313
314
315	Py_XDECREF (pArgs);
316	Py_XDECREF (pValue);
317}
318
319extern "C" bool
320scim_setup_module_query_changed ()
321{
322	PyObject *pValue = NULL;
323	bool retval = false;
324
325	pValue = Py_CallFunction ("setupui", "query_changed", NULL);
326
327	if (pValue == Py_True) {
328		retval = true;
329	}
330
331	Py_XDECREF (pValue);
332
333	return retval;
334}
335
336#if 0
337/*
338 * Overide new and delete operator.
339 **/
340static int __count = 0;
341void* operator new (size_t size)
342{
343	fprintf (stderr, "new count = %d\n", __count++);
344	if (size < 256) {
345		size = 256;
346	}
347
348	void *p = PyObject_Malloc (size);
349	assert (p != NULL);
350	return p;
351}
352
353void operator delete (void *p)
354{
355	fprintf (stderr, "delete count = %d\n", __count--);
356	assert (p != NULL);
357	PyObject_Free (p); 
358}
359#endif
360
361#endif //DISABLE_SETUPUI
362
363#ifndef DISABLE_HELPER
364
365static PyObject *g_helper_mod = NULL;
366
367extern "C" unsigned int 
368scim_helper_module_number_of_helpers (void)
369{
370	PyObject *retval = NULL;
371	int size = 0;
372	if (g_helper_mod == NULL) {
373		PyObject *name = PyString_FromString ("helper");
374		g_helper_mod = PyImport_Import (name);
375		Py_DECREF (name);
376	}
377
378	retval = Py_CallModuleFunction (g_helper_mod, "number_of_helpers", NULL);
379
380	if (retval != NULL) {
381		size =  PyInt_AsLong (retval);
382		Py_DECREF (retval);
383	}
384
385	return size;
386}
387
388extern "C" bool
389scim_helper_module_get_helper_info (unsigned int index, HelperInfo &info)
390{
391	bool result = FALSE;
392	PyObject *retval = NULL;
393	PyObject *args = NULL;
394	PyObject *helper_info = NULL;
395
396	args = Py_BuildValue ("(I)", index);
397	retval = Py_CallModuleFunction (g_helper_mod, "get_helper_info", args);
398
399	Py_DECREF (args);
400
401	if (retval == Py_None || retval == NULL) {
402		result = FALSE;
403		goto _out;
404	}
405
406	if (PyList_Check (retval)) {
407		helper_info = PyList_AsTuple (retval);
408	}
409	else if (PyTuple_Check (args)) {
410		helper_info = retval;
411		Py_INCREF (retval);
412	}
413
414	if (helper_info != NULL && PyTuple_GET_SIZE (helper_info) == 5) {
415		const char *uuid = PyString_AsString (PyTuple_GetItem (helper_info, 0));
416		const char *name = PyString_AsString (PyTuple_GetItem (helper_info, 1));
417		const char *icon = PyString_AsString (PyTuple_GetItem (helper_info, 2));
418		const char *desc = PyString_AsString (PyTuple_GetItem (helper_info, 3));
419		int opt = PyInt_AsLong (PyTuple_GetItem (helper_info, 4));
420		info = HelperInfo (uuid, name, icon, desc, opt);
421		result = TRUE;
422	}
423
424_out:
425	Py_XDECREF (args);
426	Py_XDECREF (retval);
427	Py_XDECREF (helper_info);
428
429	return result;
430}
431
432extern "C" void
433scim_helper_module_run_helper (const String &uuid, const ConfigPointer &config,
434						const String &display)
435{
436	PyObject *retval = NULL;
437	PyObject *args = NULL;
438
439	args = Py_BuildValue ("(sOs)", uuid.c_str (), PyConfig_New (config), display.c_str ());
440	retval = Py_CallModuleFunction (g_helper_mod, "run_helper", args);
441
442	Py_XDECREF (args);
443	Py_XDECREF (retval);
444
445}
446
447#endif //DISABLE_HELPER