~nataliabidart/software-center/lost-in-translation

« back to all changes in this revision

Viewing changes to softwarecenter/netstatus.py

  • Committer: Kiwinote
  • Date: 2012-03-15 22:36:31 UTC
  • mfrom: (2867 trunk)
  • mto: This revision was merged to the branch mainline in revision 2881.
  • Revision ID: kiwinote@gmail.com-20120315223631-lvea6t5sydpkkqni
mergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
from gi.repository import GObject
27
27
 
28
 
LOG=logging.getLogger(__name__)
 
28
LOG = logging.getLogger(__name__)
 
29
 
29
30
 
30
31
# enums
31
32
class NetState(object):
32
33
    """ enums for network manager status """
33
34
    # Old enum values are for NM 0.7
34
35
 
35
 
    # The NetworkManager daemon is in an unknown state. 
36
 
    NM_STATE_UNKNOWN            = 0   
37
 
    NM_STATE_UNKNOWN_LIST       = [NM_STATE_UNKNOWN]
38
 
    # The NetworkManager daemon is asleep and all interfaces managed by it are inactive. 
39
 
    NM_STATE_ASLEEP_OLD         = 1
40
 
    NM_STATE_ASLEEP             = 10
41
 
    NM_STATE_ASLEEP_LIST        = [NM_STATE_ASLEEP_OLD,
42
 
                                   NM_STATE_ASLEEP]
 
36
    # The NetworkManager daemon is in an unknown state.
 
37
    NM_STATE_UNKNOWN = 0
 
38
    NM_STATE_UNKNOWN_LIST = [NM_STATE_UNKNOWN]
 
39
    # The NetworkManager daemon is asleep and all interfaces managed by
 
40
    # it are inactive.
 
41
    NM_STATE_ASLEEP_OLD = 1
 
42
    NM_STATE_ASLEEP = 10
 
43
    NM_STATE_ASLEEP_LIST = [NM_STATE_ASLEEP_OLD,
 
44
                            NM_STATE_ASLEEP]
43
45
    # The NetworkManager daemon is connecting a device.
44
 
    NM_STATE_CONNECTING_OLD     = 2
45
 
    NM_STATE_CONNECTING         = 40
46
 
    NM_STATE_CONNECTING_LIST    = [NM_STATE_CONNECTING_OLD,
47
 
                                   NM_STATE_CONNECTING]
48
 
    # The NetworkManager daemon is connected. 
49
 
    NM_STATE_CONNECTED_OLD      = 3
50
 
    NM_STATE_CONNECTED_LOCAL    = 50
51
 
    NM_STATE_CONNECTED_SITE     = 60
52
 
    NM_STATE_CONNECTED_GLOBAL   = 70
53
 
    NM_STATE_CONNECTED_LIST     = [NM_STATE_CONNECTED_OLD,
54
 
                                   NM_STATE_CONNECTED_LOCAL,
55
 
                                   NM_STATE_CONNECTED_SITE,
56
 
                                   NM_STATE_CONNECTED_GLOBAL]
 
46
    NM_STATE_CONNECTING_OLD = 2
 
47
    NM_STATE_CONNECTING = 40
 
48
    NM_STATE_CONNECTING_LIST = [NM_STATE_CONNECTING_OLD,
 
49
                                NM_STATE_CONNECTING]
 
50
    # The NetworkManager daemon is connected.
 
51
    NM_STATE_CONNECTED_OLD = 3
 
52
    NM_STATE_CONNECTED_LOCAL = 50
 
53
    NM_STATE_CONNECTED_SITE = 60
 
54
    NM_STATE_CONNECTED_GLOBAL = 70
 
55
    NM_STATE_CONNECTED_LIST = [NM_STATE_CONNECTED_OLD,
 
56
                               NM_STATE_CONNECTED_LOCAL,
 
57
                               NM_STATE_CONNECTED_SITE,
 
58
                               NM_STATE_CONNECTED_GLOBAL]
57
59
    # The NetworkManager daemon is disconnecting.
58
 
    NM_STATE_DISCONNECTING      = 30
 
60
    NM_STATE_DISCONNECTING = 30
59
61
    NM_STATE_DISCONNECTING_LIST = [NM_STATE_DISCONNECTING]
60
62
    # The NetworkManager daemon is disconnected.
61
 
    NM_STATE_DISCONNECTED_OLD   = 4
62
 
    NM_STATE_DISCONNECTED       = 20
63
 
    NM_STATE_DISCONNECTED_LIST  = [NM_STATE_DISCONNECTED_OLD,
 
63
    NM_STATE_DISCONNECTED_OLD = 4
 
64
    NM_STATE_DISCONNECTED = 20
 
65
    NM_STATE_DISCONNECTED_LIST = [NM_STATE_DISCONNECTED_OLD,
64
66
                                   NM_STATE_DISCONNECTED]
65
67
 
66
68
 
67
69
class NetworkStatusWatcher(GObject.GObject):
68
70
    """ simple watcher which notifys subscribers to network events..."""
69
 
    __gsignals__ = {'changed':(GObject.SIGNAL_RUN_FIRST,
70
 
                               GObject.TYPE_NONE,
71
 
                               (int,)),
 
71
    __gsignals__ = {'changed': (GObject.SIGNAL_RUN_FIRST,
 
72
                                GObject.TYPE_NONE,
 
73
                                (int,)),
72
74
                   }
73
75
 
74
76
    def __init__(self):
75
77
        GObject.GObject.__init__(self)
76
78
        return
77
79
 
 
80
 
78
81
# internal helper
79
82
NETWORK_STATE = 0
 
83
 
 
84
 
80
85
def __connection_state_changed_handler(state):
81
86
    global NETWORK_STATE
82
87
 
84
89
    __WATCHER__.emit("changed", NETWORK_STATE)
85
90
    return
86
91
 
 
92
 
87
93
# init network state
88
94
def __init_network_state():
89
95
    global NETWORK_STATE
90
96
 
91
97
    # honor SOFTWARE_CENTER_NET_{DIS,}CONNECTED in the environment variables
92
98
    import os
93
 
    env_map = { 
94
 
        'SOFTWARE_CENTER_NET_DISCONNECTED' : NetState.NM_STATE_DISCONNECTED,
95
 
        'SOFTWARE_CENTER_NET_CONNECTED' : NetState.NM_STATE_CONNECTED_GLOBAL,
 
99
    env_map = {
 
100
        'SOFTWARE_CENTER_NET_DISCONNECTED': NetState.NM_STATE_DISCONNECTED,
 
101
        'SOFTWARE_CENTER_NET_CONNECTED': NetState.NM_STATE_CONNECTED_GLOBAL,
96
102
    }
97
103
    for envkey, state in env_map.iteritems():
98
104
        if envkey in os.environ:
104
110
        bus = dbus.SystemBus(mainloop=dbus_loop)
105
111
        nm = bus.get_object('org.freedesktop.NetworkManager',
106
112
                            '/org/freedesktop/NetworkManager')
107
 
        NETWORK_STATE = nm.state(dbus_interface='org.freedesktop.NetworkManager')
108
 
        bus.add_signal_receiver(__connection_state_changed_handler,
109
 
                                dbus_interface="org.freedesktop.NetworkManager",
110
 
                                signal_name="StateChanged")
 
113
        NETWORK_STATE = nm.state(
 
114
            dbus_interface='org.freedesktop.NetworkManager')
 
115
        bus.add_signal_receiver(
 
116
            __connection_state_changed_handler,
 
117
            dbus_interface="org.freedesktop.NetworkManager",
 
118
            signal_name="StateChanged")
111
119
        return
112
120
 
113
121
    except Exception as e:
121
129
    thread.start()
122
130
    return
123
131
 
 
132
 
124
133
#helper
125
134
def test_ping():
126
135
    global NETWORK_STATE
161
170
 
162
171
# global watcher
163
172
__WATCHER__ = NetworkStatusWatcher()
 
173
 
 
174
 
164
175
def get_network_watcher():
165
176
    return __WATCHER__
166
177
 
 
178
 
167
179
# simply query
168
180
def get_network_state():
169
181
    """ get the NetState state """
170
182
    global NETWORK_STATE
171
183
    return NETWORK_STATE
172
184
 
 
185
 
173
186
# simply query even more
174
187
def network_state_is_connected():
175
188
    """ get bool if we are connected """
183
196
if __name__ == '__main__':
184
197
    loop = GObject.MainLoop()
185
198
    loop.run()
186