# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# idletimer.py - check for user activity and write/remove lock file for 
#                wakeup/shutdown script
# -----------------------------------------------------------------------
#
# Notes: To use add the following to local_conf.py:
#        plugin.activate('idletimer')
#	 IDLETIMER_INTERNAL_DELAY=700 # internal idle time in sec
#	 IDLETIMER_EXTERNAL_DELAY=900 # external idle time in sec
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2003 Krister Lagerstrom, et al. 
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# 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 2 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 MER-
# CHANTABILITY or FITNESS 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */


import time
import os
import config
import plugin
#from playlist import Playlist
import rc
import event as em
#import fxditem

# Set to 1 for debug output
DEBUG = config.DEBUG

TRUE = 1
FALSE = 0

class PluginInterface(plugin.DaemonPlugin):

    def __init__(self):
        plugin.DaemonPlugin.__init__(self)
        self.plugin_name = 'idletimer'
	self.default_config()
	self.read_conf()
        self.event_listener = TRUE
	self.poll_menu_only = TRUE
	self.last_event = 0
        self.idletimer_showing = FALSE
	self.vitem = None
	self.pl = None
	self.menuw = None
	self.poll_interval = 10 * self.IDLETIMER_POLL
	self.idletimer_delay = self.IDLETIMER_INTERNAL_DELAY
	self.lockfile = '/tmp/idletimer_lock_automatically'
	try:
		os.remove(self.lockfile)
	except:	
		print 'idletimer: no lockfile present on startup'
	self.start_idletimer()
   
    def default_config(self):
        
	# default config that works, can be overridden in local_conf
	self.IDLETIMER_INTERNAL_DELAY = 30	# value in seconds
	# checks every 10 sec, no override 
	self.IDLETIMER_POLL = 100	# value in seconds/10
	
    def read_conf(self):

	# idle time that has to be exceeded before idletimer starts
	if config.IDLETIMER_INTERNAL_DELAY != '':
	    self.IDLETIMER_INTERNAL_DELAY = config.IDLETIMER_INTERNAL_DELAY

    def eventhandler(self, event = None, menuw=None, arg=None):
        """
        eventhandler to handle the events. Always return false since we
	are just a listener and really can't send back true.
        """
        _debug_("Idletimer saw %s" % event.name)
	if menuw:
	    self.menuw = menuw

        if event.name == 'IDLETIMER_START':
	    self.start_idletimer()
	    return FALSE

        if event.name == 'IDLETIMER_STOP' and self.idletimer_showing :
	    self.stop_idletimer()
	    return FALSE

        if self.idletimer_showing :
	    self.stop_idletimer()

	if not event.name == 'IDENTIFY_MEDIA':
	    self.last_event = time.time()

        return FALSE

    def poll(self):
        _debug_("Idletimer got polled %f" % time.time())
	if not self.idletimer_showing and (time.time() - self.last_event) > self.idletimer_delay :
	    rc.post_event(em.Event("IDLETIMER_START"))

    def start_idletimer (self):
	 _debug_("start idletimer")
         self.idletimer_showing = TRUE
         try:
	     os.remove(self.lockfile)
	 except:	
	     print 'idletimer: lockfile has been removed during user activity'	
    
    def stop_idletimer (self):
	 _debug_("stop idletimer")
         self.idletimer_showing = FALSE
	 open(self.lockfile, 'w').close()
