Commit 5d87134e authored by Guillaume Seguin's avatar Guillaume Seguin

Refactor DBus sleep inhibition testing and improve messages #522

parent 867e56cc
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import platform import platform
import traceback import traceback
import logging
import os import os
if platform.system() == "Darwin": if platform.system() == "Darwin":
...@@ -37,6 +38,7 @@ else: ...@@ -37,6 +38,7 @@ else:
try: try:
inhibit_sleep_handler = None inhibit_sleep_handler = None
inhibit_sleep_token = None
bus = dbus.SessionBus() bus = dbus.SessionBus()
try: try:
# GNOME uses the right object path, try it first # GNOME uses the right object path, try it first
...@@ -44,24 +46,29 @@ else: ...@@ -44,24 +46,29 @@ else:
proxy = bus.get_object(service_name, proxy = bus.get_object(service_name,
"/org/freedesktop/ScreenSaver") "/org/freedesktop/ScreenSaver")
inhibit_sleep_handler = dbus.Interface(proxy, service_name) inhibit_sleep_handler = dbus.Interface(proxy, service_name)
# Do a test run
token = inhibit_sleep_handler.Inhibit("printrun", "test")
inhibit_sleep_handler.UnInhibit(token)
except dbus.DBusException: except dbus.DBusException:
# KDE uses /ScreenSaver object path, let's try it as well # KDE uses /ScreenSaver object path, let's try it as well
proxy = bus.get_object(service_name, proxy = bus.get_object(service_name,
"/ScreenSaver") "/ScreenSaver")
inhibit_sleep_handler = dbus.Interface(proxy, service_name) inhibit_sleep_handler = dbus.Interface(proxy, service_name)
token = inhibit_sleep_handler.Inhibit("printrun", "test")
inhibit_sleep_handler.UnInhibit(token)
def inhibit_sleep(reason): def inhibit_sleep(reason):
global inhibit_sleep_handler global inhibit_sleep_handler, inhibit_sleep_token
inhibit_sleep.token = inhibit_sleep_handler.Inhibit("printrun", reason) inhibit_sleep_token = inhibit_sleep_handler.Inhibit("printrun", reason)
def deinhibit_sleep(): def deinhibit_sleep():
global inhibit_sleep_handler global inhibit_sleep_handler, inhibit_sleep_token
if inhibit_sleep_handler is None: if inhibit_sleep_handler is None or inhibit_sleep_token is None:
return return
inhibit_sleep_handler.UnInhibit(inhibit_sleep.token) inhibit_sleep_handler.UnInhibit(inhibit_sleep_token)
inhibit_sleep.token = None inhibit_sleep_token = None
except dbus.DBusException, e: except dbus.DBusException, e:
print "dbus unavailable:", e.message logging.warning("Could not setup DBus for sleep inhibition: %s" % e)
def inhibit_sleep(reason): def inhibit_sleep(reason):
return return
......
...@@ -1430,14 +1430,14 @@ class pronsole(cmd.Cmd): ...@@ -1430,14 +1430,14 @@ class pronsole(cmd.Cmd):
try: try:
powerset_print_start(reason = "Preventing sleep during print") powerset_print_start(reason = "Preventing sleep during print")
except: except:
logging.error(_("Failed to inhibit sleep:")) logging.error(_("Failed to set power settings:"))
traceback.print_exc(file = sys.stdout) traceback.print_exc(file = sys.stdout)
def endcb(self): def endcb(self):
try: try:
powerset_print_stop() powerset_print_stop()
except: except:
logging.error(_("Failed to uninhibit sleep:")) logging.error(_("Failed to set power settings:"))
traceback.print_exc(file = sys.stdout) traceback.print_exc(file = sys.stdout)
if self.p.queueindex == 0: if self.p.queueindex == 0:
print_duration = int(time.time() - self.starttime + self.extra_print_time) print_duration = int(time.time() - self.starttime + self.extra_print_time)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment