#!/usr/bin/perl -w
# 
# Frontend to rioutil. Written by Christian Hedin (c) 2001
# For suggestions and comments please mail me at christian.hedin@dsek.lth.se
# 
#  This code is really ugly for the moment.

# Init Gtk environment
use Gtk;
use strict;
set_locale Gtk;
init Gtk;

#
# VARIABLES (half of them not needed, I know)
#
my $version = "0.4";
my $false = 0;
my $true = 1;
my $window;
my $button;
my $table; 
my $text;
my $frame;
my $line;
my $fixed_font;
my $filerow;
my $statusbar;
my $defaultLabel = "Please select an option";
my $vbox;
my $hbox;
my $context_id = 1;
my @filelist;
my $filename;
my @rowlist;
my @list;
my $scrolled_window;
my $gtklist;
my $list_item;
my $tooltips;
my $clist;
my $file_dialog;
my $smallbox;
my $smallvbox;
my $selectedfile;
my $label;
my $info;
my $id;
my $no;
my $d;
my $artist;
my $title;
my $name;
my $time;
my $size;
my $sr;
my $br;
my $thesize;

my $root_menu;
my $menu_item;
my $menu;
my $menubar;
my $add_item;
my $delete_item;
my $quit_item;
my $file_item;
my $menu_bar;
my $rename_window;
my $renamedialog;
my $addlist;
my $RenameOkButton;
my $renameto;
my $addfilelist;
my $fileclist;
my $scrolled_add_window;
my $toberemoved;
my @addlistitems;
my $addamount = 0;

#
# MAIN
#

# Creating window
$window = new Gtk::Window( "toplevel" );
$window->set_usize( 500, 300 );
$window->signal_connect( "delete_event", sub { Gtk->exit( 0 ); } );
$window->title( "pRioUtil v$version" );
$window->border_width( 20 ); 
$window->set_policy($false,$false,$false);

# Creating vBox
$vbox = new Gtk::VBox( $false, 1 );
$window->add( $vbox );
$vbox->show();


# Creating hBox
$hbox = new Gtk::HBox( $false, 1);
$vbox->add( $hbox );
$hbox->show();


# Creating a (smaller) hbox in the smallhbox
$smallbox = new Gtk::HBox ( $false, 1 );
$hbox->add( $smallbox );
$smallbox->show();

# Creating scrollbox
$scrolled_window = new Gtk::ScrolledWindow( undef, undef );
$scrolled_window->set_usize( 250, 150 );
$vbox->add( $scrolled_window );
$scrolled_window->show();

my @titles = ('ID','Filename','Size');
$clist = new_with_titles Gtk::CList( @titles );
$clist->signal_connect( "select_row", \&selection_made );
$clist->set_shadow_type( 'out' );
$clist->set_column_width( 0, 20 );
$clist->set_column_width( 1, 370 );
$scrolled_window->add( $clist );
$clist->show();

  #$tooltips = new Gtk::Tooltips();
  #$tooltips->set_tip($window,"Status messages here.","");
  

# Creating frame
$frame = new Gtk::Frame( "Info" );
$frame->set_usize( 200, 50 );
$frame->set_border_width( 5 );

# Let's make a table for the buttons
$table = new Gtk::Table( 2, 6, 1 );
$vbox->add( $table );
$table->show();

# Add frame at bottom
$table->attach_defaults($frame, 0,6,1,2);
$frame->show();

# Add label
$label = new Gtk::Label( "Welcome to Prioutil v. $version!" );
$frame->add( $label );
$label->show();


# A button
$button = new Gtk::Button( "Refresh" );
$button->signal_connect( "clicked", \&RefreshButtonClicked, "get" );
$table->attach( $button, 0, 1, 0, 1,"fill","shrink",1,1 );
$button->show();

# A button
$button = new Gtk::Button( "Add tracks" );
$button->signal_connect( "clicked", \&AddButtonClicked, "add" );
$table->attach( $button, 1, 2, 0, 1,"fill","shrink",1,1 );
#$smallbox->pack_start( $button, $true, $true, 2 );
$button->show();

# The delete button
$button = new Gtk::Button( "Delete track" );
$button->signal_connect( "clicked", \&DelButtonClicked, "add" );
$table->attach( $button, 2, 3, 0, 1,"fill","shrink",1,1 );
$button->show();

# A button
$button = new Gtk::Button( "Format Rio" );
$button->signal_connect( "clicked", \&FormatButtonClicked, "get" );
$table->attach( $button, 3, 4, 0, 1,"fill","shrink",1,1 );
$button->show();

# A button
$button = new Gtk::Button( "Rename Rio" );
$button->signal_connect( "clicked", \&RenameButtonClicked, "add" );
$table->attach( $button, 4, 5, 0, 1,"fill","shrink",1,1 );
$button->show();

# Creating "Quit" button
$button = new Gtk::Button( "Quit" );
$button->signal_connect( "clicked", sub { Gtk->exit( 0 ); } ); 
$table->attach( $button, 5, 6, 0, 1, "fill","shrink",1,1);
$button->show();


$window->show();

# Create a new file selection widget
$file_dialog = new Gtk::FileSelection( "Add MP3 file" );

$file_dialog->signal_connect( "destroy", sub { Gtk->exit( 0 ); } );

# Connect the ok_button to file_ok_sel function
$file_dialog->ok_button->signal_connect( "clicked",
 \&file_ok_sel,

$file_dialog );

# Connect the cancel_button to destroy the widget
$file_dialog->cancel_button->signal_connect( "clicked",
     sub { $file_dialog->hide;} ); 
     
     #$file_dialog->complete( "*.mp3" );
     

### The rename dialog
$renamedialog = new Gtk::Dialog();
# OK
$RenameOkButton = new Gtk::Button("OK");
$RenameOkButton->signal_connect( "clicked", \&RenameOKButtonClicked, "ok" );
$renamedialog->action_area->pack_start($RenameOkButton,$true,$false,0);
$RenameOkButton->show();

# Cancel
$button = new Gtk::Button("Cancel");
$button->signal_connect( "clicked", sub { $renamedialog->hide(); } ); 
$renamedialog->action_area->pack_start($button,$true,$true,0);
$button->show();     

# text entry
$text = new Gtk::Entry( 15 );     
$renamedialog->vbox->pack_start($text,$true,$true,0);
$text->signal_connect( "activate", \&enter_callback, $text );
$text->show();
$text->append_text( "Noname" );
     

### The File add dialog

$addlist = new Gtk::Dialog();
$addlist->title( "Files to be added" );
# Go
$button = new Gtk::Button("Send files to Rio!");
$button->signal_connect( "clicked", \&GoButtonClicked, "FileAddOk");
$addlist->action_area->pack_start($button,$true,$true,0);
$button->show(); 

# Add
$button = new Gtk::Button("Add");
$button->signal_connect( "clicked", \&AddToListButtonClicked, "FileAddOk");
$addlist->action_area->pack_start($button,$true,$true,0);
$button->show();

# Remove
$button = new Gtk::Button("Remove");
$button->signal_connect( "clicked", \&RemoveFromListButtonClicked, "Remove");
$addlist->action_area->pack_start($button,$true,$true,0);
$button->show();


# Cancel
$button = new Gtk::Button("Cancel");
$button->signal_connect( "clicked", sub { $addlist->hide(); } ); 
$addlist->action_area->pack_start($button,$true,$true,0);
$button->show(); 

# Creating scrollbox
$scrolled_add_window = new Gtk::ScrolledWindow( undef, undef );
$scrolled_add_window->set_usize( 550, 150 );
$addlist->vbox->pack_start( $scrolled_add_window,$true,$true, 0 );
$scrolled_add_window->show();

# List
my @addlisttitles = ('Filename','Size');
$fileclist = new_with_titles Gtk::CList( @addlisttitles );
$fileclist->signal_connect( "select_row", \&add_selection_made );
$fileclist->set_shadow_type( 'out' );
$fileclist->set_column_width( 0, 480 );
$scrolled_add_window->add( $fileclist );
$fileclist->show();

RefreshButtonClicked();

     
### Main GTK loop
main Gtk;
exit( 0 ); 



#
# CALLBACKS
# ---------
#

# When "Refresh" is clicked 
sub RefreshButtonClicked
{

$clist->clear();
@list = ();
@filelist = ();
	open (LOADLIST, "rioutil -l|");
	while (<LOADLIST>) {
		
		push (@filelist, $_);
	}

my $counter = 0;
my $number = 0;

	foreach $filerow (@filelist)
	{
		if ($counter>2) {
		if ($filerow =~ /\|/) {
			$_ = chomp($filerow);

			@rowlist = (split(/\|/,$filerow));
			$id = $rowlist[0];
			$_ = $rowlist[2];
			my @tmpsize = (split(/\s/,$rowlist[3]));
			$thesize = $tmpsize[3];
			$thesize =  (($thesize * 1000) / 1048576);
			$thesize = int(100 * $thesize) / 100;

			
			my $filetemp = s/\s\s+//;
			$filename = $_;
			#insert id, filename and size into @list
			$list[$number][0]=$id;
			$list[$number][1]=$filename;
			$list[$number][2]=$thesize;
	}}
		$counter++;
		$number++;
}
for my $i ( 0..$#list )
    {
	    if ($list[$i][0]) {
       $clist->insert($i, $list[$i][0],$list[$i][1],$list[$i][2]);
    }
    
    }

$clist->show;
     
my $i = 0;
open (INFO, "rioutil -i|");
	while (<INFO>) {
	
		if (/Free/)
		{
		s/\\[31m//;
		$info = $_;
		}
		$i++;
	}
		

$label->set_text( "$info To be added: $addamount MB" ); 

}

# When "Add" is clicked
sub AddButtonClicked
{
$addlist->show();
}


# When selection is made
sub selection_made
{

	my ( $clist, $row, $column, $event, @data ) = @_;
	my $textchoice;
	
	$textchoice = $clist->get_text( $row, $column );
	$label->set_text( "Selected: $textchoice" );
	$selectedfile = $clist->get_text( $row, 0);
}

# When a file is selected in the add list
sub add_selection_made
{

	my ( $fileclist, $row, $column, $event, @data ) = @_;
	$toberemoved = $row;
	#print "\nROW: $row\n"; # debug info
}




# When the Delete button is clicked
sub DelButtonClicked
{
	open (DELETE, "rioutil -d \"$selectedfile\"|");
	while (<DELETE>) {
		print;
	}
$label->set_text( "Deleted file" );     
RefreshButtonClicked();

}


sub FormatButtonClicked
{
	open (FORMAT, "rioutil -f|");
	while (<FORMAT>) {
		print;
	}
$label->set_text( "Formatted Rio" );     

RefreshButtonClicked();

}

sub RenameButtonClicked
{

	$renamedialog->show();
	

}



# When add file OK is clicked
sub file_ok_sel
{
	my ( $widget, $file_selection ) = @_;
	my $file = $file_selection->get_filename();
	my $filesize = -s $file;
	$filesize =  $filesize / 1048576;
	$filesize = int(100 * $filesize) / 100;
	$file_selection->hide;
	$fileclist->append( $file, $filesize);
     	$fileclist->show();
	$addamount=$addamount+$filesize;
	RefreshButtonClicked;	
}


sub computeSizes()
{

my $j = 0;
my $sizeitem = 0;

my $addsize = 0;	
$label->set_text( "Total number of bytes to be added: $addsize" ); 

}



# Rename
sub RenameOKButtonClicked
{

	$renameto = $text->get_text();
	open (RENAME, "rioutil -n \"$renameto\"|");
	while (<RENAME>) {
		print;
	}
	$renamedialog->hide();
	$label->set_text( "Renamed your Rio to: $renameto" );
}

# test
sub enter_callback
{

	print "\nnothing now."
}


# Remove file from addlist
sub Remove
{
	{
	my $megs = $fileclist->get_text( $toberemoved, 1 );
	$addamount=$addamount-$megs;
	$fileclist->remove( $toberemoved );
	$addlist->show();
	}
}


# Add file to addlist
sub AddToListButtonClicked
{

$file_dialog->show();	
	
}

# Remove file from list
sub RemoveFromListButtonClicked
{
	Remove();
	RefreshButtonClicked();
}


# When we finally kick the add button to send files...
sub GoButtonClicked
{

	my $i = 0;
	while ($i<$fileclist->rows()) {
		my $additem = $fileclist->get_text( $i, 0 );
		push @addlistitems, $additem;
		$i++;
		}
		my $addstring;
		foreach my $fileadd (@addlistitems) {

			#open (UPLOAD, "rioutil -a \"$fileadd\"|");
			#	while (<UPLOAD>) {
				#		}
			$fileadd = "\"$fileadd\"";
			$addstring = "$addstring $fileadd";
			}
	
			open (UPLOAD, "rioutil -b $addstring|");
			while (<UPLOAD>) {
				print;
			}
			$addlist->hide();
			RefreshButtonClicked();


}

# END
