Initial Project Commit
[retroconv] / bak / pygobject.py
1 from ctypes import *
2 import sys
3 import ctypes
4
5 import gobject
6
7 # -------------------------------------------------------------------------
8 class _PyGObject_Functions(ctypes.Structure):
9     """GObject <-> Python mapping from http://faq.pygtk.org/index.py?req=show&file=faq23.041.htp"""
10     _fields_ = [
11         ('register_class',
12          ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p,
13                            ctypes.c_int, ctypes.py_object,
14                            ctypes.py_object)),
15         ('register_wrapper',
16          ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)),
17         ('register_sinkfunc',
18          ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
19         ('lookupclass',
20          ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_int)),
21         ('newgobj',
22          ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
23         ]
24
25
26 # -------------------------------------------------------------------------
27 class PyGObjectCAPI(object):
28     """GObject <-> Python mapping from http://faq.pygtk.org/index.py?req=show&file=faq23.041.htp"""
29     
30     def __init__(self):
31         addr = ctypes.pythonapi.PyCObject_AsVoidPtr(
32             ctypes.py_object(gobject._PyGObject_API))
33         self._api = _PyGObject_Functions.from_address(addr)
34
35     def pygobject_new(self, addr):
36         return self._api.newgobj(addr)
37
38
39 # -------------------------------------------------------------------------
40 class GList(Structure):
41     """GList representation and convenience functions, based on Java's Iterable.
42     
43        Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
44        Released under the Artistic Licence."""
45     
46     
47     # -----------------------------------------------------------------------
48     @classmethod
49     def new(clazz, ptr = None):
50         """Return a reference to an empty, or valid, GList at the
51            given pointer address."""
52            
53         if ptr:
54             return cast(c_void_p(ptr), POINTER(GList)).contents
55         else:
56             return GList()
57     
58     
59     # -----------------------------------------------------------------------
60     _fields_ = [('_data', c_void_p),
61                 ('_next', c_void_p),
62                 ('_prev', c_void_p)]
63                 
64     _ptr = None # Initialises to before the list for `while(has_next)...'
65     
66     
67     # -----------------------------------------------------------------------
68     def reset(self):
69         """Rewind the iterable to the start of the list."""
70       
71         self._ptr = None
72       
73     # -----------------------------------------------------------------------
74     def has_next(self):
75         """Return True if the list has an item on which next can be called."""
76       
77         return (not self._ptr and self._data) or (self._ptr and self._ptr._next)
78     
79     
80     # -----------------------------------------------------------------------
81     def next(self, as_a = None):
82         """Move the pointer on to the next item in the list and return its value, or
83            raise an exception if already on the last."""
84            
85         if self._ptr and not self._ptr._next:
86             raise Exception("IndexOutOfBounds")
87         
88         self._ptr = self._ptr and cast(self._ptr._next, POINTER(GList)).contents or self
89         if not self._ptr._data:
90             return None
91         elif as_a:
92             return cast(self._ptr._data, POINTER(as_a)).contents
93         else:
94             return self._ptr._data
95         
96         
97     # -----------------------------------------------------------------------
98     def set(self, value):
99         """Set the data in the current position in the list."""
100       
101         if not self._ptr:
102             self._ptr = self
103         
104         self._ptr._data = cast(value, c_void_p);
105       
106       
107       
108     # -----------------------------------------------------------------------
109     def add(self):
110         """Add a new entry on to the end of the list, ready to be "set"."""
111         
112         self.reset()
113         while self.has_next():
114             self.next()
115           
116         if not self._ptr:
117             self._ptr = self
118         else:      
119             new = GList()
120             new._prev = addressof(self._ptr)
121             self._ptr._next = addressof(new)
122             self._ptr = new
123
124
125 # -------------------------------------------------------------------------
126 class EContactPhoto_inlined(Structure):
127     _fields_ = [('mime_type', c_char_p),
128                 ('length', c_uint),
129                 ('data', c_void_p)]
130
131 class EContactPhoto_data(Union):
132     _fields_ = [('inlined', EContactPhoto_inlined),
133                 ('uri', c_char_p)]
134
135 class EContactPhoto(Structure):
136     _fields_ = [('type', c_int),
137                 ('data', EContactPhoto_data)]
138
139 class EContactDate(Structure):
140     _fields_ = [('year', c_uint),
141                 ('month', c_uint),
142                 ('day', c_uint)]
143
144 # -------------------------------------------------------------------------
145 class EVCardAttribute(Structure):
146     _fields_ = [('group', c_char_p),
147                ('name', c_char_p),
148                ('params', POINTER(GList)),
149                ('values', POINTER(GList)),]
150             
151     def value(self):
152         if not self.values:
153             return None
154      
155         return self.values.contents
156