Fixing an issue with finding images
[watersofshiloah] / src / watersofshiloah_gtk.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 @todo Reverse order option.  Toggle between playing ascending/descending chronological order
6 """
7
8 from __future__ import with_statement
9
10 import os
11 import gc
12 import logging
13 import ConfigParser
14
15 import gobject
16 import dbus
17 import dbus.mainloop.glib
18 import gtk
19
20 try:
21         import osso as _osso
22         osso = _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 WatersOfShiloahProgram(hildonize.get_app_class()):
41
42         def __init__(self):
43                 super(WatersOfShiloahProgram, self).__init__()
44                 currentPath = os.path.abspath(__file__)
45                 for dirName in ["share", "data"]:
46                         storePath = os.path.join(os.path.split(os.path.dirname(currentPath))[0], dirName)
47                         if os.path.isdir(storePath):
48                                 break
49                 self._store = imagestore.ImageStore(storePath, constants._cache_path_)
50                 self._index = stream_index.AudioIndex()
51                 self._player = player.Player(self._index)
52
53                 self._store.start()
54                 self._index.start()
55                 try:
56                         if not hildonize.IS_HILDON_SUPPORTED:
57                                 _moduleLogger.info("No hildonization support")
58
59                         if osso is not None:
60                                 self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
61                                 self._deviceState = osso.DeviceState(self._osso_c)
62                                 self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
63                         else:
64                                 _moduleLogger.info("No osso support")
65                                 self._osso_c = None
66                                 self._deviceState = None
67
68                         self._sourceSelector = windows.source.SourceSelector(self, self._player, self._store, self._index)
69                         self._sourceSelector.window.connect("destroy", self._on_destroy)
70                         self._sourceSelector.window.set_default_size(400, 800)
71                         self._sourceSelector.show()
72                         self._load_settings()
73                 except:
74                         self._index.stop()
75                         self._store.stop()
76                         raise
77
78         def _save_settings(self):
79                 config = ConfigParser.SafeConfigParser()
80
81                 self._sourceSelector.save_settings(config, "Windows")
82
83                 with open(constants._user_settings_, "wb") as configFile:
84                         config.write(configFile)
85
86         def _load_settings(self):
87                 config = ConfigParser.SafeConfigParser()
88                 config.read(constants._user_settings_)
89
90                 self._sourceSelector.load_settings(config, "Windows")
91
92         @misc_utils.log_exception(_moduleLogger)
93         def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
94                 """
95                 For system_inactivity, we have no background tasks to pause
96
97                 @note Hildon specific
98                 """
99                 if memory_low:
100                         gc.collect()
101
102                 if save_unsaved_data or shutdown:
103                         self._save_settings()
104
105         @misc_utils.log_exception(_moduleLogger)
106         def _on_destroy(self, widget = None, data = None):
107                 try:
108                         self.quit()
109                 finally:
110                         gtk.main_quit()
111
112         def quit(self):
113                 try:
114                         self._save_settings()
115                 except Exception:
116                         _moduleLogger.exception("Error saving settigns")
117
118                 try:
119                         self._player.stop()
120                 except Exception:
121                         _moduleLogger.exception("Error stopping player")
122                 try:
123                         self._index.stop()
124                 except Exception:
125                         _moduleLogger.exception("Error stopping index")
126                 try:
127                         self._store.stop()
128                 except Exception:
129                         _moduleLogger.exception("Error stopping store")
130
131                 try:
132                         self._deviceState.close()
133                 except AttributeError:
134                         pass # Either None or close was removed (in Fremantle)
135                 except Exception:
136                         _moduleLogger.exception("Error closing device state")
137                 try:
138                         self._osso_c.close()
139                 except AttributeError:
140                         pass # Either None or close was removed (in Fremantle)
141                 except Exception:
142                         _moduleLogger.exception("Error closing osso state")
143
144
145 def run():
146         gobject.threads_init()
147         gtk.gdk.threads_init()
148         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
149
150         # HACK Playback while silent on Maemo 5
151         hildonize.set_application_name("FMRadio")
152
153         app = WatersOfShiloahProgram()
154         if not PROFILE_STARTUP:
155                 try:
156                         gtk.main()
157                 except KeyboardInterrupt:
158                         app.quit()
159                         raise
160         else:
161                 app.quit()
162
163
164 if __name__ == "__main__":
165         logging.basicConfig(level=logging.DEBUG)
166         run()