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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
package Gscan2pdf::Dialog::MultipleMessage;
use warnings;
use strict;
use Gtk3;
use Glib 1.220 qw(TRUE FALSE); # To get TRUE and FALSE
use Gscan2pdf::Translation '__'; # easier to extract strings with xgettext
use Gscan2pdf::Dialog;
use Glib::Object::Subclass Gscan2pdf::Dialog::;
use Readonly;
Readonly my $COL_MESSAGE => 3;
Readonly my $COL_CHECKBOX => 4;
my %types;
our $VERSION = '2.13.5';
my $SPACE = q{ };
my $HEXREGEX = qr{^(.*) # start of message
\b0x[[:xdigit:]]+\b # hex (e.g. address)
(.*)$ # rest of message
}xsm;
my $INTREGEX = qr{^(.*) # start of message
\b[[:digit:]]+\b # integer
(.*)$ # rest of message
}xsm;
my $TEMPFILEREGEX = qr{^(.*) # start of message
gscan2pdf[-][[:alnum:]]+/[[:word:]]+ # temp filename
(.*)$ # rest of message
}xsm;
sub INIT_INSTANCE {
my $self = shift;
%types = (
error => __('Error'),
warning => __('Warning'),
);
$self->set_position('center-on-parent');
my $vbox = $self->get_content_area;
# to ensure dialog can't grow too big if we have too many messages
my $scwin = Gtk3::ScrolledWindow->new;
$vbox->pack_start( $scwin, TRUE, TRUE, 0 );
$self->{grid} = Gtk3::Grid->new;
$self->{grid_rows} = 0;
$self->{stored_responses} = [];
$self->{grid}
->attach( Gtk3::Label->new( __('Page') ), 0, $self->{grid_rows}, 1, 1 );
$self->{grid}
->attach( Gtk3::Label->new( __('Process') ), 1, $self->{grid_rows}, 1,
1 );
$self->{grid}->attach( Gtk3::Label->new( __('Message type') ),
2, $self->{grid_rows}, 1, 1 );
$self->{grid}->attach( Gtk3::Label->new( __('Message') ),
$COL_MESSAGE, $self->{grid_rows}, 1, 1 );
$self->{grid}->attach(
Gtk3::Label->new( __('Hide') ),
$COL_CHECKBOX, $self->{grid_rows}++,
1, 1
);
$self->{cbl} = Gtk3::Label->new( __("Don't show this message again") );
$self->{cbl}->set_halign('end');
$self->{grid}
->attach( $self->{cbl}, $COL_MESSAGE, $self->{grid_rows}, 1, 1 );
$self->{cb} = Gtk3::CheckButton->new;
$self->{cb}->set_halign('center');
$self->{grid}
->attach( $self->{cb}, $COL_CHECKBOX, $self->{grid_rows}, 1, 1 );
$scwin->add( $self->{grid} );
$self->{cb}->signal_connect(
toggled => sub {
my $state = $self->{cb}->get_active;
for my $cb ( $self->_list_checkboxes ) {
$cb->set_active($state);
}
}
);
$self->add_actions( 'gtk-close', \&close_callback );
return $self;
}
sub add_row {
my ( $self, %options ) = @_;
$self->{grid}->insert_row( $self->{grid_rows} );
$self->{grid}->attach( Gtk3::Label->new( $options{page} ),
0, $self->{grid_rows}, 1, 1 );
$self->{grid}->attach( Gtk3::Label->new( $options{process} ),
1, $self->{grid_rows}, 1, 1 );
$self->{grid}->attach( Gtk3::Label->new( $types{ $options{type} } ),
2, $self->{grid_rows}, 1, 1 );
my $view = Gtk3::TextView->new;
my $buffer = $view->get_buffer;
# strip newlines from the end of the string, but not the end of the line
$options{text} =~ s/\s+\z//xsm;
$buffer->set_text( $options{text} );
$view->set_editable(FALSE);
$view->set_wrap_mode('word-char');
$view->set( 'expand', TRUE );
$self->{grid}->attach( $view, $COL_MESSAGE, $self->{grid_rows}++, 1, 1 );
if ( $options{'store-response'} ) {
my $button = Gtk3::CheckButton->new();
$button->signal_connect( toggled => \&_checkbutton_consistency, $self );
$button->set_halign('center');
$self->{grid}
->attach( $button, $COL_CHECKBOX, $self->{grid_rows} - 1, 1, 1 );
if ( $options{'stored-responses'} ) {
$self->{stored_responses}[ $self->{grid_rows} - 1 ] =
$options{'stored-responses'};
}
_checkbutton_consistency( $button, $self );
}
if ( $self->{grid_rows} > 2 ) {
$self->{cbl}->set_label( __("Don't show these messages again") );
}
return;
}
sub _checkbutton_consistency {
my ( $widget, $self ) = @_;
my $state;
for my $cb ( $self->_list_checkboxes ) {
if ( not defined $state ) {
$state = $cb->get_active;
}
elsif ( $state != $cb->get_active ) {
undef $state;
last;
}
}
if ( defined $state ) {
$self->{cb}->set_inconsistent(FALSE);
$self->{cb}->set_active($state);
}
else {
$self->{cb}->set_inconsistent(TRUE);
}
return;
}
sub add_message {
my ( $self, %options ) = @_;
# possibly split messages or explain them
my $text = munge_message( $options{text} );
if ( ref($text) eq 'ARRAY' ) {
for ( @{$text} ) {
$text = filter_message($_);
if ( not response_stored( $text, $options{responses} ) ) {
$options{text} = $_;
$self->add_row(%options);
}
}
}
else {
my $filter = filter_message($text);
if ( not response_stored( $filter, $options{responses} ) ) {
$options{text} = $text;
$self->add_row(%options);
}
}
return;
}
sub store_responses {
my ( $self, $response, $responses ) = @_;
for my $text ( $self->list_messages_to_ignore($response) ) {
$responses->{ filter_message($text) }{response} = $response;
}
return;
}
sub response_stored {
my ( $text, $responses ) = @_;
return ( defined $responses->{$text}
and defined $responses->{$text}{response} );
}
sub _list_checkboxes {
my ($self) = @_;
my @cbs;
for my $row ( 1 .. $self->{grid_rows} - 1 ) {
my $cb = $self->{grid}->get_child_at( $COL_CHECKBOX, $row );
if ( defined $cb ) {
push @cbs, $cb;
}
}
return @cbs;
}
sub list_messages_to_ignore {
my ( $self, $response ) = @_;
my (@list);
for my $row ( 1 .. $self->{grid_rows} - 1 ) {
my $cb = $self->{grid}->get_child_at( $COL_CHECKBOX, $row );
if ( defined $cb and $cb->get_active ) {
my $filter = TRUE;
if ( $self->{stored_responses}[$row] ) {
$filter = FALSE;
for ( @{ $self->{stored_responses}[$row] } ) {
if ( $_ eq $response ) {
$filter = TRUE;
last;
}
}
}
if ($filter) {
my $buffer =
$self->{grid}->get_child_at( $COL_MESSAGE, $row )->get_buffer;
push @list,
$buffer->get_text( $buffer->get_start_iter,
$buffer->get_end_iter, TRUE );
}
}
}
return @list;
}
# Has to be carried out separately to filter_message in order to show the user
# any addresses, error numbers, etc.
sub munge_message {
my ($message) = @_;
my @out = ();
# split up gimp messages
while (
defined $message
and ( $message =~ /^([(]gimp:\d+[)]:[^\n]+)\n(.*)/xsm
or $message =~
/^([[]\S+\s@\s\b0x[[:xdigit:]]+\b\][^\n]+)\n(.*)/xsm )
)
{
push @out, munge_message($1);
$message = $2;
}
if (@out) {
if ( defined $message and $message !~ /^\s*$/xsm ) {
push @out, munge_message($message);
}
return \@out;
}
if ( defined $message
and $message =~
/Exception[ ](:?400|445):[ ]memory[ ]allocation[ ]failed/xsm )
{
$message .= "\n\n"
. __(
'This error is normally due to ImageMagick exceeding its resource limits.'
)
. $SPACE
. __(
'These can be extended by editing its policy file, which on my system is found at /etc/ImageMagick-6/policy.xml'
)
. $SPACE
. __(
'Please see https://imagemagick.org/script/resources.php for more information'
);
}
return $message;
}
# External tools sometimes throws warning messages including a number,
# e.g. hex address. As the number is very rarely the same, although the message
# itself is, filter out the number from the message
sub filter_message {
my ($message) = @_;
$message =~ s/\s+$//xsm;
while ( $message =~ /$TEMPFILEREGEX/xsmo ) {
$message =~ s/$TEMPFILEREGEX/$1%%t$2/xsmo;
}
while ( $message =~ /$HEXREGEX/xsmo ) {
$message =~ s/$HEXREGEX/$1%%x$2/xsmo;
}
while ( $message =~ /$INTREGEX/xsmo ) {
$message =~ s/$INTREGEX/$1%%d$2/xsmo;
}
return $message;
}
sub close_callback {
}
1;
__END__
|