######################################################################
#
# Log_Window.pm
# $Id$
#
######################################################################
package Log_Window;

use strict;
use CSL::Gnome2;
use Gnome2;
use Gtk2::GladeXML;
use Crashbackend;
use Text::Wrap;

use fields qw(log_store xml);

sub new
{
    my Log_Window $self = shift;
    $self = fields::new($self) unless ref($self);

    my $GLADEDIR = $main::GLADEDIR || ".";
    $self->{xml} = Gtk2::GladeXML->new("$GLADEDIR/crashman3.glade",
                                       'log_window')
                                 or die "Error Opening Glade File";
    $self->{xml}->signal_autoconnect_from_package($self);

    return $self;
}

sub run
{
    my ($class, $disk_name) = @_;

    Gnome2::Program->init("crashman", "3");

    my $self = $class->new;

    # Set up the list store
    my $log_store;
    $log_store = Gtk2::ListStore->new('Glib::String',  # Date
                                      'Glib::String',  # Comment
                                      'Glib::String',  # User name
                                      'Glib::String',  # Staff
                                      'Glib::Scalar'); # Data
    # Set up the list view
    my $log_view;
    $log_view = $self->wgt('log_view');

    $log_view->set_model($log_store);
    $log_view->get_selection->set_mode('single');
    my @columns = ("Date", "Comment", "User Name", "Staff");
    for (my $i = 0; $i <= $#columns; $i++)
    {
        $log_view->insert_column_with_attributes(-1, $columns[$i],
                                                  Gtk2::CellRendererText->new,
                                                  "text", $i);
    }
        
    # Populate the list store
    my @logs = Crashbackend::read_log($disk_name);
    my %log_entries;
    foreach my $row (@logs)
    {
        $log_entries{$row->{disk_name}} = $row;
        $row->{date} =~ s/\..*//;
        $log_store->set($log_store->append,
                         0, $row->{date},
                         1, $row->{comment},
                         2, $row->{user_name},
                         3, $row->{staff},
                         4, $row);
    }
    $self->{log_store} = $log_store;

    $log_view->get_selection->signal_connect_swapped(
        changed => \&on_log_view_selection_changed, $self);
}

sub on_log_view_selection_changed
{
    my ($self, $selection, $event) = @_;
    
    my ($model, $iter) = $selection->get_selected;

    my $buf = $self->wgt('log_text')->get_buffer;
    my ($start, $end) = $buf->get_bounds;
    $buf->delete($start, $end);
    my $log_row = $model->get($iter, 4);

    my $comment = $log_row->{comment};
    $Text::Wrap::columns = 80;
    my @ncomment = Text::Wrap::wrap("", "\t", $comment);

    $buf->insert($start, <<"EOF"
User: $log_row->{user_name}
Comment: @ncomment
Date: $log_row->{date}
Staff: $log_row->{staff}
EOF
                );

    1; # Handled
}

sub on_ok_button_clicked
{
    my ($self, $widget, $event) = @_;
    
    $self->wgt('log_window')->destroy;
}

sub wgt { return $_[0]->{xml}->get_widget($_[1]); }

1;

