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