GEmeTool/lib/GEmeTool/View/LogWindow.pm

180 lines
5.2 KiB
Perl

package GEmeTool::View::LogWindow;
use v5.16.3;
use strict;
use warnings;
use utf8;
use Glib::IO;
use Glib::Object::Introspection;
use Path::Tiny;
use GEmeTool::Log qw/logger/;
use Moo;
use namespace::clean;
Glib::Object::Introspection->setup(
basename => 'Gtk',
version => '4.0',
package => 'Gtk4',
);
Glib::Object::Introspection->setup(
basename => 'Gdk',
version => '4.0',
package => 'Gdk',
);
use namespace::clean;
has app => (
is => 'ro',
required => 1,
);
has main_window => (
is => 'ro',
required => 1,
);
has _win => ( is => 'rw', );
sub start {
my $self = shift;
$self->_win( Gtk4::Window->new );
my $win = $self->_win;
$win->set_title('GEmeTool Log Viewer');
$win->set_default_size( 600, 600 );
$self->fill_logs_win;
$win->present;
}
sub fill_logs_win {
my $self = shift;
my $win = $self->_win;
my $logs = GEmeTool::Log->new->get_logs;
my $grid_log = Gtk4::Grid->new;
my $i = 0;
for my $log_message (@$logs) {
my $date = $log_message->{date};
my $message = $log_message->{message};
my $input_file = $log_message->{input_file};
my $output_file = $log_message->{output_file};
my $backup_input_file = $log_message->{backup_input_file};
my $backup_output_file = $log_message->{backup_output_file};
$message = "$date $message";
if ( defined $input_file ) {
$message .= "\n with input file $input_file";
}
if ( defined $output_file ) {
$message .= "\n with output file $output_file";
}
my $label = Gtk4::Label->new($message);
$label->set_halign('start');
$grid_log->attach( $label, 0, $i, 10, 1 );
$self->create_button_restore( $grid_log, 11, $i, 'Restore input file',
$backup_input_file, $input_file );
$self->create_button_restore( $grid_log, 12, $i, 'Restore output file',
$backup_output_file, $output_file );
$i++;
}
my $scroll = Gtk4::ScrolledWindow->new;
$scroll->set_propagate_natural_width(1);
$scroll->set_policy( 'never', 'automatic' );
$scroll->set_child($grid_log);
$win->set_child($scroll);
}
sub open_save {
my $self = shift;
my $file = shift;
my $dest_file = shift;
my $file_dialog = Gtk4::FileDialog->new;
if ( defined $dest_file ) {
$file_dialog->set_initial_folder(
Glib::IO::File::new_for_path( $dest_file->parent ) );
$file_dialog->set_initial_name( $dest_file->basename );
}
$file_dialog->save(
$self->_win,
undef,
sub {
my ( $dialog, $res ) = @_;
if ( $res->had_error ) {
return;
}
my $dest_file = $dialog->save_finish($res);
return if !defined $dest_file;
$dest_file = path( $dest_file->get_path );
logger()->msg( 'Restoring backup...', $file, $dest_file );
$dest_file->spew_raw( $file->slurp_raw );
$self->offer_to_edit_the_new_file($dest_file);
}
);
}
sub offer_to_edit_the_new_file {
my $self = shift;
my $file = shift;
my $main_window = $self->main_window;
my $confirm = Gtk4::Window->new;
my $box_window = Gtk4::Box->new('vertical', 30);
my $label_question = Gtk4::Label->new("Do you want to open the restored file?");
my $label_warning = Gtk4::Label->new("Any unsaved changes in the save editor will be lost forever");
my $box_options = Gtk4::Box->new('horizontal', 0);
my $cancel_button = Gtk4::Button->new_with_label('Do nothing');
my $confirm_button = Gtk4::Button->new_with_label('Load new file');
$box_options->set_property('hexpand', 1);
$cancel_button->set_property('halign', 'start');
$confirm_button->set_property('halign', 'end');
$confirm_button->set_property('hexpand', 1);
$cancel_button->signal_connect('clicked' => sub {
$confirm->close;
});
$confirm_button->signal_connect('clicked' => sub {
$main_window->open_file($file);
$main_window->focus();
$confirm->close;
});
$confirm->set_titlebar(Gtk4::Label->new('Open the restored file'));
$box_window->append($label_question);
$box_window->append($label_warning);
$box_options->append($cancel_button);
$box_options->append($confirm_button);
$box_window->append($box_options);
$confirm->set_child($box_window);
$confirm->present;
}
sub create_button_restore {
my $self = shift;
my $grid_log = shift;
my $column = shift;
my $row = shift;
my $restore_label = shift;
my $file = shift;
my $dest_file = shift;
if ( !defined $file ) {
return;
}
if ( defined $dest_file ) {
$dest_file = path($dest_file);
}
$file = path($file);
return if !-e $file;
my $button_restore_backup = Gtk4::Button->new_with_label($restore_label);
$button_restore_backup->signal_connect(
clicked => sub {
$self->open_save( $file, $dest_file );
}
);
$grid_log->attach( $button_restore_backup, $column, $row, 1, 1 );
}
1;