runitx Claude Opus 5.5 commited on
Commit
001cde3
·
unverified ·
1 Parent(s): 39c802d

Extract filename helpers into Shutter::App::Filename

Browse files

Move fct_validate_filename, fct_parse_filename_wildcards and
fct_get_next_filename into a functional module (EXPORT_OK). The original
names remain as one-line wrappers in bin/shutter that pass the globals
explicitly ($d; $sc, $combobox_settings_profiles; $sc, $d, $sd, $shf).
Adds t/Shutter/App/12_filename.t covering $w/$h/$profile/$name/$nb_name,
%N numbering, $R hashes and the reserved-character key handler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

bin/shutter CHANGED
@@ -5886,32 +5886,10 @@ modes are currently experimental, your desktop environment might fall back to an
5886
  return TRUE;
5887
  }
5888
 
5889
- sub fct_validate_filename{
5890
- my $myfilename = shift;
5891
- my $myfilename_hint = shift;
5892
- my @invalid_codes = (47, 92);
5893
- $myfilename->signal_connect(
5894
- 'key-press-event' => sub {
5895
- shift;
5896
- my $event = shift;
5897
-
5898
- my $input = Gtk3::Gdk::keyval_to_unicode($event->keyval);
5899
-
5900
- #invalid input
5901
- #~ print $input."\n";
5902
- if (grep($input == $_, @invalid_codes)) {
5903
- my $char = chr($input);
5904
- $char = 'amp();' if $char eq '&';
5905
- $myfilename_hint->set_markup("<span size='small'>" . sprintf($d->get("Reserved character %s is not allowed to be in a filename."), "'" . $char . "'") . "</span>");
5906
- return TRUE;
5907
- } else {
5908
 
5909
- #clear possible message when valid char is entered
5910
- $myfilename_hint->set_markup("<span size='small'></span>");
5911
- return FALSE;
5912
- }
5913
- });
5914
- }
5915
 
5916
  sub fct_undo {
5917
 
@@ -7998,180 +7976,12 @@ modes are currently experimental, your desktop environment might fall back to an
7998
  return TRUE;
7999
  }
8000
 
8001
- sub fct_parse_filename_wildcards {
8002
- my ($filename_value, $screenshooter, $screenshot, $capture_mode) = @_;
8003
-
8004
- my $screenshot_name = $filename_value;
8005
-
8006
- print "Parsing wildcards for $screenshot_name\n"
8007
- if $sc->get_debug;
8008
-
8009
- #parse width and height
8010
- my $swidth = $screenshot->get_width;
8011
- my $sheight = $screenshot->get_height;
8012
-
8013
- $screenshot_name =~ s/\$w/$swidth/g;
8014
- $screenshot_name =~ s/\$h/$sheight/g;
8015
-
8016
- print "Parsed \$width and \$height: $screenshot_name\n"
8017
- if $sc->get_debug;
8018
-
8019
- #parse profile name
8020
- my $current_pname = $combobox_settings_profiles->get_active_text;
8021
- $screenshot_name =~ s/\$profile/$current_pname/g;
8022
 
8023
- print "Parsed \$profile: $screenshot_name\n"
8024
- if $sc->get_debug;
8025
-
8026
- #set name
8027
- #e.g. window or workspace name
8028
- my $resolved_action_name = undef;
8029
-
8030
- if (my $action_name = $screenshooter->get_action_name) {
8031
- $resolved_action_name = $action_name;
8032
- }
8033
 
8034
- # Unified processing for both X11 and Wayland strings
8035
- if ($resolved_action_name) {
8036
- utf8::decode $resolved_action_name;
8037
- $resolved_action_name =~ s/(\/|\#|\>|\<|\%|\*)/-/g;
8038
- $screenshot_name =~ s/\$name/$resolved_action_name/g;
8039
-
8040
- #no blanks (special wildcard)
8041
- $resolved_action_name =~ s/\ //g;
8042
- $screenshot_name =~ s/\$nb_name/$resolved_action_name/g;
8043
- } else {
8044
- $screenshot_name =~ s/(\$name|\$nb_name)/unknown/g;
8045
- }
8046
-
8047
- print "Parsed \$name: $screenshot_name\n"
8048
- if $sc->get_debug;
8049
-
8050
- return $screenshot_name;
8051
- }
8052
-
8053
-
8054
- sub fct_get_next_filename {
8055
- my ($filename_value, $folder, $filetype_value) = @_;
8056
-
8057
- #remove possible dots
8058
- $filetype_value =~ s/\.//;
8059
-
8060
- $filename_value =~ s/\\//g;
8061
-
8062
- #random number - should be earlier than %N reading, as $R is actually a part of date
8063
- if ($filename_value =~ /\$R{1,}/) {
8064
-
8065
- #how many Rs are used? (important for formatting)
8066
- my $pos_proc = index($filename_value, "\$R", 0);
8067
- my $r_counter = 0;
8068
- my $last_pos = $pos_proc;
8069
- $pos_proc++;
8070
-
8071
- while ($pos_proc <= length($filename_value)) {
8072
- $last_pos = index($filename_value, "R", $pos_proc);
8073
- if ($last_pos != -1 && ($last_pos - $pos_proc <= 1)) {
8074
- $r_counter++;
8075
- $pos_proc++;
8076
- } else {
8077
- last;
8078
- }
8079
- }
8080
-
8081
- #prepare filename
8082
- print "---$r_counter Rs used in wild-card\n" if $sc->get_debug;
8083
- my $marks = "";
8084
- my $i = 0;
8085
-
8086
- # Md5 will contain a salt (shutter) and a seconds since 1970
8087
- my $md5_data = "shutter" . time;
8088
- my $md5_hash = md5_hex($md5_data);
8089
-
8090
- # TODO: set random offset? I guess, current implementation is sufficient
8091
- $marks = substr($md5_hash, 0, $r_counter);
8092
-
8093
- #switch $Rs to a part of the hash
8094
- $filename_value =~ s/\$R{1,}/$marks/g;
8095
- }
8096
-
8097
- #auto increment (%NNN is the pattern for the increment placeholder)
8098
- if ($filename_value =~ /\%(N{1,})/) {
8099
- #how many Ns are used? (important for formatting)
8100
- my $n_counter = length($1);
8101
-
8102
- #prepare filename
8103
- print "$n_counter Ns used in wild-card\n" if $sc->get_debug;
8104
-
8105
- my $filename_template = quotemeta $filename_value;
8106
-
8107
- #replace %NNN by a \d+ regex to search for digits
8108
- #also take into account conflicted filenames Ex.: "_014(002)"
8109
- $filename_template =~ s/\\\%N+/(\\d+)(?:\\(\\d+\\))?/g;
8110
- #store regex to string
8111
- my $search_pattern = qr/$filename_template\.$filetype_value/;
8112
-
8113
- print "Searching for files with pattern: $search_pattern\n"
8114
- if $sc->get_debug;
8115
-
8116
- #get_all files from directory
8117
- #we handle the listing with GnomeVFS to read remote dirs as well
8118
- my $dir = Glib::IO::File::new_for_path($folder);
8119
- my $next_count = 0;
8120
- eval {
8121
- my $enumerator = $dir->enumerate_children('standard::*', []);
8122
- while (my $fileinfo = $enumerator->next_file) {
8123
- my $fname = $shf->utf8_decode($fileinfo->get_name);
8124
-
8125
- #not a regular file? -> skip
8126
- next unless $fileinfo->get_file_type eq 'regular';
8127
-
8128
- #does the current file match the pattern?
8129
- # print "Comparing $fname\n" if $sc->get_debug;
8130
- if ($fname =~ $search_pattern) {
8131
- my $curr_value = $1;
8132
- if ($curr_value && $curr_value > $next_count) {
8133
- $next_count = $curr_value;
8134
- print "$next_count is currently greatest value...\n"
8135
- if $sc->get_debug;
8136
- }
8137
- }
8138
- }
8139
- $enumerator->close;
8140
- };
8141
- if ($@) {
8142
- my $response = $sd->dlg_error_message(
8143
- sprintf($d->get("Error while opening directory %s."), "'" . $folder . "'"),
8144
- $d->get("There was an error determining the filename."),
8145
- undef, undef, undef, undef, undef, undef, $@
8146
- );
8147
- return FALSE;
8148
- }
8149
-
8150
- $next_count = 0 unless $next_count =~ /^(\d+\.?\d*|\.\d+)$/;
8151
-
8152
- $next_count = sprintf("%0" . $n_counter . "d", $next_count + 1);
8153
-
8154
- #switch placeholder to $next_count
8155
- $filename_value =~ s/\%N+/$next_count/g;
8156
-
8157
- }
8158
-
8159
- #create new uri
8160
- my $new_giofile = Glib::IO::File::new_for_path("$folder/$filename_value.$filetype_value");
8161
- if ($new_giofile->query_exists) {
8162
- my $count = 1;
8163
- my $existing_filename = $filename_value;
8164
- while ($new_giofile->query_exists) {
8165
- $filename_value = $existing_filename . "(" . sprintf("%03d", $count++) . ")";
8166
- $new_giofile = Glib::IO::File::new_for_path($folder);
8167
- $new_giofile = $new_giofile->append_string("$filename_value.$filetype_value");
8168
- print "Checking new uri: " . $new_giofile->to_string . "\n"
8169
- if $sc->get_debug;
8170
- }
8171
- }
8172
-
8173
- return $new_giofile;
8174
- }
8175
 
8176
  sub fct_check_installed_plugins {
8177
 
 
5886
  return TRUE;
5887
  }
5888
 
5889
+ require Shutter::App::Filename;
5890
+ Shutter::App::Filename->import(qw(validate_filename));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5891
 
5892
+ sub fct_validate_filename { return Shutter::App::Filename::validate_filename($d, @_) }
 
 
 
 
 
5893
 
5894
  sub fct_undo {
5895
 
 
7976
  return TRUE;
7977
  }
7978
 
7979
+ require Shutter::App::Filename;
7980
+ Shutter::App::Filename->import(qw(parse_filename_wildcards get_next_filename));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7981
 
7982
+ sub fct_parse_filename_wildcards { return Shutter::App::Filename::parse_filename_wildcards($sc, $combobox_settings_profiles, @_) }
 
 
 
 
 
 
 
 
 
7983
 
7984
+ sub fct_get_next_filename { return Shutter::App::Filename::get_next_filename($sc, $d, $sd, $shf, @_) }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7985
 
7986
  sub fct_check_installed_plugins {
7987
 
share/shutter/resources/modules/Shutter/App/Filename.pm ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################
2
+ #
3
+ # Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
4
+ #
5
+ # This file is part of Shutter.
6
+ #
7
+ # Shutter is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # Shutter is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Shutter; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #
21
+ ###################################################
22
+
23
+ package Shutter::App::Filename;
24
+
25
+ # Filename helpers extracted from bin/shutter: wildcard expansion,
26
+ # auto-incremented "next filename" lookup and filename entry validation.
27
+ #
28
+ # All application state that these functions used to read from file-scoped
29
+ # variables in bin/shutter is passed explicitly as leading arguments:
30
+ # $sc - Shutter::App::Common (only ->get_debug is used)
31
+ # $d - Locale::gettext domain (only ->get is used)
32
+ # $sd - Shutter::App::SimpleDialogs (only ->dlg_error_message is used)
33
+ # $shf - Shutter::App::HelperFunctions (only ->utf8_decode is used)
34
+ #
35
+ # Glib::IO (Gio via Glib::Object::Introspection) and Gtk3 are expected to be
36
+ # set up by the caller (bin/shutter does this in a BEGIN block).
37
+
38
+ use utf8;
39
+ use strict;
40
+ use warnings;
41
+
42
+ use Exporter qw/ import /;
43
+ use Digest::MD5 qw/ md5_hex /;
44
+ use Time::HiRes qw/ time /;
45
+ use Glib qw/ TRUE FALSE /;
46
+
47
+ our @EXPORT_OK = qw/ validate_filename parse_filename_wildcards get_next_filename /;
48
+
49
+ sub validate_filename {
50
+ my $d = shift;
51
+ my $myfilename = shift;
52
+ my $myfilename_hint = shift;
53
+ my @invalid_codes = (47, 92);
54
+ return $myfilename->signal_connect(
55
+ 'key-press-event' => sub {
56
+ shift;
57
+ my $event = shift;
58
+
59
+ my $input = Gtk3::Gdk::keyval_to_unicode($event->keyval);
60
+
61
+ #invalid input
62
+ #~ print $input."\n";
63
+ if (grep { $input == $_ } @invalid_codes) {
64
+ my $char = chr($input);
65
+ $char = 'amp();' if $char eq '&';
66
+ $myfilename_hint->set_markup("<span size='small'>" . sprintf($d->get("Reserved character %s is not allowed to be in a filename."), "'" . $char . "'") . "</span>");
67
+ return TRUE;
68
+ } else {
69
+
70
+ #clear possible message when valid char is entered
71
+ $myfilename_hint->set_markup("<span size='small'></span>");
72
+ return FALSE;
73
+ }
74
+ });
75
+ }
76
+
77
+ sub parse_filename_wildcards {
78
+ my ($sc, $combobox_settings_profiles, $filename_value, $screenshooter, $screenshot, $capture_mode) = @_;
79
+
80
+ my $screenshot_name = $filename_value;
81
+
82
+ print "Parsing wildcards for $screenshot_name\n"
83
+ if $sc->get_debug;
84
+
85
+ #parse width and height
86
+ my $swidth = $screenshot->get_width;
87
+ my $sheight = $screenshot->get_height;
88
+
89
+ $screenshot_name =~ s/\$w/$swidth/g;
90
+ $screenshot_name =~ s/\$h/$sheight/g;
91
+
92
+ print "Parsed \$width and \$height: $screenshot_name\n"
93
+ if $sc->get_debug;
94
+
95
+ #parse profile name
96
+ my $current_pname = $combobox_settings_profiles->get_active_text;
97
+ $screenshot_name =~ s/\$profile/$current_pname/g;
98
+
99
+ print "Parsed \$profile: $screenshot_name\n"
100
+ if $sc->get_debug;
101
+
102
+ #set name
103
+ #e.g. window or workspace name
104
+ my $resolved_action_name = undef;
105
+
106
+ if (my $action_name = $screenshooter->get_action_name) {
107
+ $resolved_action_name = $action_name;
108
+ }
109
+
110
+ # Unified processing for both X11 and Wayland strings
111
+ if ($resolved_action_name) {
112
+ utf8::decode $resolved_action_name;
113
+ $resolved_action_name =~ s/(\/|\#|\>|\<|\%|\*)/-/g;
114
+ $screenshot_name =~ s/\$name/$resolved_action_name/g;
115
+
116
+ #no blanks (special wildcard)
117
+ $resolved_action_name =~ s/\ //g;
118
+ $screenshot_name =~ s/\$nb_name/$resolved_action_name/g;
119
+ } else {
120
+ $screenshot_name =~ s/(\$name|\$nb_name)/unknown/g;
121
+ }
122
+
123
+ print "Parsed \$name: $screenshot_name\n"
124
+ if $sc->get_debug;
125
+
126
+ return $screenshot_name;
127
+ }
128
+
129
+ sub get_next_filename {
130
+ my ($sc, $d, $sd, $shf, $filename_value, $folder, $filetype_value) = @_;
131
+
132
+ #remove possible dots
133
+ $filetype_value =~ s/\.//;
134
+
135
+ $filename_value =~ s/\\//g;
136
+
137
+ #random number - should be earlier than %N reading, as $R is actually a part of date
138
+ if ($filename_value =~ /\$R{1,}/) {
139
+
140
+ #how many Rs are used? (important for formatting)
141
+ my $pos_proc = index($filename_value, "\$R", 0);
142
+ my $r_counter = 0;
143
+ my $last_pos = $pos_proc;
144
+ $pos_proc++;
145
+
146
+ while ($pos_proc <= length($filename_value)) {
147
+ $last_pos = index($filename_value, "R", $pos_proc);
148
+ if ($last_pos != -1 && ($last_pos - $pos_proc <= 1)) {
149
+ $r_counter++;
150
+ $pos_proc++;
151
+ } else {
152
+ last;
153
+ }
154
+ }
155
+
156
+ #prepare filename
157
+ print "---$r_counter Rs used in wild-card\n" if $sc->get_debug;
158
+ my $marks = "";
159
+ my $i = 0;
160
+
161
+ # Md5 will contain a salt (shutter) and a seconds since 1970
162
+ my $md5_data = "shutter" . time;
163
+ my $md5_hash = md5_hex($md5_data);
164
+
165
+ # TODO: set random offset? I guess, current implementation is sufficient
166
+ $marks = substr($md5_hash, 0, $r_counter);
167
+
168
+ #switch $Rs to a part of the hash
169
+ $filename_value =~ s/\$R{1,}/$marks/g;
170
+ }
171
+
172
+ #auto increment (%NNN is the pattern for the increment placeholder)
173
+ if ($filename_value =~ /\%(N{1,})/) {
174
+
175
+ #how many Ns are used? (important for formatting)
176
+ my $n_counter = length($1);
177
+
178
+ #prepare filename
179
+ print "$n_counter Ns used in wild-card\n" if $sc->get_debug;
180
+
181
+ my $filename_template = quotemeta $filename_value;
182
+
183
+ #replace %NNN by a \d+ regex to search for digits
184
+ #also take into account conflicted filenames Ex.: "_014(002)"
185
+ $filename_template =~ s/\\\%N+/(\\d+)(?:\\(\\d+\\))?/g;
186
+
187
+ #store regex to string
188
+ my $search_pattern = qr/$filename_template\.$filetype_value/;
189
+
190
+ print "Searching for files with pattern: $search_pattern\n"
191
+ if $sc->get_debug;
192
+
193
+ #get_all files from directory
194
+ #we handle the listing with GnomeVFS to read remote dirs as well
195
+ my $dir = Glib::IO::File::new_for_path($folder);
196
+ my $next_count = 0;
197
+ eval {
198
+ my $enumerator = $dir->enumerate_children('standard::*', []);
199
+ while (my $fileinfo = $enumerator->next_file) {
200
+ my $fname = $shf->utf8_decode($fileinfo->get_name);
201
+
202
+ #not a regular file? -> skip
203
+ next unless $fileinfo->get_file_type eq 'regular';
204
+
205
+ #does the current file match the pattern?
206
+ # print "Comparing $fname\n" if $sc->get_debug;
207
+ if ($fname =~ $search_pattern) {
208
+ my $curr_value = $1;
209
+ if ($curr_value && $curr_value > $next_count) {
210
+ $next_count = $curr_value;
211
+ print "$next_count is currently greatest value...\n"
212
+ if $sc->get_debug;
213
+ }
214
+ }
215
+ }
216
+ $enumerator->close;
217
+ };
218
+ if ($@) {
219
+ my $response = $sd->dlg_error_message(
220
+ sprintf($d->get("Error while opening directory %s."), "'" . $folder . "'"),
221
+ $d->get("There was an error determining the filename."),
222
+ undef, undef, undef, undef, undef, undef, $@
223
+ );
224
+ return FALSE;
225
+ }
226
+
227
+ $next_count = 0 unless $next_count =~ /^(\d+\.?\d*|\.\d+)$/;
228
+
229
+ $next_count = sprintf("%0" . $n_counter . "d", $next_count + 1);
230
+
231
+ #switch placeholder to $next_count
232
+ $filename_value =~ s/\%N+/$next_count/g;
233
+
234
+ }
235
+
236
+ #create new uri
237
+ my $new_giofile = Glib::IO::File::new_for_path("$folder/$filename_value.$filetype_value");
238
+ if ($new_giofile->query_exists) {
239
+ my $count = 1;
240
+ my $existing_filename = $filename_value;
241
+ while ($new_giofile->query_exists) {
242
+ $filename_value = $existing_filename . "(" . sprintf("%03d", $count++) . ")";
243
+ $new_giofile = Glib::IO::File::new_for_path($folder);
244
+ $new_giofile = $new_giofile->append_string("$filename_value.$filetype_value");
245
+ print "Checking new uri: " . $new_giofile->to_string . "\n"
246
+ if $sc->get_debug;
247
+ }
248
+ }
249
+
250
+ return $new_giofile;
251
+ }
252
+
253
+ 1;
t/Shutter/App/12_filename.t ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/perl
2
+ use 5.010;
3
+ use strict;
4
+ use warnings;
5
+
6
+ use Test::More;
7
+ use File::Temp qw/ tempdir /;
8
+ use Glib qw/ TRUE FALSE /;
9
+ use Gtk3;
10
+
11
+ BEGIN {
12
+ use Glib::Object::Introspection;
13
+ Glib::Object::Introspection->setup(
14
+ basename => 'Gio',
15
+ version => '2.0',
16
+ package => 'Glib::IO',
17
+ );
18
+ }
19
+
20
+ require_ok("Shutter::App::Filename");
21
+ can_ok("Shutter::App::Filename", qw/ validate_filename parse_filename_wildcards get_next_filename /);
22
+
23
+ # --- minimal stand-ins for the application objects --------------------------
24
+
25
+ {
26
+
27
+ package T::Stub;
28
+ sub new { my ($class, %f) = @_; return bless {%f}, $class }
29
+ sub get_debug { return $_[0]{debug} }
30
+ sub get { return $_[1] } # gettext: identity
31
+ sub get_active_text { return $_[0]{profile} }
32
+ sub get_width { return $_[0]{width} }
33
+ sub get_height { return $_[0]{height} }
34
+ sub get_action_name { return $_[0]{action} }
35
+ sub utf8_decode { return $_[1] }
36
+ sub dlg_error_message { my $self = shift; push @{$self->{errors}}, [@_]; return 1 }
37
+ }
38
+
39
+ my $sc = T::Stub->new(debug => 0);
40
+ my $d = T::Stub->new;
41
+ my $shf = T::Stub->new;
42
+
43
+ sub parse {
44
+ my ($template, %o) = @_;
45
+ my $combo = T::Stub->new(profile => $o{profile} // 'default');
46
+ my $shooter = T::Stub->new(action => $o{action});
47
+ my $shot = T::Stub->new(width => $o{width} // 800, height => $o{height} // 600);
48
+ return Shutter::App::Filename::parse_filename_wildcards($sc, $combo, $template, $shooter, $shot, 'full');
49
+ }
50
+
51
+ subtest "parse_filename_wildcards" => sub {
52
+ is(parse('shot_$wx$h'), 'shot_800x600', '$w and $h are width and height');
53
+ is(parse('$w-$w', width => 1920), '1920-1920', 'all occurrences replaced');
54
+ is(parse('p_$profile', profile => 'work'), 'p_work', '$profile is the active profile name');
55
+ is(parse('$name', action => 'My Window'), 'My Window', '$name is the action name');
56
+ is(parse('$nb_name', action => 'My Window'), 'MyWindow', '$nb_name strips blanks');
57
+ is(parse('$name', action => 'a/b#c>d<e%f*g'), 'a-b-c-d-e-f-g', 'reserved chars in $name become dashes');
58
+ is(parse('$name_$nb_name', action => undef), 'unknown_unknown', 'no action name -> unknown');
59
+ is(parse('$name', action => ''), 'unknown', 'empty action name -> unknown');
60
+ is(parse('%Y-%m_$N'), '%Y-%m_$N', 'unknown/strftime/increment wildcards are left alone');
61
+
62
+ my $decoded = parse('$name', action => "caf\xc3\xa9");
63
+ is($decoded, "caf\x{e9}", 'action name is utf8-decoded');
64
+
65
+ is(parse('$h$hello'), '600600ello', '$h also matches inside longer words (documents current behaviour)');
66
+ };
67
+
68
+ sub next_name {
69
+ my ($dir, $name, $type, $sd) = @_;
70
+ $sd //= T::Stub->new(errors => []);
71
+ return Shutter::App::Filename::get_next_filename($sc, $d, $sd, $shf, $name, $dir, $type);
72
+ }
73
+
74
+ sub touch {
75
+ my $path = shift;
76
+ open my $fh, '>', $path or die "cannot create $path: $!";
77
+ close $fh;
78
+ return;
79
+ }
80
+
81
+ subtest "get_next_filename: plain names" => sub {
82
+ my $dir = tempdir(CLEANUP => 1);
83
+
84
+ my $f = next_name($dir, 'shot', 'png');
85
+ isa_ok($f, 'Glib::IO::File');
86
+ is($f->get_path, "$dir/shot.png", 'plain name kept');
87
+
88
+ is(next_name($dir, 'shot', '.png')->get_path, "$dir/shot.png", 'leading dot removed from filetype');
89
+ is(next_name($dir, 'a\\b', 'png')->get_path, "$dir/ab.png", 'backslashes removed from name');
90
+ };
91
+
92
+ subtest "get_next_filename: %N auto increment" => sub {
93
+ my $dir = tempdir(CLEANUP => 1);
94
+
95
+ is(next_name($dir, 'shot_%NNN', 'png')->get_path, "$dir/shot_001.png", 'empty dir starts at 1, padded to N count');
96
+ is(next_name($dir, 'shot_%N', 'png')->get_path, "$dir/shot_1.png", 'single N -> no padding');
97
+
98
+ touch("$dir/shot_001.png");
99
+ touch("$dir/shot_007.png");
100
+ touch("$dir/shot_042.jpg"); # other filetype: ignored
101
+ touch("$dir/other_099.png"); # other prefix: ignored
102
+ mkdir "$dir/shot_050.png"; # not a regular file: ignored
103
+
104
+ is(next_name($dir, 'shot_%NNN', 'png')->get_path, "$dir/shot_008.png", 'next after highest existing number');
105
+ is(next_name($dir, 'shot_%NNN', 'jpg')->get_path, "$dir/shot_043.jpg", 'counts per filetype');
106
+
107
+ touch("$dir/shot_012(002).png");
108
+ is(next_name($dir, 'shot_%NNN', 'png')->get_path, "$dir/shot_013.png", 'conflict-suffixed names "(002)" are counted');
109
+
110
+ is(next_name($dir, 'shot_%NN', 'png')->get_path, "$dir/shot_13.png", 'width follows N count, not existing files');
111
+
112
+ touch("$dir/x_1234.png");
113
+ is(next_name($dir, 'x_%NN', 'png')->get_path, "$dir/x_1235.png", 'numbers wider than the pattern are kept');
114
+
115
+ touch("$dir/a.b_005.png");
116
+ touch("$dir/aXb_009.png");
117
+ is(next_name($dir, 'a.b_%NNN', 'png')->get_path, "$dir/a.b_006.png", 'regex metachars in the name are quoted');
118
+ };
119
+
120
+ subtest "get_next_filename: \$R random hash" => sub {
121
+ my $dir = tempdir(CLEANUP => 1);
122
+
123
+ my $p = next_name($dir, 'r_$RRRR', 'png')->get_path;
124
+ like($p, qr{^\Q$dir\E/r_[0-9a-f]{4}\.png$}, '$RRRR -> 4 hex chars');
125
+
126
+ $p = next_name($dir, 'r_$R_x', 'png')->get_path;
127
+ like($p, qr{^\Q$dir\E/r_[0-9a-f]_x\.png$}, '$R -> 1 hex char');
128
+
129
+ $p = next_name($dir, '$RR_%NN', 'png')->get_path;
130
+ like($p, qr{^\Q$dir\E/[0-9a-f]{2}_01\.png$}, '$R combined with %N');
131
+ };
132
+
133
+ subtest "get_next_filename: unreadable folder" => sub {
134
+ my $sd = T::Stub->new(errors => []);
135
+ my $ret = next_name('/nonexistent/' . $$ . '/dir', 'shot_%NNN', 'png', $sd);
136
+ is($ret, FALSE, 'returns FALSE when directory cannot be enumerated');
137
+ is(scalar @{$sd->{errors}}, 1, 'error dialog shown once');
138
+ like($sd->{errors}[0][0], qr/Error while opening directory/, 'error message text');
139
+
140
+ # without %N the directory is not enumerated, so no error
141
+ $sd = T::Stub->new(errors => []);
142
+ $ret = next_name('/nonexistent/' . $$ . '/dir', 'shot', 'png', $sd);
143
+ isa_ok($ret, 'Glib::IO::File', 'plain name in missing dir');
144
+ is(scalar @{$sd->{errors}}, 0, 'no error dialog');
145
+ };
146
+
147
+ subtest "get_next_filename: existing target" => sub {
148
+ my $dir = tempdir(CLEANUP => 1);
149
+ touch("$dir/shot.png");
150
+
151
+ TODO: {
152
+ local $TODO = 'conflict branch calls Glib::IO::File->append_string, which does not exist (pre-existing bug)';
153
+ my $f = eval { next_name($dir, 'shot', 'png') };
154
+ is(($f ? $f->get_path : undef), "$dir/shot(001).png", 'existing file gets a (NNN) suffix');
155
+ }
156
+ };
157
+
158
+ subtest "validate_filename" => sub {
159
+ plan skip_all => 'Gtk3 cannot be initialised (no display)' unless eval { Gtk3::init(); 1 };
160
+
161
+ my $entry = Gtk3::Entry->new;
162
+ my $hint = Gtk3::Label->new;
163
+ ok(Shutter::App::Filename::validate_filename($d, $entry, $hint), 'handler connected');
164
+
165
+ my $press = sub {
166
+ my $keyval = shift;
167
+ my $ev = Gtk3::Gdk::Event->new('key-press');
168
+ $ev->keyval($keyval);
169
+ $ev->window($entry->get_window) if $entry->get_window;
170
+ return $entry->signal_emit('key-press-event', $ev);
171
+ };
172
+
173
+ ok($press->(Gtk3::Gdk::unicode_to_keyval(ord '/')), "'/' is rejected");
174
+ like($hint->get_label, qr/Reserved character '\/' is not allowed/, 'hint explains rejection');
175
+
176
+ ok($press->(Gtk3::Gdk::unicode_to_keyval(ord '\\')), "'\\' is rejected");
177
+ like($hint->get_label, qr/Reserved character '\\'/, 'hint names backslash');
178
+
179
+ $press->(Gtk3::Gdk::unicode_to_keyval(ord 'a'));
180
+ is($hint->get_label, "<span size='small'></span>", 'valid char clears hint');
181
+ };
182
+
183
+ done_testing();