After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 739080 - [PATCH] DBus property for current connection type
[PATCH] DBus property for current connection type
Status: RESOLVED FIXED
Product: NetworkManager
Classification: Platform
Component: general
unspecified
Other All
: Normal enhancement
: ---
Assigned To: NetworkManager maintainer(s)
NetworkManager maintainer(s)
Depends on:
Blocks:
 
 
Reported: 2014-10-23 15:12 UTC by Allison Karlitskaya (desrt)
Modified: 2014-10-24 09:35 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add PrimaryConnectionType property to the manager (5.09 KB, patch)
2014-10-23 18:19 UTC, Allison Karlitskaya (desrt)
none Details | Review
Add PrimaryConnectionType property to the manager (5.10 KB, patch)
2014-10-23 18:36 UTC, Allison Karlitskaya (desrt)
accepted-commit_now Details | Review

Description Allison Karlitskaya (desrt) 2014-10-23 15:12:11 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.
Comment 1 Dan Winship 2014-10-23 16:51:12 UTC
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.)
Comment 2 Dan Williams 2014-10-23 17:11:19 UTC
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?
Comment 3 Allison Karlitskaya (desrt) 2014-10-23 17:34:52 UTC
(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.
Comment 4 Dan Williams 2014-10-23 17:45:53 UTC
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")
Comment 5 Allison Karlitskaya (desrt) 2014-10-23 17:47:44 UTC
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.
Comment 6 Dan Williams 2014-10-23 17:49:48 UTC
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.
Comment 7 Dan Williams 2014-10-23 18:10:52 UTC
Ryan, once you've got the patch, please change the bug title to "[PATCH] <description>" and make this bug block 'nm-review'.  Thanks!
Comment 8 Allison Karlitskaya (desrt) 2014-10-23 18:19:41 UTC
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.
Comment 9 Allison Karlitskaya (desrt) 2014-10-23 18:21:07 UTC
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.
Comment 10 Allison Karlitskaya (desrt) 2014-10-23 18:36:26 UTC
Created attachment 289226 [details] [review]
Add PrimaryConnectionType property to the manager

Fixes a small copy-paste error in the previous patch.
Comment 11 Dan Williams 2014-10-23 18:36:45 UTC
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...
Comment 12 Dan Williams 2014-10-23 18:37:27 UTC
Review of attachment 289226 [details] [review]:

Looks good now
Comment 13 Dan Williams 2014-10-23 18:43:32 UTC
Pushed to git master and NM 0.9.10
Comment 14 Allison Karlitskaya (desrt) 2014-10-23 19:50:57 UTC
Thanks very much for the (very) fast commit, and sorry again for adding some ugly to the API...