2 # -*- coding: utf-8 -*-
5 @todo Reverse order option. Toggle between playing ascending/descending chronological order
8 from __future__ import with_statement
13 import logging.handlers
18 import dbus.mainloop.glib
29 import util.misc as misc_utils
37 _moduleLogger = logging.getLogger(__name__)
38 PROFILE_STARTUP = False
41 class WatersOfShiloahProgram(hildonize.get_app_class()):
44 super(WatersOfShiloahProgram, self).__init__()
45 currentPath = os.path.abspath(__file__)
46 for dirName in ["share", "data"]:
47 storePath = os.path.join(os.path.split(os.path.dirname(currentPath))[0], dirName)
48 if os.path.isdir(storePath):
50 self._store = imagestore.ImageStore(storePath, constants._cache_path_)
51 self._index = stream_index.AudioIndex()
52 self._player = player.Player(self._index)
57 if not hildonize.IS_HILDON_SUPPORTED:
58 _moduleLogger.info("No hildonization support")
61 self._osso_c = osso.Context(constants.__app_name__, constants.__version__, False)
62 self._deviceState = osso.DeviceState(self._osso_c)
63 self._deviceState.set_device_state_callback(self._on_device_state_change, 0)
65 _moduleLogger.info("No osso support")
67 self._deviceState = None
69 self._sourceSelector = windows.source.SourceSelector(self, self._player, self._store, self._index)
70 self._sourceSelector.window.connect("destroy", self._on_destroy)
71 self._sourceSelector.window.set_default_size(400, 800)
72 self._sourceSelector.show()
79 def _save_settings(self):
80 config = ConfigParser.SafeConfigParser()
82 self._sourceSelector.save_settings(config, "Windows")
84 with open(constants._user_settings_, "wb") as configFile:
85 config.write(configFile)
87 def _load_settings(self):
88 config = ConfigParser.SafeConfigParser()
89 config.read(constants._user_settings_)
91 self._sourceSelector.load_settings(config, "Windows")
93 @misc_utils.log_exception(_moduleLogger)
94 def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
96 For system_inactivity, we have no background tasks to pause
103 if save_unsaved_data or shutdown:
104 self._save_settings()
106 @misc_utils.log_exception(_moduleLogger)
107 def _on_destroy(self, widget = None, data = None):
115 self._save_settings()
117 _moduleLogger.exception("Error saving settigns")
122 _moduleLogger.exception("Error stopping player")
126 _moduleLogger.exception("Error stopping index")
130 _moduleLogger.exception("Error stopping store")
133 self._deviceState.close()
134 except AttributeError:
135 pass # Either None or close was removed (in Fremantle)
137 _moduleLogger.exception("Error closing device state")
140 except AttributeError:
141 pass # Either None or close was removed (in Fremantle)
143 _moduleLogger.exception("Error closing osso state")
148 os.makedirs(constants._data_path_)
154 os.makedirs(constants._cache_path_)
159 logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
160 logging.basicConfig(level=logging.DEBUG, format=logFormat)
161 rotating = logging.handlers.RotatingFileHandler(constants._user_logpath_, maxBytes=512*1024, backupCount=1)
162 rotating.setFormatter(logging.Formatter(logFormat))
163 root = logging.getLogger()
164 root.addHandler(rotating)
165 _moduleLogger.info("%s %s-%s" % (constants.__app_name__, constants.__version__, constants.__build__))
166 _moduleLogger.info("OS: %s" % (os.uname()[0], ))
167 _moduleLogger.info("Kernel: %s (%s) for %s" % os.uname()[2:])
168 _moduleLogger.info("Hostname: %s" % os.uname()[1])
170 gobject.threads_init()
171 gtk.gdk.threads_init()
172 l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
174 # HACK Playback while silent on Maemo 5
175 hildonize.set_application_name("FMRadio")
177 app = WatersOfShiloahProgram()
178 if not PROFILE_STARTUP:
181 except KeyboardInterrupt:
188 if __name__ == "__main__":
189 logging.basicConfig(level=logging.DEBUG)