Back to index

gcompris  8.2.2
py-gcompris-board.c
Go to the documentation of this file.
00001 #include "py-gcompris-board.h"
00002 #include <pygobject.h>
00003 
00004 staticforward PyTypeObject pyGcomprisBoardType;
00005 
00006 //static char pyGcomprisBoardType_doc[]= "Python GcomprisBoars structure binding";
00007 
00008 
00009 /* Special function created for the python plugin to be able to create
00010  * a pyGcomprisBoardObject form the existing GcomprisBoard structure
00011  */
00012 PyObject*
00013 gcompris_new_pyGcomprisBoardObject(GcomprisBoard* aboard)
00014 {
00015   pyGcomprisBoardObject* theboard = NULL;
00016 
00017   theboard = PyObject_New(pyGcomprisBoardObject, &pyGcomprisBoardType);
00018   if (theboard!=NULL)
00019     theboard->cdata = aboard;
00020 
00021   return (PyObject*)theboard;
00022 }
00023 
00024 
00025 /* Free the python gcompris board */
00026 static void
00027 pyGcomprisBoardType_dealloc(pyGcomprisBoardObject *self)
00028 {
00029   self->cdata = NULL;
00030   PyObject_DEL(self);
00031 }
00032 
00033 
00034 /* Methods defined in the pyGcomprisBoard class */
00035 static PyMethodDef pyGcomprisBoardType_methods[] = {
00036         {NULL,          NULL}           /* sentinel */
00037 };
00038 
00039 
00040 /* Return the value of the members contained in the GcomprisBoard structure */
00041 static PyObject *
00042 pyGcomprisBoardType_getattr(pyGcomprisBoardObject *self, char *name)
00043 {
00044   if (self->cdata != NULL) {
00045     /* Board type */
00046     if(strcmp(name,"type")==0) return Py_BuildValue("s", self->cdata->type);
00047     if(strcmp(name,"board_ready")==0){
00048       if(self->cdata->board_ready){
00049        Py_INCREF(Py_True);
00050        return Py_True;
00051       } else {
00052        Py_INCREF(Py_False);
00053        return Py_False;
00054       }
00055     }
00056 
00057     if(strcmp(name,"disable_im_context")==0) return PyBool_FromLong(self->cdata->disable_im_context);
00058 
00059     /* Board specific mode */
00060     if(strcmp(name,"mode")==0) return Py_BuildValue("s", self->cdata->mode);
00061 
00062     /* Name of this Board */
00063     if(strcmp(name,"name")==0) return Py_BuildValue("s", self->cdata->name);
00064     if(strcmp(name,"title")==0) return Py_BuildValue("s", self->cdata->title);
00065     if(strcmp(name,"description")==0) return Py_BuildValue("s", self->cdata->description);
00066     if(strcmp(name,"icon_name")==0) return Py_BuildValue("s", self->cdata->icon_name);
00067     if(strcmp(name,"author")==0) return Py_BuildValue("s", self->cdata->author);
00068     if(strcmp(name,"boarddir")==0) return Py_BuildValue("s", self->cdata->boarddir);
00069     if(strcmp(name,"filename")==0) return Py_BuildValue("s", self->cdata->filename);
00070     if(strcmp(name,"difficulty")==0) return Py_BuildValue("s", self->cdata->difficulty);
00071     if(strcmp(name,"mandatory_sound_file")==0) return Py_BuildValue("s", self->cdata->mandatory_sound_file);
00072 
00073     /* Menu positionning */
00074     if(strcmp(name,"section")==0) return Py_BuildValue("s", self->cdata->section);
00075     if(strcmp(name,"menuposition")==0) return Py_BuildValue("s", self->cdata->menuposition);
00076 
00077     /* Help Information */
00078     if(strcmp(name,"prerequisite")==0) return Py_BuildValue("s", self->cdata->prerequisite);
00079     if(strcmp(name,"goal")==0) return Py_BuildValue("s", self->cdata->goal);
00080     if(strcmp(name,"manual")==0) return Py_BuildValue("s", self->cdata->manual);
00081     if(strcmp(name,"credit")==0) return Py_BuildValue("s", self->cdata->credit);
00082 
00083     /* Size of the window in which we have to run */
00084     if(strcmp(name,"width")==0) return Py_BuildValue("i", self->cdata->width);
00085     if(strcmp(name,"height")==0) return Py_BuildValue("i", self->cdata->height);
00086 
00087     /* Dynamic board information (status) */
00088     if(strcmp(name,"level")==0) return Py_BuildValue("i", self->cdata->level);
00089     if(strcmp(name,"maxlevel")==0) return Py_BuildValue("i", self->cdata->maxlevel);
00090     if(strcmp(name,"sublevel")==0) return Py_BuildValue("i", self->cdata->sublevel);
00091     if(strcmp(name,"number_of_sublevel")==0) return Py_BuildValue("i", self->cdata->number_of_sublevel);
00092 
00093     if(strcmp(name,"previous_level")==0)
00094       return gcompris_new_pyGcomprisBoardObject(self->cdata->previous_board);
00095 
00096     if(strcmp(name,"canvas")==0)
00097       return (PyObject*) pygobject_new((GObject*)self->cdata->canvas);
00098 
00099     if(strcmp(name,"is_configurable")==0){
00100       if (!self->cdata->plugin){
00101        gc_board_check_file(self->cdata);
00102        if (!self->cdata->plugin){
00103          g_warning("board %s/%s seems not working !", self->cdata->section, self->cdata->name);
00104          Py_INCREF(Py_False);
00105          return Py_False;
00106        }
00107       }
00108 
00109       if (self->cdata->plugin->config_start && self->cdata->plugin->config_stop){
00110        g_warning ("Board %s is configurable\n", self->cdata->name);
00111        Py_INCREF(Py_True);
00112        return Py_True;
00113       } else {
00114        Py_INCREF(Py_False);
00115        return Py_False;
00116       }
00117     }
00118 
00119     if(strcmp(name,"board_id")==0) return Py_BuildValue("i", self->cdata->board_id);
00120 
00121     if(strcmp(name,"section_id")==0) return Py_BuildValue("i", self->cdata->section_id);
00122 
00123     /* Other members are special one...
00124      * TODO: Does we need to write a BoardPlugin structure wrapper ?
00125      */
00126   }
00127   return Py_FindMethod(pyGcomprisBoardType_methods, (PyObject *)self, name);
00128 }
00129 
00130 /* Set the value of a GcomprisBoard structure member */
00131 static int
00132 pyGcomprisBoardType_setattr(pyGcomprisBoardObject *self, char *name, PyObject *v)
00133 {
00134   int value;
00135 
00136   if (self->cdata==NULL) return -1;
00137   if (v==NULL) return -1;
00138 
00139   if (strcmp(name,"level")==0){
00140     value = (int) PyInt_AsLong(v);
00141     if ( value < 0 ) return -1;
00142     self->cdata->level=value;
00143     return 0;
00144   }
00145   if (strcmp(name,"maxlevel")==0){
00146     value = PyInt_AsLong(v);
00147     if ( value < 0 ) return -1;
00148     self->cdata->maxlevel=value;
00149     return 0;
00150   }
00151   if (strcmp(name,"sublevel")==0){
00152     value = PyInt_AsLong(v);
00153     if ( value < 0 ) return -1;
00154     self->cdata->sublevel=value;
00155     return 0;
00156   }
00157   if (strcmp(name,"number_of_sublevel")==0){
00158     value = PyInt_AsLong(v);
00159     if ( value < 0 ) return -1;
00160     self->cdata->number_of_sublevel=value;
00161     return 0;
00162   }
00163   if (strcmp(name,"disable_im_context")==0){
00164     value = PyObject_IsTrue( v);
00165     self->cdata->disable_im_context = value;
00166     return 0;
00167   }
00168   /* Other members are supposed to be read only */
00169 
00170   return -1;
00171 }
00172 
00173 static PyTypeObject pyGcomprisBoardType = {
00174 #if defined(WIN32)
00175   PyObject_HEAD_INIT(NULL)
00176 #else /* ! WIN32 */
00177   PyObject_HEAD_INIT(&PyType_Type)
00178 #endif
00179   0,                                        /*ob_size*/
00180   "pyGcomprisBoard",                        /*tp_name*/
00181   sizeof(pyGcomprisBoardObject),            /*tp_basicsize*/
00182   0,                                        /*tp_itemsize*/
00183   /* methods */
00184   (destructor)pyGcomprisBoardType_dealloc,  /*tp_dealloc*/
00185   0,                                        /*tp_print*/
00186   (getattrfunc)pyGcomprisBoardType_getattr, /*tp_getattr*/
00187   (setattrfunc)pyGcomprisBoardType_setattr, /*tp_setattr*/
00188   0,                                        /*tp_compare*/
00189   0,                                        /*tp_repr*/
00190   0,                                        /*tp_as_number*/
00191   0,                                        /*tp_as_sequence*/
00192   0,                                        /*tp_as_mapping*/
00193   0,                                        /*tp_hash*/
00194 };