-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore shutdown #631
base: master
Are you sure you want to change the base?
Restore shutdown #631
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (C) 2020 Sébastien Granjoux <[email protected]> | ||
|
||
# This file is part of Project Hamster. | ||
|
||
# Project Hamster 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, either version 3 of the License, or | ||
# (at your option) any later version. | ||
|
||
# Project Hamster 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 Project Hamster. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import dbus | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
class DbusSessionListener(object): | ||
"""Listen for GNOME manager end session event.""" | ||
|
||
|
||
def end_session(self): | ||
"""Override this method to do something at the end of a session. | ||
It must not attempt to interact with the user and will be given | ||
a maximum of ten seconds to perform the actions.""" | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should raise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think pass is fine, there is no need to do something. |
||
|
||
|
||
def __init__(self): | ||
"""Connect to the GNOME manager session signals""" | ||
|
||
# Get SessionManager interface | ||
session_bus = dbus.SessionBus() | ||
try: | ||
session_manager = session_bus.get_object("org.gnome.SessionManager", | ||
"/org/gnome/SessionManager") | ||
self.__session_manager_iface = dbus.Interface(session_manager, | ||
dbus_interface="org.gnome.SessionManager") | ||
self.__client_id = self.__session_manager_iface.RegisterClient("", "") | ||
|
||
# Get SessionManager.ClientPrivate interface | ||
session_client = session_bus.get_object("org.gnome.SessionManager", | ||
self.__client_id) | ||
self.__session_client_private_iface = dbus.Interface(session_client, | ||
dbus_interface="org.gnome.SessionManager.ClientPrivate") | ||
|
||
# Connect to the needed signals | ||
session_bus.add_signal_receiver(self.__query_end_session_handler, | ||
signal_name = "QueryEndSession", | ||
dbus_interface = "org.gnome.SessionManager.ClientPrivate", | ||
bus_name = "org.gnome.SessionManager") | ||
|
||
session_bus.add_signal_receiver(self.__end_session_handler, | ||
signal_name = "EndSession", | ||
dbus_interface = "org.gnome.SessionManager.ClientPrivate", | ||
bus_name = "org.gnome.SessionManager") | ||
|
||
session_bus.add_signal_receiver(self.__stop_handler, | ||
signal_name = "Stop", | ||
dbus_interface = "org.gnome.SessionManager.ClientPrivate", | ||
bus_name = "org.gnome.SessionManager") | ||
except dbus.exceptions.DBusException: | ||
logger.info("Unable to connect to GNOME session manager, stop tracking on logout won't work") | ||
|
||
def __query_end_session_handler(self, flags): | ||
"""Inform that the session is about to end. It must reply with | ||
EndSessionResponse within one second telling if it is ok to proceed | ||
or not and why. If flags is true, the session will end anyway.""" | ||
self.__session_client_private_iface.EndSessionResponse(True, "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this handler needed? If we're not doing anything other than saying it is ok to end the session, isn't this just the default? Or is it so that if you register something (I'm not sure about right terminology here) against SessionManager, that you need to also handle this QueryEndSession event? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have not checked, but according to the comment I have understood that when you register against the SessionManager you have to handle this message. |
||
|
||
def __end_session_handler(self, flags): | ||
"""Inform that the session is about to end. It must reply with | ||
EndSessionResponse within ten seconds.""" | ||
self.end_session() | ||
self.__session_client_private_iface.EndSessionResponse(True, "") | ||
|
||
def __stop_handler(self): | ||
"""Remove from the session.""" | ||
self.__session_manager_iface.UnregisterClient(self.__client_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if adding a mixin class here is the cleanest way to implement this. Also, it doesn't seem a responsibility of a class called Storage to implement a "stop-tracking-on-shutdown", but maybe that class already does more than its name implies (haven't looked too closely now).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with that. It's probably the simplest way but not the cleanest one.
I think I can create a separate object having a reference on the storage object to call StopTracking. To make it even more separated, this object can send a stop tracking message on the dbus, it could be even a separate process.