#!/usr/bin/perl -w



###########################################################################
#    LICENSE
#
#
#    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 3 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, see <http://www.gnu.org/licenses/>.
#
###########################################################################



use Gtk2 -init;
use Gtk2::GladeXML;
use Gtk2::Ex::Simple::List;


$appdir = '/home/israel/.local/share/xmmslibrary';

$gladexml = Gtk2::GladeXML->new("$appdir/xmmslibrary.glade");
$gladexml->signal_autoconnect_from_package();
$entry = $gladexml->get_widget('entry1');
$treeview = $gladexml->get_widget('treeview1');

$treeview->set_rubber_banding('1');


$slist = Gtk2::Ex::Simple::List->new_from_treeview($treeview, 
							  'Title' => 'text', 
							  'Artist'=> 'text', 
							  'Album' => 'text', 
							  'Location' => 'text'
							 );

$slist->get_selection->set_mode('multiple');



Gtk2->main;



#####Let's start the graphic show

sub on_entry1_activate {
    &print_results;
}


sub on_button1_clicked {
    &print_results;
}


sub on_enqueuebutton_clicked {
    my @indices = $slist->get_selected_indices;
    &queue_selection(@indices);
}

sub on_playbutton_clicked {
    my @indices = $slist->get_selected_indices;
    &play_engine(@indices);
}


sub on_window1_delete_event {Gtk2->main_quit;}



sub print_results {
    my $text = $entry->get_text;
    my @results = &search_engine($text);

    @{$slist->{data}} = ( @results );

}



####XMMS Integration:

sub queue_selection {

    foreach my $i (@_) {
	my $filename = $slist->{data}[$i][3];
	$filename =~ s|^.*file://||;
	system "xmmsctrl +file \"$filename\"";
    }
}


sub play_engine {

    my $i = shift;
    my $selection = $slist->{data}[$i][3];


    $selection =~ s|^.*file://||;

    system "xmmsctrl +file \"$selection\"";
    system "xmmsctrl track last & xmmsctrl play";

    &queue_selection;
}

#####



sub search_engine {

    $dbase = "/home/israel/.gnome2/rhythmbox/rhythmdb.xml";  
    $/ = "</entry>";



    open DBASE, $dbase; #Just reciclying rhythmbox's database, ;-)

#    $word = join(' +', @_);


    my $word = shift;
    my $results = [];

    my %entry=(
	    title => '',
	    artist => '',
	    album => '',
	    location => '' );


    while (<DBASE>){
	if (/$word/i){
	    my @lines = split /\n/;
	    foreach my $i (@lines){
		if ($i =~ m|</title>|) {
		    $i =~ s|<title>||;
		    $i =~ s|</title>||;
		    $entry{title} = $i;
		}
		if ($i =~ m|</artist>|) {
		    $i =~ s|<artist>||;
		    $i =~ s|</artist>||;
		    $entry{artist} = $i;
		}
		if ($i =~ m|</album>|) {
		    $i =~ s|<album>||;
		    $i =~ s|</album>||;
		    $entry{album} = $i;
		}
		if ($i =~ m|</location>|) {
		    $i =~ s|<location>||;
		    $i =~ s|</location>||;
		    $i =~ s|%20| |g;
		    $i =~ s|%C3%BC|ü|g;
		    $entry{location} = $i;
		    last;
		}
	    }
	    push (@$results, [$entry{title}, $entry{artist}, $entry{album}, $entry{location}]);
	}
    }

#    for  (my $i = 0;  $i < $#$results/4; $i++){
#
#	$text = $text . <<EOF;
#        Title:         $results->[4*$i]
#        Artist:       $results->[4*$i+1]
#        Album:     $results->[4*$i+2]
#        Location:  $results->[4*$i+3]
#
#EOF
#    }
    return @$results;
}
