diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-03-06 14:16:47 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-03-06 14:16:47 +0100 |
commit | 4659883ba00bd499f41f4cc1a5f96e4bdd5ac9de (patch) | |
tree | 74211870b90bb20c0041c8a76d677579a88206a9 /pylock.py | |
parent | e342d5c7241cd04352948fb6937e05a98000b70c (diff) | |
download | pylock-4659883ba00bd499f41f4cc1a5f96e4bdd5ac9de.tar pylock-4659883ba00bd499f41f4cc1a5f96e4bdd5ac9de.zip |
Rework pylock to behave like xlock
Diffstat (limited to 'pylock.py')
-rw-r--r-- | pylock.py | 77 |
1 files changed, 35 insertions, 42 deletions
@@ -30,33 +30,27 @@ import sys import os import pwd import locale -import argparse +import configparser -from gi.repository import Gtk, Gdk, Gio +from gi.repository import Gtk, GLib from Locker import Locker from LockWindow import LockWindow -import Message +import Selection import pam -_ = locale.gettext - +CONFIG_FILE = '/etc/pylock.conf' -def get_username(): - return pwd.getpwuid(os.getuid())[0] - -theme = 'UzL-login' +_ = locale.gettext -parser = argparse.ArgumentParser(prog='pylock', description=_('GTK-based screen locker.')) -parser.add_argument('-t', '--timeout', type=int, default=0, help=_('Start in daemon mode that will automatically activate after a given time of inactivity.')) -parser.add_argument('-l', '--logout', type=int, default=0, help=_('Allow to logout the current user after a given time.')) -parser.add_argument('-c', '--logout-command', help=_('Specifies the command to be called to log out a user.')) -parser.add_argument('-u', '--username', default=get_username(), help=_('Set the name of the user that is able to unlock the screen.')) +configParser = configparser.ConfigParser() +configParser.read_file(open(CONFIG_FILE)) -args = parser.parse_args() +config = configParser['pylock'] +username = pwd.getpwuid(os.getuid())[0] def handler(signum, frame): @@ -71,66 +65,65 @@ signal.signal(signal.SIGQUIT, handler) locale.setlocale(locale.LC_ALL, '') locale.textdomain('pylock') -Gtk.Settings.get_default().set_property('gtk-theme-name', theme) +Gtk.Settings.get_default().set_property('gtk-theme-name', config['theme']) -if Message.getSelection() != 0: - if args.timeout == 0: - Message.sendLockMessage() - exit(0) - else: - print('There seems to be a Pylock instance already running.', file=sys.stderr) - exit(1) +def waitForSelection(): + if Selection.get() != 0: + return True + + Gtk.main_quit() + return False + +if Selection.get() != 0: + GLib.timeout_add_seconds(1, waitForSelection) + Gtk.main() + exit(0) window = LockWindow() -if not Message.acquireSelection(window): - print('Unable to register for lock messages', file=sys.stderr) +if not Selection.acquire(window): + print('Unable to register pylock at X server', file=sys.stderr) + exit(1) def lock(timeLeft): - window.updateLockMessage(args.username, timeLeft) + window.updateLockMessage(username, timeLeft) return window.lock() def logout(): try: - os.system(args.logout_command) + os.system(config['logout_command']) except: pass window.reset(False) def updateTimeout(timeLeft): - window.updateLockMessage(args.username, timeLeft) + window.updateLockMessage(username, timeLeft) -if args.logout_command is None: - locker = Locker(args.timeout, lock, window.unlock) +if not ('logout_timeout' in config and 'logout_command' in config): + locker = Locker(lock, window.unlock) else: - locker = Locker(args.timeout, lock, window.unlock, args.logout, logout, updateTimeout) - - -def _triggerLock(w, e): - locker.lock() - -window.connect('map-event', _triggerLock) - + locker = Locker(lock, window.unlock, int(config['logout_timeout']), logout, updateTimeout) pamAuth = pam.pam() def tryUnlock(w, password): - if pamAuth.authenticate(args.username, password): + if pamAuth.authenticate(username, password): locker.unlock() - - if args.timeout == 0: - Gtk.main_quit() + Gtk.main_quit() else: window.reset() window.setAuthMessage(pamAuth.reason) return True + window.connect('logout', lambda w: locker.logout()) window.connect('tryUnlock', tryUnlock) +GLib.idle_add(lambda: locker.lock()) + Gtk.main() |