#	XDateTime.pm
#
#	Author: Felix Mueller <felix(dot)mueller(at)gwendesign(dot)com>
#
#	Copyright (c) 2004-2005 by GWENDESIGN
#	All rights reserved.
#
#	Based on: Original DateTime
#
#	----------------------------------------------------------------------
#	Function:
#
#	Only shows the date and time for about X seconds falling back to
#	 regular display automatically.
#
#	----------------------------------------------------------------------
#	Usage:
#
#	- Select the plugin in the web- or playerinterface
#
#	----------------------------------------------------------------------
#	Installation:
#
#	- Copy this file into the 'Plugins' directory
#	- Restart SlimServer
#	----------------------------------------------------------------------
#	History:
#
#	2005/11/05 v0.1	- Initial version
#	----------------------------------------------------------------------
#       To do:
#
#       - Clean up code
#
#	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
#	MERCHANTABILITY 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
#

use strict;

###########################################
### Section 1. Change these as required ###
###########################################

package Plugins::XDateTime;

use Slim::Control::Command;

use vars qw($VERSION);
$VERSION = substr(q$Revision: 0.1 $,10);


# ----------------------------------------------------------------------------
# Change to you liking
# ----------------------------------------------------------------------------

my $showTime = 4.0;


# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
sub getDisplayName {
	return 'PLUGIN_SCREENSAVER_XDATETIME';
}

# ----------------------------------------------------------------------------
sub strings { return '
PLUGIN_SCREENSAVER_XDATETIME
	CZ	XDateTime spořič
	DE	XDateTime Bildschirmschoner
	EN	XDateTime
	ES	Salvapantallas de XDateTime
	FR	Ecran de veille XDateTime
	
PLUGIN_SCREENSAVER_XDATETIME_ENABLE
	DE	PLAY drücken zum Aktivieren des Bildschirmschoners
	EN	Press PLAY to enable this screensaver
	ES	Presionar PLAY para activar este salvapantallas
	FR	Appuyer sur PLAY pour activer

PLUGIN_SCREENSAVER_XDATETIME_DISABLE
	CZ	Stiskněte PLAY pro zakázání spořiče
	DE	PLAY drücken zum Deaktivieren dieses Bildschirmschoners 
	EN	Press PLAY to disable this screensaver
	ES	Presionar PLAY para desactivar este salvapantallas
	FR	Appuyer sur PLAY pour désactiver
	
PLUGIN_SCREENSAVER_XDATETIME_ENABLING
	DE	XDateTime Bildschirmschoner aktivieren
	EN	Enabling XDateTime as current screensaver
	ES	Activando XDateTime como salvapantallas actual
	FR	Activation écran de veille XDateTime

PLUGIN_SCREENSAVER_XDATETIME_DISABLING
	CZ	Nastavit výchozí spořič
	DE	Standard-Bildschirmschoner aktivieren
	EN	Resetting to default screensaver
	ES	Restableciendo el salvapantallas por defecto
	FR	Retour à l\'écran de veille par défaut
'};

##################################################
### Section 2. Your variables and code go here ###
##################################################

# ----------------------------------------------------------------------------
sub enabled {
	return ($::VERSION ge '6.1');
}

# ----------------------------------------------------------------------------
sub setMode {
	my $client = shift;
	$client->lines(\&lines);

	# setting this param will call client->update() frequently
	$client->param('modeUpdateInterval', 1); # seconds
}

# ----------------------------------------------------------------------------
our %functions = (
	'up' => sub  {
		my $client = shift;
		my $button = shift;
		$client->bumpUp() if ($button !~ /repeat/);
	},
	'down' => sub  {
	    my $client = shift;
		my $button = shift;
		$client->bumpDown() if ($button !~ /repeat/);;
	},
	'left' => sub  {
		my $client = shift;
		Slim::Buttons::Common::popModeRight($client);
	},
	'right' => sub  {
		my $client = shift;
		$client->bumpRight();
	},
	'play' => sub  {
		my $client = shift;
		if ($client->prefGet('screensaver') ne 'SCREENSAVER.xdatetime') {
			$client->prefSet('screensaver','SCREENSAVER.xdatetime');
			$client->showBriefly( {
				'line1' => $client->string('PLUGIN_SCREENSAVER_XDATETIME'),
				'line2' => $client->string('PLUGIN_SCREENSAVER_XDATETIME_ENABLING'),
			});
		} else {
			$client->prefSet('screensaver','screensaver');
			$client->showBriefly( {
				'line1' => $client->string('PLUGIN_SCREENSAVER_XDATETIME'),
				'line2' => $client->string('PLUGIN_SCREENSAVER_XDATETIME_DISABLING'),
			});
		}
	},
	'stop' => sub {
		my $client = shift;
		Slim::Buttons::Common::pushMode($client, 'SCREENSAVER.xdatetime');
	}
);

# ----------------------------------------------------------------------------
sub lines {
	my $client = shift;
	my $line2;
	if ($client->prefGet('screensaver') ne 'SCREENSAVER.xdatetime') {
		$line2 = $client->string('PLUGIN_SCREENSAVER_XDATETIME_ENABLE');
	} else {
		$line2 = $client->string('PLUGIN_SCREENSAVER_XDATETIME_DISABLE');
	};
	return {
		'line1' => $client->string('PLUGIN_SCREENSAVER_XDATETIME'),
		'line2' => $line2,
	};
}

# ----------------------------------------------------------------------------
sub getFunctions {
	return \%functions;
}

###################################################################
### Section 3. Your variables for your screensaver mode go here ###
###################################################################

# First, Register the screensaver mode here.  Must make the call to addStrings in order to have plugin
# localization available at this point.
# ----------------------------------------------------------------------------
sub screenSaver {
	Slim::Buttons::Common::addSaver(
		'SCREENSAVER.xdatetime',
		getScreensaverXDatetime(),
		\&setScreensaverXDateTimeMode,
		undef,
		'PLUGIN_SCREENSAVER_XDATETIME',
	);
}

# ----------------------------------------------------------------------------
our %screensaverXDateTimeFunctions = (
	'done' => sub  {
		my ($client ,$funct ,$functarg) = @_;

		Slim::Buttons::Common::popMode($client);
		$client->update();

		# pass along ir code to new mode if requested
		if (defined $functarg && $functarg eq 'passback') {
			Slim::Hardware::IR::resendButton($client);
		}
	},
);

# ----------------------------------------------------------------------------
sub getScreensaverXDatetime {
	return \%screensaverXDateTimeFunctions;
}

# ----------------------------------------------------------------------------
sub setScreensaverXDateTimeMode() {
	my $client = shift;
	$client->lines(\&screensaverXDateTimelines);

	# setting this param will call client->update() frequently
	$client->param('modeUpdateInterval', 1); # seconds
	
	
	Slim::Utils::Timers::killTimers( $client, \&autoTurnOff);
	Slim::Utils::Timers::setTimer( $client, Time::HiRes::time() + $showTime, \&autoTurnOff);
}

# ----------------------------------------------------------------------------
sub autoTurnOff {
	my $client = shift;

	# Prevent screensaver from kicking in again immediately
	Slim::Hardware::IR::setLastIRTime( $client, time());
	Slim::Buttons::Common::popMode( $client);
	$client->update();
}

# ----------------------------------------------------------------------------
sub screensaverXDateTimelines {
	my $client = shift;
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	my $alarmOn = $client->prefGet("alarm", 0) || $client->prefGet("alarm", $wday);

	my $nextUpdate = $client->periodicUpdateTime();
	Slim::Buttons::Common::syncPeriodicUpdates($client, int($nextUpdate)) if (($nextUpdate - int($nextUpdate)) > 0.01);

	return {
		'center1' => Slim::Utils::Misc::longDateF(),
		'center2' => &getFuzzyTimeF($client),
		'overlay1'=> ($alarmOn ? $client->symbols('bell') : undef),
		'fonts'   => {	'graphic-280x16'  => { 'overlay1' => \ 'small.1' },
				'graphic-320x32'  => { 'overlay1' => \ 'standard.1' },
				'text' =>            { 'displayoverlays' => 1 },
				   },
	};
}

# FuzzyTime timeF() routine:
sub getFuzzyTimeF {
        my ($client, $time, $format) = @_;
        if ( defined($Plugins::FuzzyTime::Plugin::apiVersion) ) {
                return Plugins::FuzzyTime::Public::timeF($client,$time,$format);        }
        return Slim::Utils::DateTime::timeF($time,$format);
}

1;

__END__

# Local Variables:
# tab-width:4
# indent-tabs-mode:t
# End:
