Tuesday, October 29, 2013

Two weeks back I decided to switch to Ubuntu 13.04. I was using 12.04 LTS version and everything was fine.  However I was seeing crash in bzr-gtk  upon bzr gcommit and hence I decided to upgrade. All was well until I saw the sys-tray icon of Network Manager crash upon switching connections due to  bug#1164631 and bug#1159063. I have to switch between connections often and this was frustrating.

The worst part of all this is that you cannot go online and find a solution to the issue since you are no longer connected to the internet.  So much for our dependency on the Internet! And you cannot restart your system if you have a huge test suite being run.

Skimming through the man pages I came across the old friend,   NMCLI

$  nmcli dev
DEVICE     TYPE              STATE        
ttyUSB0    cdma              disconnected    
wlan0      802-11-wireless   disconnected 
eth0       802-3-ethernet    unavailable 


Okay, So this was the epiphany I was looking for, Cool! So what's next.


$ nmcli con
NAME                      UUID                                   TYPE              TIMESTAMP-REAL                    
VPN connection 1          de9fd222-7a27-4e29-ad9e-9095f953e837   vpn               never                             
LAN                       c26d0089-0f41-422a-9997-8a9b832d7159   802-3-ethernet    Saturday 26 October 2013 07:23:48 PM IST
Tata Indicom (Photon+) connection b3c0f820-7521-4eb4-8507-8c931f44d691   cdma              Tuesday 29 October 2013 04:25:28 PM IST

To connect copy the uuid or the name of the connection you want to connect to  e.g. I wanted to connect to my cdma dongle and hence I used

$ nmcli con up uuid b3c0f820-7521-4eb4-8507-8c931f44d691

This is cool but I wanted to switch the connection from the systray. I used Python appIndicator3  to create an appindicator and added a menu with the list of all the available connections.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from gi.repository import AppIndicator3 as appindicator

def getIndicator():                                                             
   indicator = appindicator.Indicator.new (                                     
                     "Network-switcher",                                        
                     "indicator-messages",                                      
                     appindicator.IndicatorCategory.APPLICATION_STATUS)         
   indicator.set_icon('/usr/share/icons/ubuntu-mono-dark/status/24/gnome-netstatus-0-24.svg')
   indicator.set_status (appindicator.IndicatorStatus.ACTIVE)                   
   indicator.set_attention_icon('/usr/share/icons/ubuntu-mono-dark/status/24/gnome-netstatus-75-100.svg')
   NSSignalHandler.registerSignalHandler(indicator)                             
   return indicator

Next was to register the appindcator to dbus notification interface.  This will ensure that we get notifications of network connect or disconnect,  whether from this appindicator or from outside.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from gi.repository import Gdk                                                   
import dbus                                                                     
from dbus.mainloop.glib import DBusGMainLoop 

def registerSignalHandler(ind):                                                 
        global indicator                                                        
        indicator= ind                                                          
        dbus_loop = DBusGMainLoop()                                                 
        system_bus = dbus.SystemBus(mainloop=dbus_loop)                             
        system_bus.add_signal_receiver(connection_handler,                          
                dbus_interface="org.freedesktop.NetworkManager.Connection.Active",    
                signal_name="PropertiesChanged")                                    
        Gdk.threads_init()


 From here everything was downhill :)  I added a call back to check if the notification is a  connect or a disconnect.  You may get the active connection from


$ nmcli con status
NAME                      UUID                         launchpad          DEVICES    DEFAULT  VPN   MASTER-PATH                                 
Tata Indicom (Photon+) connection b3c0f820-7521-4eb4-8507-8c931f44d691   ttyUSB0    yes      no    --  

A screenshot of the systray icon.



It is a simple python script and is available  on launchpad.