Aggiunto support alle notifiche.

Leonardo Robol [2010-10-12 12:46]
Aggiunto support alle notifiche.
Filename
99_poisson_vpn.py
Makefile
diff --git a/99_poisson_vpn.py b/99_poisson_vpn.py
index 66ae999..ef243a9 100755
--- a/99_poisson_vpn.py
+++ b/99_poisson_vpn.py
@@ -20,15 +20,20 @@
 #
 #

+
 __author__ = "Leonardo Robol <robol@poisson.phc.unipi.it>"

 # Defaults
 apt_proxy_file = "/etc/apt/apt.conf.d/99poisson_proxy"


-import dbus, sys, os
+import dbus, sys, os, subprocess, re, time
 from dbus.mainloop.glib import DBusGMainLoop

+
+notify_body = "La connessione VPN a poisson è stata effettuata ed è ora possibile navigare in Internet."
+using_notify = True
+
 DBusGMainLoop(set_as_default=True)

 def init():
@@ -110,7 +115,7 @@ def get_base_connection():

 	# Otteniamo la lista di tutte le connessioni attive al momento
 	proxy = bus.get_object("org.freedesktop.NetworkManager",
-						   "/org/freedesktop/NetworkManager")
+			       "/org/freedesktop/NetworkManager")
 	interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
 	active_connections = interface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')

@@ -123,10 +128,10 @@ def get_base_connection():
 		path = interface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection')

 		proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings',
-							   path)
+				       path)
 		interface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings.Connection')
 		settings = interface.GetSettings()
-
+
 		if settings.has_key("802-11-wireless"):

 			# MAC address of CDCWL1 ap
@@ -142,6 +147,14 @@ def get_base_connection():
 	# anche lasciar perdere
 	return None

+def has_default_route():
+	"""Return True if exists a default route"""
+	p = subprocess.Popen(["ip", "route", "show"], stdout = subprocess.PIPE)
+	output = p.communicate()[0]
+	if "default" in output:
+		return True
+	else:
+		return False

 def activate_connection(connection):
 	"""
@@ -159,7 +172,65 @@ def activate_connection(connection):
 									 connection,
 									 dbus.ObjectPath("/"),
 									 base_connection)
+
+		# Setup apt proxy on poisson:3142
 		setup_apt_proxy ()
+
+		# If configured to try notification, make a bubble appear
+		# in the user desktop
+		if using_notify:
+
+			# Wait for the connection to be completed. We should test
+			# that user has internet access
+			starting_time = time.time()
+			while(time.time() < starting_time + 10):
+				if has_default_route():
+					continue
+				else:
+					time.sleep(0.5)
+
+			if not has_default_route():
+				return
+
+			# To get user Session bus we should get the content
+			# of DBUS_SESSION_BUS_ADDRESS of user
+			# so we can start getting the pid of gnome-session
+			p = subprocess.Popen(["ps", "-C", "gnome-session", "-o", "pid="],
+					      stdout = subprocess.PIPE)
+			output = p.communicate()[0]
+			environments = []
+			for pid in output.split("\n"):
+				try:
+					pid = int(pid.strip())
+				except:
+					continue
+				with open("/proc/%d/environ" % pid, "r") as handle:
+					env = {}
+					content = handle.read()
+					for pair in content.split("\0"):
+						pieces = pair.split("=")
+						key = pieces[0]
+						value = "=".join(pieces[1:])
+						env[key] = value
+					environments.append(env)
+
+			# Copy environment of users so we can send graphical notifications
+			# to them. Quite hack, but...
+			for env in environments:
+
+				for key, value in env.items():
+					os.putenv(key, value)
+
+				# Get SessionBus
+				mybus = dbus.SessionBus()
+				notifyService = mybus.get_object("org.freedesktop.Notifications",
+								 "/org/freedesktop/Notifications")
+				interface = dbus.Interface(notifyService, "org.freedesktop.Notifications")
+				interface.Notify('poisson-vpn', 0,
+						 'poisson-vpn', "Connessione effettuata",
+						 notify_body, [], {}, 15000)
+
+
 	else:
 		reset_apt_proxy ()

diff --git a/Makefile b/Makefile
index 367b891..f2e0975 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
 SCRIPT_FILE=99_poisson_vpn.py
 NM_DISPATCHER_DIR=$(DESTDIR)/etc/NetworkManager/dispatcher.d/
+IMAGE_FILE=poisson-vpn.png

 all:
 	@echo ""
@@ -13,6 +14,7 @@ all:
 install:
 	mkdir -p $(NM_DISPATCHER_DIR)
 	install -m 755 -o root $(SCRIPT_FILE) $(NM_DISPATCHER_DIR)
+	install -m 644 -o root $(IMAGE_FILE) /usr/share/pixmaps/

 uninstall:
 	rm -f $(NM_DISPATCHER_DIR)$(SCRIPT_FILE)
ViewGit