3
# Copyright (c) 2010 Mohamed Amine IL Idrissi
5
# Author: Mohamed Amine IL Idrissi <ilidrissiamine@gmail.com>
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.
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.
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
22
from gi.repository import GObject
24
from dbus.mainloop.glib import DBusGMainLoop
26
class AlertWatcher(GObject.GObject):
27
""" a class that checks for alerts and reports them, like a battery
28
or network warning """
30
__gsignals__ = {"network-alert": (GObject.SignalFlags.RUN_FIRST,
33
"battery-alert": (GObject.SignalFlags.RUN_FIRST,
35
(GObject.TYPE_BOOLEAN,)),
36
"network-3g-alert": (GObject.SignalFlags.RUN_FIRST,
38
(GObject.TYPE_BOOLEAN,
39
GObject.TYPE_BOOLEAN,)),
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
48
def check_alert_state(self):
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)
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")
65
self._update_3g_state()
66
except dbus.exceptions.DBusException:
69
def _on_network_state_changed(self, state):
70
self._network_alert(state)
71
self._update_3g_state()
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)
80
def _network_3g_alert(self, on_3g, is_roaming):
81
self.emit("network-3g-alert", on_3g, is_roaming)
83
def _network_alert(self, state):
84
self.network_state = state
85
self.emit("network-alert", state)
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)