1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
1 |
# Copyright (C) 2009 Canonical
|
2 |
#
|
|
3 |
# Authors:
|
|
4 |
# Matthew McGowan
|
|
5 |
# Michael Vogt
|
|
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; version 3.
|
|
10 |
#
|
|
11 |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
14 |
# details.
|
|
15 |
#
|
|
16 |
# You should have received a copy of the GNU General Public License along with
|
|
17 |
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
19 |
||
1617.2.8
by Matthew McGowan
desensitive action buttons when the netconnection drops |
20 |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
21 |
import dbus |
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
22 |
import logging |
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
23 |
|
24 |
from dbus.mainloop.glib import DBusGMainLoop |
|
25 |
||
2338.1.2
by Michael Vogt
remove gtk2 glade files, simplify imports |
26 |
from gi.repository import GObject |
2112.1.2
by Martin Pitt
All files: Do not import the gi.repository.GObject module with static |
27 |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
28 |
# enums
|
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
29 |
class NetState(object): |
30 |
""" enums for network manager status """
|
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
31 |
# Old enum values are for NM 0.7
|
32 |
||
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
33 |
# The NetworkManager daemon is in an unknown state.
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
34 |
NM_STATE_UNKNOWN = 0 |
35 |
NM_STATE_UNKNOWN_LIST = [NM_STATE_UNKNOWN] |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
36 |
# The NetworkManager daemon is asleep and all interfaces managed by it are inactive.
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
37 |
NM_STATE_ASLEEP_OLD = 1 |
38 |
NM_STATE_ASLEEP = 10 |
|
39 |
NM_STATE_ASLEEP_LIST = [NM_STATE_ASLEEP_OLD, |
|
40 |
NM_STATE_ASLEEP] |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
41 |
# The NetworkManager daemon is connecting a device.
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
42 |
NM_STATE_CONNECTING_OLD = 2 |
43 |
NM_STATE_CONNECTING = 40 |
|
44 |
NM_STATE_CONNECTING_LIST = [NM_STATE_CONNECTING_OLD, |
|
45 |
NM_STATE_CONNECTING] |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
46 |
# The NetworkManager daemon is connected.
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
47 |
NM_STATE_CONNECTED_OLD = 3 |
48 |
NM_STATE_CONNECTED_LOCAL = 50 |
|
49 |
NM_STATE_CONNECTED_SITE = 60 |
|
50 |
NM_STATE_CONNECTED_GLOBAL = 70 |
|
51 |
NM_STATE_CONNECTED_LIST = [NM_STATE_CONNECTED_OLD, |
|
52 |
NM_STATE_CONNECTED_LOCAL, |
|
53 |
NM_STATE_CONNECTED_SITE, |
|
54 |
NM_STATE_CONNECTED_GLOBAL] |
|
55 |
# The NetworkManager daemon is disconnecting.
|
|
56 |
NM_STATE_DISCONNECTING = 30 |
|
57 |
NM_STATE_DISCONNECTING_LIST = [NM_STATE_DISCONNECTING] |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
58 |
# The NetworkManager daemon is disconnected.
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
59 |
NM_STATE_DISCONNECTED_OLD = 4 |
60 |
NM_STATE_DISCONNECTED = 20 |
|
61 |
NM_STATE_DISCONNECTED_LIST = [NM_STATE_DISCONNECTED_OLD, |
|
62 |
NM_STATE_DISCONNECTED] |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
63 |
|
64 |
||
1945.1.3
by Michael Vogt
convert all gobject to gi.repository/GObject |
65 |
class NetworkStatusWatcher(GObject.GObject): |
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
66 |
""" simple watcher which notifys subscribers to network events..."""
|
1945.1.3
by Michael Vogt
convert all gobject to gi.repository/GObject |
67 |
__gsignals__ = {'changed':(GObject.SIGNAL_RUN_FIRST, |
68 |
GObject.TYPE_NONE, |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
69 |
(int,)), |
70 |
}
|
|
71 |
||
72 |
def __init__(self): |
|
1945.1.3
by Michael Vogt
convert all gobject to gi.repository/GObject |
73 |
GObject.GObject.__init__(self) |
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
74 |
return
|
75 |
||
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
76 |
# internal helper
|
1782.3.5
by Michael Vogt
more pyflakes fixes |
77 |
NETWORK_STATE = 0 |
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
78 |
def __connection_state_changed_handler(state): |
79 |
global NETWORK_STATE |
|
80 |
||
81 |
NETWORK_STATE = int(state) |
|
82 |
__WATCHER__.emit("changed", NETWORK_STATE) |
|
83 |
return
|
|
84 |
||
85 |
# init network state
|
|
86 |
def __init_network_state(): |
|
87 |
global NETWORK_STATE |
|
1617.2.8
by Matthew McGowan
desensitive action buttons when the netconnection drops |
88 |
|
89 |
# check is SOFTWARE_CENTER_NET_DISCONNECTED is in the environment variables
|
|
90 |
# if so force the network status to be disconnected
|
|
91 |
import os |
|
2055
by Michael Vogt
py3 compat fixes, |
92 |
if ("SOFTWARE_CENTER_NET_DISCONNECTED" in os.environ and |
93 |
os.environ["SOFTWARE_CENTER_NET_DISCONNECTED"] == 1): |
|
1617.2.8
by Matthew McGowan
desensitive action buttons when the netconnection drops |
94 |
NETWORK_STATE = NetState.NM_STATE_DISCONNECTED |
2055
by Michael Vogt
py3 compat fixes, |
95 |
print('forced netstate into disconnected mode...') |
1617.2.8
by Matthew McGowan
desensitive action buttons when the netconnection drops |
96 |
return
|
97 |
||
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
98 |
dbus_loop = DBusGMainLoop() |
99 |
try: |
|
100 |
bus = dbus.SystemBus(mainloop=dbus_loop) |
|
101 |
nm = bus.get_object('org.freedesktop.NetworkManager', |
|
102 |
'/org/freedesktop/NetworkManager') |
|
103 |
NETWORK_STATE = nm.state(dbus_interface='org.freedesktop.NetworkManager') |
|
104 |
bus.add_signal_receiver(__connection_state_changed_handler, |
|
105 |
dbus_interface="org.freedesktop.NetworkManager", |
|
106 |
signal_name="StateChanged") |
|
107 |
except Exception as e: |
|
108 |
logging.warn("failed to init network state watcher '%s'" % e) |
|
109 |
NETWORK_STATE = NetState.NM_STATE_UNKNOWN |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
110 |
|
111 |
# global watcher
|
|
112 |
__WATCHER__ = NetworkStatusWatcher() |
|
113 |
def get_network_watcher(): |
|
114 |
return __WATCHER__ |
|
115 |
||
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
116 |
# simply query
|
117 |
def get_network_state(): |
|
1499.1.1
by Michael Vogt
use "offline" mode in piston mini client if available and simplify the code a bit |
118 |
""" get the NetState state """
|
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
119 |
global NETWORK_STATE |
120 |
return NETWORK_STATE |
|
121 |
||
1499.1.1
by Michael Vogt
use "offline" mode in piston mini client if available and simplify the code a bit |
122 |
# simply query even more
|
123 |
def network_state_is_connected(): |
|
124 |
""" get bool if we are connected """
|
|
125 |
# unkown because in doubt, just assume we have network
|
|
1825.1.1
by Michael Terry
use NetworkManager 0.9 status enumerations |
126 |
return get_network_state() in NetState.NM_STATE_UNKNOWN_LIST + \ |
127 |
NetState.NM_STATE_CONNECTED_LIST |
|
1499.1.1
by Michael Vogt
use "offline" mode in piston mini client if available and simplify the code a bit |
128 |
|
1449
by Michael Vogt
merged lp:~mmcg069/software-center/reviews-and-netstatus |
129 |
# init it once
|
130 |
__init_network_state() |
|
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
131 |
|
132 |
if __name__ == '__main__': |
|
1945.1.3
by Michael Vogt
convert all gobject to gi.repository/GObject |
133 |
loop = GObject.MainLoop() |
1354.2.9
by Matthew McGowan
add missing file netstatus.py |
134 |
loop.run() |
135 |