| 1 | package App::Pimpd::Collection::Search; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | BEGIN { | ||
| 5 | use Exporter; | ||
| 6 | use vars qw(@ISA @EXPORT); | ||
| 7 | @ISA = qw(Exporter); | ||
| 8 | @EXPORT = qw( | ||
| 9 | search_db_quick | ||
| 10 | search_db_artist | ||
| 11 | search_db_album | ||
| 12 | search_db_title | ||
| 13 | ); | ||
| 14 | } | ||
| 15 | |||
| 16 | use App::Pimpd; | ||
| 17 | use App::Pimpd::Validate; | ||
| 18 | use Carp 'confess'; | ||
| 19 | |||
| 20 | |||
| 21 | sub search_db_quick { | ||
| 22 | my $query = shift; | ||
| 23 | return if !$query; | ||
| 24 | |||
| 25 | if(invalid_regex($query)) { | ||
| 26 | confess("Invalid regex: '$query'"); | ||
| 27 | } | ||
| 28 | |||
| 29 | my @result; | ||
| 30 | for($mpd->collection->all_pathes) { | ||
| 31 | if($_ =~ /$query/im) { | ||
| 32 | push(@result, $_); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | return (wantarray()) ? @result : scalar(@result); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub search_db_artist { | ||
| 39 | my $artist = shift; # Not a regex | ||
| 40 | return if !$artist; | ||
| 41 | |||
| 42 | my @tracks = $mpd->collection->songs_by_artist_partial($artist); | ||
| 43 | |||
| 44 | if(!@tracks) { | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | # check if called from the interactive shell | ||
| 49 | my $caller_sub = (caller(1))[3]; | ||
| 50 | |||
| 51 | if($caller_sub =~ m/App::Pimpd::Shell/) { | ||
| 52 | print "$_\n" for map { | ||
| 53 | sprintf("%s [%s] %s", $_->artist, $_->album, $_->title); | ||
| 54 | } @tracks; | ||
| 55 | } | ||
| 56 | |||
| 57 | map{ $_ = $_->file } @tracks; | ||
| 58 | |||
| 59 | return (wantarray()) ? @tracks : scalar(@tracks); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub search_db_title { | ||
| 63 | my $title = shift; | ||
| 64 | return if !$title; | ||
| 65 | |||
| 66 | my @titles = $mpd->collection->songs_with_title_partial($title); | ||
| 67 | |||
| 68 | if(!@titles) { | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | # check if called from the interactive shell | ||
| 72 | my $caller_sub = (caller(1))[3]; | ||
| 73 | |||
| 74 | if($caller_sub =~ m/App::Pimpd::Shell/) { | ||
| 75 | print "$_\n" for map { | ||
| 76 | sprintf("%s [%s] %s", $_->artist, $_->album, $_->title); | ||
| 77 | } @titles; | ||
| 78 | } | ||
| 79 | |||
| 80 | map{ $_ = $_->file } @titles; | ||
| 81 | |||
| 82 | return (wantarray()) ? @titles : scalar(@titles); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub search_db_album { | ||
| 86 | my $album = shift; | ||
| 87 | return if !$album; | ||
| 88 | my @albums = $mpd->collection->songs_from_album_partial($album); | ||
| 89 | |||
| 90 | if(!@albums) { | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | # check if called from the interactive shell | ||
| 95 | my $caller_sub = (caller(1))[3]; | ||
| 96 | |||
| 97 | if($caller_sub =~ m/App::Pimpd::Shell/) { | ||
| 98 | print "$_\n" for map { | ||
| 99 | sprintf("%s [%s] %s", $_->artist, $_->album, $_->title); | ||
| 100 | } @albums; | ||
| 101 | } | ||
| 102 | |||
| 103 | map { $_ = $_->file } @albums; | ||
| 104 | |||
| 105 | return (wantarray()) ? @albums : scalar(@albums); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |