Adding additional sources
[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 Reverse order option.  Toggle between playing ascending/descending chronological order
11 @todo Podcast integration
12 """
13
14 from __future__ import with_statement
15
16 import gc
17 import logging
18 import ConfigParser
19
20 import gobject
21 import dbus
22 import dbus.mainloop.glib
23 import gtk
24
25 try:
26         import osso
27 except ImportError:
28         osso = None
29
30 import constants
31 import hildonize
32 import util.misc as misc_utils
33
34 import imagestore
35 import player
36 import stream_index
37 import windows
38
39
40 _moduleLogger = logging.getLogger(__name__)
41 PROFILE_STARTUP = False
42
43
44 class MormonChannelProgram(hildonize.get_app_class()):
45
46         def __init__(self):
47                 super(MormonChannelProgram, self).__init__()
48                 self._store = imagestore.ImageStore("../data", "../data")
49                 self._index = stream_index.AudioIndex()
50                 self._player = player.Player(self._index)
51
52                 self._index.start()
53                 try:
54                         if not hildonize.IS_HILDON_SUPPORTED:
55                                 _moduleLogger.info("No hildonization support")
56
57                         if osso is not None:
58                                 self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
59                                 self._deviceState = osso.DeviceState(self._osso_c)
60                                 self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
61                         else:
62                                 _moduleLogger.info("No osso support")
63                                 self._osso_c = None
64                                 self._deviceState = None
65
66                         self._sourceSelector = windows.source.SourceSelector(self._player, self._store, self._index)
67                         self._sourceSelector.window.connect("destroy", self._on_destroy)
68                         self._sourceSelector.show()
69                         self._load_settings()
70                 except:
71                         self._index.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                 self.quit()
104
105         def quit(self):
106                 try:
107                         self._save_settings()
108
109                         self._index.stop()
110
111                         try:
112                                 self._deviceState.close()
113                         except AttributeError:
114                                 pass # Either None or close was removed (in Fremantle)
115                         try:
116                                 self._osso_c.close()
117                         except AttributeError:
118                                 pass # Either None or close was removed (in Fremantle)
119                 finally:
120                         gtk.main_quit()
121
122         @misc_utils.log_exception(_moduleLogger)
123         def _on_show_about(self, widget = None, data = None):
124                 dialog = gtk.AboutDialog()
125                 dialog.set_position(gtk.WIN_POS_CENTER)
126                 dialog.set_name(constants.__pretty_app_name__)
127                 dialog.set_version(constants.__version__)
128                 dialog.set_copyright("")
129                 dialog.set_website("")
130                 comments = "Mormon Radio and Audiobook Player"
131                 dialog.set_comments(comments)
132                 dialog.set_authors(["Ed Page <eopage@byu.net>"])
133                 dialog.run()
134                 dialog.destroy()
135
136
137 def run():
138         gobject.threads_init()
139         gtk.gdk.threads_init()
140         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
141
142         hildonize.set_application_title(constants.__pretty_app_name__)
143         app = MormonChannelProgram()
144         if not PROFILE_STARTUP:
145                 try:
146                         gtk.main()
147                 except KeyboardInterrupt:
148                         app.quit()
149                         raise
150
151
152 if __name__ == "__main__":
153         logging.basicConfig(level=logging.DEBUG)
154         run()