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