Switching over to pressed icons and matching the view of the home icon with other...
[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 @bug Fix presenter display
8 @todo Track recent
9 @todo Favorites
10 @todo Sequential playback
11 @todo Enable Call Monitor
12 @todo Audio seek bar
13 @todo Persisted Pause
14 @todo Reverse order option.  Toggle between playing ascending/descending chronological order
15 @todo Podcast integration
16 """
17
18 from __future__ import with_statement
19
20 import gc
21 import logging
22 import ConfigParser
23
24 import gobject
25 import gtk
26
27 try:
28         import osso
29 except ImportError:
30         osso = None
31
32 import constants
33 import hildonize
34 import util.misc as misc_utils
35
36 import imagestore
37 import player
38 import stream_index
39 import windows
40
41
42 _moduleLogger = logging.getLogger(__name__)
43 PROFILE_STARTUP = False
44
45
46 class MormonChannelProgram(hildonize.get_app_class()):
47
48         def __init__(self):
49                 super(MormonChannelProgram, self).__init__()
50                 self._store = imagestore.ImageStore("../data", "../data")
51                 self._index = stream_index.AudioIndex()
52                 self._player = player.Player(self._index)
53
54                 self._index.start()
55                 try:
56
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
144         hildonize.set_application_title(constants.__pretty_app_name__)
145         app = MormonChannelProgram()
146         if not PROFILE_STARTUP:
147                 try:
148                         gtk.main()
149                 except KeyboardInterrupt:
150                         app.quit()
151                         raise
152
153
154 if __name__ == "__main__":
155         logging.basicConfig(level=logging.DEBUG)
156         run()