#!/usr/bin/perl
# Copyright (C) 2007 Mandriva
#                    Mathieu RENARD <mrenard@mandriva.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 lib qw(/usr/lib/libDrakX /usr/share/drakmsync);

# i18n: IMPORTANT: to get correct namespace (drakmsync instead of libDrakX)
BEGIN { unshift @::textdomains, 'drakmsync' }

use common;
use standalone;
use mygtk2 qw(gtknew);
use ugtk2 qw(:create :helpers :wrappers :dialogs);
use Gtk2::SimpleList;
use Gnome2::Vte;
use interactive;

use drakmsync;
use msynctool;

use constant TRUE => 1 ;
use constant FALSE => 0 ;

my %sysh = distrib();
my $distro_name = $sysh{system};

$ugtk2::wm_icon = drakmsync::get_icon();

my $title = N("Mobile devices syncronization");
my $w = ugtk2->new($title);
$::main_window = $w->{real_window}; #- so that transient_for is defined for wait messages and dialogs
my @popups;

my $sync_icon = gtkcreate_pixbuf('pda_section');
my $add_icon = gtkcreate_pixbuf('pda_section');
my $option_icon = gtkcreate_pixbuf('pda_section');
my $usb_icon = gtkcreate_pixbuf('pda_section');
my $bluetooth_icon = gtkcreate_pixbuf('pda_section');

my @profiles;

my $profiles_list = Gtk2::SimpleList->new(
	N("Name") => "text",
#	N("Address") => "text",
#	N("Type") => "pixbuf",
);

sub get_current_profile() {
	print("get_current_profile\n" ) ;
	my ($selected) = $profiles_list->get_selected_indices;
	#print "select $profiles_list->{data}[$selected][0]\n";
	defined $selected && $profiles_list->{data}[$selected][0];
}

sub sync_profile {
	my $profile = get_current_profile() or return;
	print("Run synchronization for [ $profile ]\n" ) ;
	#TODO progress bar (PULSE)
	msynctool::sync_group( $profile );
}

sub update_profiles() {
	print("update_profiles\n" ) ;
	my @profiles = msynctool::get_groups();
	@{$profiles_list->{data}} = ();
	#print "Dumper profiles:\n";
	#print Dumper(@profiles);
	foreach (@profiles){
		push(@{$profiles_list->{data}} , $_->{name});
	}
}

sub del_profile{ #TODO add confirmation msgbox before delete
	my $profile = get_current_profile() or return;
	#my $in = interactive->vnew;
	#$in->ask_warn(N("Warning"), N("Are you sure to delete %s profile?\n", $profile));
	print("del profile [ $profile ]\n" );
	msynctool::del_group( $profile );
	update_profiles();
}

sub create_profile {
	print("create_profile\n" ) ;
	#Assistant de creation des profiles
	drakmsync::create_profile();
	update_profiles();
}

sub gtkset_image {
    my ($w, $file, $o_size) = @_;
    my $image = $o_size
    ?  Gtk2::Image->new_from_pixbuf(gtkcreate_pixbuf($file)->scale_simple($o_size, $o_size, 'hyper'))
    :  gtknew('Image', file => $file);
    $w->set_image($image);
    $w;
}


gtkadd($w->{window},
	gtknew('VBox', spacing => 5, children => [
		1, gtknew('VBox', spacing => 5, children => [((
	$::isEmbedded ? () : (0, Gtk2::Banner->new(drakmsync::get_icon(), $title)),
		0, gtknew('Title2', label => N("This tool allows you to syncronize your mobile devices.")),
		1, gtknew('VBox', spacing => 10, children => [
		(
		1, gtknew('ScrolledWindow', width => 50, height => 150, child => $profiles_list),
			0, gtknew('HButtonBox', layout => 'edge', children_loose => [
			gtkset_image(gtknew('Button', text => N("Create new profile"), clicked => sub { create_profile()  }),'pda_section'),
			gtkset_image(gtknew('Button', text => N("Run syncronization"), clicked => sub { sync_profile() }),'pda_section'),
			gtkset_image(gtknew('Button', text => N("Delete current profile"), clicked => sub { del_profile() }),'pda_section')])
			)])
		))])
	]));

update_profiles();


#- remove over-zealous main quit connected to delete_event by mygtk2
Gtk2::Widget->signal_add_emission_hook('delete_event', sub {
					my (undef, $args) = @_;
					member($args->[0], @popups) and $Gtk2::block_next_main_quit = 1;
					1;
				});
{
	package Gtk2;
	our $block_next_main_quit = 0;
	my $main_quit = \&Gtk2::main_quit;
	*main_quit = sub {
		my $ret = !$Gtk2::block_next_main_quit && Gtk2->$main_quit;
		$Gtk2::block_next_main_quit = 0;
		$ret;
	};
}

$w->main;


