Add rpm build scripts.
[ibus.git] / ibus / property.py
bloba5eb04088d7b5b2733f1795807200ea560faf2c3
1 # vim:set noet ts=4:
3 # ibus - The Input Bus
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307 USA
22 __all__ = (
23 "PROP_TYPE_NORMAL",
24 "PROP_TYPE_TOGGLE",
25 "PROP_TYPE_RADIO",
26 "PROP_TYPE_SEPARATOR",
27 "PROP_TYPE_MENU",
28 "PROP_STATE_UNCHECKED",
29 "PROP_STATE_CHECKED",
30 "PROP_STATE_INCONSISTENT",
31 "Property",
32 "PropList",
33 "property_from_dbus_value",
34 "prop_list_from_dbus_value",
37 import dbus
39 PROP_TYPE_NORMAL = 0
40 PROP_TYPE_TOGGLE = 1
41 PROP_TYPE_RADIO = 2
42 PROP_TYPE_MENU = 3
43 PROP_TYPE_SEPARATOR = 4
45 PROP_STATE_UNCHECKED = 0
46 PROP_STATE_CHECKED = 1
47 PROP_STATE_INCONSISTENT = 2
49 class Property:
50 def __init__ (self, name,
51 type = PROP_TYPE_NORMAL,
52 label = "",
53 icon = "",
54 tooltip = "",
55 sensitive = True,
56 visible = True,
57 state = PROP_STATE_UNCHECKED):
58 self._name = name
59 self._type = type
60 self._label = label
61 self._icon = icon
62 self._tooltip = tooltip
63 self._sensitive = sensitive
64 self._visible = visible
65 self._state = state
66 self._sub_props = PropList ()
68 def set_sub_props (self, props):
69 self._sub_props = props
71 def get_sub_props (self):
72 return self._sub_props
74 def is_same (self, prop, test_all = True):
75 if self._name != prop._name or self._type != prop._type:
76 return False
77 if not test_all:
78 return True
79 if self._label != prop._label or \
80 self._icon != prop._icon or \
81 self._tooltip != prop._tooltip or \
82 self._sensitive != prop._sensitive or \
83 self._visible != prop._visible or \
84 self._state != prop._state:
85 return False
86 return self._sub_props.is_same (prop._sub_props, test_all)
89 def to_dbus_value (self):
90 sub_props = self._sub_props.to_dbus_value ()
91 values = (dbus.String (self._name),
92 dbus.Int32 (self._type),
93 dbus.String (self._label),
94 dbus.String (self._icon),
95 dbus.String (self._tooltip),
96 dbus.Boolean (self._sensitive),
97 dbus.Boolean (self._visible),
98 dbus.Int32 (self._state),
99 sub_props)
100 return dbus.Struct (values)
102 def from_dbus_value (self, value):
103 self._name, \
104 self._type, \
105 self._label, \
106 self._icon, \
107 self._tooltip, \
108 self._sensitive, \
109 self._visible, \
110 self._state, \
111 props = value
113 self._sub_props = prop_list_from_dbus_value (props)
115 def property_from_dbus_value (value):
116 p = Property ("")
117 p.from_dbus_value (value)
118 return p
120 class PropList:
121 def __init__ (self):
122 self._props = []
124 def append (self, prop):
125 self._props.append (prop)
127 def prepand (self, prop):
128 self._props.insert (0, prop)
130 def insert (self, index, prop):
131 self._props.insert (index, prop)
133 def get_properties (self):
134 return self._props[:]
136 def is_same (self, props, test_all = True):
137 if len (props.get_properties ()) != len (self.get_properties ()):
138 return False
140 for a, b in zip (self.get_properties(), props.get_properties ()):
141 if not a.is_same (b, test_all):
142 return False
143 return False
145 def to_dbus_value (self):
146 props = map (lambda p: p.to_dbus_value (), self._props)
147 return dbus.Array (props, signature = "v")
149 def from_dbus_value (self, value):
150 props = []
151 for p in value:
152 props.append (property_from_dbus_value (p))
153 self._props = props
155 def __iter__ (self):
156 return self._props.__iter__ ()
158 def prop_list_from_dbus_value (value):
159 props = PropList ()
160 props.from_dbus_value (value)
161 return props
163 def test ():
164 props = PropList ()
165 props.append (Property ("a"))
166 props.append (Property ("b"))
167 props.append (Property ("c"))
168 props.append (Property ("d"))
169 value = props.to_dbus_value ()
170 print prop_list_from_dbus_value (value)
172 p = Property ("z")
173 p.set_sub_props (props)
174 props = PropList ()
175 props.append (p)
176 value = props.to_dbus_value ()
177 print prop_list_from_dbus_value (value)
179 if __name__ == "__main__":
180 test ()