40727abab21df7ab95ef29a28a38c61343c5eb3d
[watersofshiloah] / src / mormonchannel_gtk.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 @bug Fix segfault on closing of window while playing
6 @todo Add images for Magazines and Issues
7 @todo Need to confirm id's are persistent (not just for todos but broken behavior on transition)
8         @todo Track recent
9         @todo Persisted Pause
10         @todo Favorites
11 @todo Sleep timer
12 @todo Reverse order option.  Toggle between playing ascending/descending chronological order
13 @todo Podcast integration
14 """
15
16 from __future__ import with_statement
17
18 import os
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                 currentPath = os.path.abspath(__file__)
52                 storePath = os.path.join(os.path.split(os.path.dirname(currentPath))[0], "data")
53                 self._store = imagestore.ImageStore(storePath, constants._cache_path_)
54                 self._index = stream_index.AudioIndex()
55                 self._player = player.Player(self._index)
56
57                 self._index.start()
58                 try:
59                         if not hildonize.IS_HILDON_SUPPORTED:
60                                 _moduleLogger.info("No hildonization support")
61
62                         if osso is not None:
63                                 self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
64                                 self._deviceState = osso.DeviceState(self._osso_c)
65                                 self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
66                         else:
67                                 _moduleLogger.info("No osso support")
68                                 self._osso_c = None
69                                 self._deviceState = None
70
71                         self._sourceSelector = windows.source.SourceSelector(self._player, self._store, self._index)
72                         self._sourceSelector.window.connect("destroy", self._on_destroy)
73                         self._sourceSelector.show()
74                         self._load_settings()
75                 except:
76                         self._index.stop()
77                         raise
78
79         def _save_settings(self):
80                 config = ConfigParser.SafeConfigParser()
81
82                 self._sourceSelector.save_settings(config, "Windows")
83
84                 with open(constants._user_settings_, "wb") as configFile:
85                         config.write(configFile)
86
87         def _load_settings(self):
88                 config = ConfigParser.SafeConfigParser()
89                 config.read(constants._user_settings_)
90
91                 self._sourceSelector.load_settings(config, "Windows")
92
93         @misc_utils.log_exception(_moduleLogger)
94         def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
95                 """
96                 For system_inactivity, we have no background tasks to pause
97
98                 @note Hildon specific
99                 """
100                 if memory_low:
101                         gc.collect()
102
103                 if save_unsaved_data or shutdown:
104                         self._save_settings()
105
106         @misc_utils.log_exception(_moduleLogger)
107         def _on_destroy(self, widget = None, data = None):
108                 self.quit()
109
110         def quit(self):
111                 try:
112                         self._save_settings()
113
114                         self._index.stop()
115
116                         try:
117                                 self._deviceState.close()
118                         except AttributeError:
119                                 pass # Either None or close was removed (in Fremantle)
120                         try:
121                                 self._osso_c.close()
122                         except AttributeError:
123                                 pass # Either None or close was removed (in Fremantle)
124                 finally:
125                         gtk.main_quit()
126
127         @misc_utils.log_exception(_moduleLogger)
128         def _on_show_about(self, widget = None, data = None):
129                 dialog = gtk.AboutDialog()
130                 dialog.set_position(gtk.WIN_POS_CENTER)
131                 dialog.set_name(constants.__pretty_app_name__)
132                 dialog.set_version(constants.__version__)
133                 dialog.set_copyright("")
134                 dialog.set_website("")
135                 comments = "Mormon Radio and Audiobook Player"
136                 dialog.set_comments(comments)
137                 dialog.set_authors(["Ed Page <eopage@byu.net>"])
138                 dialog.run()
139                 dialog.destroy()
140
141
142 def run():
143         gobject.threads_init()
144         gtk.gdk.threads_init()
145         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
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()