X-Git-Url: http://git.maemo.org/git/?p=jamaendo;a=blobdiff_plain;f=jamaui%2Fpostoffice.py;h=675f9bffa7c9e8e14e1f2bc34fb885bfc966ddf1;hp=88143fe9c6a3233714b9bbb1dfc11487ddd09d4b;hb=44b759cfcfb80d94ddac5ea11302a6f94cb307b4;hpb=75215e5b54a5357384db5166fbecaa65164d8b94;ds=sidebyside diff --git a/jamaui/postoffice.py b/jamaui/postoffice.py index 88143fe..675f9bf 100644 --- a/jamaui/postoffice.py +++ b/jamaui/postoffice.py @@ -1,34 +1,60 @@ +#!/usr/bin/env python +# +# This file is part of Jamaendo. +# Copyright (c) 2010 Kristoffer Gronlund +# +# Jamaendo is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Jamaendo is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Jamaendo. If not, see . +# +# Player code heavily based on http://thpinfo.com/2008/panucci/: +# A resuming media player for Podcasts and Audiobooks +# Copyright (c) 2008-05-26 Thomas Perl +# (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html) +# # message central +import logging + +log = logging.getLogger(__name__) + class PostOffice(object): - class Client(object): - def __init__(self): - self.tags = {} - def has_tag(self, tag): - return tag in self.tags - def notify(self, tags, data): - for tag in tags: - cb = self.tags.get(tag) - if cb: - cb(data) - def register(self, tag, callback): - self.tags[tag] = callback def __init__(self): - self.clients = {} - - def notify(self, tags, data): - if not isinstance(tags, list): - tags = [tags] - for client in clients: - client.notify(tags, data) - - def register(self, client_id, tag, callback): - client = self.clients.get(client_id) - if not client: - client = Client() - self.clients[client_id] = client - client.register(tag, callback) + self.tags = {} # tag -> [callback] + + def notify(self, tag, *data): + clients = self.tags.get(tag) + if clients: + log.debug("(%s %s) -> [%s]", + tag, + " ".join(str(x) for x in data), + " ".join(str(x) for x in clients)) + for client in clients: + client(*data) + + def connect(self, tag, callback): + if tag not in self.tags: + self.tags[tag] = [] + clients = self.tags[tag] + if callback not in clients: + clients.append(callback) + + def disconnect(self, tag, callback): + if tag not in self.tags: + self.tags[tag] = [] + clients = self.tags[tag] + if callback in clients: + clients.remove(callback) postoffice = PostOffice()