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