1
1
#!/usr/bin/env python
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.
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.
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.
17
# Copyright (C) 2010 Red Hat, Inc.
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
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
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
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
# -----------------------------------------------------------------------------
21
""" Communicate with Network Manager over its D-Bus API
23
API spec: http://projects.gnome.org/NetworkManager/developers/api/09/spec.html
28
# A network device is connected, with global network connectivity.
29
NM_STATE_CONNECTED_GLOBAL = 70
23
31
def is_connection_up():
25
Returns True if network-manager reports that at least one connection is up
32
""" Returns True if GTG can access the Internet """
30
33
bus = dbus.SystemBus()
32
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
33
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
35
manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
36
active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections")
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")
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")
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']
54
if state == 2: # activated
34
proxy = bus.get_object('org.freedesktop.NetworkManager',
35
'/org/freedesktop/NetworkManager')
36
network_manager = dbus.Interface(proxy, 'org.freedesktop.NetworkManager')
38
return network_manager.state() == NM_STATE_CONNECTED_GLOBAL
40
if __name__ == "__main__":
41
print "is_connection_up() == %s" % is_connection_up()