#!/usr/bin/env python
# -*- coding: utf-8 -*-

###########################################################################
# Lock the screen.
#
# Copyright (C) 2010 Fotis Tsamis <ftsamis@gmail.com>
#
# This program 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FINESS 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL".
###########################################################################

import gtk
import gtk.gdk as gdk
import pango
import gobject
import sys
import time

class lockscreen:
    def lock(self, msg="The screen is locked by a system administrator."):
        backlock = gtk.Window(gtk.WINDOW_POPUP)
        backlock.resize(1, 1)
        backlock.modify_bg(gtk.STATE_NORMAL, gdk.Color("#000000"))
        frontview = gtk.Window()
        frontview.modify_bg(gtk.STATE_NORMAL, gdk.Color("#000000"))

        vbox = gtk.VBox(spacing=75)

        imagepb = gtk.gdk.pixbuf_new_from_file_at_size('lock.svg', 250, 250)
        image = gtk.Image()
        image.set_from_pixbuf(imagepb)
        vbox.pack_start(image, True)

        # Italic fonts are trancated in the sides, so add a space
        self.label = gtk.Label(" " + msg + " ")
        self.label.modify_font(pango.FontDescription('FreeSans italic 18'))
        self.fade(True)
        vbox.pack_start(self.label, True)

        align = gtk.Alignment(0.5, 0.5, 0, 0)
        align.add(vbox)
        frontview.add(align)

        backlock.show_all()
        frontview.show_all()

        frontview.set_keep_above(True)
        frontview.fullscreen()
        gdk.beep()
        gdk.keyboard_grab(backlock.window, False, 0L)

        # To automatically unlock the screen after 7 seconds, uncomment this:
        #gobject.timeout_add(7000, self.unlock)


    def fade(self, firsttime=False):
        if firsttime:
            self.fadesecs = 3.0
            self.fadestart = time.time()
            # For debugging:
            #print self.fadestart, self.fadesecs

        percentage = (time.time() - self.fadestart) / self.fadesecs
        i = min(round(255 * percentage), 255)
        c = "#%02x%02x%02x" % (i, i, i)
        # For debugging:
        #print time.time(), '-', 100 * percentage, "completed, rendering color ", c
        self.label.modify_fg(gtk.STATE_NORMAL, gdk.Color(c))
        # Put a sleep() here if you want to test for slow thin clients:
        #time.sleep(0.6)
        if i < 255:
            gobject.timeout_add(60, self.fade)

        return False


    def unlock(self):
        gdk.keyboard_ungrab(0L)
        exit()


if len(sys.argv) <= 1:
    lockscreen().lock()
else:
    lockscreen().lock(sys.argv[1])
gtk.main()
