File: History.pm

package info (click to toggle)
clamtk 6.07-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,468 kB
  • sloc: perl: 4,510; python: 44; makefile: 23
file content (269 lines) | stat: -rw-r--r-- 7,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# ClamTk, copyright (C) 2004-2021 Dave M
#
# This file is part of ClamTk
# (https://gitlab.com/dave_m/clamtk-gtk3/).
#
# ClamTk is free software; you can redistribute it and/or modify it
# under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
# Foundation; either version 1, or (at your option) any later version, or
#
# b) the "Artistic License".
package ClamTk::History;

# use strict;
# use warnings;

use File::Basename 'basename';
use Locale::gettext;
use Encode 'decode';
# use Gtk3::Gdk::Keysyms;

use Glib 'TRUE', 'FALSE';

sub show_window {
    my $box = Gtk3::Box->new( vertical, 5 );
    $box->set_homogeneous( FALSE );

    my $sort = 0;    # 0 = asc, 1 = desc

    my $swin = Gtk3::ScrolledWindow->new( undef, undef );
    $swin->set_policy( 'automatic', 'automatic' );
    $swin->set_vexpand( TRUE );
    $box->pack_start( $swin, TRUE, TRUE, 0 );

    my $store = create_model();

    my $view = Gtk3::TreeView->new_with_model( $store );
    $view->set_rules_hint( TRUE );
    my $column = Gtk3::TreeViewColumn->new_with_attributes(
        _( 'History' ),
        Gtk3::CellRendererText->new,
        text => 0,
    );
    $column->set_sort_column_id( 0 );
    $view->append_column( $column );
    $swin->add( $view );

    # Add delete signals
    $box->signal_connect(
        key_press_event => sub {
            my ( $widget, $event ) = @_;
            if ( $event->keyval == $Gtk3::Gdk::Delete ) {
                del_history( undef, $view );
            }
            return TRUE;
        }
    );

    # Jump to a row to make keyboard use easier
    my $first_iter = $store->get_iter_first;
    # Make sure they *have* a history
    if ( $first_iter ) {
        if ( $store->iter_is_valid( $first_iter ) ) {
            my $viewpath = $store->get_path( $store->get_iter_first );
            $view->set_cursor( $viewpath, $column, FALSE ) if ( $viewpath );
        }
    }

    $box->pack_start( Gtk3::Separator->new( 'vertical' ), FALSE, FALSE, 0 );

    my $viewbar = Gtk3::Toolbar->new;
    $box->pack_start( $viewbar, FALSE, FALSE, 5 );
    $viewbar->set_style( 'both-horiz' );

    my $button = Gtk3::ToolButton->new();
    # my $use_image = ClamTk::Icons->get_image('text-x-preview');
    my $use_image = ClamTk::Icons->get_image( 'document-new' );
    $button->set_icon_name( $use_image );
    $button->set_label( _( 'View' ) );
    $viewbar->insert( $button, -1 );
    $button->set_is_important( TRUE );
    $button->signal_connect( clicked => \&view_history, $view );

    $button    = Gtk3::ToolButton->new();
    $use_image = ClamTk::Icons->get_image( 'document-print' );
    $button->set_icon_name( $use_image );
    $button->set_label( _( 'Print' ) );
    $viewbar->insert( $button, -1 );
    $button->set_is_important( TRUE );
    $button->signal_connect(
        clicked => sub {
            my $p = Gtk3::PrintOperation->new();
        }
    );

    my $v_sep = Gtk3::SeparatorToolItem->new;
    $v_sep->set_draw( FALSE );
    $v_sep->set_expand( TRUE );
    $viewbar->insert( $v_sep, -1 );

    $button    = Gtk3::ToolButton->new();
    $use_image = ClamTk::Icons->get_image( 'edit-delete' );
    $button->set_icon_name( $use_image );
    $button->set_label( _( 'Delete' ) );
    $viewbar->insert( $button, -1 );
    $button->set_is_important( TRUE );
    $button->signal_connect( clicked => \&del_history, $view );

    $box->show_all;
    return $box;
}

sub history_sort {
    my %orcish;
    return
        #<<<
        sort {
        ( $orcish{ $a } ||= -M $a )
                <=> ( $orcish{ $b } ||= -M $b ) }
                        @_;
        #>>>
}

sub view_history {
    my ( $button, $view ) = @_;
    my $select = $view->get_selection;
    return unless ( $select );

    my ( $model, $iter ) = $select->get_selected;
    return unless $iter;

    my $basename = '';
    $select->selected_foreach(
        sub {
            my ( $model, $path, $iter ) = @_;
            $basename = $model->get( $iter, 0 );
        }
    );

    #<<<
    my $full_path
        = ClamTk::App->get_path( 'history' )
        . '/'
        . $basename
        . '.log';
    #>>>

    my $win = Gtk3::Dialog->new(
        sprintf( _( 'Viewing %s' ), $basename ),
        undef, [ qw| modal destroy-with-parent | ],
    );
    $win->signal_connect( destroy => sub { $win->destroy; 1 } );
    $win->set_default_size( 800, 350 );

    my $textview = Gtk3::TextView->new;
    $textview->set( editable       => FALSE );
    $textview->set( cursor_visible => FALSE );

    my $text;
    $text = do {
        my $FILE;    # filehandle for histories log
        #<<<
        unless ( open( $FILE, '<:encoding(UTF-8)', $full_path ) ) {
            my $notice
                = sprintf _( 'Problems opening %s...' ), $full_path;
                return;
        }
        #>>>
        local $/ = undef;
        <$FILE>;
    };
    #close( $FILE )
    #    or warn sprintf( "Unable to close FILE %s! %s\n" ),
    #    $full_path;

    my $textbuffer = $textview->get_buffer;
    # I hate setting a font here, but it makes the printf stuff
    # look MUCH better.
    $textbuffer->create_tag( 'mono', family => 'Monospace' );
    $textbuffer->insert_with_tags_by_name( $textbuffer->get_start_iter,
        $text, 'mono' );

    my $scroll_win = Gtk3::ScrolledWindow->new( undef, undef );
    $scroll_win->set_vexpand( TRUE );
    $scroll_win->set_border_width( 5 );
    $scroll_win->set_shadow_type( 'none' );
    $scroll_win->set_policy( 'automatic', 'automatic' );

    my $scrollbox = Gtk3::Box->new( 'vertical', 5 );
    $scrollbox->set_homogeneous( FALSE );
    $win->get_content_area->add( $scrollbox );

    $scrollbox->pack_start( $scroll_win, TRUE, TRUE, 0 );
    $scroll_win->add( $textview );

    my $viewbar = Gtk3::Toolbar->new;
    $scrollbox->pack_start( $viewbar, FALSE, FALSE, 0 );
    $viewbar->set_style( 'both-horiz' );

    my $v_sep = Gtk3::SeparatorToolItem->new;
    $v_sep->set_draw( FALSE );
    $v_sep->set_expand( TRUE );
    $viewbar->insert( $v_sep, -1 );

    my $close_btn = Gtk3::ToolButton->new();
    my $use_image = ClamTk::Icons->get_image( 'window-close' );
    $close_btn->set_icon_name( $use_image );
    $close_btn->set_label( _( 'Close' ) );
    $close_btn->set_is_important( TRUE );
    $viewbar->insert( $close_btn, -1 );
    $close_btn->signal_connect( clicked => sub { $win->destroy } );

    $win->show_all();
    $win->run;
    $win->destroy;
    return;
}

sub del_history {
    my ( $button, $tree ) = @_;
    my $sel = $tree->get_selection;

    my ( $model, $iter ) = $sel->get_selected;
    return unless $iter;

    my $row        = $model->get( $iter, 0 );
    my $first_iter = $model->get_iter_first;
    # my $next_iter  = $model->iter_next($iter);
    my $new_path = $model->get_path( $iter );

    my $paths     = ClamTk::App->get_path( 'history' );
    my $top_dir   = $paths . '/';
    my $full_path = $top_dir . $row . '.log';
    # $row = undef;
    return FALSE unless ( -e $full_path );
    unlink( $full_path ) or warn "couldn't delete $full_path: $!\n";

    $model->remove( $iter );
    if ( $model->iter_is_valid( $iter ) ) {
        $sel->select_iter( $iter );
    } else {
        return;
    }
    # $sel->select_path( $new_path );
    return TRUE;
}

sub create_model {
    my $paths   = ClamTk::App->get_path( 'history' );
    my @h_files = glob "$paths/*.log";
    for ( @h_files ) {
        $_ = decode( 'utf8', $_ );
    }
    if ( @h_files > 1 ) {
        @h_files = history_sort( @h_files );
    }

    my $liststore = Gtk3::ListStore->new( 'Glib::String' );
    for my $log ( @h_files ) {
        my $iter     = $liststore->append;
        my $basename = basename( $log );
        $basename =~ s/(.*?)\.log/$1/;
        $liststore->set( $iter, 0, $basename );
    }
    return $liststore;
}

1;