Tweaking about box
[jamaendo] / jamaui / postoffice.py
1 #!/usr/bin/env python
2 #
3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
5 #
6 # Jamaendo is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Jamaendo is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Player code heavily based on http://thpinfo.com/2008/panucci/:
20 #  A resuming media player for Podcasts and Audiobooks
21 #  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
22 #  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
23 #
24 # message central
25
26 class PostOffice(object):
27     class Client(object):
28         def __init__(self):
29             self.tags = {}
30         def has_tag(self, tag):
31             return tag in self.tags
32         def notify(self, tags, data):
33             for tag in tags:
34                 cb = self.tags.get(tag)
35                 if cb:
36                     cb(data)
37         def register(self, tag, callback):
38             self.tags[tag] = callback
39
40     def __init__(self):
41         self.clients = {}
42
43     def notify(self, tags, data):
44         if not isinstance(tags, list):
45             tags = [tags]
46         for client in clients:
47             client.notify(tags, data)
48
49     def register(self, client_id, tag, callback):
50         client = self.clients.get(client_id)
51         if not client:
52             client = Client()
53             self.clients[client_id] = client
54         client.register(tag, callback)
55
56 postoffice = PostOffice()
57
58