# # This code is derived from code with the following copyright message: # # SlimServer Copyright (C) 2001 Sean Adams, Slim Devices Inc. # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License, # version 2. package Plugins::StationPlaylist; use vars qw($VERSION); $VERSION = substr(q$Revision: 0.1 $,10); use Slim::Player::Playlist; use Slim::Player::Source; use Slim::Player::Sync; use File::Spec::Functions qw(:ALL); use File::Spec::Functions qw(updir); use Slim::Utils::Misc; use Slim::Utils::Strings qw (string); our $interval; # check every x seconds our %playlist; our %monthNames; our %active; sub getDisplayName { return 'PLUGIN_STATIONPLAYLIST'; } my %functions; sub initPlugin { setTimer(); @monthNames = ('January','February','March','April','May','June','July','August','September','October','November','December'); %functions = ( 'play' => sub { my $client = shift; if ($active{$client}) { $active{$client} = 0; $client->prefSet("PLUGIN.stationplaylist_active",0); $client->showBriefly({ 'line1' => $client->string(getDisplayName()), 'line2' => $client->string('PLUGIN_STATIONPLAYLIST_OFF') }); } else { $active{$client} = 1; $client->prefSet("PLUGIN.stationplaylist_active",1); $client->showBriefly({ 'line1' => $client->string(getDisplayName()), 'line2' => $client->string('PLUGIN_STATIONPLAYLIST_ON') }); } }, 'left' => sub { my $client = shift; Slim::Buttons::Common::popModeRight($client); }, 'right' => sub { my $client = shift; $client->bumpRight($client); }, ); }; sub setTimer { # dont' have more than one. Slim::Utils::Timers::killTimers($client, \&checkAlarms); #timer to check alarms on an interval Slim::Utils::Timers::setTimer(0, Time::HiRes::time() + ($interval || 1), \&checkTime); } sub checkTime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my $time = $hour * 60 * 60 + $min * 60; #increment month to convert from index to number $mon++; $::d_plugins && msg("Alarm Plugin $alarmID: Checking timer\n") if defined $interval && $min == 0; if ($sec == 0) { # once we've reached the beginning of a minute, only check every 60s $interval = 60; } if ($sec >= 50 || ! defined $interval) { # if we end up falling behind, go back to checking each second $interval = 60-$sec; } foreach my $client (Slim::Player::Client::clients()) { $active{$client} = $client->prefGet("PLUGIN.stationplaylist_active") || 0; #skip inactive players next unless $active{$client}; #use double digit month and days $mon = '0'.$mon if $mon <= 9; $mday = '0'.$mday if $mon <= 9; #only work if a playlist folder is set for use if ($client->prefGet('PLUGIN.stationplaylist_location')) { #create the full path $playlist{$client} = catdir($client->prefGet('PLUGIN.stationplaylist_location'),$mon.$mday."-".$hour.".m3u"); #check vs current playlist and only reload if it is a new one my $current = Slim::Utils::Misc::pathFromFileURL($client->currentPlaylist()) unless $client->currentPlaylist() eq ''; $::d_plugins && msg("StationPlaylist: checking for $playlist{$client}, current is: $current\n"); if (-e $playlist{$client} && $playlist{$client} ne $current) { $interval = 60; Slim::Utils::Timers::setTimer($client, Time::HiRes::time() + 2, \&trigger, $client); } else { $::d_plugins && msg("StationPlaylist: required playlist already loaded.\n"); } } } setTimer(); } sub getFunctions { return \%functions; } sub lines { my $client = shift; return { 'line1' => $client->string(getDisplayName())." (".$client->string(($active{$client}?"ON":"OFF")).")", 'line2' => $client->string("PLUGIN_STATIONPLAYLIST_TURN_".($active{$client}?"OFF":"ON")), }; } sub setMode { my $client = shift; $active{$client} = $client->prefGet("PLUGIN.stationplaylist_active") || 0; $client->lines(\&lines); } sub trigger { my $client = shift; Slim::Hardware::IR::setLastIRTime( $client, Time::HiRes::time() + ($client->prefGet("screensavertimeout") * 5), ); $::d_plugins && msg("StationPlaylist: loading $playlist{$client}\n"); Slim::Control::Command::execute($client, ['stop']); Slim::Control::Command::execute($client, ["playlist", "play", $playlist{$client}]); Slim::Buttons::Common::pushModeLeft($client, 'playlist'); }; sub setupGroup { my $client = shift; my %setupGroup = ( 'PrefOrder' => ["PLUGIN.stationplaylist_location"] ,'GroupHead' => string(getDisplayName()) ,'GroupDesc' => string('SETUP_GROUP_STATIONPLAYLIST_DESC') ,'GroupLine' => 1 ,'GroupSub' => 1 ,'PrefsInTable' => 1 ,'Suppress_PrefHead' => 1 ,'Suppress_PrefDesc' => 1 ,'Suppress_PrefLine' => 1 ,'Suppress_PrefSub' => 1 ); my %setupPrefs = ( 'PLUGIN.stationplaylist_location' => { 'validate' => \&Slim::Web::Setup::validateIsDir, 'validateArgs' => [1], 'changeIntro' => string('SETUP_PLAYLISTDIR_OK'), 'rejectMsg' => string('SETUP_BAD_DIRECTORY'), 'PrefSize' => 'large', }, ); return (\%setupGroup,\%setupPrefs,1); } #Set common text strings. can be placed in strings.txt for language templates. sub strings { return ' PLUGIN_STATIONPLAYLIST EN Station Playlist SETUP_GROUP_STATIONPLAYLIST_DESC EN Enter the location of your Station Playlist Creator playlists: PLUGIN_STATIONPLAYLIST_ON EN Activating Auto Playlists PLUGIN_STATIONPLAYLIST_OFF EN Deactivating Auto Playlists PLUGIN_STATIONPLAYLIST_TURN_ON EN Press PLAY to turn ON PLUGIN_STATIONPLAYLIST_TURN_OFF EN Press PLAY to turn OFF '}; 1; __END__