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