~elementary-os/ubuntu-package-imports/update-manager-precise

« back to all changes in this revision

Viewing changes to UpdateManager/Core/AlertWatcher.py

  • Committer: Ivo Nunes
  • Date: 2012-11-17 11:04:03 UTC
  • Revision ID: ivo@elementaryos.org-20121117110403-saeovxt71tn0sktw
Initial import, version 1:0.156.14.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# AlertWatcher.py 
 
2
#
 
3
#  Copyright (c) 2010 Mohamed Amine IL Idrissi
 
4
#  
 
5
#  Author: Mohamed Amine IL Idrissi <ilidrissiamine@gmail.com>
 
6
 
7
#  This program is free software; you can redistribute it and/or 
 
8
#  modify it under the terms of the GNU General Public License as 
 
9
#  published by the Free Software Foundation; either version 2 of the
 
10
#  License, or (at your option) any later version.
 
11
 
12
#  This program is distributed in the hope that it will be useful,
 
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#  GNU General Public License for more details.
 
16
 
17
#  You should have received a copy of the GNU General Public License
 
18
#  along with this program; if not, write to the Free Software
 
19
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
20
#  USA
 
21
 
 
22
from gi.repository import GObject
 
23
import dbus
 
24
from dbus.mainloop.glib import DBusGMainLoop
 
25
 
 
26
class AlertWatcher(GObject.GObject):
 
27
    """ a class that checks for alerts and reports them, like a battery
 
28
    or network warning """
 
29
    
 
30
    __gsignals__ = {"network-alert": (GObject.SignalFlags.RUN_FIRST,
 
31
                                      None,
 
32
                                      (GObject.TYPE_INT,)),
 
33
                    "battery-alert": (GObject.SignalFlags.RUN_FIRST,
 
34
                                      None,
 
35
                                      (GObject.TYPE_BOOLEAN,)),
 
36
                    "network-3g-alert": (GObject.SignalFlags.RUN_FIRST,
 
37
                                         None,
 
38
                                        (GObject.TYPE_BOOLEAN,
 
39
                                         GObject.TYPE_BOOLEAN,)),
 
40
                    }
 
41
    
 
42
    def __init__(self):
 
43
        GObject.GObject.__init__(self)
 
44
        DBusGMainLoop(set_as_default=True)
 
45
        self.bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
 
46
        self.network_state = 3 # make it always connected if NM isn't available
 
47
        
 
48
    def check_alert_state(self):
 
49
        try:
 
50
            obj = self.bus.get_object("org.freedesktop.NetworkManager",
 
51
                                      "/org/freedesktop/NetworkManager")
 
52
            obj.connect_to_signal("StateChanged",
 
53
                                  self._on_network_state_changed,
 
54
                                  dbus_interface="org.freedesktop.NetworkManager")
 
55
            interface = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
 
56
            self.network_state = interface.Get("org.freedesktop.NetworkManager", "State")
 
57
            self._network_alert(self.network_state)
 
58
            # power
 
59
            obj = self.bus.get_object('org.freedesktop.UPower',
 
60
                                      '/org/freedesktop/UPower')
 
61
            obj.connect_to_signal("Changed", self._power_changed,
 
62
                                  dbus_interface="org.freedesktop.UPower")
 
63
            self._power_changed()
 
64
            # 3g
 
65
            self._update_3g_state()
 
66
        except dbus.exceptions.DBusException:
 
67
            pass
 
68
 
 
69
    def _on_network_state_changed(self, state):
 
70
        self._network_alert(state)
 
71
        self._update_3g_state()
 
72
 
 
73
    def _update_3g_state(self):
 
74
        from roam import NetworkManagerHelper
 
75
        nm = NetworkManagerHelper()
 
76
        on_3g = nm.is_active_connection_gsm_or_cdma()
 
77
        is_roaming = nm.is_active_connection_gsm_or_cdma_roaming()
 
78
        self._network_3g_alert(on_3g, is_roaming)
 
79
 
 
80
    def _network_3g_alert(self, on_3g, is_roaming):
 
81
        self.emit("network-3g-alert", on_3g, is_roaming)
 
82
    
 
83
    def _network_alert(self, state):
 
84
        self.network_state = state
 
85
        self.emit("network-alert", state)
 
86
        
 
87
    def _power_changed(self):
 
88
        obj = self.bus.get_object("org.freedesktop.UPower", \
 
89
                                "/org/freedesktop/UPower")
 
90
        interface = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
 
91
        on_battery = interface.Get("org.freedesktop.UPower", "OnBattery")
 
92
        self.emit("battery-alert", on_battery)
 
93
        
 
94