7c570cde0e90b3bc9d7fd79b6f4d9a9ca72a871a
[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 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 os
18 import gc
19 import logging
20 import ConfigParser
21
22 import gobject
23 import dbus
24 import dbus.mainloop.glib
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                 currentPath = os.path.abspath(__file__)
51                 storePath = os.path.join(os.path.split(os.path.dirname(currentPath))[0], "data")
52                 self._store = imagestore.ImageStore(storePath, constants._cache_path_)
53                 self._index = stream_index.AudioIndex()
54                 self._player = player.Player(self._index)
55
56                 self._store.start()
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                         self._store.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                         self._store.stop()
117
118                         try:
119                                 self._deviceState.close()
120                         except AttributeError:
121                                 pass # Either None or close was removed (in Fremantle)
122                         try:
123                                 self._osso_c.close()
124                         except AttributeError:
125                                 pass # Either None or close was removed (in Fremantle)
126                 finally:
127                         gtk.main_quit()
128
129         @misc_utils.log_exception(_moduleLogger)
130         def _on_show_about(self, widget = None, data = None):
131                 dialog = gtk.AboutDialog()
132                 dialog.set_position(gtk.WIN_POS_CENTER)
133                 dialog.set_name(constants.__pretty_app_name__)
134                 dialog.set_version(constants.__version__)
135                 dialog.set_copyright("")
136                 dialog.set_website("")
137                 comments = "Mormon Radio and Audiobook Player"
138                 dialog.set_comments(comments)
139                 dialog.set_authors(["Ed Page <eopage@byu.net>"])
140                 dialog.run()
141                 dialog.destroy()
142
143
144 def run():
145         gobject.threads_init()
146         gtk.gdk.threads_init()
147         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
148
149         hildonize.set_application_title(constants.__pretty_app_name__)
150         app = MormonChannelProgram()
151         if not PROFILE_STARTUP:
152                 try:
153                         gtk.main()
154                 except KeyboardInterrupt:
155                         app.quit()
156                         raise
157
158
159 if __name__ == "__main__":
160         logging.basicConfig(level=logging.DEBUG)
161         run()