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