#
#	$Id: PhoneBook.pm,v 1.19 2004/12/30 03:19:31 kevin Exp $
#
#	Author: Kevin Walsh <kevin@cursor.biz>
#
#	Copyright (c) 2003-2004 Cursor Software Limited.
#	All rights reserved.
#
#	----------------------------------------------------------------------
#
#	Simple phone book plugin for the SLIMP3 server.  Allows the SLIMP3
#	clients to scroll through phone book entries saved in a text file.
#
#	The text file format consists of multiple lines in the following
#	format:
#
#		name | number
#
#	for instance:
#
#		Some person     | (020) 8111 1111
#		Another person  | (020) 8222 2222
#		And another     | (020) 8333 3333
#
#	The module will sort the list for you (by name), so you don't have
#	to worry about that.
#
#	The phonebook.txt file should be placed in the SLIMP3 server
#	user's home directory, which is the same directory as the
#	.slimp3.pref file.
#
#	Scroll through the list using the up/down buttons or use the numeric
#	keys like a phone keypad (1=ABC, 2=DEF etc.) to jump to an entry in
#	the list.  The 'add' (rec) key will refresh the phone book list from
#	the text file if required.  The phone book file will be read every
#	time this module is called so the 'add' key is largely unnecessary.
#
#	----------------------------------------------------------------------
#
#	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
#
package Plugins::PhoneBook;
use Slim::Utils::Strings qw(string);
use File::Spec::Functions qw(catdir);
use strict;

#
#	the name and location of the phone book text file
#
my $phonebook_location = catdir(Slim::Utils::Prefs::preferencesPath(),'phonebook.txt');

#
#	end of configurable variables
#

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

my @phone_list = ();
my %context;

my %functions = (
    'left' => sub {
	Slim::Buttons::Common::popModeRight(shift);
    },
    'up' => sub {
	my $client = shift;

	$context{$client} = Slim::Buttons::Common::scroll(
	    $client,
	    -1,
	    $#phone_list + 1,
	    $context{$client} || 0,
	);
	$client->update();
    },
    'down' => sub {
	my $client = shift;

	$context{$client} = Slim::Buttons::Common::scroll(
	    $client,
	    1,
	    $#phone_list + 1,
	    $context{$client} || 0,
	);
	$client->update();
    },
    'numberScroll' => sub  {
	my ($client,undef,$digit) = @_;

	$context{$client} = Slim::Buttons::Common::numberScroll(
	    $client,
	    $digit,
	    \@phone_list,
	    1,
	    sub { $phone_list[shift]->[0] },
	);
	$client->update();
    },
    'add' => sub {
	#
	#	refresh the phonebook list from the text file
	#
	#	note that the [add] key is labeled as [rec] on the Sony remotes
	#
	my $client = shift;

	Slim::Display::Animation::showBriefly(
	    $client,
	    string('PLUGIN_PHONEBOOK_MODULE_NAME') . ' ' . string('PLUGIN_PHONEBOOK_UPDATING'),
	);
	read_textfile();
    },
);

#
#	lines()
#	-------
#	Create and return the two-line display.
#
sub lines
{
    my $client = shift;

    if (scalar(@phone_list)) {
	return @{$phone_list[$context{$client} || 0]};
    }
    return (string('PLUGIN_PHONEBOOK_EMPTY'),'');
}	

#
#	read_textfile()
#	---------------
#	Overwrites the @phone_list with data collected from the
#	text file configured into the $phonebook_location variable.
#
sub read_textfile
{
    @phone_list = ();

    open(PHONEBOOK,$phonebook_location) or return undef;

    while (<PHONEBOOK>) {
	s/^\s+//;
	s/[\s\r\n]+$//;
	next unless $_;

	my @cols = split('\s*\|\s*',$_);
	push(@phone_list,\@cols);
    }
    close PHONEBOOK;

    @phone_list = sort { lc($a->[0]) cmp lc($b->[0]) } @phone_list;
    undef;
}

sub setMode
{
    my $client = shift;

    read_textfile();

    $client->lines(\&lines);
    $client->update();
}

sub getFunctions
{
    \%functions;
}

sub getDisplayName
{
    my $name = 'PLUGIN_PHONEBOOK_MODULE_NAME';

    $::VERSION =~ /^(\d+)/;
    return ($1 >= 6) ? $name : string($name);
}

sub strings
{
    local $/ = undef;
    <DATA>;
}

1;

__DATA__

PLUGIN_PHONEBOOK_MODULE_NAME
	EN	Phone Book

PLUGIN_PHONEBOOK_EMPTY
	EN	Your phone book is empty

PLUGIN_PHONEBOOK_UPDATING
	EN	updating...


