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/Locker.py

52 lines
1.5 KiB
Python

from gi.repository import Gtk, GLib
import Idle
class Locker(object):
def __init__(self, lockTimeout, doLock, doUnlock, logoutTimeout = 0, doLogout = None, updateLogoutTimeout = None):
self.locked = False
self.lockTimeout = lockTimeout
self.logoutTimeout = logoutTimeout
self.doLock = doLock
self.doUnlock = doUnlock
self.doLogout = doLogout
self.updateLogoutTimeout = updateLogoutTimeout
GLib.timeout_add_seconds(1, self._checkLock)
def _checkLock(self):
if self.locked:
return False
idle = Idle.getIdleSec()
if (idle >= self.lockTimeout):
GLib.idle_add(self.lock)
return False
else:
return True
def _checkLogout(self):
self.currentLogoutTimeout = self.currentLogoutTimeout - 1
if self.updateLogoutTimeout is not None:
self.updateLogoutTimeout(self.currentLogoutTimeout)
return self.currentLogoutTimeout > 0
def lock(self):
if not self.locked:
self.doLock(self.logoutTimeout)
self.locked = True
if self.doLogout is not None:
self.currentLogoutTimeout = self.logoutTimeout
if self.currentLogoutTimeout > 0:
GLib.timeout_add_seconds(1, self._checkLogout)
def unlock(self):
if self.locked:
self.doUnlock()
self.locked = False
GLib.timeout_add_seconds(1, self._checkLock)