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