Mostly improvements to packaging
[gc-dialer] / src / hildonize.py
1 #!/usr/bin/env python
2
3
4 import gtk
5 import dbus
6
7
8 class FakeHildonModule(object):
9         pass
10
11
12 try:
13         import hildon
14 except (ImportError, OSError):
15         hildon = FakeHildonModule
16
17
18 IS_HILDON = hildon is not FakeHildonModule
19
20
21 class FakeHildonProgram(object):
22
23         pass
24
25
26 if IS_HILDON:
27         def get_app_class():
28                 return hildon.Program
29 else:
30         def get_app_class():
31                 return FakeHildonProgram
32
33
34 if IS_HILDON:
35         def set_application_title(window, title):
36                 pass
37 else:
38         def set_application_title(window, title):
39                 window.set_title(title)
40
41
42 if IS_HILDON:
43         def hildonize_window(app, window):
44                 oldWindow = window
45                 newWindow = hildon.Window()
46                 oldWindow.get_child().reparent(newWindow)
47                 app.add_window(newWindow)
48                 return newWindow
49 else:
50         def hildonize_window(app, window):
51                 return window
52
53
54 if IS_HILDON:
55         def hildonize_menu(window, gtkMenu):
56                 hildonMenu = gtk.Menu()
57                 for child in gtkMenu.get_children():
58                         child.reparent(hildonMenu)
59                 window.set_menu(hildonMenu)
60                 gtkMenu.destroy()
61                 return hildonMenu
62 else:
63         def hildonize_menu(window, gtkMenu):
64                 return gtkMenu
65
66
67 if IS_HILDON:
68         def set_cell_thumb_selectable(renderer):
69                 renderer.set_property("scale", 1.5)
70 else:
71         def set_cell_thumb_selectable(renderer):
72                 pass
73
74
75 if IS_HILDON:
76         def hildonize_text_entry(textEntry):
77                 textEntry.set_property('hildon-input-mode', 7)
78 else:
79         def hildonize_text_entry(textEntry):
80                 pass
81
82
83 if IS_HILDON:
84         def mark_window_rotatable(window):
85                 raise NotImplementedError("TODO distinguish between Diablo and Fremantle")
86                 # gtk documentation is unclear whether this does a "=" or a "|="
87                 window.set_flags(hildon.HILDON_PORTRAIT_MODE_SUPPORT)
88 else:
89         def mark_window_rotatable(window):
90                 pass
91
92
93 if IS_HILDON:
94         def window_to_portrait(window):
95                 raise NotImplementedError("TODO distinguish between Diablo and Fremantle")
96                 # gtk documentation is unclear whether this does a "=" or a "|="
97                 window.set_flags(hildon.HILDON_PORTRAIT_MODE_SUPPORT)
98
99         def window_to_landscape(window):
100                 raise NotImplementedError("TODO distinguish between Diablo and Fremantle")
101                 # gtk documentation is unclear whether this does a "=" or a "&= ~"
102                 window.unset_flags(hildon.HILDON_PORTRAIT_MODE_REQUEST)
103 else:
104         def window_to_portrait(window):
105                 pass
106
107         def window_to_landscape(window):
108                 pass
109
110
111 def get_device_orientation():
112         raise NotImplementedError()
113         bus = dbus.SystemBus()
114         try:
115                 rawMceRequest = bus.get_object("com.nokia.mce", "/com/nokia/mce/request")
116                 mceRequest = dbus.Interface(rawMceRequest, dbus_interface="com.nokia.mce.request")
117                 orientation, standState, faceState, xAxis, yAxis, zAxis = mceRequest.get_device_orientation()
118         except dbus.exception.DBusException:
119                 # catching for documentation purposes that when a system doesn't
120                 # support this, this is what to expect
121                 raise
122
123         if orientation == "":
124                 return gtk.ORIENTATION_HORIZONTAL
125         elif orientation == "":
126                 return gtk.ORIENTATION_VERTICAL
127         else:
128                 raise RuntimeError("Unknown orientation: %s" % orientation)
129
130
131 if IS_HILDON:
132         def hildonize_password_entry(textEntry):
133                 textEntry.set_property('hildon-input-mode', 7 | (1 << 29))
134 else:
135         def hildonize_password_entry(textEntry):
136                 pass
137
138
139 if IS_HILDON:
140         def hildonize_combo_entry(comboEntry):
141                 comboEntry.set_property('hildon-input-mode', 1 << 4)
142 else:
143         def hildonize_combo_entry(textEntry):
144                 pass
145
146
147 if IS_HILDON:
148         def set_thumb_scrollbar(scrolledWindow):
149                 hildon.hildon_helper_set_thumb_scrollbar(scrolledWindow, True)
150 else:
151         def set_thumb_scrollbar(scrolledWindow):
152                 pass
153
154
155 if IS_HILDON:
156         def request_number(parent, title, range, default):
157                 spinner = hildon.NumberEditor(*range)
158                 spinner.set_value(default)
159
160                 dialog = gtk.Dialog(
161                         title,
162                         parent,
163                         gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
164                         (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
165                 )
166                 dialog.set_default_response(gtk.RESPONSE_CANCEL)
167                 dialog.get_child().add(spinner)
168
169                 try:
170                         dialog.show_all()
171                         response = dialog.run()
172                 finally:
173                         dialog.hide()
174
175                 if response == gtk.RESPONSE_OK:
176                         return spinner.get_value()
177                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
178                         raise RuntimeError("User cancelled request")
179                 else:
180                         raise RuntimeError("Unrecognized response %r", response)
181 else:
182         def request_number(parent, title, range, default):
183                 adjustment = gtk.Adjustment(default, range[0], range[1], 1, 5, 0)
184                 spinner = gtk.SpinButton(adjustment, 0, 0)
185                 spinner.set_wrap(False)
186
187                 dialog = gtk.Dialog(
188                         title,
189                         parent,
190                         gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
191                         (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
192                 )
193                 dialog.set_default_response(gtk.RESPONSE_CANCEL)
194                 dialog.get_child().add(spinner)
195
196                 try:
197                         dialog.show_all()
198                         response = dialog.run()
199                 finally:
200                         dialog.hide()
201
202                 if response == gtk.RESPONSE_OK:
203                         return spinner.get_value_as_int()
204                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
205                         raise RuntimeError("User cancelled request")
206                 else:
207                         raise RuntimeError("Unrecognized response %r", response)