This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
pylock/LockWindow.py

154 lines
5.8 KiB
Python
Raw Normal View History

2012-02-28 13:49:53 +01:00
# Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2012-01-27 15:44:45 +01:00
import sys
import locale
2012-01-27 15:44:45 +01:00
from gi.repository import Gtk, Gdk, GObject, GLib
_ = locale.gettext
2012-01-27 15:44:45 +01:00
class LockWindow(Gtk.Window):
__gsignals__ = {
2012-02-02 14:55:11 +01:00
'logout': (GObject.SIGNAL_RUN_FIRST, None, ()),
2012-01-27 15:44:45 +01:00
'tryUnlock': (GObject.SIGNAL_RUN_FIRST, None, (str,))
}
def __init__(self):
Gtk.Window.__init__(self)
bg = Gtk.Image.new_from_file("bg.svg")
self.add(bg)
self.set_decorated(False)
self.set_skip_taskbar_hint(True)
self.set_skip_pager_hint(True)
self.set_keep_above(True)
self.fullscreen()
builder = Gtk.Builder()
if not builder.add_from_file("unlock.ui"):
sys.exit(1)
self.unlockWindow = builder.get_object('unlock_window')
self.lockLabel = builder.get_object('lock_label')
2012-01-27 15:44:45 +01:00
self.promptEntry = builder.get_object('prompt_entry')
self.messageLabel = builder.get_object('message_label')
self.logoutButton = builder.get_object('logout_button')
self.unlockButton = builder.get_object('unlock_button')
self.unlockWindow.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
self.unlockWindow.set_transient_for(self)
self.unlockWindow.set_modal(True)
self.connect('delete-event', lambda w, e: True)
self.unlockWindow.connect('delete-event', lambda w, e: True)
self.promptEntry.connect('activate', lambda w: self._tryUnlock())
2012-02-02 14:55:11 +01:00
self.logoutButton.connect('clicked', lambda w: self._logout())
2012-01-27 15:44:45 +01:00
self.unlockButton.connect('clicked', lambda w: self._tryUnlock())
2012-02-02 14:55:11 +01:00
self.reset(True)
2012-02-08 16:10:46 +01:00
self.display = Gdk.Display.get_default()
self.deviceManager = self.display.get_device_manager()
self.unlockWindow.connect('map-event', self._grab_devices)
2012-02-02 14:55:11 +01:00
def _logout(self):
self.promptEntry.set_sensitive(False)
self.unlockButton.set_sensitive(False)
self.logoutButton.set_sensitive(False)
GLib.idle_add(lambda: self.emit('logout'))
2012-01-27 15:44:45 +01:00
def _tryUnlock(self):
self.promptEntry.set_sensitive(False)
self.unlockButton.set_sensitive(False)
2012-02-02 14:55:11 +01:00
self.logoutButton.set_sensitive(False)
2012-01-27 15:44:45 +01:00
GLib.idle_add(lambda: self.emit('tryUnlock', self.promptEntry.get_text()))
2012-02-02 14:55:11 +01:00
def _update(self):
self.promptEntry.set_sensitive(self._enablePromptEntry)
self.logoutButton.set_sensitive(self._enableLogoutButton)
self.unlockButton.set_sensitive(self._enableUnlockButton)
2012-02-02 14:55:11 +01:00
def reset(self, resetButtons = False):
if resetButtons:
self._enablePromptEntry = True
self._enableLogoutButton = False
self._enableUnlockButton = True
2012-02-02 14:55:11 +01:00
self.promptEntry.set_text('')
2012-01-27 15:44:45 +01:00
self.messageLabel.set_label('')
2012-02-02 14:55:11 +01:00
self._update()
self.promptEntry.grab_focus()
2012-01-27 15:44:45 +01:00
def updateLockMessage(self, username, logoutTime = None):
if logoutTime is None:
self.lockLabel.set_markup(_('This computer is currently locked by the user <i>{username}</i>.').format(username=GLib.markup_escape_text(username)))
2012-02-02 14:55:11 +01:00
self._enableLogoutButton = False
elif logoutTime > 0:
self.lockLabel.set_markup(
_('This computer is currently locked by the user <i>{username}</i>.\nThe user can be logged out in {minutes:02}:{seconds:02} minutes.').format(
username=GLib.markup_escape_text(username), minutes=logoutTime//60, seconds=logoutTime%60))
2012-02-02 14:55:11 +01:00
self._enableLogoutButton = False
else:
self.lockLabel.set_markup(
_('This computer is currently locked by the user <i>{username}</i>.\nThe user can be logged out now.').format(
username=GLib.markup_escape_text(username)))
2012-02-02 14:55:11 +01:00
self._enableLogoutButton = True
self._update()
def setAuthMessage(self, message):
2012-01-27 15:44:45 +01:00
self.messageLabel.set_label(message)
2012-02-08 16:10:46 +01:00
def _grab_devices(self, w, e):
for device in self.deviceManager.list_devices(Gdk.DeviceType.MASTER):
device.grab(self.unlockWindow.get_window(), Gdk.GrabOwnership.APPLICATION, True, Gdk.EventMask.ALL_EVENTS_MASK, None, Gdk.CURRENT_TIME)
2012-02-08 16:10:46 +01:00
return False
def _ungrab_devices(self):
for device in self.deviceManager.list_devices(Gdk.DeviceType.MASTER):
device.ungrab(Gdk.CURRENT_TIME)
2012-01-27 15:44:45 +01:00
def lock(self):
self.show_all()
self.unlockWindow.show_all()
self.promptEntry.grab_focus()
def unlock(self):
2012-02-08 16:10:46 +01:00
self._ungrab_devices()
2012-01-27 15:44:45 +01:00
self.unlockWindow.hide()
self.hide()
2012-02-02 14:55:11 +01:00
self.reset(True)