[gnome-applets] invest-applet: connects to the network manager via dbus to react on network changes, fixes bug #6057



commit 6bf5c60795549c0bd8f5802708e5295f8e6978fa
Author: Enrico Minack <enrico-minack gmx de>
Date:   Mon Jan 18 15:00:41 2010 +0100

    invest-applet: connects to the network manager via dbus to react on network changes, fixes bug #605739

 invest-applet/invest/Makefile.am       |    1 +
 invest-applet/invest/__init__.py       |    5 +++
 invest-applet/invest/networkmanager.py |   58 ++++++++++++++++++++++++++++++++
 invest-applet/invest/quotes.py         |   30 ++++++++++++++++-
 4 files changed, 93 insertions(+), 1 deletions(-)
---
diff --git a/invest-applet/invest/Makefile.am b/invest-applet/invest/Makefile.am
index 2399257..308149c 100644
--- a/invest-applet/invest/Makefile.am
+++ b/invest-applet/invest/Makefile.am
@@ -13,6 +13,7 @@ invest_PYTHON = \
 	chart.py \
 	widgets.py \
 	quotes.py \
+	networkmanager.py \
 	preferences.py
 nodist_invest_PYTHON = \
 	defs.py
diff --git a/invest-applet/invest/__init__.py b/invest-applet/invest/__init__.py
index fb75341..63cc9dd 100644
--- a/invest-applet/invest/__init__.py
+++ b/invest-applet/invest/__init__.py
@@ -6,6 +6,8 @@ import datetime
 import gtk, gtk.gdk, gconf, gobject
 import cPickle
 
+import networkmanager
+
 # Autotools set the actual data_dir in defs.py
 from defs import *
 
@@ -146,3 +148,6 @@ def get_gnome_proxy(client):
 		return None
 
 PROXY = get_gnome_proxy(client)
+
+# connect to Network Manager to identify current network connectivity
+nm = networkmanager.NetworkManager()
diff --git a/invest-applet/invest/networkmanager.py b/invest-applet/invest/networkmanager.py
new file mode 100644
index 0000000..00863ad
--- /dev/null
+++ b/invest-applet/invest/networkmanager.py
@@ -0,0 +1,58 @@
+import invest
+from dbus.mainloop.glib import DBusGMainLoop
+import dbus
+
+# possible states, see http://projects.gnome.org/NetworkManager/developers/spec-08.html#type-NM_STATE
+STATE_UNKNOWN		= dbus.UInt32(0)
+STATE_ASLEEP		= dbus.UInt32(1)
+STATE_CONNECTING	= dbus.UInt32(2)
+STATE_CONNECTED		= dbus.UInt32(3)
+STATE_DISCONNEDTED	= dbus.UInt32(4)
+
+class NetworkManager:
+	def __init__(self):
+		self.state = STATE_UNKNOWN
+		self.statechange_callback = None
+
+		try:
+			# get an event loop
+			loop = DBusGMainLoop()
+
+			# get the NetworkManager object from D-Bus
+			invest.debug("Connecting to Network Manager via D-Bus")
+			bus = dbus.SystemBus(mainloop=loop)
+			nmobj = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
+			nm = dbus.Interface(nmobj, 'org.freedesktop.NetworkManager')
+
+			# connect the signal handler to the bus
+			bus.add_signal_receiver(self.handler, None,
+					'org.freedesktop.NetworkManager',
+					'org.freedesktop.NetworkManager',
+					'/org/freedesktop/NetworkManager')
+
+			# get the current status of the network manager
+			self.state = nm.state()
+			invest.debug("Current Network Manager status is %d" % self.state)
+		except Exception, msg:
+			invest.error("Could not connect to the Network Manager: %s" % msg )
+
+	def online(self):
+		return self.state == STATE_UNKNOWN or self.state == STATE_CONNECTED
+
+	def offline(self):
+		return not self.online()
+
+	# the signal handler for signals from the network manager
+	def handler(self,signal=None):
+		if isinstance(signal, dict):
+			state = signal.get('State')
+			if state != None:
+				invest.debug("Network Manager change state %d => %d" % (self.state, state) );
+				self.state = state
+
+				# notify about state change
+				if self.statechange_callback != None:
+					self.statechange_callback()
+
+	def set_statechange_callback(self,handler):
+		self.statechange_callback = handler
diff --git a/invest-applet/invest/quotes.py b/invest-applet/invest/quotes.py
index 09d0f3a..3f1a096 100644
--- a/invest-applet/invest/quotes.py
+++ b/invest-applet/invest/quotes.py
@@ -66,16 +66,44 @@ class QuoteUpdater(gtk.ListStore):
 	updated = False
 	last_updated = None
 	quotes_valid = False
+	timeout_id = None
 	SYMBOL, LABEL, TICKER_ONLY, BALANCE, BALANCE_PCT, VALUE, VARIATION_PCT, PB = range(8)
 	def __init__ (self, change_icon_callback, set_tooltip_callback):
 		gtk.ListStore.__init__ (self, gobject.TYPE_STRING, gobject.TYPE_STRING, bool, float, float, float, float, gtk.gdk.Pixbuf)
-		gobject.timeout_add(AUTOREFRESH_TIMEOUT, self.refresh)
+		self.set_update_interval(AUTOREFRESH_TIMEOUT)
 		self.change_icon_callback = change_icon_callback
 		self.set_tooltip_callback = set_tooltip_callback
 		self.set_sort_column_id(1, gtk.SORT_ASCENDING)
 		self.refresh()
 
+		# tell the network manager to notify me when network status changes
+		invest.nm.set_statechange_callback(self.nm_state_changed)
+
+	def set_update_interval(self, interval):
+		if self.timeout_id != None:
+			invest.debug("Canceling refresh timer")
+			gobject.source_remove(self.timeout_id)
+			self.timeout_id = None
+		if interval > 0:
+			invest.debug("Setting refresh timer to %s:%02d.%03d" % ( interval / 60000, interval % 60000 / 1000, interval % 1000) )
+			self.timeout_id = gobject.timeout_add(interval, self.refresh)
+
+	def nm_state_changed(self):
+		# when nm is online but we do not have an update timer, create it and refresh
+		if invest.nm.online():
+			if self.timeout_id == None:
+				self.set_update_interval(AUTOREFRESH_TIMEOUT)
+				self.refresh()
+
 	def refresh(self):
+		invest.debug("Refreshing")
+
+		# when nm tells me I am offline, stop the update interval
+		if invest.nm.offline():
+			invest.debug("We are offline, stopping update timer")
+			self.set_update_interval(0)
+			return False
+
 		if len(invest.STOCKS) == 0:
 			return True
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]