| 1 | package App::Pimpd::Collection::Album; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | BEGIN { | ||
| 5 | use Exporter; | ||
| 6 | use vars qw(@ISA @EXPORT); | ||
| 7 | @ISA = qw(Exporter); | ||
| 8 | @EXPORT = qw( | ||
| 9 | songs_on_album | ||
| 10 | albums_by_artist | ||
| 11 | delete_album | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | |||
| 15 | use App::Pimpd; | ||
| 16 | use App::Pimpd::Validate; | ||
| 17 | use Term::ExtendedColor qw(fg); | ||
| 18 | |||
| 19 | sub delete_album { | ||
| 20 | my $file = $mpd->current->file; | ||
| 21 | |||
| 22 | my($path) = $file =~ m|(.+)/.+$|m; | ||
| 23 | my @songs = $mpd->collection->all_items_simple($path); | ||
| 24 | |||
| 25 | |||
| 26 | printf("Remove %s ? [y/N] ", fg('bold', $path)); | ||
| 27 | |||
| 28 | chomp(my $answer = <STDIN>); | ||
| 29 | if(lc($answer) ne 'y') { | ||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | $path = "$config{music_directory}/$path"; | ||
| 34 | |||
| 35 | if(remote_host()) { | ||
| 36 | open(OLD_STDOUT, '>&', STDOUT) or die("Cant dupe STDOUT: $!"); | ||
| 37 | close(STDOUT); | ||
| 38 | system( | ||
| 39 | 'ssh', "-p $config{ssh_port}", | ||
| 40 | "$config{ssh_user}\@$config{ssh_host}", | ||
| 41 | "rm -rv '$path'", | ||
| 42 | ) == 0 and do { | ||
| 43 | open(STDOUT, '>&', OLD_STDOUT) or die("Cant reopen STDOUT: $!"); | ||
| 44 | printf("Removed %s successfully\n", fg('bold', $path)); | ||
| 45 | return; | ||
| 46 | }; | ||
| 47 | open(STDOUT, '>&', OLD_STDOUT) or die("Cant reopen STDOUT: $!"); | ||
| 48 | |||
| 49 | } | ||
| 50 | else { | ||
| 51 | if(remove_path($path)) { | ||
| 52 | printf("removed '%s'\n", fg('bold', $path)); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | print STDERR "remove_path($path): $!\n"; | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub albums_by_artist { | ||
| 63 | my $artist = shift; | ||
| 64 | if( (!defined($artist)) or ($artist eq '') ) { | ||
| 65 | $artist = $mpd->current->artist; | ||
| 66 | } | ||
| 67 | |||
| 68 | return wantarray() | ||
| 69 | ? sort($mpd->collection->albums_by_artist($artist)) | ||
| 70 | : scalar($mpd->collection->albums_by_artist($artist)) | ||
| 71 | ; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub songs_on_album { | ||
| 75 | my($album, $artist) = @_; | ||
| 76 | #my $album = shift // $mpd->current->album; | ||
| 77 | #my $artist = shift // $mpd->current->artist; | ||
| 78 | |||
| 79 | $album or $album = $mpd->current->album; | ||
| 80 | if(!defined($album) or $album eq '') { | ||
| 81 | print STDERR "Album tag missing!\n"; | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | my @tracks; | ||
| 86 | if($artist) { | ||
| 87 | # We dont want _all_ albums named 'Best Of'. | ||
| 88 | @tracks = grep { $_->artist eq $artist } | ||
| 89 | $mpd->collection->songs_from_album($album); | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | @tracks = $mpd->collection->songs_from_album($album); | ||
| 93 | } | ||
| 94 | |||
| 95 | return (wantarray()) ? @tracks : scalar(@tracks); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ |