~nmu-sscheel/gtg/rework-task-editor

« back to all changes in this revision

Viewing changes to GTG/tools/networkmanager.py

  • Committer: Bertrand Rousseau
  • Date: 2012-05-09 22:33:25 UTC
  • mfrom: (1178 trunk)
  • mto: This revision was merged to the branch mainline in revision 1179.
  • Revision ID: bertrand.rousseau@gmail.com-20120509223325-a53d8nwo0x9g93bc
Merge nimit branch and trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License along
14
 
# with this program; if not, write to the Free Software Foundation, Inc.,
15
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
 
#
17
 
# Copyright (C) 2010 Red Hat, Inc.
18
 
#
 
2
# -*- coding: utf-8 -*-
 
3
# -----------------------------------------------------------------------------
 
4
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
 
5
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation, either version 3 of the License, or (at your option) any later
 
10
# version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but WITHOUT
 
13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
15
# details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along with
 
18
# this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
# -----------------------------------------------------------------------------
 
20
 
 
21
""" Communicate with Network Manager over its D-Bus API
 
22
 
 
23
API spec: http://projects.gnome.org/NetworkManager/developers/api/09/spec.html
 
24
"""
19
25
 
20
26
import dbus
21
27
 
 
28
# A network device is connected, with global network connectivity. 
 
29
NM_STATE_CONNECTED_GLOBAL = 70
22
30
 
23
31
def is_connection_up():
24
 
    '''
25
 
    Returns True if network-manager reports that at least one connection is up
26
 
 
27
 
    @returns bool
28
 
    '''
29
 
    state = False
 
32
    """ Returns True if GTG can access the Internet """
30
33
    bus = dbus.SystemBus()
31
 
 
32
 
    proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
33
 
    manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
34
 
 
35
 
    manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
36
 
    active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections")
37
 
    for a in active:
38
 
        ac_proxy = bus.get_object("org.freedesktop.NetworkManager", a)
39
 
        prop_iface = dbus.Interface(ac_proxy, "org.freedesktop.DBus.Properties")
40
 
        state = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "State")
41
 
 
42
 
        # Connections in NM are a collection of settings that describe everything
43
 
        # needed to connect to a specific network.  Lets get those details so we
44
 
        # can find the user-readable name of the connection.
45
 
        con_path = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "Connection")
46
 
        con_service = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "ServiceName")
47
 
 
48
 
        # ask the provider of the connection for its details
49
 
        service_proxy = bus.get_object(con_service, con_path)
50
 
        con_iface = dbus.Interface(service_proxy, "org.freedesktop.NetworkManagerSettings.Connection")
51
 
        con_details = con_iface.GetSettings()
52
 
        con_name = con_details['connection']['id']
53
 
 
54
 
        if state == 2:   # activated
55
 
            state = True
56
 
    return state
57
 
 
 
34
    proxy = bus.get_object('org.freedesktop.NetworkManager', 
 
35
                            '/org/freedesktop/NetworkManager')
 
36
    network_manager = dbus.Interface(proxy, 'org.freedesktop.NetworkManager')
 
37
 
 
38
    return network_manager.state() == NM_STATE_CONNECTED_GLOBAL
 
39
 
 
40
if __name__ == "__main__":
 
41
    print "is_connection_up() == %s" % is_connection_up()