Removed URLFetch module (its functionality is now provided by Fetcher module).
[straw.git] / straw / MessageManager.py
blob6513b4100b59f31fc28fcfb2a74fc19f7d37a39d
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 from subscribe import subscribe_show
30 # Based on the values from NetworkManager/include/NetworkManager.h
31 # We only care about CONNECTED and DISCONNECTED at the moment.
32 NM_STATE_CONNECTED = 3
33 NM_STATE_DISCONNECTED = 4
35 class FeedReader(dbus.service.Object):
36 service_name = "org.gnome.feed.Reader"
37 object_path = "/org/gnome/feed/Reader"
39 def __init__(self):
40 try:
41 self._session_bus = dbus.SessionBus()
42 self._service = dbus.service.BusName(self.service_name, bus=self._session_bus)
43 dbus.service.Object.__init__(self, self._service, self.object_path)
44 except DBusException, e:
45 logging.info(_("Error while initializing feed subscribe service"))
47 @dbus.service.method("org.gnome.feed.Reader")
48 def Subscribe(self, url):
49 subscribe_show(url)
50 return True
52 class NetworkListener:
53 SERVICE_NAME = "org.freedesktop.NetworkManager"
54 SERVICE_PATH = "/org/freedesktop/NetworkManager"
56 def __init__(self):
57 self._config = Config.get_instance()
59 def set_state(self, state):
60 if state == NM_STATE_CONNECTED:
61 self._config.offline = False
62 else:
63 self._config.offline = True
65 def active_cb(self, path):
66 self._config.offline = False
68 def inactive_cb(self, path):
69 self._config.offline = True
72 def start_services():
73 fr = FeedReader()
75 try:
76 systemBus = dbus.SystemBus()
77 proxy_obj = systemBus.get_object(NetworkListener.SERVICE_NAME,
78 NetworkListener.SERVICE_PATH)
79 nl = NetworkListener()
80 # don't touch offline if it has been previously set.
81 if not Config.get_instance().offline:
82 nl.set_state(proxy_obj.state())
84 nm_interface = dbus.Interface(proxy_obj, NetworkListener.SERVICE_NAME)
85 nm_interface.connect_to_signal('DeviceNowActive', nl.active_cb)
86 nm_interface.connect_to_signal('DeviceNoLongerActive', nl.inactive_cb)
87 except DBusException, de:
88 logging.info(_("Unable to find NetworkManager service"))
91 class StatusMessageManager(gobject.GObject):
93 __gsignals__ = {
94 'changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,())
97 def __init__(self):
98 gobject.GObject.__init__(self)
99 self.__messages = []
101 def post_message(self, message):
102 self.__messages.append(message)
103 self.emit('changed')
105 def read_message(self):
106 return self.__messages.pop(0)
108 def number_of_messages(self):
109 return len(self.__messages)
111 _smmanager = None
112 def post_status_message(text):
113 global _smmanager
114 if not _smmanager:
115 get_status_manager()
116 _smmanager.post_message(text)
118 def get_status_manager():
119 global _smmanager
120 if not _smmanager:
121 _smmanager = StatusMessageManager()
122 return _smmanager