import sys
import locale
from gi.repository import Gtk, Gdk, GObject, GLib
_ = locale.gettext
class LockWindow(Gtk.Window):
__gsignals__ = {
'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')
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())
self.unlockButton.connect('clicked', lambda w: self._tryUnlock())
self.reset()
def _tryUnlock(self):
self.promptEntry.set_sensitive(False)
self.unlockButton.set_sensitive(False)
GLib.idle_add(lambda: self.emit('tryUnlock', self.promptEntry.get_text()))
def reset(self):
self.promptEntry.set_text('')
self.promptEntry.set_sensitive(True)
self.promptEntry.grab_focus()
self.unlockButton.set_sensitive(True)
self.messageLabel.set_label('')
self.logoutButton.set_sensitive(False)
def updateLockMessage(self, username, logoutTime = None):
if logoutTime is None:
self.lockLabel.set_markup(_('This computer is currently locked by the user {username}.').format(username=GLib.markup_escape_text(username)))
self.logoutButton.set_sensitive(False)
elif logoutTime > 0:
self.lockLabel.set_markup(
_('This computer is currently locked by the user {username}.\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))
self.logoutButton.set_sensitive(False)
else:
self.lockLabel.set_markup(
_('This computer is currently locked by the user {username}.\nThe user can be logged out now.').format(
username=GLib.markup_escape_text(username)))
self.logoutButton.set_sensitive(True)
def setAuthMessage(self, message):
self.messageLabel.set_label(message)
def lock(self):
self.show_all()
self.unlockWindow.show_all()
self.promptEntry.grab_focus()
def unlock(self):
self.unlockWindow.hide()
self.hide()
self.reset()