b23179954ba237a447f751725a73c3b646fb56a8
[watersofshiloah] / src / mormonchannel_gtk.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 @todo Reverse order option.  Toggle between playing ascending/descending chronological order
6 """
7
8 from __future__ import with_statement
9
10 import os
11 import gc
12 import logging
13 import ConfigParser
14
15 import gobject
16 import dbus
17 import dbus.mainloop.glib
18 import gtk
19
20 try:
21         import osso
22 except ImportError:
23         osso = None
24
25 import constants
26 import hildonize
27 import util.misc as misc_utils
28
29 import imagestore
30 import player
31 import stream_index
32 import windows
33
34
35 _moduleLogger = logging.getLogger(__name__)
36 PROFILE_STARTUP = False
37
38
39 class MormonChannelProgram(hildonize.get_app_class()):
40
41         def __init__(self):
42                 super(MormonChannelProgram, self).__init__()
43                 currentPath = os.path.abspath(__file__)
44                 storePath = os.path.join(os.path.split(os.path.dirname(currentPath))[0], "data")
45                 self._store = imagestore.ImageStore(storePath, constants._cache_path_)
46                 self._index = stream_index.AudioIndex()
47                 self._player = player.Player(self._index)
48
49                 self._store.start()
50                 self._index.start()
51                 try:
52                         if not hildonize.IS_HILDON_SUPPORTED:
53                                 _moduleLogger.info("No hildonization support")
54
55                         if osso is not None:
56                                 self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
57                                 self._deviceState = osso.DeviceState(self._osso_c)
58                                 self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
59                         else:
60                                 _moduleLogger.info("No osso support")
61                                 self._osso_c = None
62                                 self._deviceState = None
63
64                         self._sourceSelector = windows.source.SourceSelector(self, self._player, self._store, self._index)
65                         self._sourceSelector.window.connect("destroy", self._on_destroy)
66                         self._sourceSelector.window.set_default_size(400, 800)
67                         self._sourceSelector.show()
68                         self._load_settings()
69                 except:
70                         self._index.stop()
71                         self._store.stop()
72                         raise
73
74         def _save_settings(self):
75                 config = ConfigParser.SafeConfigParser()
76
77                 self._sourceSelector.save_settings(config, "Windows")
78
79                 with open(constants._user_settings_, "wb") as configFile:
80                         config.write(configFile)
81
82         def _load_settings(self):
83                 config = ConfigParser.SafeConfigParser()
84                 config.read(constants._user_settings_)
85
86                 self._sourceSelector.load_settings(config, "Windows")
87
88         @misc_utils.log_exception(_moduleLogger)
89         def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
90                 """
91                 For system_inactivity, we have no background tasks to pause
92
93                 @note Hildon specific
94                 """
95                 if memory_low:
96                         gc.collect()
97
98                 if save_unsaved_data or shutdown:
99                         self._save_settings()
100
101         @misc_utils.log_exception(_moduleLogger)
102         def _on_destroy(self, widget = None, data = None):
103                 try:
104                         self.quit()
105                 finally:
106                         gtk.main_quit()
107
108         def quit(self):
109                 try:
110                         self._save_settings()
111                 except Exception:
112                         _moduleLogger.exception("Error saving settigns")
113
114                 try:
115                         self._player.stop()
116                 except Exception:
117                         _moduleLogger.exception("Error stopping player")
118                 try:
119                         self._index.stop()
120                 except Exception:
121                         _moduleLogger.exception("Error stopping index")
122                 try:
123                         self._store.stop()
124                 except Exception:
125                         _moduleLogger.exception("Error stopping store")
126
127                 try:
128                         self._deviceState.close()
129                 except AttributeError:
130                         pass # Either None or close was removed (in Fremantle)
131                 except Exception:
132                         _moduleLogger.exception("Error closing device state")
133                 try:
134                         self._osso_c.close()
135                 except AttributeError:
136                         pass # Either None or close was removed (in Fremantle)
137                 except Exception:
138                         _moduleLogger.exception("Error closing osso state")
139
140
141 def run():
142         gobject.threads_init()
143         gtk.gdk.threads_init()
144         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
145
146         # HACK Playback while silent on Maemo 5
147         hildonize.set_application_name("FMRadio")
148
149         app = MormonChannelProgram()
150         if not PROFILE_STARTUP:
151                 try:
152                         gtk.main()
153                 except KeyboardInterrupt:
154                         app.quit()
155                         raise
156         else:
157                 app.quit()
158
159
160 if __name__ == "__main__":
161         logging.basicConfig(level=logging.DEBUG)
162         run()