#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
##############################################################################
# Copyright (c) 2003
# Author Sylvain Daubert <gwine@free.fr>
#
# 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 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, write to the Free Software Foundation, Inc., 675 Mass 
# Ave, Cambridge, MA 02139, USA.
#############################################################################

use warnings;
use strict;

package AppGwine;


BEGIN {
    use Gnome2;
    use Gtk2::GladeXML;
    use Gwine::gwine;
    use Gwine::Config;
    use Gwine::Gdb;
    use Gwine::L10N;
    use Locale::TextDomain qw( gwine );
}

##################################################################
## Global variables
##################################################################
our ($config, $db, $gxml, %pixmaps, $VERSION);


##################################################################
## Gwine
##################################################################
Gtk2->init;
my $p = Gnome2::Program->init('gwine', $VERSION, 'libgnomeui');

# Set GNOME_PARAM_APP_* (for displaying help) from GNOME_PARAM_GNOME_*
{
    my @prop = ('datadir', 'libdir', 'prefix', 'sysconfdir');
    $p->set("app-$_", $p->get("gnome-$_")) foreach @prop;
}


# Initialisation
###########################################
my ($home) = (getpwuid($<))[7];
$config = Gwine::Config->new;
# Restore previous config
$config->home($home);
$config->restore;
my $prefix = `which $0`;
chomp $prefix;
$prefix =~ s|bin/gwine||;
$config->icon_path("$prefix/share/gwine/pixmaps/");
# Creation of database
$db = Gwine::Gdb->new;

# Creation of UI
my $xpath = $config->xml_path("$prefix/share/gwine/");
$gxml = Gtk2::GladeXML->new("$xpath/gwine.glade", 'app_gwine', 
			    'gwine');
$gxml->signal_autoconnect_from_package('AppGwine');

# End initialize GUI (treeview)
AppGwine->init;

# Set visibility for each columns of lists
wine_list_columns_set_visible( @{$config->wine_visible} );
prod_list_columns_set_visible( @{$config->prod_visible} );

# Set handler for visibility modifying
$config->set_handler_for_wine_visible(\&AppGwine::wine_list_columns_set_visible);
$config->set_handler_for_prod_visible(\&AppGwine::prod_list_columns_set_visible);

# Create a pixbuf
my $path = $config->icon_path;
my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file("$path/gwine.png");
# And attach it as window icons
$gxml->get_widget('app_gwine')->set_icon($pixbuf);

# Initialise icons
$pixmaps{vines} = Gtk2::Gdk::Pixbuf->new_from_file("$path/vines.png");
$pixmaps{zone} = Gtk2::Gdk::Pixbuf->new_from_file("$path/zone.png");
$pixmaps{appel} = Gtk2::Gdk::Pixbuf->new_from_file("$path/vine.png");
$pixmaps{ext} = Gtk2::Gdk::Pixbuf->new_from_file("$path/bottle.png");
$pixmaps{prod} = Gtk2::Gdk::Pixbuf->new_from_file("$path/prod.png");



# Set geometry, if required
my ($width, $heigth) = split /x/, $config->get_geometry if $config->get_geometry;
if (defined $width) { 
    $gxml->get_widget('app_gwine')->resize($width, $heigth);
    $gxml->get_widget('hpaned')->set_position($config->paned_pos)
}

# If a file is passed on command line, open it
# If more than one file is passed, only first db will be opened
# If none of passed files can be opened, gwine exits
if (@ARGV > 0) {
    my $ret = 1;
    foreach my $file (@ARGV) {
	$ret = open_file($file);
	last if $ret;
    }
    if (! $ret) {
	exit(1)
    }
}
else {
    # Open last recently opened file, if required
    if ((defined $config->get_opened_file)
	&& ($config->opened_file ne __('New Cellar'))
	&& ($config->opened_file ne 'New Cellar')) {
	my $ret = open_file($config->opened_file);
	if (!$ret) {
	    # Open failed
	    $gxml->get_widget('appbar')->set_status(__x('Cannot open {file}', 
							file => $config->opened_file));
	    $config->opened_file(__('New Cellar'));
	    $gxml->get_widget('ztreeview')->signal_emit('map');
	}
    }
    else { $config->opened_file(__('New Cellar')) }
}


# Set view style
my $group = $gxml->get_widget('radio_wines')->get_group;
$group->[2 - $config->view_style]->activate;

if ($config->view_style == 2) {
    # Select Root node if tree view is selected
    my $select = $gxml->get_widget('ztreeview')->get_selection;
    $select->select_path(Gtk2::TreePath->new("0"));
}


Gtk2->main;



1;

__END__

=head1 NAME

gwine - Manage a wine cellar

=head1 DESCRIPTION

Gwine is a wine cellar manager for Gnome 2.x. It uses Gtk2-Perl and 
Gnome2-Perl. For more information, please see README in perl_lib_dir/Gwine.

=head1 SEE ALSO

perl_lib_dir/Gwine/INSTALL

perl_lib_dir/Gwine/README

http://gwine.tuxfamily.org

=head1 AUTHOR

Sylvain Daubert <sly_@ifrance.com>

Copyright (c) 2003, Sylvain Daubert. All rights reserved.
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 of the License, or any later version.

=cut
