/usr/share/perl5/Gscan2pdf/Dialog/Renumber.pm is in gscan2pdf 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | package Gscan2pdf::Dialog::Renumber;
use strict;
use warnings;
use Glib 1.220 qw(TRUE FALSE); # To get TRUE and FALSE
use Gscan2pdf::Dialog;
use Gscan2pdf::Document;
use Gscan2pdf::PageRange;
use Gscan2pdf::Translation '__'; # easier to extract strings with xgettext
use Readonly;
Readonly my $_MAX_PAGES => 9999;
Readonly my $_MAX_INCREMENT => 99;
use Glib::Object::Subclass Gscan2pdf::Dialog::, signals => {
'changed-start' => {
param_types => ['Glib::UInt'], # new start page
},
'changed-increment' => {
param_types => ['Glib::Int'], # new increment
},
'changed-document' => {
param_types => ['Glib::Scalar'], # new document
},
'changed-range' => {
param_types => ['Gscan2pdf::PageRange::Range'], # new range
},
'before-renumber' => { param_types => [], },
'error' => { param_types => ['Glib::String'], },
},
properties => [
Glib::ParamSpec->int(
'start', # name
'Number of first page', # nickname
'Number of first page', # blurb
1, # min
999, # max
1, # default
[qw/readable writable/] # flags
),
Glib::ParamSpec->int(
'increment', # name
'Increment', # nickname
'Amount to increment page number when renumbering multiple pages'
, # blurb
-99, # min
99, # max
1, # default
[qw/readable writable/] # flags
),
Glib::ParamSpec->scalar(
'document', # name
'Document', # nick
'Gscan2pdf::Document object to renumber', # blurb
[qw/readable writable/] # flags
),
Glib::ParamSpec->enum(
'range', # name
'Page Range to renumber', # nickname
'Page Range to renumber', # blurb
'Gscan2pdf::PageRange::Range',
'selected', # default
[qw/readable writable/] # flags
),
Glib::ParamSpec->scalar(
'logger', # name
'Logger', # nick
'Log::Log4perl::get_logger object', # blurb
[qw/readable writable/] # flags
),
];
our $VERSION = '2.1.0';
my ( $start_old, $step_old, $logger );
# Normally, we would initialise the widget in INIT_INSTANCE and use the
# default constructor new(). However, we have to override the default contructor
# in order to be able to access any properties assigned in ->new(), which are
# not available in INIT_INSTANCE. Therefore, we use the default INIT_INSTANCE,
# and override new(). If we ever need to subclass Gscan2pdf::Dialog::Renumber,
# then we would need to put the bulk of this code back into INIT_INSTANCE,
# and leave just that which assigns the required properties.
sub new {
my ( $class, @arguments ) = @_;
my $self = Glib::Object::new( $class, @arguments );
$self->set( 'title', __('Renumber') );
my $vbox = $self->get('vbox');
# Frame for page range
my $frame = Gtk3::Frame->new( __('Page Range') );
$vbox->pack_start( $frame, FALSE, FALSE, 0 );
my $pr = Gscan2pdf::PageRange->new;
$pr->signal_connect(
changed => sub {
$self->set( 'range', $pr->get_active );
$self->update;
}
);
$self->signal_connect(
'changed-range' => sub {
my ( $widget, $value ) = @_;
$pr->set_active($value);
}
);
$pr->set_active( $self->get('range') );
$frame->add($pr);
# Frame for page numbering
my $framex = Gtk3::Frame->new( __('Page numbering') );
$vbox->pack_start( $framex, FALSE, FALSE, 0 );
my $vboxx = Gtk3::VBox->new;
$vboxx->set_border_width( $self->get('border_width') );
$framex->add($vboxx);
# SpinButton for starting page number
my $hboxxs = Gtk3::HBox->new;
$vboxx->pack_start( $hboxxs, FALSE, FALSE, 0 );
my $labelxs = Gtk3::Label->new( __('Start') );
$hboxxs->pack_start( $labelxs, FALSE, FALSE, 0 );
my $spin_buttons = Gtk3::SpinButton->new_with_range( 1, $_MAX_PAGES, 1 );
$spin_buttons->signal_connect(
'value-changed' => sub {
$self->set( 'start', $spin_buttons->get_value );
$self->update;
}
);
$self->signal_connect(
'changed-start' => sub {
my ( $widget, $value ) = @_;
$spin_buttons->set_value($value);
}
);
$spin_buttons->set_value( $self->get('start') );
$hboxxs->pack_end( $spin_buttons, FALSE, FALSE, 0 );
# SpinButton for page number increment
my $hboxi = Gtk3::HBox->new;
$vboxx->pack_start( $hboxi, FALSE, FALSE, 0 );
my $labelxi = Gtk3::Label->new( __('Increment') );
$hboxi->pack_start( $labelxi, FALSE, FALSE, 0 );
my $spin_buttoni =
Gtk3::SpinButton->new_with_range( -$_MAX_INCREMENT, $_MAX_INCREMENT, 1 );
$spin_buttoni->signal_connect(
'value-changed' => sub {
$self->set( 'increment', $spin_buttoni->get_value );
$self->update;
}
);
$self->signal_connect(
'changed-increment' => sub {
my ( $widget, $value ) = @_;
$spin_buttoni->set_value($value);
}
);
$spin_buttoni->set_value( $self->get('increment') );
$hboxi->pack_end( $spin_buttoni, FALSE, FALSE, 0 );
# Check whether the settings are possible
my ( $row_signal, $selection_signal, $document );
$self->signal_connect(
'changed-document' => sub {
if ( defined $row_signal and defined $document ) {
$document->signal_handler_disconnect($row_signal);
}
if ( defined $selection_signal and defined $document ) {
$document->signal_handler_disconnect($selection_signal);
}
$self->update;
$document = $self->get('document');
$row_signal = $document->get_model->signal_connect(
'row-changed' => sub {
$self->update;
}
);
$selection_signal = $document->get_selection->signal_connect(
changed => sub {
$self->update;
}
);
}
);
# HBox for buttons
my $hbox = Gtk3::HBox->new;
$vbox->pack_start( $hbox, FALSE, TRUE, 0 );
# Start button
my $obutton = Gtk3::Button->new( __('Renumber') );
$hbox->pack_start( $obutton, TRUE, TRUE, 0 );
$obutton->signal_connect( clicked => sub { $self->renumber } );
# Close button
my $cbutton = Gtk3::Button->new_from_stock('gtk-close');
$hbox->pack_end( $cbutton, FALSE, FALSE, 0 );
$cbutton->signal_connect( clicked => sub { $self->hide; } );
return $self;
}
sub SET_PROPERTY {
my ( $self, $pspec, $newval ) = @_;
my $name = $pspec->get_name;
my $oldval = $self->get($name);
$self->{$name} = $newval;
# Have to set logger separately as it has already been set in the subclassed
# widget
if ( $name eq 'logger' ) {
$logger = $newval;
$logger->debug('Set logger in Gscan2pdf::Dialog::Renumber');
}
elsif (( defined $newval and defined $oldval and $newval ne $oldval )
or ( defined $newval xor defined $oldval ) )
{
if ( defined $logger ) {
$logger->debug( "Started setting $name from "
. Gscan2pdf::Dialog::dump_or_stringify($oldval) . ' to '
. Gscan2pdf::Dialog::dump_or_stringify($newval) );
}
$self->signal_emit( "changed-$name", $newval );
}
return;
}
# Helper function to prevent impossible settings in renumber dialog
sub update {
my ($self) = @_;
my $start = $self->get('start');
my $step = $self->get('increment');
my $dstart = defined $start_old ? $start - $start_old : 0;
my $dstep = defined $step_old ? $step - $step_old : 0;
if ( $dstart == 0 and $dstep == 0 ) {
$dstart = 1;
}
elsif ( $dstart != 0 and $dstep != 0 ) {
$dstep = 0;
}
# Check for clash with non_selected
my $slist = $self->get('document');
if ( defined $slist ) {
my $range = $self->get('range');
while ( not $slist->valid_renumber( $start, $step, $range ) ) {
my $n;
if ( $range eq 'all' ) {
$n = $#{ $slist->{data} };
}
else {
my @page = $slist->get_selected_indices;
$n = $#page;
}
if ( $start + $step * $n < 1 ) {
if ( $dstart < 0 ) {
$dstart = 1;
}
else {
$dstep = 1;
}
}
$start += $dstart;
$step += $dstep;
if ( $step == 0 ) { $step += $dstep }
}
$self->set( 'start', $start );
$self->set( 'increment', $step );
}
$start_old = $start;
$step_old = $step;
return;
}
sub renumber {
my ($self) = @_;
my $slist = $self->get('document');
my $start = $self->get('start');
my $step = $self->get('increment');
my $range = $self->get('range');
if ( $slist->valid_renumber( $start, $step, $range ) ) {
$self->signal_emit('before-renumber');
if ( defined $slist->{row_changed_signal} ) {
$slist->get_model->signal_handler_block(
$slist->{row_changed_signal} );
}
$slist->renumber( $start, $step, $range );
# Note selection before sorting
my @page = $slist->get_selected_indices;
# Convert to page numbers
for (@page) {
$_ = $slist->{data}[$_][0];
}
# Block selection_changed_signal to prevent its firing changing pagerange to all
if ( defined $slist->{selection_changed_signal} ) {
$slist->get_selection->signal_handler_block(
$slist->{selection_changed_signal} );
}
# Select new page, deselecting others. This fires the select callback,
# displaying the page
$slist->get_selection->unselect_all;
$slist->manual_sort_by_column(0);
if ( defined $slist->{selection_changed_signal} ) {
$slist->get_selection->signal_handler_unblock(
$slist->{selection_changed_signal} );
}
if ( defined $slist->{row_changed_signal} ) {
$slist->get_model->signal_handler_unblock(
$slist->{row_changed_signal} );
}
# Convert back to indices
for (@page) {
# Due to the sort, must search for new page
my $page = 0;
while ( $page < $#{ $slist->{data} }
and $slist->{data}[$page][0] != $_ )
{
++$page;
}
$_ = $page;
}
# Reselect pages
$slist->select(@page);
}
else {
my $msg = __(
'The current settings would result in duplicate page numbers. Please select new start and increment values.'
);
$logger->error($msg);
$self->signal_emit( 'error', $msg );
}
return;
}
1;
__END__
|