Updating based on work with MormonChannel
[gc-dialer] / src / REPLACEME_gtk.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import with_statement
5
6 import gc
7 import logging
8 import ConfigParser
9
10 import gobject
11 import dbus
12 import gtk
13
14 try:
15         import osso
16 except ImportError:
17         osso = None
18
19 import constants
20 import hildonize
21 import util.misc as misc_utils
22
23
24 _moduleLogger = logging.getLogger(__name__)
25 PROFILE_STARTUP = False
26
27
28 class REPLACEMElProgram(hildonize.get_app_class()):
29
30         def __init__(self):
31                 super(REPLACEMElProgram, self).__init__()
32
33                 if not hildonize.IS_HILDON_SUPPORTED:
34                         _moduleLogger.info("No hildonization support")
35
36                 if osso is not None:
37                         self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
38                         self._deviceState = osso.DeviceState(self._osso_c)
39                         self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
40                 else:
41                         _moduleLogger.info("No osso support")
42                         self._osso_c = None
43                         self._deviceState = None
44
45         def _save_settings(self):
46                 config = ConfigParser.SafeConfigParser()
47
48                 self._REPLACEME.save_settings(config, "Windows")
49
50                 with open(constants._user_settings_, "wb") as configFile:
51                         config.write(configFile)
52
53         def _load_settings(self):
54                 config = ConfigParser.SafeConfigParser()
55                 config.read(constants._user_settings_)
56
57                 self._REPLACEME.load_settings(config, "Windows")
58
59         @misc_utils.log_exception(_moduleLogger)
60         def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
61                 """
62                 For system_inactivity, we have no background tasks to pause
63
64                 @note Hildon specific
65                 """
66                 if memory_low:
67                         gc.collect()
68
69                 if save_unsaved_data or shutdown:
70                         self._save_settings()
71
72         @misc_utils.log_exception(_moduleLogger)
73         def _on_destroy(self, widget = None, data = None):
74                 try:
75                         self.quit()
76                 finally:
77                         gtk.main_quit()
78
79         def quit(self):
80                 try:
81                         self._save_settings()
82                 except Exception:
83                         _moduleLogger.exception("Error saving settigns")
84
85                 try:
86                         self._deviceState.close()
87                 except AttributeError:
88                         pass # Either None or close was removed (in Fremantle)
89                 except Exception:
90                         _moduleLogger.exception("Error closing device state")
91                 try:
92                         self._osso_c.close()
93                 except AttributeError:
94                         pass # Either None or close was removed (in Fremantle)
95                 except Exception:
96                         _moduleLogger.exception("Error closing osso state")
97
98
99 def run():
100         gobject.threads_init()
101         gtk.gdk.threads_init()
102         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
103
104         # HACK Playback while silent on Maemo 5
105         hildonize.set_application_name("FMRadio")
106
107         app = REPLACEMElProgram()
108         if not PROFILE_STARTUP:
109                 try:
110                         gtk.main()
111                 except KeyboardInterrupt:
112                         app.quit()
113                         raise
114         else:
115                 app.quit()
116
117
118 if __name__ == "__main__":
119         logging.basicConfig(level=logging.DEBUG)
120         run()