[Slim-Checkins] r11415 - in /trunk/server/Slim: Plugin/Favorites/OpmlFavorites.pm Plugin/Favorites/Plugin.pm Utils/Favorites.pm
adrian at svn.slimdevices.com
adrian at svn.slimdevices.com
Mon Feb 12 12:29:43 PST 2007
Author: adrian
Date: Mon Feb 12 12:29:43 2007
New Revision: 11415
URL: http://svn.slimdevices.com?rev=11415&view=rev
Log:
Bug: N/A
Description: Tentative cli support for opml based favorites. NB this
makes the favorites list indexed from 0 for consitency with xmlbrowser and cli.
Modified:
trunk/server/Slim/Plugin/Favorites/OpmlFavorites.pm
trunk/server/Slim/Plugin/Favorites/Plugin.pm
trunk/server/Slim/Utils/Favorites.pm
Modified: trunk/server/Slim/Plugin/Favorites/OpmlFavorites.pm
URL: http://svn.slimdevices.com/trunk/server/Slim/Plugin/Favorites/OpmlFavorites.pm?rev=11415&r1=11414&r2=11415&view=diff
==============================================================================
--- trunk/server/Slim/Plugin/Favorites/OpmlFavorites.pm (original)
+++ trunk/server/Slim/Plugin/Favorites/OpmlFavorites.pm Mon Feb 12 12:29:43 2007
@@ -65,7 +65,7 @@
$level = $class->toplevel;
}
- my $i = 1;
+ my $i = 0;
for my $entry (@{$level}) {
if ($entry->{'type'} eq 'audio' && ($entry->{'URL'} || $entry->{'url'}) ) {
@@ -105,6 +105,36 @@
$class->save;
}
+sub levelForIndex {
+ my $class = shift;
+ my $index = shift;
+
+ my @ind = split(/\./, $index);
+ my $pos = $class->toplevel;
+ my $prefix = '';
+
+ while (scalar @ind > 1 && ref $pos eq 'ARRAY') {
+ $prefix .= $ind[0] . '.';
+ $pos = $pos->[shift @ind]->{'outline'};
+ }
+
+ my $i = shift @ind;
+
+ if (!@ind) {
+
+ if ($pos->[$i] && ref $pos->[$i] eq 'HASH') {
+
+ return $pos, $i, $prefix;
+
+ } else {
+
+ return $pos, undef, $prefix;
+ }
+ }
+
+ return undef, undef, undef;
+}
+
sub clientAdd {
my $class = shift;
my $client = shift;
@@ -138,7 +168,7 @@
$class->save;
- return scalar @{$class->toplevel};
+ return scalar @{$class->toplevel} - 1;
}
sub findByClientAndURL {
@@ -164,7 +194,28 @@
return undef;
}
-sub findByClientAndId {
+sub deleteByClientAndURL {
+ my $class = shift;
+ my $client = shift;
+ my $url = shift;
+
+ if (blessed($url) && $url->can('url')) {
+ $url = $url->url;
+ }
+
+ $url =~ s/\?sessionid.+//i; # Bug 3362, ignore sessionID's within URLs (Live365)
+
+ if ($class->{'urlindex'}->{ $url }) {
+
+ $class->deleteByClientAndId($client, $class->{'urlindex'}->{ $url }->{'ind'});
+
+ } else {
+
+ $log->warn("Can't delete $url index does not exist");
+ }
+}
+
+sub deleteByClientAndId {
my $class = shift;
my $client = shift;
my $index = shift;
@@ -174,64 +225,14 @@
my $i;
while (scalar @ind > 1 && ref $pos eq 'ARRAY') {
- $pos = $pos->[(shift @ind) - 1]->{'outline'};
+ $pos = $pos->[shift @ind]->{'outline'};
}
my $i = shift @ind;
- my $entry = @{$pos}[ $i - 1 ];
-
- if (ref $entry eq 'HASH') {
-
- my $url = $entry->{'URL'} || $entry->{'url'};
- my $title = $entry->{'text'};
-
- $log->info("Found favorite at index $index: $title $url");
-
- return $url, $title;
- }
-
- return undef, undef;
-}
-
-sub deleteByClientAndURL {
- my $class = shift;
- my $client = shift;
- my $url = shift;
-
- if (blessed($url) && $url->can('url')) {
- $url = $url->url;
- }
-
- $url =~ s/\?sessionid.+//i; # Bug 3362, ignore sessionID's within URLs (Live365)
-
- if ($class->{'urlindex'}->{ $url }) {
-
- $class->deleteByClientAndId($client, $class->{'urlindex'}->{ $url }->{'ind'});
-
- } else {
-
- $log->warn("Can't delete $url index does not exist");
- }
-}
-
-sub deleteByClientAndId {
- my $class = shift;
- my $client = shift;
- my $index = shift;
-
- my @ind = split(/\./, $index);
- my $pos = $class->toplevel;
- my $i;
-
- while (scalar @ind > 1 && ref $pos eq 'ARRAY') {
- $pos = $pos->[(shift @ind) - 1]->{'outline'};
- }
-
- my $i = shift @ind;
-
- if (ref @{$pos}[ $i - 1 ] eq 'HASH') {
-
- splice @{$pos}, $i - 1, 1;
+
+ if (ref @{$pos}[ $i ] eq 'HASH') {
+
+ splice @{$pos}, $i, 1;
$log->info("Removed entry at index $index");
Modified: trunk/server/Slim/Plugin/Favorites/Plugin.pm
URL: http://svn.slimdevices.com/trunk/server/Slim/Plugin/Favorites/Plugin.pm?rev=11415&r1=11414&r2=11415&view=diff
==============================================================================
--- trunk/server/Slim/Plugin/Favorites/Plugin.pm (original)
+++ trunk/server/Slim/Plugin/Favorites/Plugin.pm Mon Feb 12 12:29:43 2007
@@ -51,6 +51,12 @@
# register handler for playing favorites by remote hot button
Slim::Buttons::Common::setFunction('playFavorite', \&playFavorite);
+
+ # register cli handlers
+ Slim::Control::Request::addDispatch(['favorites', '_index', '_quantity'], [0, 1, 1, \&cliBrowse]);
+ Slim::Control::Request::addDispatch(['favorites', 'add', '_url', '_title', '_index'], [0, 0, 0, \&cliAdd]);
+ Slim::Control::Request::addDispatch(['favorites', 'addlevel', '_title', '_index'], [0, 0, 0, \&cliAdd]);
+ Slim::Control::Request::addDispatch(['favorites', 'delete', '_index'], [0, 0, 0, \&cliDelete]);
}
sub setMode {
@@ -95,13 +101,9 @@
my $button = shift;
my $digit = shift;
- if ($digit == 0) {
- $digit = 10;
- }
-
- my ($url, $title) = Slim::Plugin::Favorites::OpmlFavorites->new->findByClientAndId($client, $digit);
-
- if (!$url) {
+ my ($level, $index, undef) = Slim::Plugin::Favorites::OpmlFavorites->new->levelForIndex($digit);
+
+ if (!defined $index) {
$client->showBriefly({
'line' => [ sprintf($client->string('FAVORITES_NOT_DEFINED'), $digit) ],
@@ -110,6 +112,11 @@
return;
} else {
+
+ my $entry = $level->[$index];
+
+ my $url = $entry->{'URL'} || $entry->{'url'};
+ my $title = $entry->{'title'};
$log->info("Playing favorite number $digit $title $url");
@@ -443,4 +450,134 @@
return Slim::Web::HTTP::filltemplatefile('plugins/Favorites/edit.html', $params);
}
+sub cliBrowse {
+ my $request = shift;
+
+ if ($request->isNotQuery([['favorites']])) {
+ $request->setStatusBadDispatch();
+ return;
+ }
+
+ my $index = $request->getParam('_index');
+ my $quantity = $request->getParam('_quantity');
+
+ my ($level, $start, $prefix) = Slim::Plugin::Favorites::OpmlFavorites->new->levelForIndex($index);
+
+ my $count = $level ? scalar @$level : 0;
+
+ $request->addResult('count', $count);
+
+ if (defined $start) {
+
+ $log->info("found start index $index in favorites, returning entries");
+
+ my $ind = $start;
+ my $cnt = 0;
+
+ while ($level->[$ind] && $cnt < $quantity) {
+
+ my $entry = $level->[$ind];
+
+ $request->addResultLoop('@favorites', $cnt, 'id', $prefix . $ind );
+ $request->addResultLoop('@favorites', $cnt, 'title', $entry->{'text'});
+ $request->addResultLoop('@favorites', $cnt, 'url', $entry->{'URL'} || $entry->{'url'});
+
+ if ($entry->{'outline'} && ref $entry->{'outline'} eq 'ARRAY') {
+ $request->addResultLoop('@favorites', $cnt, 'hasitems', scalar @{$entry->{'outline'}} );
+ }
+
+ ++$ind;
+ ++$cnt;
+ }
+
+ } else {
+
+ $log->info("start index $index does not exist in favorites");
+ }
+
+ $request->setStatusDone();
+}
+
+sub cliAdd {
+ my $request = shift;
+
+ if ($request->isNotCommand([['favorites'], ['add', 'addlevel']])) {
+ $request->setStatusBadDispatch();
+ return;
+ }
+
+ my $command= $request->getRequest(1);
+ my $url = $request->getParam('_url');
+ my $title = $request->getParam('_title');
+ my $index = $request->getParam('_index');
+
+ my $favs = Slim::Plugin::Favorites::OpmlFavorites->new;
+
+ my ($level, $i) = defined $index ? $favs->levelForIndex($index) : ($favs->toplevel, scalar @{$favs->toplevel});
+
+ if ($level) {
+
+ my $entry;
+
+ if ($command eq 'add' && defined $title && defined $url) {
+
+ $log->info("adding entry $title $url at index $index");
+
+ $entry = {
+ 'text' => $title,
+ 'URL' => $url,
+ 'type' => 'audio',
+ };
+
+ } elsif ($command eq 'addlevel' && defined $title) {
+
+ $log->info("adding new level $title at index $index");
+
+ $entry = {
+ 'text' => $title,
+ 'outline' => [],
+ };
+
+ } else {
+
+ $log->info("can't perform $command bad title or url");
+ request->setStatusBadParams();
+ return;
+ }
+
+ splice @$level, $i, 0, $entry;
+
+ $favs->save;
+
+ $request->setStatusDone();
+
+ } else {
+
+ $log->info("index $index invalid");
+
+ request->setStatusBadParams();
+ }
+}
+
+sub cliDelete {
+ my $request = shift;
+
+ if ($request->isNotCommand([['favorites'], ['delete']])) {
+ $request->setStatusBadDispatch();
+ return;
+ }
+
+ my $index = $request->getParam('_index');;
+
+ if (!defined $index) {
+ $request->setStatusBadParams();
+ return;
+ }
+
+ Slim::Plugin::Favorites::OpmlFavorites->new->deleteByClientAndId(undef, $index);
+
+ $request->setStatusDone();
+}
+
+
1;
Modified: trunk/server/Slim/Utils/Favorites.pm
URL: http://svn.slimdevices.com/trunk/server/Slim/Utils/Favorites.pm?rev=11415&r1=11414&r2=11415&view=diff
==============================================================================
--- trunk/server/Slim/Utils/Favorites.pm (original)
+++ trunk/server/Slim/Utils/Favorites.pm Mon Feb 12 12:29:43 2007
@@ -47,7 +47,6 @@
# sub new ( $class ) - contructor
# sub clientAdd ( $class, $client, $url, $title ) - add $url with $title to favorites
# sub findByClientAndURL ( $class, $client, $url ) - returns index or undef for $url from favorites
-# sub findByClientAndId ( $class, $client, $index ) - returns ($url, $title) for $index from favorites
# sub deleteByClientAndURL( $class, $client, $url ) - deletes $url from favorites
# sub deleteByClientAndId ( $class, $client, $index ) - deletes favorite with index $index
More information about the checkins
mailing list