runitx Claude Opus 5.5 commited on
Commit
b36e93e
·
unverified ·
1 Parent(s): 6bc278f

Extract upload error and profile name dialogs into Shutter::App::Dialogs

Browse files

Move dlg_upload_error_message_gnome_vfs, dlg_upload_error_message and
dlg_profile_name into plain functions in Shutter::App::Dialogs. bin/shutter
keeps one-line wrappers with the original names that pass $d, $shf, $sd,
$window and the unescape_string/fct_validate_filename helpers (as coderefs)
explicitly. Shutter::App::Dialogs is used instead of UploadDialogs because
dlg_profile_name is not upload-related.

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

bin/shutter CHANGED
@@ -11120,116 +11120,10 @@ modes are currently experimental, your desktop environment might fall back to an
11120
  } #dialog loop
11121
  }
11122
 
11123
- sub dlg_upload_error_message_gnome_vfs {
11124
- my $target_giofile = shift;
11125
- my $result = shift;
11126
-
11127
- #dialogs (main window != parent window)
11128
- my $sd = Shutter::App::SimpleDialogs->new;
11129
-
11130
- my $target_path = $shf->utf8_decode(unescape_string($target_giofile->get_path // $target_giofile->get_uri));
11131
-
11132
- #retry button
11133
- my $retry_btn = Gtk3::Button->new_with_mnemonic($d->get("_Retry"));
11134
- $retry_btn->set_image(Gtk3::Image->new_from_stock('gtk-redo', 'button'));
11135
-
11136
- my $response = $sd->dlg_error_message(
11137
- sprintf($d->get("Error while copying the image %s."), "'" . $target_giofile->get_basename . "'"),
11138
- sprintf($d->get("There was an error copying the image into %s."), "'" . $target_path . "'"),
11139
- $d->get("Skip all"), $d->get("Skip"), undef, $retry_btn, undef, undef, $result
11140
- );
11141
-
11142
- return $response;
11143
- }
11144
-
11145
- sub dlg_upload_error_message {
11146
- my ($status, $max_filesize) = @_;
11147
-
11148
- #dialogs (main window != parent window)
11149
- my $sd = Shutter::App::SimpleDialogs->new;
11150
-
11151
- my $response;
11152
- if ($status == 999) {
11153
- $response = $sd->dlg_error_message($d->get("Please check your credentials and try again."), $d->get("Error while login"));
11154
- } elsif ($status == 998) {
11155
- $response = $sd->dlg_error_message(
11156
- $d->get("Maximum filesize reached"),
11157
- $d->get("Error while uploading"),
11158
- $d->get("Skip all"), $d->get("Skip"), undef, undef, undef, undef, sprintf($d->get("Maximum filesize: %s"), $max_filesize));
11159
- } else {
11160
- $response = $sd->dlg_error_message($status, $d->get("Error while connecting"), $d->get("Skip all"), $d->get("Skip"), $d->get("Retry"),);
11161
- }
11162
- return $response;
11163
- }
11164
-
11165
- sub dlg_profile_name {
11166
- my ($curr_profile_name, $combobox_settings_profiles) = @_;
11167
-
11168
- my $profile_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'other', 'none', undef);
11169
-
11170
- $profile_dialog->set_title("Shutter");
11171
-
11172
- $profile_dialog->set('image' => Gtk3::Image->new_from_stock('gtk-dialog-question', 'dialog'));
11173
-
11174
- $profile_dialog->set('text' => $d->get("Save current preferences as new profile"));
11175
-
11176
- $profile_dialog->set('secondary-text' => $d->get("New profile name") . ": ");
11177
-
11178
- $profile_dialog->add_button('gtk-cancel', 'reject');
11179
- $profile_dialog->add_button('gtk-save', 'accept');
11180
-
11181
- $profile_dialog->set_default_response('accept');
11182
-
11183
- my $new_profile_name_vbox = Gtk3::VBox->new();
11184
- my $new_profile_name_hint = Gtk3::Label->new();
11185
- my $new_profile_name = Gtk3::Entry->new();
11186
- $new_profile_name->set_activates_default(TRUE);
11187
-
11188
- fct_validate_filename($new_profile_name, $new_profile_name_hint);
11189
-
11190
- #show name of current profile
11191
- $new_profile_name->set_text($curr_profile_name)
11192
- if defined $curr_profile_name;
11193
-
11194
- $new_profile_name_vbox->pack_start($new_profile_name, TRUE, TRUE, 0);
11195
- $new_profile_name_vbox->pack_start($new_profile_name_hint, TRUE, TRUE, 0);
11196
- $profile_dialog->get_child->add($new_profile_name_vbox);
11197
- $profile_dialog->show_all;
11198
-
11199
- #run dialog
11200
- my $profile_response = $profile_dialog->run;
11201
-
11202
- #handle user responses here
11203
- if ($profile_response eq 'accept') {
11204
- my $entered_name = $new_profile_name->get_text;
11205
-
11206
- if ($shf->file_exists("$ENV{'HOME'}/.shutter/profiles/$entered_name.xml")) {
11207
-
11208
- #ask the user to replace the profile
11209
- #replace button
11210
- my $replace_btn = Gtk3::Button->new_with_mnemonic($d->get("_Replace"));
11211
- $replace_btn->set_image(Gtk3::Image->new_from_stock('gtk-save-as', 'button'));
11212
-
11213
- my $response = $sd->dlg_warning_message(
11214
- $d->get("Replacing it will overwrite its contents."),
11215
- sprintf($d->get("A profile named %s already exists. Do you want to replace it?"), "'" . $entered_name . "'"),
11216
- undef, undef, undef, $replace_btn, undef, undef
11217
- );
11218
-
11219
- #40 == replace_btn was hit
11220
- if ($response != 40) {
11221
- $profile_dialog->destroy();
11222
- return FALSE;
11223
- }
11224
- }
11225
-
11226
- $profile_dialog->destroy();
11227
- return $entered_name;
11228
- } else {
11229
- $profile_dialog->destroy();
11230
- return FALSE;
11231
- }
11232
- }
11233
 
11234
  require Shutter::App::View;
11235
 
 
11120
  } #dialog loop
11121
  }
11122
 
11123
+ require Shutter::App::Dialogs;
11124
+ sub dlg_upload_error_message_gnome_vfs { return Shutter::App::Dialogs::upload_error_message_gnome_vfs($d, $shf, \&unescape_string, @_) }
11125
+ sub dlg_upload_error_message { return Shutter::App::Dialogs::upload_error_message($d, @_) }
11126
+ sub dlg_profile_name { return Shutter::App::Dialogs::profile_name($window, $d, $sd, $shf, \&fct_validate_filename, @_) }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11127
 
11128
  require Shutter::App::View;
11129
 
share/shutter/resources/modules/Shutter/App/Dialogs.pm ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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::Dialogs;
24
+
25
+ # Application-level modal dialogs that used to live in bin/shutter.
26
+ #
27
+ # These are plain functions: everything they previously read from the
28
+ # file-scoped variables of bin/shutter ($d, $shf, $sd, $window and helper
29
+ # subs) is now passed in explicitly by thin wrappers in bin/shutter.
30
+
31
+ use utf8;
32
+ use strict;
33
+ use warnings;
34
+
35
+ use Gtk3;
36
+ use Glib qw/TRUE FALSE/;
37
+
38
+ require Shutter::App::SimpleDialogs;
39
+
40
+ use Exporter qw/import/;
41
+ our @EXPORT_OK = qw/upload_error_message_gnome_vfs upload_error_message profile_name/;
42
+
43
+ #--------------------------------------
44
+
45
+ # $d - gettext object (Locale::gettext)
46
+ # $shf - Shutter::App::HelperFunctions instance
47
+ # $unescape - coderef that unescapes a (URI-escaped) string
48
+ sub upload_error_message_gnome_vfs {
49
+ my ($d, $shf, $unescape, $target_giofile, $result) = @_;
50
+
51
+ #dialogs (main window != parent window)
52
+ my $sd = Shutter::App::SimpleDialogs->new;
53
+
54
+ my $target_path = $shf->utf8_decode($unescape->($target_giofile->get_path // $target_giofile->get_uri));
55
+
56
+ #retry button
57
+ my $retry_btn = Gtk3::Button->new_with_mnemonic($d->get("_Retry"));
58
+ $retry_btn->set_image(Gtk3::Image->new_from_stock('gtk-redo', 'button'));
59
+
60
+ my $response = $sd->dlg_error_message(
61
+ sprintf($d->get("Error while copying the image %s."), "'" . $target_giofile->get_basename . "'"),
62
+ sprintf($d->get("There was an error copying the image into %s."), "'" . $target_path . "'"),
63
+ $d->get("Skip all"), $d->get("Skip"), undef, $retry_btn, undef, undef, $result
64
+ );
65
+
66
+ return $response;
67
+ }
68
+
69
+ # $d - gettext object (Locale::gettext)
70
+ sub upload_error_message {
71
+ my ($d, $status, $max_filesize) = @_;
72
+
73
+ #dialogs (main window != parent window)
74
+ my $sd = Shutter::App::SimpleDialogs->new;
75
+
76
+ my $response;
77
+ if ($status == 999) {
78
+ $response = $sd->dlg_error_message($d->get("Please check your credentials and try again."), $d->get("Error while login"));
79
+ } elsif ($status == 998) {
80
+ $response = $sd->dlg_error_message(
81
+ $d->get("Maximum filesize reached"),
82
+ $d->get("Error while uploading"),
83
+ $d->get("Skip all"), $d->get("Skip"), undef, undef, undef, undef, sprintf($d->get("Maximum filesize: %s"), $max_filesize));
84
+ } else {
85
+ $response = $sd->dlg_error_message($status, $d->get("Error while connecting"), $d->get("Skip all"), $d->get("Skip"), $d->get("Retry"),);
86
+ }
87
+ return $response;
88
+ }
89
+
90
+ # $window - parent Gtk3::Window (main window)
91
+ # $d - gettext object (Locale::gettext)
92
+ # $sd - Shutter::App::SimpleDialogs instance (main window)
93
+ # $shf - Shutter::App::HelperFunctions instance
94
+ # $validate_filename - coderef: ($entry, $hint_label) installs filename validation
95
+ sub profile_name {
96
+ my ($window, $d, $sd, $shf, $validate_filename, $curr_profile_name, $combobox_settings_profiles) = @_;
97
+
98
+ my $profile_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'other', 'none', undef);
99
+
100
+ $profile_dialog->set_title("Shutter");
101
+
102
+ $profile_dialog->set('image' => Gtk3::Image->new_from_stock('gtk-dialog-question', 'dialog'));
103
+
104
+ $profile_dialog->set('text' => $d->get("Save current preferences as new profile"));
105
+
106
+ $profile_dialog->set('secondary-text' => $d->get("New profile name") . ": ");
107
+
108
+ $profile_dialog->add_button('gtk-cancel', 'reject');
109
+ $profile_dialog->add_button('gtk-save', 'accept');
110
+
111
+ $profile_dialog->set_default_response('accept');
112
+
113
+ my $new_profile_name_vbox = Gtk3::VBox->new();
114
+ my $new_profile_name_hint = Gtk3::Label->new();
115
+ my $new_profile_name = Gtk3::Entry->new();
116
+ $new_profile_name->set_activates_default(TRUE);
117
+
118
+ $validate_filename->($new_profile_name, $new_profile_name_hint);
119
+
120
+ #show name of current profile
121
+ $new_profile_name->set_text($curr_profile_name)
122
+ if defined $curr_profile_name;
123
+
124
+ $new_profile_name_vbox->pack_start($new_profile_name, TRUE, TRUE, 0);
125
+ $new_profile_name_vbox->pack_start($new_profile_name_hint, TRUE, TRUE, 0);
126
+ $profile_dialog->get_child->add($new_profile_name_vbox);
127
+ $profile_dialog->show_all;
128
+
129
+ #run dialog
130
+ my $profile_response = $profile_dialog->run;
131
+
132
+ #handle user responses here
133
+ if ($profile_response eq 'accept') {
134
+ my $entered_name = $new_profile_name->get_text;
135
+
136
+ if ($shf->file_exists("$ENV{'HOME'}/.shutter/profiles/$entered_name.xml")) {
137
+
138
+ #ask the user to replace the profile
139
+ #replace button
140
+ my $replace_btn = Gtk3::Button->new_with_mnemonic($d->get("_Replace"));
141
+ $replace_btn->set_image(Gtk3::Image->new_from_stock('gtk-save-as', 'button'));
142
+
143
+ my $response = $sd->dlg_warning_message(
144
+ $d->get("Replacing it will overwrite its contents."),
145
+ sprintf($d->get("A profile named %s already exists. Do you want to replace it?"), "'" . $entered_name . "'"),
146
+ undef, undef, undef, $replace_btn, undef, undef
147
+ );
148
+
149
+ #40 == replace_btn was hit
150
+ if ($response != 40) {
151
+ $profile_dialog->destroy();
152
+ return FALSE;
153
+ }
154
+ }
155
+
156
+ $profile_dialog->destroy();
157
+ return $entered_name;
158
+ } else {
159
+ $profile_dialog->destroy();
160
+ return FALSE;
161
+ }
162
+ }
163
+
164
+ 1;
t/Shutter/App/17_dialogs.t ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/perl
2
+ use 5.010;
3
+ use strict;
4
+ use warnings;
5
+
6
+ use Gtk3; # to escape warnings "Too late to run INIT block"
7
+
8
+ use Test::More;
9
+ use Test::MockModule;
10
+ use Glib qw/ TRUE FALSE /;
11
+ use File::Temp qw/ tempdir /;
12
+
13
+ require_ok("Shutter::App::Dialogs");
14
+
15
+ can_ok("Shutter::App::Dialogs", qw/upload_error_message_gnome_vfs upload_error_message profile_name/);
16
+
17
+ # Minimal fake objects: a class whose methods are the given closures
18
+ my $fake_class_counter = 0;
19
+
20
+ sub fake_object {
21
+ my (%methods) = @_;
22
+ my $class = "Local::Fake" . ++$fake_class_counter;
23
+ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
24
+ *{"${class}::$_"} = $methods{$_} for keys %methods;
25
+ return bless {}, $class;
26
+ }
27
+
28
+ # fake gettext: returns the msgid unchanged
29
+ my $d = fake_object(get => sub { return $_[1] });
30
+
31
+ # fake helper functions
32
+ sub fake_shf {
33
+ return fake_object(
34
+ utf8_decode => sub { return "decoded(" . $_[1] . ")" },
35
+ file_exists => sub { my ($self, $f) = @_; return -e $f },
36
+ );
37
+ }
38
+
39
+ # capture what is handed to SimpleDialogs
40
+ my @calls;
41
+ my $sd_mock = Test::MockModule->new("Shutter::App::SimpleDialogs");
42
+ $sd_mock->mock("dlg_error_message", sub { shift; push @calls, ["error", @_]; return 42 });
43
+ $sd_mock->mock("dlg_warning_message", sub { shift; push @calls, ["warning", @_]; return $main::warning_response });
44
+
45
+ my $gtk_ok = Gtk3::init_check();
46
+
47
+ subtest "upload_error_message: login error (999)" => sub {
48
+ @calls = ();
49
+ is(Shutter::App::Dialogs::upload_error_message($d, 999), 42, "returns dialog response");
50
+ is(scalar @calls, 1, "one dialog");
51
+ is_deeply($calls[0], ["error", "Please check your credentials and try again.", "Error while login"], "login error dialog args");
52
+ };
53
+
54
+ subtest "upload_error_message: max filesize (998)" => sub {
55
+ @calls = ();
56
+ Shutter::App::Dialogs::upload_error_message($d, 998, "5 MB");
57
+ is_deeply($calls[0], ["error", "Maximum filesize reached", "Error while uploading", "Skip all", "Skip", undef, undef, undef, undef, "Maximum filesize: 5 MB"], "filesize dialog args");
58
+ };
59
+
60
+ subtest "upload_error_message: other status" => sub {
61
+ @calls = ();
62
+ Shutter::App::Dialogs::upload_error_message($d, "Connection refused");
63
+ is_deeply($calls[0], ["error", "Connection refused", "Error while connecting", "Skip all", "Skip", "Retry"], "generic dialog args");
64
+ };
65
+
66
+ subtest "upload_error_message_gnome_vfs" => sub {
67
+ plan skip_all => "no display" unless $gtk_ok;
68
+
69
+ my $shf = fake_shf();
70
+ my $unescape = sub { return "unescaped(" . $_[0] . ")" };
71
+
72
+ @calls = ();
73
+ my $file = fake_object(
74
+ get_path => sub { return undef },
75
+ get_uri => sub { return "sftp://host/a%20b.png" },
76
+ get_basename => sub { return "a b.png" },
77
+ );
78
+ is(Shutter::App::Dialogs::upload_error_message_gnome_vfs($d, $shf, $unescape, $file, "details"), 42, "returns dialog response");
79
+
80
+ my @args = @{$calls[0]};
81
+ is($args[1], "Error while copying the image 'a b.png'.", "header uses basename");
82
+ is($args[2], "There was an error copying the image into 'decoded(unescaped(sftp://host/a%20b.png))'.", "falls back to uri when no path");
83
+ is($args[3], "Skip all", "extra1");
84
+ is($args[4], "Skip", "extra2");
85
+ isa_ok($args[6], "Gtk3::Button", "retry button");
86
+ is($args[6]->get_label, "_Retry", "retry button label");
87
+ is($args[9], "details", "detail text passed through");
88
+ };
89
+
90
+ subtest "profile_name" => sub {
91
+ plan skip_all => "no display" unless $gtk_ok;
92
+
93
+ my $home = tempdir(CLEANUP => 1);
94
+ local $ENV{HOME} = $home;
95
+ mkdir "$home/.shutter";
96
+ mkdir "$home/.shutter/profiles";
97
+
98
+ my $shf = fake_shf();
99
+ my $sd = Shutter::App::SimpleDialogs->new;
100
+
101
+ my ($validated_entry, $validated_hint);
102
+
103
+ # simulate the user answering the modal dialog
104
+ my $response = 'accept';
105
+ my $typed_name = undef;
106
+ my $validate = sub {
107
+ ($validated_entry, $validated_hint) = @_;
108
+
109
+ # once the dialog's modal loop is running, type and answer it
110
+ Glib::Idle->add(
111
+ sub {
112
+ $validated_entry->set_text($typed_name) if defined $typed_name;
113
+ $validated_entry->get_toplevel->response($response);
114
+ return FALSE;
115
+ });
116
+ };
117
+
118
+ my $name = Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current");
119
+ is($name, "current", "accept returns the prefilled current profile name");
120
+ isa_ok($validated_entry, "Gtk3::Entry", "validator got the entry");
121
+ isa_ok($validated_hint, "Gtk3::Label", "validator got the hint label");
122
+
123
+ $typed_name = "fresh";
124
+ is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, undef), "fresh", "accept returns the typed name");
125
+
126
+ $response = 'reject';
127
+ $typed_name = undef;
128
+ is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current"), FALSE, "reject returns FALSE");
129
+
130
+ # existing profile -> replace confirmation
131
+ $response = 'accept';
132
+ open my $fh, '>', "$home/.shutter/profiles/current.xml" or die $!;
133
+ close $fh;
134
+
135
+ @calls = ();
136
+ local $main::warning_response = 40;
137
+ is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current"), "current", "replace confirmed returns name");
138
+ is($calls[0][0], "warning", "asked before replacing");
139
+ is($calls[0][2], "A profile named 'current' already exists. Do you want to replace it?", "warning text");
140
+
141
+ local $main::warning_response = 20;
142
+ is(Shutter::App::Dialogs::profile_name(undef, $d, $sd, $shf, $validate, "current"), FALSE, "replace declined returns FALSE");
143
+ };
144
+
145
+ done_testing();