initial import of ussd-pad
[ussd-widget] / ussd-pad / src / opt / ussd-pad / platforms / maemo4.py
1 PLATFORM = "maemo4"
2
3 _osso_ctx = None
4
5
6 def create_osso_context(name, version, v):
7     global _osso_ctx
8     
9     import osso
10     _osso_ctx = osso.Context(name, version, v)
11
12
13 def get_system_bus():
14     """
15     Returns the DBus system bus.
16     @since: 0.96
17     
18     @return: dbus system bus
19     """
20
21     return _system_bus
22     
23
24 def get_session_bus():
25     """
26     Returns the DBus session bus.
27     @since: 0.96
28     
29     @return: dbus session bus
30     """
31
32     return _session_bus
33
34
35 def get_device_state():
36     """
37     Returns the OSSO device state object.
38     @since: 0.96.3
39     
40     @return: OSSO device state
41     """
42     
43     import osso
44     return osso.DeviceState(_osso_ctx)
45
46
47 def is_offline_mode():
48     """
49     Returns whether the device is in offline (flight) mode.
50     
51     @return: whether the device is in offline mode.
52     """
53     
54     import dbus
55     bus = get_system_bus()
56     obj = bus.get_object("com.nokia.mce", "/com/nokia/mce/request")
57     req = dbus.Interface(obj, "com.nokia.mce.request")
58     mode = req.get_device_mode()
59     
60     return (mode in ["offline", "flight"])
61
62
63 def inhibit_screen_blanking():
64     """
65     Inhibits screen blanking. This function must be called repeatedly as long
66     as blanking must not take place.
67     """
68     
69     devstate = get_device_state()
70     devstate.display_blanking_pause()
71     
72
73 def get_product_code():
74     """
75     Returns the product code of the device.
76
77      - Nokia 770:    SU-18
78      - Nokia N800:   RX-34
79      - Nokia N810:   RX-44
80      - Nokia N810WE: RX-48
81      - Nokia N900:   RX-51
82      - Unknown:      ?
83     
84     @since: 0.96
85     
86     @return: product code
87     """
88     
89     # you can override the product code by setting the environment variable
90     # MEDIABOX_MAEMO_DEVICE
91     import os
92     product = os.environ.get("MEDIABOX_MAEMO_DEVICE")
93     
94     if (not product):
95         try:
96             lines = open("/proc/component_version", "r").readlines()
97         except:
98             lines = []
99             
100         product = "?"
101         for line in lines:
102             line = line.strip()
103             if (line.startswith("product")):
104                 parts = line.split()
105                 product = parts[1].strip()
106                 break
107         #end for
108     #end if
109
110     return product
111
112
113 def request_connection():
114     """
115     If the device is not connected, tries to establish the default connection
116     or pop up the connection dialog.
117     Does nothing if the device does already have a network connection.
118     @since: 0.96.3
119     """
120     
121     # dbus-send --type=method_call --system --dest=com.nokia.icd/com/nokia/icd com.nokia.icd.connect
122     # dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
123     
124     try:
125         import conic
126         conn = conic.Connection()
127         conn.request_connection(conic.CONNECT_FLAG_NONE)
128     except:
129         pass
130
131
132 def plugin_execute(so_file):
133
134     import osso
135     plugin = osso.Plugin(_osso_ctx)
136     plugin.plugin_execute(so_file, True)
137
138
139 if (get_product_code() == "SU-18"):
140     # bad hack!
141     # work around broken D-Bus bindings on OS 2006; this breaks urllib2 for us,
142     # but we don't use it anyway
143     def _f(*args): raise RuntimeError("Ignore me...")
144     import urllib2
145     urllib2.AbstractHTTPHandler.do_open = _f
146 #end if
147
148 import dbus, dbus.glib
149 _system_bus = dbus.SystemBus()
150 _session_bus = dbus.SessionBus()