Misc work from Maemo 5 usage
[watersofshiloah] / src / mormonchannel_gtk.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 @todo Timezone issue with Mormon CHannel
6 @todo Fix scrollto for Fremantle
7 @todo backgrounds need some resizing
8 @todo Icons on source selector need work
9 @bug For some reason, the back/close button doesn't work when I nest multiple levels
10 @todo Need to confirm id's are persistent (not just for todos but broken behavior on transition)
11         @todo Track recent
12         @todo Persisted Pause
13         @todo Favorites
14 @todo Sleep timer
15 @todo Reverse order option.  Toggle between playing ascending/descending chronological order
16 @todo Podcast integration
17 """
18
19 from __future__ import with_statement
20
21 import os
22 import gc
23 import logging
24 import ConfigParser
25
26 import gobject
27 import dbus
28 import dbus.mainloop.glib
29 import gtk
30
31 try:
32         import osso
33 except ImportError:
34         osso = None
35
36 import constants
37 import hildonize
38 import util.misc as misc_utils
39
40 import imagestore
41 import player
42 import stream_index
43 import windows
44
45
46 _moduleLogger = logging.getLogger(__name__)
47 PROFILE_STARTUP = False
48
49
50 class MormonChannelProgram(hildonize.get_app_class()):
51
52         def __init__(self):
53                 super(MormonChannelProgram, self).__init__()
54                 currentPath = os.path.abspath(__file__)
55                 storePath = os.path.join(os.path.split(os.path.dirname(currentPath))[0], "data")
56                 self._store = imagestore.ImageStore(storePath, constants._cache_path_)
57                 self._index = stream_index.AudioIndex()
58                 self._player = player.Player(self._index)
59
60                 self._store.start()
61                 self._index.start()
62                 try:
63                         if not hildonize.IS_HILDON_SUPPORTED:
64                                 _moduleLogger.info("No hildonization support")
65
66                         if osso is not None:
67                                 self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
68                                 self._deviceState = osso.DeviceState(self._osso_c)
69                                 self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
70                         else:
71                                 _moduleLogger.info("No osso support")
72                                 self._osso_c = None
73                                 self._deviceState = None
74
75                         self._sourceSelector = windows.source.SourceSelector(self, self._player, self._store, self._index)
76                         self._sourceSelector.window.connect("destroy", self._on_destroy)
77                         self._sourceSelector.window.set_default_size(400, 800)
78                         self._sourceSelector.show()
79                         self._load_settings()
80                 except:
81                         self._index.stop()
82                         self._store.stop()
83                         raise
84
85         def _save_settings(self):
86                 config = ConfigParser.SafeConfigParser()
87
88                 self._sourceSelector.save_settings(config, "Windows")
89
90                 with open(constants._user_settings_, "wb") as configFile:
91                         config.write(configFile)
92
93         def _load_settings(self):
94                 config = ConfigParser.SafeConfigParser()
95                 config.read(constants._user_settings_)
96
97                 self._sourceSelector.load_settings(config, "Windows")
98
99         @misc_utils.log_exception(_moduleLogger)
100         def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
101                 """
102                 For system_inactivity, we have no background tasks to pause
103
104                 @note Hildon specific
105                 """
106                 if memory_low:
107                         gc.collect()
108
109                 if save_unsaved_data or shutdown:
110                         self._save_settings()
111
112         @misc_utils.log_exception(_moduleLogger)
113         def _on_destroy(self, widget = None, data = None):
114                 self.quit()
115
116         def quit(self):
117                 try:
118                         self._save_settings()
119
120                         self._index.stop()
121                         self._store.stop()
122
123                         try:
124                                 self._deviceState.close()
125                         except AttributeError:
126                                 pass # Either None or close was removed (in Fremantle)
127                         try:
128                                 self._osso_c.close()
129                         except AttributeError:
130                                 pass # Either None or close was removed (in Fremantle)
131                 finally:
132                         gtk.main_quit()
133
134         @misc_utils.log_exception(_moduleLogger)
135         def _on_show_about(self, widget = None, data = None):
136                 dialog = gtk.AboutDialog()
137                 dialog.set_position(gtk.WIN_POS_CENTER)
138                 dialog.set_name(constants.__pretty_app_name__)
139                 dialog.set_version(constants.__version__)
140                 dialog.set_copyright("")
141                 dialog.set_website("")
142                 comments = "Mormon Radio and Audiobook Player"
143                 dialog.set_comments(comments)
144                 dialog.set_authors(["Ed Page <eopage@byu.net>"])
145                 dialog.run()
146                 dialog.destroy()
147
148
149 def run():
150         gobject.threads_init()
151         gtk.gdk.threads_init()
152         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
153
154         hildonize.set_application_title(constants.__pretty_app_name__)
155         app = MormonChannelProgram()
156         if not PROFILE_STARTUP:
157                 try:
158                         gtk.main()
159                 except KeyboardInterrupt:
160                         app.quit()
161                         raise
162
163
164 if __name__ == "__main__":
165         logging.basicConfig(level=logging.DEBUG)
166         run()