#!/usr/bin/perl

# Copyright (C) 2006 Mandriva
#                    Olivier Blin <blino@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);

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

use standalone;
use common;
use interactive;
use do_pkgs;
use Xconfig::glx;
use any;
use Getopt::Long;

my $in = eval { interactive->vnew };
$in || $::auto or die $@;
my $do_pkgs = $in ? $in->do_pkgs : do_pkgs_standalone->new;

require_root_capability();

sub exit_program { $in ? $in->exit(@_) : exit(@_) }

my $glx = Xconfig::glx::detect_may_install($do_pkgs) or exit_program();
my $running_wm = any::running_window_manager();

my $o_method;
my $o_wm;
my $force;
GetOptions(
    "method=s" => \$o_method,
    "wm=s" => \$o_wm,
    "force" => \$force,
) or warn "invalid options";

my $use_arguments = $force || !$o_method || $glx->{capabilities}{$o_method};
$glx->{method} = $use_arguments && $o_method if $o_method;
$glx->{wm} = $use_arguments && $o_wm if $o_wm;

my @advanced_settings = (
    [ N_("A graphical server supporting OpenGL compositing should be selected."), 'methods', 'method', \@Xconfig::glx::gl_compositing_servers, 1 ],
    [ N_("An OpenGL compositing window manager should be selected. It provides 3D desktop and compositing effects in window management, such as translucent windows and moving effects."), 'wms', 'wm', \@Xconfig::glx::gl_compositing_wms, 0 ],
);

sub choose_gtk {
    my ($glx) = @_;
    require mygtk3;
    import mygtk3 qw(gtknew);
    require ugtk3;
    import ugtk3 qw(:create :helpers :wrappers :dialogs);

    $ugtk3::wm_icon = 'drak3d-16';
    my $title = N("3D Desktop effects");
    my $w = ugtk3->new($title, no_Window_Manager => !$running_wm);

    $::main_window = $w->{real_window}; #- so that transient_for is defined for wait messages and dialogs

    my $types_group;
    my %state;
    (my $available_types, $state{type}) = Xconfig::glx::detect_types($glx);
    $state{type} and $state{$state{type}{type}}{$_} = $glx->{$_} foreach map { $_->[2] } @advanced_settings;
    foreach my $type (@Xconfig::glx::gl_compositing_types) {
        foreach (@advanced_settings) {
            my (undef, $available, $field, $list, $check_capabilities) = @$_;
            my @available = @{$type->{$available} || []};
            my $default = find {
                member($_->{$field}, @available) && (!$check_capabilities || $glx->{capabilities}{$_->{$field}});
            } @$list;
            $state{$type->{type}}{$field} ||= $default && $default->{$field};
        }
    }

    gtkadd($w->{window},
           gtknew('VBox', spacing => 5, children => [
               $::isEmbedded ? () : (0, Gtk3::Banner->new('drak3d', $title)),
               0, gtknew('Title2', label => N("This tool allows you to configure 3D desktop effects.")),
               if_(!$glx->{supported}, 0, N("Your system does not support 3D desktop effects.")),
               1, gtknew('VBox', spacing => 5, children_tight => [
                   (map {
                       my $type = $_;
                       my @advanced = map {
                           my ($text, $available, $field, $list, $check_capabilities) = @$_;
                           my $radio_group;
                           @{$type->{$available} || []} > 1 ? (
                               gtknew('WrappedLabel', text => translate($text),
                                      alignment => [ 0, 0 ], padding => [ 10, 0 ]),
                               gtknew('HBox', children => [
                                   0, gtknew('Label', text => ' ' x 10),
                                   1, gtknew('VBox', children_tight => [
                                       map {
                                           my $val = $_;
                                           my $match = find { $_->{$field} eq $val } @$list;
                                           $radio_group = gtknew('RadioButton', text => translate($match->{name}),
                                                                 $radio_group ? (join => $radio_group) : (),
                                                                 if_($check_capabilities, sensitive => $glx->{capabilities}{$val}),
                                                                 active => $state{$type->{type}}{$field} eq $match->{$field},
                                                                 toggled => sub { $state{$type->{type}}{$field} = $match->{$field} if $_[0]->get_active },
                                                             );
                                       } @{$type->{$available}}
                                   ]),
                               ]),
                           ) : ();
                       } @advanced_settings;
                       gtknew('HBox', children_tight => [
                           gtknew('VBox', children => [
                               0, gtknew('Image', file => "IC-3D-" . $type->{type} . "-64.png"),
                               1, gtknew('Label'),
                           ]),
                           gtknew('VBox', children_tight => [
                               gtknew('Label'),
                               $types_group = gtknew('RadioButton', text => translate($type->{name}),
                                                       $types_group ? (join => $types_group) : (),
                                                       sensitive => member($type, @$available_types),
                                                       active => $state{type} eq $type,
                                                       toggled => sub {
                                                           $state{type} = $type if $_[0]->get_active;
                                                           $state{advanced}{$_}->set_sensitive($state{type}{type} eq $_) foreach keys %{$state{advanced}};
                                                       },
                                                   ),
                               @advanced ?
                                 gtknew('HBox', children_tight => [
                                     gtknew('Label', text => ' ' x 5),
                                     $state{advanced}{$type->{type}} =  gtknew('Expander', text => N("Advanced settings"),
                                            sensitive => $state{type} eq $type,
                                            child => gtknew('VBox', children_tight => \@advanced)),
                                 ]) : (),
                           ]),
                       ]);
                   } @Xconfig::glx::gl_compositing_types),
               ]),
               0, $w->create_okcancel(undef, undef, undef, [ N("Help"), sub { run_program::raw({ detach => 1 }, 'drakhelp', '--id', 'drak3d') } ]),
           ]),
       );

    $w->main;
    $glx->{method} = $state{$state{type}{type}}{method} || find { $glx->{capabilities}{$_} } @{$state{type}{methods}};
    $glx->{wm} = $state{$state{type}{type}}{wm} || first(@{$state{type}{wms}});
    $w->{retval};
}

my $res;
if ($::auto) {
    $res = 1;
} elsif ($in->isa('interactive::gtk')) {
    $res = choose_gtk($glx);
} else {
    $res = Xconfig::glx::choose_interactive($in, $glx);
}
if ($res && Xconfig::glx::install($do_pkgs, $glx)) {
    Xconfig::glx::write($glx);
    any::ask_for_X_restart($in) if $in;
}

exit_program();
