GNOME Bugzilla – Bug 739080
[PATCH] DBus property for current connection type
Last modified: 2014-10-24 09:35:21 UTC
It would be nice if there was a single D-Bus property (with change notification) that I could look at to find out the current connection type in very broad strokes (none, Wifi, Wired, 3G, etc). The purpose of this would be to make it very easy for applications to know when they are on 3G in order to avoid using data (in order to avoid data charges). The Unity dash is currently doing this by bringing up the entire object tree worth of devices. This is causing trouble due to dodgy locking in QtDBus. That bug needs to be fixed anyway, but it would really be much nicer to avoid the entire problem, which we could do if there was a single simple property to consult.
While there's no property that's exactly that, 0.9.10 has the PrimaryConnection property on org.freedesktop.NetworkManager, which points to the ActiveConnection with the default route, and you can then look up the corresponding device from there. So, the answer you want is two hops away from that property, but you do have proper change notification, since the type of an ActiveConnection will never change, so the current-connection-type only changes when PrimaryConnection changes. (This is what gnome-shell currently uses for deciding what icon to show.)
Since 0.9.10 the ActiveConnection object has a 'Type' property that corresponds to the connection type. So: #!/usr/bin/env python from gi.repository import GLib, NM client = NM.Client.new(None) primary = client.get_primary_connection() if primary: print "Type: %s" % primary.get_connection_type() else: print "No primary connection" ---> "Type: 802-11-wireless" Does that do what you want?
(In reply to comment #2) > Does that do what you want? It does, but I would still prefer if it was possible to have this as a single property that could be directly monitored. After thinking about the issue of how to do indicators in a shell for a while (ie: how do we avoid too much code running in the process that composites the desktop) I've started to like the idea of having _stupidly_ simple APIs on backend services that give exactly the information you need from a single call, and nothing more. Of course we could make due with the approach suggested but it would be even better if we could just watch the property directly and have it be updated. I straced the python script above (after some minor fixes, below): #!/usr/bin/env python from gi.repository import GLib, NMClient client = NMClient.Client.new() primary = client.get_primary_connection() if primary: print "Type: %s" % primary.get_connection_type() else: print "No primary connection" Just running it now on my system, it seems to perform 435 D-Bus calls, which is sort of my point with this bug in the first place. Ideally we could dodge the library entirely (removing a bunch of extra code) and be able to directly subscribe to a D-Bus property with the minimum possible level of intelligence.
Or if you prefer pure D-Bus: import dbus bus = dbus.SystemBus() proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager") nm = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") primary_path = nm.Get("org.freedesktop.NetworkManager", "PrimaryConnection") ac_proxy = bus.get_object("org.freedesktop.NetworkManager", primary_path) ac = dbus.Interface(ac_proxy, "org.freedesktop.DBus.Properties") print "Type: %s" % ac.Get("org.freedesktop.NetworkManager.Connection.Active", "Type")
Another thing to consider: perhaps one day we want to allow sandboxed apps to be able to determine "am I on wifi or 3G?" without being able to arbitrarily access all network connections. We could then allow access to this one API.
Ok, so I'll take it you want an org.freedesktop.NetworkManager.Type property that has the primary active connection type in it. Correct? I can see an argument that this should be easier to get at than two steps.
Ryan, once you've got the patch, please change the bug title to "[PATCH] <description>" and make this bug block 'nm-review'. Thanks!
Created attachment 289222 [details] [review] Add PrimaryConnectionType property to the manager This will provide an extremely easy way for applications to find out what type of connection the system is currently using. They might want to do this to avoid using data if a phone is on a 3G connection, for example. Having this as a separate property provides at least two advantages: 1) it reduces code complexity for those wanting only this one simple piece of information 2) we could allow access to this property (but nothing else) to privilege-separated applications in the future This patch adds the missing nm_active_connection_get_connection_type() which was in the header file but never actually implemented.
I was unable to test this patch with my 'current level of technology' (which is building in jhbuild). Bringing up the modified NetworkManager worked (and the property was visible) but I wasn't able to see or connect to any networks.
Created attachment 289226 [details] [review] Add PrimaryConnectionType property to the manager Fixes a small copy-paste error in the previous patch.
Review of attachment 289222 [details] [review]: ::: src/nm-manager.c @@ +5172,2 @@ g_object_class_install_property + (object_class, PROP_PRIMARY_CONNECTION, I assume you want PROP_PRIMARY_CONNECTION_TYPE here...
Review of attachment 289226 [details] [review]: Looks good now
Pushed to git master and NM 0.9.10
Thanks very much for the (very) fast commit, and sorry again for adding some ugly to the API...