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