PageRenderTime 138ms CodeModel.GetById 122ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/scim-python-property.cpp

http://scim-python.googlecode.com/
C++ | 256 lines | 196 code | 35 blank | 25 comment | 18 complexity | 306ab888f64f0d4bd062c23c4d97ea06 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 <Python.h>
 26#include "structmember.h"
 27#include "scim-python-property.h"
 28
 29struct PyProperty {
 30	PyObject_HEAD
 31	/* Type-specific fields go here. */
 32	Property prop;
 33};
 34
 35static PyObject *
 36PyProperty_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 37{
 38       PyProperty *self;
 39
 40       self = (PyProperty *)type->tp_alloc (type, 0);
 41       return (PyObject *)self;
 42}
 43
 44static int
 45PyProperty_init (PyProperty *self, PyObject *args, PyObject *kwds)
 46{
 47	char *key = NULL;
 48	char *label = NULL;
 49	char *icon = NULL;
 50	char *tip = NULL;
 51
 52	if (!PyArg_ParseTuple (args, "ss|ss:__init__", &key, &label, &icon, &tip))
 53		return -1;
 54
 55	if (icon == NULL)
 56		icon = "";
 57	if (tip == NULL)
 58		tip = "";
 59
 60	new (&self->prop) Property (String (key), String (label), String (icon), String (tip));
 61	return 0;
 62}
 63
 64static void
 65PyProperty_dealloc (PyProperty *self)
 66{
 67	self->prop.~Property ();
 68	((PyObject *)self)->ob_type->tp_free (self);
 69}
 70
 71static PyMethodDef PyProperty_methods[] = {
 72#if 0
 73	{ "show_preedit_string", (PyCFunction)PythonInstance::_show_preedit_string, METH_NOARGS,
 74	 "Return the name, combining the first and last name"
 75	},
 76#endif
 77	{NULL}  /* Sentinel */
 78};
 79
 80static PyMemberDef PyProperty_members[] = {
 81	{NULL}  /* Sentinel */
 82};
 83
 84static PyObject *
 85PyProperty_get_key (PyProperty *self, void *closure)
 86{
 87	return PyString_FromString (self->prop.get_key ().c_str ());
 88}
 89
 90static int
 91PyProperty_set_key (PyProperty *self, PyObject *value, void *closure)
 92{
 93	if (value == NULL) {
 94		PyErr_SetString (PyExc_TypeError,
 95						"Cannot delete the key attribute");
 96		return -1;
 97	}
 98
 99	if (! PyString_Check (value)) {
100		PyErr_SetString (PyExc_TypeError, 
101						"The key attribute value must be a string");
102		return -1;
103	}
104
105	self->prop.set_key (String (PyString_AsString (value)));
106
107	return 0;
108}
109
110static PyObject *
111PyProperty_get_label (PyProperty *self, void *closure)
112{
113	return PyString_FromString (self->prop.get_label ().c_str ());
114}
115
116static int
117PyProperty_set_label (PyProperty *self, PyObject *value, void *closure)
118{
119	if (value == NULL) {
120		PyErr_SetString (PyExc_TypeError,
121						"Cannot delete the label attribute");
122		return -1;
123	}
124
125	if (! PyString_Check (value)) {
126		PyErr_SetString (PyExc_TypeError, 
127						"The key attribute label must be a string");
128		return -1;
129	}
130
131	self->prop.set_label (String (PyString_AsString (value)));
132
133	return 0;
134}
135
136static PyObject *
137PyProperty_get_icon (PyProperty *self, void *closure)
138{
139	return PyString_FromString (self->prop.get_icon ().c_str ());
140}
141
142static int
143PyProperty_set_icon (PyProperty *self, PyObject *value, void *closure)
144{
145	if (value == NULL) {
146		PyErr_SetString (PyExc_TypeError,
147						"Cannot delete the icon attribute");
148		return -1;
149	}
150
151	if (! PyString_Check (value)) {
152		PyErr_SetString (PyExc_TypeError, 
153						"The icon attribute value must be a string");
154		return -1;
155	}
156
157	self->prop.set_icon (String (PyString_AsString (value)));
158
159	return 0;
160}
161
162static PyObject *
163PyProperty_get_tip (PyProperty *self, void *closure)
164{
165	return PyString_FromString (self->prop.get_tip ().c_str ());
166}
167
168static int
169PyProperty_set_tip (PyProperty *self, PyObject *value, void *closure)
170{
171	if (value == NULL) {
172		PyErr_SetString (PyExc_TypeError,
173						"Cannot delete the tip attribute");
174		return -1;
175	}
176
177	if (! PyString_Check (value)) {
178		PyErr_SetString (PyExc_TypeError, 
179						"The tip attribute value must be a string");
180		return -1;
181	}
182
183	self->prop.set_tip (String (PyString_AsString (value)));
184
185	return 0;
186}
187static PyGetSetDef PyProperty_getseters[] = {
188	{"key", (getter)PyProperty_get_key, (setter)PyProperty_set_key,
189		"Key", NULL},
190	{"label", (getter)PyProperty_get_label, (setter)PyProperty_set_label,
191		"label", NULL},
192	{"icon", (getter)PyProperty_get_icon, (setter)PyProperty_set_icon,
193		"icon", NULL},
194	{"tip", (getter)PyProperty_get_tip, (setter)PyProperty_set_tip,
195		"tip", NULL},
196	{NULL}  /* Sentinel */
197};
198
199static PyTypeObject PyPropertyType = {
200	PyObject_HEAD_INIT (NULL)
201	0,						 	/*ob_size*/
202	"scim.Property", 			/*tp_name*/
203	sizeof (PyProperty), 		/*tp_basicsize*/
204	0,						 	/*tp_itemsize*/
205	(destructor) PyProperty_dealloc,	/*tp_dealloc*/
206	0,			  				/*tp_print*/
207	0,						 	/*tp_getattr*/
208	0,							/*tp_setattr*/
209	0,							/*tp_compare*/
210	0,			  				/*tp_repr*/
211	0,							/*tp_as_number*/
212	0,			  				/*tp_as_sequence*/
213	0,							/*tp_as_mapping*/
214	0,			  				/*tp_hash */
215	0,							/*tp_call*/
216	0,							/*tp_str*/
217	0,					   		/*tp_getattro*/
218	0,							/*tp_setattro*/
219	0,					 		/*tp_as_buffer*/
220	Py_TPFLAGS_DEFAULT,			/*tp_flags*/
221	"Property objects",			/* tp_doc */
222	0,					   		/* tp_traverse */
223	0,					   		/* tp_clear */
224	0,					   		/* tp_richcompare */
225	0,					   		/* tp_weaklistoffset */
226	0,					   		/* tp_iter */
227	0,					   		/* tp_iternext */
228	PyProperty_methods,			/* tp_methods */
229	PyProperty_members,			/* tp_members */
230	PyProperty_getseters,		/* tp_getset */
231	0,						 	/* tp_base */
232	0,						 	/* tp_dict */
233	0,						 	/* tp_descr_get */
234	0,						 	/* tp_descr_set */
235	0,						 	/* tp_dictoffset */
236	(initproc)PyProperty_init,	/* tp_init */
237	0,						 	/* tp_alloc */
238	PyProperty_new,				/* tp_new */
239	PyObject_Del,				/* tp_free */
240};
241
242
243Property&
244PyProperty_AsProperty (PyObject *self)
245{
246	return ((PyProperty *)self)->prop;
247}
248
249void init_property (PyObject *module)
250{
251	if (PyType_Ready (&PyPropertyType) < 0)
252		return;
253	Py_INCREF (&PyPropertyType);
254	PyModule_AddObject (module, "Property", (PyObject *)&PyPropertyType);
255}
256