#!/usr/bin/perl -w
# Mandrake Currency converter
#
# Copyright (C) 2001 MandrakeSoft Daouda Lo (daouda@mandrakesoft.com)
#
# 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 2, 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.       
####################################################################################

use Config;
use Gtk;
use lib qw(/usr/lib/libDrakX);
use POSIX;
use POSIX qw(strtod);
use MDK::Common;
init Gtk;
Gtk->set_locale;
use c;

#-------------------------------------------------------------
# i18n routines
# IMPORTANT: next two routines have to be redefined here to
#         get correct namespace (moneydrake instead of libDrakX)
#         (This version is now UTF8 compliant - Sg 2001-08-31)
#-------------------------------------------------------------

sub _ {
    my $s = shift @_; my $t = translate($s);
    sprintf $t, @_;
}

sub translate {
    my ($s) = @_;
    $s ? c::dgettext('moneydrake', $s) : '';
}

my $_version = "0.9.7";

my $window_global = new Gtk::Window -toplevel;
$window_global->set_policy($false,$false,$true);
$window_global->set_position( 1 );
$window_global->border_width( 5 );
$window_global->set_title(_("Currency converter"));
$window_global->set_usize(540, 200);
$window_global->realize;
$window_global->signal_connect( "delete_event", \&quit_global);

my $vbox_global=new Gtk::VBox(0,0);
$window_global->add($vbox_global);

######### menus
my @menu_items = ( 
		  { path => _("/_File"), type => '<Branch>' },
		  { path => _("/_File")._("/_Quit"), accelerator => _("<control>Q"), callback => \&quit_global },
		  { path => _("/_Help"),type => '<Branch>' },
		  { path => _("/_Help")._("/_About..."), callback => \&about_moneydrake }
		 );
my $menubar = get_main_menu( $window_global );
$vbox_global->pack_start($menubar, 0, 0, 0);
$vbox_global->set_spacing(10);
######### menus end
my (@cur_symb,@cur_names,@cur_first_val,@cur_last_val);
parse_file();
$vbox_global->pack_start(new Gtk::HSeparator, 0, 1, 0);
my $gtkentry1= new Gtk::Entry();
my $gtkcombo2= new Gtk::Combo();
$gtkcombo2->set_popdown_strings(@cur_symb);
my $gtkcombo3= new Gtk::Combo();
$gtkcombo3->set_popdown_strings(@cur_symb);
my $label_cur1=new Gtk::Label(_("United_States_Dollars"));
my $label_cur2=new Gtk::Label(_("United_States_Dollars"));
pack_vbox($vbox_global);
$gtkcombo2->entry->signal_connect ('changed', sub {
    $i=0;
    foreach $cur1 (@cur_symb) {
	if($cur1 eq $gtkcombo2->entry->get_text()) {
	    $label_cur1->set_text("$cur_names[$i]");
	}else {
	    $i++;
	}
    }
});
$gtkcombo3->entry->signal_connect ('changed', sub {
    $i=0;
    foreach $cur2 (@cur_symb) {
	if($cur2 eq $gtkcombo3->entry->get_text()) {
	    $label_cur2->set_text("$cur_names[$i]");
	}else {
	    $i++;
	}
    }
});
$vbox_global->pack_start(new Gtk::HSeparator, 0, 1, 0);
my $convert_button= new Gtk::Button(_(" Convert "));
$convert_button->signal_connect( clicked => \&on_convert_signal_clicked);
my $quit_button= new Gtk::Button(_(" Quit "));
$quit_button->signal_connect( clicked => \&quit_global);
my $lab_res=new Gtk::Label(_("Result"));
my $result=new Gtk::Entry();
my $hbox = new Gtk::HBox(0,10);
$hbox=pack_widget_horiz($quit_button,$convert_button);
$hbox->pack_end($result,0,0,1);
$hbox->pack_end($lab_res,0,0,1);
$vbox_global->pack_start($hbox, 0, 0, 1);

$window_global->show_all;

Gtk->main_iteration while Gtk->events_pending;
Gtk->main;

sub quit_global {
    Gtk->exit(0);
}

sub pack_vbox {
    my ($vbox_id) = @_;
    map {
	pack_hbox($vbox_id,$_->[0],$_->[1],$_->[2]);
    } ([_("Convert this amount:"), $gtkentry1,0]); 
    map {
	pack_table($vbox_id,$_->[0],$_->[1],$_->[2]);
	} ([_("From this currency"),$gtkcombo2,$label_cur1], [_("To this currency"), $gtkcombo3,$label_cur2]);
}

sub pack_hbox {
    my ($vbox, $label, $Entry_or_Combo,$is_combo)=@_;
    my $hbox= new Gtk::HBox(0,5);
    my $Label= new Gtk::Label($label);
    $hbox->pack_start($Label,0,0,1);
    $hbox->pack_start($Entry_or_Combo,0,0,1);
    if ($is_combo==1) {
	my $Label_cur=new Gtk::Label('');
	$hbox->pack_start($Label_cur,0,0,1);
    }
    $vbox->pack_start($hbox,0,0,1);
}

sub pack_table {
    my ($vbox,$label1,$entry1,$label11)=@_;
    my $Label_wg1=new Gtk::Label($label1);
    my $table = new Gtk::Table( 1, 3, 0);
    $table->set_col_spacings(20);
    $table->attach_defaults($Label_wg1,0,1,0,1);
    $table->attach_defaults($entry1,1,2,0,1);
    $table->attach_defaults($label11,2,3,0,1);
    $vbox->pack_start($table,$true,$true,0);
}

sub parse_file () {
    my ($line,@det);
    local *F;
    if ($ENV{LANG} eq 'fr') {
	open F, "< /usr/share/moneydrake/moneydrake-fr.conf" or die "cannot read file /etc/moneydrake.conf failed: $!\n";
    }else {
	open F, "< /usr/share/moneydrake/moneydrake.conf" or die "cannot read file /etc/moneydrake.conf failed: $!\n";
    }
    foreach $line (<F>) {
	@det=split /\s+/, $line;
	push (@cur_symb, $det[0]);
	push (@cur_names, $det[1]);
	push (@cur_first_val, $det[2]);
	push (@cur_last_val, $det[3])
    }
}

sub on_convert_signal_clicked {
    my ($amount,$curre1,$curre2);
    $amount=$gtkentry1->get_chars(0,-1);
    chomp($amount);
    $curre1=$gtkcombo2->entry->get_text();
    $curre2=$gtkcombo3->entry->get_text();
    $amount=~ s/^\s+//;
    $amount=~ s/\s+$//;
    $!=0;
    my ($num, $pa)=strtod($amount);
    if (($amount eq '')||($pa != 0) || $!) {
	raise_error(_("\nEnter a correct number \n"));
	$gtkentry1->set_text('');
    }else {
	my ($j,$c1,$c2,$p1,$p2);
	$j=0;
	foreach $c1 (@cur_symb) {
	    if($c1 eq $curre1) {
		($n1, $p1)=strtod($cur_first_val[$j]);
	    }else {
		$j++;
	    }
	}
	$j=0;
	foreach $c2 (@cur_symb) {
	    if($c2 eq $curre2) {
		($n2, $p2)=strtod($cur_last_val[$j]);
	    }else {
		$j++;
	    }
	}
 
	my $res=sprintf("%.3f",$n1*$n2*$num);
	$result->set_text("$res");
    }
}

sub raise_error {
    my ($label) = @_;
    my $win_about = new Gtk::Dialog();
    $win_about->set_position(1);

    my $bbox1 = new Gtk::HButtonBox;
    $win_about->action_area->pack_start($bbox1,0,0,0);
    
    my $button = new Gtk::Button(_("Close"));
    $button->signal_connect_object("clicked",$win_about => 'destroy');
    $bbox1->add($button);
    
    my $lab = new Gtk::Label($label);
    my $style1 = new Gtk::Style;
    $win_about->vbox->pack_start($lab, 1, 1, 0);

    $win_about->show_all();
    $win_about->set_modal(1);
}

sub pack_widget_horiz {
    my ($widget1, $widget2)=@_;
    my $hbox = new Gtk::HBox(0,0);
    $hbox->pack_start($widget1,0,0,1);
    $hbox->pack_start($widget2,0,0,1);
    $hbox->set_spacing(10);
    return $hbox;
}

sub about_moneydrake {
    my $window_about = new Gtk::Dialog();
    $window_about->set_position(1);

    my $bbox1 = new Gtk::HButtonBox;
    $window_about->action_area->pack_start($bbox1,0,0,0);

    my $button = new Gtk::Button(_("Close"));
    $button->signal_connect_object("clicked",$window_about => 'destroy');
    $bbox1->add($button);

    my $label = new Gtk::Label( _("Mandrake currency converter %s \n \n Copyright (C) 2001 Mandrakesoft SA\n", $_version) );
    $window_about->vbox->pack_start( $label, 1, 1, 0 );

    $window_about->vbox->pack_start( new Gtk::Label( _("Authors: \n\n Daouda Lo <daouda\@mandrakesoft.com> \n\n You can update values of currencies \nby connecting to http://www.xe.com\n")), 1, 1, 0 );

    $window_about->show_all();
    $window_about->set_modal(1);
}

sub get_main_menu {
  my ($window) = @_;
  my $accel_group = new Gtk::AccelGroup();
  my $item_factory = new Gtk::ItemFactory( 'Gtk::MenuBar', '<main>', $accel_group );
  $item_factory->create_items( @menu_items );
  $window->add_accel_group( $accel_group );
  return ( $item_factory->get_widget( '<main>' ) );
}
