Updated TODO.
[straw.git] / straw / MessageManager.py
blob03ea9858bf027e3d6c891e31b2c1278b51c93d12
1 """ MessageManager.py
3 Manages status messages
4 """
5 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__ = """
7 Straw is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
12 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 import logging
21 import dbus
22 import dbus.service
23 import dbus.glib
24 from dbus import DBusException
25 import gobject
26 import Config
28 import straw
31 # Based on the values from NetworkManager/include/NetworkManager.h
32 # We only care about CONNECTED and DISCONNECTED at the moment.
33 NM_STATE_CONNECTED = 3
34 NM_STATE_DISCONNECTED = 4
36 class FeedReader(dbus.service.Object):
37 service_name = "org.gnome.feed.Reader"
38 object_path = "/org/gnome/feed/Reader"
40 def __init__(self):
41 try:
42 self._session_bus = dbus.SessionBus()
43 self._service = dbus.service.BusName(self.service_name, bus=self._session_bus)
44 dbus.service.Object.__init__(self, self._service, self.object_path)
45 except DBusException, e:
46 logging.info(_("Error while initializing feed subscribe service"))
48 @dbus.service.method("org.gnome.feed.Reader")
49 def Subscribe(self, url):
50 straw.subscribe_show(url)
51 return True
53 class NetworkListener:
54 SERVICE_NAME = "org.freedesktop.NetworkManager"
55 SERVICE_PATH = "/org/freedesktop/NetworkManager"
57 def __init__(self):
58 self._config = Config.get_instance()
60 def set_state(self, state):
61 if state == NM_STATE_CONNECTED:
62 self._config.offline = False
63 else:
64 self._config.offline = True
66 def active_cb(self, path):
67 self._config.offline = False
69 def inactive_cb(self, path):
70 self._config.offline = True
73 def start_services():
74 fr = FeedReader()
76 try:
77 systemBus = dbus.SystemBus()
78 proxy_obj = systemBus.get_object(NetworkListener.SERVICE_NAME,
79 NetworkListener.SERVICE_PATH)
80 nl = NetworkListener()
81 # don't touch offline if it has been previously set.
82 if not Config.get_instance().offline:
83 nl.set_state(proxy_obj.state())
85 nm_interface = dbus.Interface(proxy_obj, NetworkListener.SERVICE_NAME)
86 nm_interface.connect_to_signal('DeviceNowActive', nl.active_cb)
87 nm_interface.connect_to_signal('DeviceNoLongerActive', nl.inactive_cb)
88 except DBusException, de:
89 logging.info(_("Unable to find NetworkManager service"))
92 class StatusMessageManager(gobject.GObject):
94 __gsignals__ = {
95 'changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,())
98 def __init__(self):
99 gobject.GObject.__init__(self)
100 self.__messages = []
102 def post_message(self, message):
103 self.__messages.append(message)
104 self.emit('changed')
106 def read_message(self):
107 return self.__messages.pop(0)
109 def number_of_messages(self):
110 return len(self.__messages)
112 _smmanager = None
113 def post_status_message(text):
114 global _smmanager
115 if not _smmanager:
116 get_status_manager()
117 _smmanager.post_message(text)
119 def get_status_manager():
120 global _smmanager
121 if not _smmanager:
122 _smmanager = StatusMessageManager()
123 return _smmanager