From b9dc8201611f50b529a365dd8f9cd5edfdc5294f Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Mon, 5 Sep 2011 22:00:23 +0200
Subject: [PATCH] [Kamil Paral & I] upower support. Fixes #1962

---
 src/features_window.py |  4 ++++
 src/gui_interface.py   |  3 +++
 src/upower_listener.py | 50 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 src/upower_listener.py

diff --git a/src/features_window.py b/src/features_window.py
index 0c6a8adb0b..8a9b39274b 100644
--- a/src/features_window.py
+++ b/src/features_window.py
@@ -111,6 +111,10 @@ class FeaturesWindow:
                 _('Ability to request your router to forward port for file transfer.'),
                 _('Requires python-gupnp-igd.'),
                 _('Feature not available under Windows.')),
+            _('UPower'): (self.gupnp_igd_available,
+                _('Ability to disconnect properly just before suspending the machine.'),
+                _('Requires upower and python-dbus.'),
+                _('Feature not available under Windows.')),
         }
 
         # name, supported
diff --git a/src/gui_interface.py b/src/gui_interface.py
index f0f3fd73f1..92bc7a8bec 100644
--- a/src/gui_interface.py
+++ b/src/gui_interface.py
@@ -2718,6 +2718,9 @@ class Interface:
         dbus_support.supported:
             import network_manager_listener
 
+        if dbus_support.supported:
+            import upower_listener
+
         # Handle gnome screensaver
         if dbus_support.supported:
             def gnome_screensaver_ActiveChanged_cb(active):
diff --git a/src/upower_listener.py b/src/upower_listener.py
new file mode 100644
index 0000000000..f8d6c79dee
--- /dev/null
+++ b/src/upower_listener.py
@@ -0,0 +1,50 @@
+## src/upower_listener.py
+##
+## Copyright (C) 2006-2011 Yann Leboulanger <asterix AT lagaule.org>
+##
+## This file is part of Gajim.
+##
+## Gajim is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published
+## by the Free Software Foundation; version 3 only.
+##
+## Gajim is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
+##
+
+
+supported = False
+
+from common import dbus_support
+from common import gajim
+
+def on_suspend(*args, **kwargs):
+    for name, conn in gajim.connections.items():
+        if gajim.account_is_connected(name):
+            conn.old_show = gajim.SHOW_LIST[conn.connected]
+            st = conn.status
+            conn.change_status('offline', _('Machine going to sleep'))
+            conn.status = st
+            conn.time_to_reconnect = 5
+
+if dbus_support.supported:
+    import dbus
+    import dbus.glib
+
+    try:
+        from common.dbus_support import system_bus
+        bus = system_bus.bus()
+        if 'org.freedesktop.UPower' in bus.list_names():
+            up_object = bus.get_object('org.freedesktop.UPower',
+                '/org/freedesktop/UPower')
+            bus.add_signal_receiver(on_suspend, 'Sleeping',
+                'org.freedesktop.UPower', 'org.freedesktop.UPower',
+                '/org/freedesktop/UPower')
+            supported = True
+    except Exception:
+        pass
\ No newline at end of file
-- 
GitLab