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

Extract plugin discovery into Shutter::App::PluginRegistry

Browse files

Move fct_check_installed_plugins, fct_plugin_get_info,
fct_check_installed_upload_plugins and fct_upload_plugin_get_info into a
new module. bin/shutter keeps one-line wrappers that pass $sc, $d, $lp,
$shf, $window, $shutter_root, $pathclass, \%plugins / \%accounts and
\&fct_update_gui explicitly. Adds t/Shutter/App/13_plugins.t.

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

bin/shutter CHANGED
@@ -7983,294 +7983,16 @@ modes are currently experimental, your desktop environment might fall back to an
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
 
7988
- my $plugin_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'info', 'close', $d->get("Updating plugin information"));
7989
- $plugin_dialog->{destroyed} = FALSE;
7990
 
7991
- $plugin_dialog->set_title("Shutter");
7992
 
7993
- $plugin_dialog->set('secondary-text' => $d->get("Please wait while Shutter updates the plugin information") . ".");
7994
 
7995
- $plugin_dialog->signal_connect(response => sub { $plugin_dialog->{destroyed} = TRUE; $_[0]->destroy; });
7996
-
7997
- $plugin_dialog->set_resizable(TRUE);
7998
-
7999
- my $plugin_progress = Gtk3::ProgressBar->new;
8000
- $plugin_progress->set_no_show_all(TRUE);
8001
- $plugin_progress->set_ellipsize('middle');
8002
- $plugin_progress->set_orientation('horizontal');
8003
- $plugin_progress->set_fraction(0);
8004
-
8005
- $plugin_dialog->get_child->add($plugin_progress);
8006
- my $current_plugin = Gtk3::Label->new("");
8007
- $current_plugin->set_line_wrap(TRUE);
8008
- $plugin_dialog->get_child->add($current_plugin);
8009
-
8010
- my @plugin_paths = ("$shutter_root/share/shutter/resources/system/plugins/*/*", "$ENV{'HOME'}/.shutter/plugins/*/*");
8011
-
8012
- #fallback icon
8013
- # maybe the plugin
8014
- # does not provide a custom icon
8015
- my $fb_pixbuf_path = "$shutter_root/share/shutter/resources/icons/executable.svg";
8016
- my $fb_pixbuf = $lp->load($fb_pixbuf_path, $shf->icon_size('menu'));
8017
-
8018
- foreach my $plugin_path (@plugin_paths) {
8019
- my @plugins = bsd_glob($plugin_path);
8020
- foreach my $pkey (@plugins) {
8021
- if (-d $pkey) {
8022
- my $dir_name = $pkey;
8023
-
8024
- #parse filename
8025
- my ($name, $folder, $type) = fileparse($dir_name, qr/\.[^.]*/);
8026
-
8027
- #file exists
8028
- if ($shf->file_exists("$dir_name/$name")) {
8029
-
8030
- #file is executable
8031
- if ($shf->file_executable("$dir_name/$name")) {
8032
-
8033
- #new plugin information?
8034
- unless ($plugins{$pkey}->{'binary'}
8035
- && $plugins{$pkey}->{'name'}
8036
- && $plugins{$pkey}->{'category'}
8037
- && $plugins{$pkey}->{'tooltip'}
8038
- && $plugins{$pkey}->{'lang'})
8039
- {
8040
-
8041
- #show dialog and progress bar
8042
- if ( !$plugin_dialog->get_window
8043
- && !$plugin_dialog->{destroyed})
8044
- {
8045
- $plugin_progress->show;
8046
- $plugin_dialog->show_all;
8047
- }
8048
-
8049
- print "\nINFO: new plugin information detected - $dir_name/$name\n";
8050
-
8051
- #path to executable
8052
- $plugins{$pkey}->{'binary'} = "$dir_name/$name";
8053
-
8054
- #name
8055
- $plugins{$pkey}->{'name'} = fct_plugin_get_info($plugins{$pkey}->{'binary'}, 'name');
8056
-
8057
- #category
8058
- $plugins{$pkey}->{'category'} = fct_plugin_get_info($plugins{$pkey}->{'binary'}, 'sort');
8059
-
8060
- #tooltip
8061
- $plugins{$pkey}->{'tooltip'} = fct_plugin_get_info($plugins{$pkey}->{'binary'}, 'tip');
8062
-
8063
- #language (shell, perl etc.)
8064
- #=> directory name
8065
- my $folder_name = dirname($dir_name);
8066
- $folder_name =~ /.*\/(.*)/;
8067
- $plugins{$pkey}->{'lang'} = $1;
8068
-
8069
- #refresh the progressbar
8070
- $plugin_progress->pulse;
8071
- $current_plugin->set_text($plugins{$pkey}->{'binary'});
8072
-
8073
- #refresh gui
8074
- fct_update_gui();
8075
-
8076
- }
8077
-
8078
- $plugins{$pkey}->{'lang'} = "shell"
8079
- if $plugins{$pkey}->{'lang'} eq "";
8080
-
8081
- chomp($plugins{$pkey}->{'name'});
8082
- chomp($plugins{$pkey}->{'category'});
8083
- chomp($plugins{$pkey}->{'tooltip'});
8084
- chomp($plugins{$pkey}->{'lang'});
8085
-
8086
- #pixbuf
8087
- $plugins{$pkey}->{'pixbuf'} = $plugins{$pkey}->{'binary'} . ".png"
8088
- if ($shf->file_exists($plugins{$pkey}->{'binary'} . ".png"));
8089
- $plugins{$pkey}->{'pixbuf'} = $plugins{$pkey}->{'binary'} . ".svg"
8090
- if ($shf->file_exists($plugins{$pkey}->{'binary'} . ".svg"));
8091
-
8092
- if ($shf->file_exists($plugins{$pkey}->{'pixbuf'})) {
8093
- $plugins{$pkey}->{'pixbuf_object'} = $lp->load($plugins{$pkey}->{'pixbuf'}, $shf->icon_size('menu'));
8094
- } else {
8095
- $plugins{$pkey}->{'pixbuf'} = $fb_pixbuf_path;
8096
- $plugins{$pkey}->{'pixbuf_object'} = $fb_pixbuf;
8097
- }
8098
- if ($sc->get_debug) {
8099
- print "$plugins{$pkey}->{'name'} - $plugins{$pkey}->{'binary'}\n";
8100
- }
8101
-
8102
- } else {
8103
- my $changed = chmod(0755, "$dir_name/$name");
8104
- unless ($changed) {
8105
- print "\nERROR: plugin exists but is not executable - $dir_name/$name\n";
8106
- delete $plugins{$pkey};
8107
- }
8108
- } #endif plugin is executable
8109
-
8110
- } else {
8111
- delete $plugins{$pkey};
8112
- } #endif plugin exists
8113
-
8114
- }
8115
- }
8116
- }
8117
-
8118
- #destroys the plugin dialog
8119
- $plugin_dialog->response('ok');
8120
-
8121
- return TRUE;
8122
- }
8123
-
8124
- sub fct_plugin_get_info {
8125
- my ($plugin, $info) = @_;
8126
-
8127
- my $plugin_info = `$plugin $info`;
8128
- utf8::decode $plugin_info;
8129
-
8130
- return $plugin_info;
8131
- }
8132
-
8133
- sub fct_check_installed_upload_plugins {
8134
-
8135
- my $upload_plugin_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'info', 'close', $d->get("Updating upload plugin information"));
8136
- $upload_plugin_dialog->{destroyed} = FALSE;
8137
-
8138
- $upload_plugin_dialog->set_title("Shutter");
8139
-
8140
- $upload_plugin_dialog->set('secondary-text' => $d->get("Please wait while Shutter updates the upload plugin information") . ".");
8141
-
8142
- $upload_plugin_dialog->signal_connect(response => sub { $upload_plugin_dialog->{destroyed} = TRUE; $_[0]->destroy; });
8143
-
8144
- $upload_plugin_dialog->set_resizable(TRUE);
8145
-
8146
- my $upload_plugin_progress = Gtk3::ProgressBar->new;
8147
- $upload_plugin_progress->set_no_show_all(TRUE);
8148
- $upload_plugin_progress->set_ellipsize('middle');
8149
- $upload_plugin_progress->set_orientation('horizontal');
8150
- $upload_plugin_progress->set_fraction(0);
8151
-
8152
- $upload_plugin_dialog->get_child->add($upload_plugin_progress);
8153
- my $current_plugin = Gtk3::Label->new("");
8154
- $current_plugin->set_line_wrap(TRUE);
8155
- $upload_plugin_dialog->get_child->add($current_plugin);
8156
-
8157
- #plugins in user-home not supported yet FIXME
8158
- my @upload_plugin_paths = ("$shutter_root/share/shutter/resources/system/upload_plugins/upload/*");
8159
-
8160
- foreach my $upload_plugin_path (@upload_plugin_paths) {
8161
- my @upload_plugins = bsd_glob($upload_plugin_path);
8162
- foreach my $ukey (@upload_plugins) {
8163
-
8164
- #Checking if file exists
8165
- if ($shf->file_exists("$ukey")) {
8166
-
8167
- #file is executable
8168
- if ($shf->file_executable("$ukey")) {
8169
-
8170
- #parse filename
8171
- my ($name, $folder, $type) = fileparse($ukey, qr/\.[^.]*/);
8172
-
8173
- #skip Imgur if optional dependency Path::Class not met
8174
- next if ($name eq 'Imgur' && !$pathclass);
8175
-
8176
- #~ print $name, $folder, $type, "\n";
8177
-
8178
- if ( !exists $accounts{$name}
8179
- || !exists $accounts{$name}->{module}
8180
- || $accounts{$name}->{supports_anonymous_upload} ne fct_upload_plugin_get_info($ukey, 'supports_anonymous_upload')
8181
- || $accounts{$name}->{supports_authorized_upload} ne fct_upload_plugin_get_info($ukey, 'supports_authorized_upload')
8182
- || $accounts{$name}->{supports_oauth_upload} ne fct_upload_plugin_get_info($ukey, 'supports_oauth_upload'))
8183
- {
8184
-
8185
- #show dialog and progress bar
8186
- if ( !$upload_plugin_dialog->get_window
8187
- && !$upload_plugin_dialog->{destroyed})
8188
- {
8189
- $upload_plugin_progress->show;
8190
- $upload_plugin_dialog->show_all;
8191
- }
8192
-
8193
- print "\nINFO: new upload-plugin information detected - $folder$name\n";
8194
-
8195
- if (fct_upload_plugin_get_info($ukey, 'module')) {
8196
-
8197
- # Path
8198
- $accounts{$name}->{path} = $ukey;
8199
-
8200
- # Module Name
8201
- $accounts{$name}->{module} = fct_upload_plugin_get_info($ukey, 'module');
8202
-
8203
- # URL
8204
- $accounts{$name}->{host} = fct_upload_plugin_get_info($ukey, 'url');
8205
-
8206
- # Folder
8207
- $accounts{$name}->{folder} = $folder;
8208
-
8209
- # Description
8210
- $accounts{$name}->{description} = fct_plugin_get_info($ukey, 'description');
8211
-
8212
- # Username
8213
- $accounts{$name}->{username} = ""
8214
- unless defined $accounts{$name}->{username};
8215
-
8216
- # Password
8217
- $accounts{$name}->{password} = ""
8218
- unless defined $accounts{$name}->{password};
8219
-
8220
- # Register Color
8221
- $accounts{$name}->{register_color} = "blue";
8222
-
8223
- # Register Text
8224
- $accounts{$name}->{register_text} = fct_upload_plugin_get_info($ukey, 'registration');
8225
-
8226
- # Upload Features
8227
- $accounts{$name}->{supports_anonymous_upload} = fct_upload_plugin_get_info($ukey, 'supports_anonymous_upload');
8228
- $accounts{$name}->{supports_authorized_upload} = fct_upload_plugin_get_info($ukey, 'supports_authorized_upload');
8229
- $accounts{$name}->{supports_oauth_upload} = fct_upload_plugin_get_info($ukey, 'supports_oauth_upload');
8230
-
8231
- #refresh the progressbar
8232
- $upload_plugin_progress->pulse;
8233
- $current_plugin->set_text($accounts{$name}->{path});
8234
-
8235
- } else {
8236
- print "\nERROR: upload-plugin exists but does not work properly - $folder$name\n";
8237
- delete $accounts{$name};
8238
- }
8239
-
8240
- #refresh gui
8241
- fct_update_gui();
8242
-
8243
- }
8244
-
8245
- } else {
8246
- my $changed = chmod(0755, "$ukey");
8247
- unless ($changed) {
8248
-
8249
- #parse filename
8250
- my ($name, $folder, $type) = fileparse($ukey, qr/\.[^.]*/);
8251
- print "\nERROR: upload-plugin exists but is not executable - $ukey\n";
8252
- delete $accounts{$name};
8253
- } #endif plugin is executable
8254
- }
8255
- }
8256
- }
8257
-
8258
- }
8259
-
8260
- #destroys the upload_plugin dialog
8261
- $upload_plugin_dialog->response('ok');
8262
-
8263
- return TRUE;
8264
- }
8265
-
8266
- sub fct_upload_plugin_get_info {
8267
- my ($upload_plugin, $info) = @_;
8268
-
8269
- my $upload_plugin_info = `$upload_plugin $info`;
8270
- utf8::decode $upload_plugin_info;
8271
-
8272
- return $upload_plugin_info;
8273
- }
8274
 
8275
  sub fct_iter_programs {
8276
  my ($model, $path, $iter, $search_for) = @_;
 
7983
 
7984
  sub fct_get_next_filename { return Shutter::App::Filename::get_next_filename($sc, $d, $sd, $shf, @_) }
7985
 
7986
+ require Shutter::App::PluginRegistry;
7987
+ Shutter::App::PluginRegistry->import(qw(check_installed_plugins plugin_get_info check_installed_upload_plugins upload_plugin_get_info));
7988
 
7989
+ sub fct_check_installed_plugins { return check_installed_plugins($sc, $d, $lp, $shf, $window, $shutter_root, \%plugins, \&fct_update_gui) }
 
7990
 
7991
+ sub fct_plugin_get_info { return plugin_get_info(@_) }
7992
 
7993
+ sub fct_check_installed_upload_plugins { return check_installed_upload_plugins($d, $shf, $window, $shutter_root, $pathclass, \%accounts, \&fct_update_gui) }
7994
 
7995
+ sub fct_upload_plugin_get_info { return upload_plugin_get_info(@_) }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7996
 
7997
  sub fct_iter_programs {
7998
  my ($model, $path, $iter, $search_for) = @_;
share/shutter/resources/modules/Shutter/App/PluginRegistry.pm ADDED
@@ -0,0 +1,341 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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::PluginRegistry;
24
+
25
+ use utf8;
26
+ use 5.010;
27
+ use strict;
28
+ use warnings;
29
+
30
+ use Exporter 'import';
31
+ use Gtk3;
32
+ use Glib qw/ TRUE FALSE /;
33
+ use File::Glob qw/ bsd_glob /;
34
+ use File::Basename qw/ fileparse dirname /;
35
+
36
+ our @EXPORT_OK = qw(
37
+ check_installed_plugins
38
+ plugin_get_info
39
+ check_installed_upload_plugins
40
+ upload_plugin_get_info
41
+ );
42
+
43
+ # Scan the system and user plugin directories and (re)populate the
44
+ # plugin hash. A progress dialog is shown while new plugin information
45
+ # is collected; $update_gui is called to let the main loop breathe.
46
+ sub check_installed_plugins {
47
+ my ($sc, $d, $lp, $shf, $window, $shutter_root, $plugins, $update_gui) = @_;
48
+
49
+ my $plugin_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'info', 'close', $d->get("Updating plugin information"));
50
+ $plugin_dialog->{destroyed} = FALSE;
51
+
52
+ $plugin_dialog->set_title("Shutter");
53
+
54
+ $plugin_dialog->set('secondary-text' => $d->get("Please wait while Shutter updates the plugin information") . ".");
55
+
56
+ $plugin_dialog->signal_connect(response => sub { $plugin_dialog->{destroyed} = TRUE; $_[0]->destroy; });
57
+
58
+ $plugin_dialog->set_resizable(TRUE);
59
+
60
+ my $plugin_progress = Gtk3::ProgressBar->new;
61
+ $plugin_progress->set_no_show_all(TRUE);
62
+ $plugin_progress->set_ellipsize('middle');
63
+ $plugin_progress->set_orientation('horizontal');
64
+ $plugin_progress->set_fraction(0);
65
+
66
+ $plugin_dialog->get_child->add($plugin_progress);
67
+ my $current_plugin = Gtk3::Label->new("");
68
+ $current_plugin->set_line_wrap(TRUE);
69
+ $plugin_dialog->get_child->add($current_plugin);
70
+
71
+ my @plugin_paths = ("$shutter_root/share/shutter/resources/system/plugins/*/*", "$ENV{'HOME'}/.shutter/plugins/*/*");
72
+
73
+ #fallback icon
74
+ # maybe the plugin
75
+ # does not provide a custom icon
76
+ my $fb_pixbuf_path = "$shutter_root/share/shutter/resources/icons/executable.svg";
77
+ my $fb_pixbuf = $lp->load($fb_pixbuf_path, $shf->icon_size('menu'));
78
+
79
+ foreach my $plugin_path (@plugin_paths) {
80
+ my @plugins = bsd_glob($plugin_path);
81
+ foreach my $pkey (@plugins) {
82
+ if (-d $pkey) {
83
+ my $dir_name = $pkey;
84
+
85
+ #parse filename
86
+ my ($name, $folder, $type) = fileparse($dir_name, qr/\.[^.]*/);
87
+
88
+ #file exists
89
+ if ($shf->file_exists("$dir_name/$name")) {
90
+
91
+ #file is executable
92
+ if ($shf->file_executable("$dir_name/$name")) {
93
+
94
+ #new plugin information?
95
+ unless ($plugins->{$pkey}->{'binary'}
96
+ && $plugins->{$pkey}->{'name'}
97
+ && $plugins->{$pkey}->{'category'}
98
+ && $plugins->{$pkey}->{'tooltip'}
99
+ && $plugins->{$pkey}->{'lang'})
100
+ {
101
+
102
+ #show dialog and progress bar
103
+ if ( !$plugin_dialog->get_window
104
+ && !$plugin_dialog->{destroyed})
105
+ {
106
+ $plugin_progress->show;
107
+ $plugin_dialog->show_all;
108
+ }
109
+
110
+ print "\nINFO: new plugin information detected - $dir_name/$name\n";
111
+
112
+ #path to executable
113
+ $plugins->{$pkey}->{'binary'} = "$dir_name/$name";
114
+
115
+ #name
116
+ $plugins->{$pkey}->{'name'} = plugin_get_info($plugins->{$pkey}->{'binary'}, 'name');
117
+
118
+ #category
119
+ $plugins->{$pkey}->{'category'} = plugin_get_info($plugins->{$pkey}->{'binary'}, 'sort');
120
+
121
+ #tooltip
122
+ $plugins->{$pkey}->{'tooltip'} = plugin_get_info($plugins->{$pkey}->{'binary'}, 'tip');
123
+
124
+ #language (shell, perl etc.)
125
+ #=> directory name
126
+ my $folder_name = dirname($dir_name);
127
+ $folder_name =~ /.*\/(.*)/;
128
+ $plugins->{$pkey}->{'lang'} = $1;
129
+
130
+ #refresh the progressbar
131
+ $plugin_progress->pulse;
132
+ $current_plugin->set_text($plugins->{$pkey}->{'binary'});
133
+
134
+ #refresh gui
135
+ $update_gui->();
136
+
137
+ }
138
+
139
+ $plugins->{$pkey}->{'lang'} = "shell"
140
+ if $plugins->{$pkey}->{'lang'} eq "";
141
+
142
+ chomp($plugins->{$pkey}->{'name'});
143
+ chomp($plugins->{$pkey}->{'category'});
144
+ chomp($plugins->{$pkey}->{'tooltip'});
145
+ chomp($plugins->{$pkey}->{'lang'});
146
+
147
+ #pixbuf
148
+ $plugins->{$pkey}->{'pixbuf'} = $plugins->{$pkey}->{'binary'} . ".png"
149
+ if ($shf->file_exists($plugins->{$pkey}->{'binary'} . ".png"));
150
+ $plugins->{$pkey}->{'pixbuf'} = $plugins->{$pkey}->{'binary'} . ".svg"
151
+ if ($shf->file_exists($plugins->{$pkey}->{'binary'} . ".svg"));
152
+
153
+ if ($shf->file_exists($plugins->{$pkey}->{'pixbuf'})) {
154
+ $plugins->{$pkey}->{'pixbuf_object'} = $lp->load($plugins->{$pkey}->{'pixbuf'}, $shf->icon_size('menu'));
155
+ } else {
156
+ $plugins->{$pkey}->{'pixbuf'} = $fb_pixbuf_path;
157
+ $plugins->{$pkey}->{'pixbuf_object'} = $fb_pixbuf;
158
+ }
159
+ if ($sc->get_debug) {
160
+ print "$plugins->{$pkey}->{'name'} - $plugins->{$pkey}->{'binary'}\n";
161
+ }
162
+
163
+ } else {
164
+ my $changed = chmod(0755, "$dir_name/$name");
165
+ unless ($changed) {
166
+ print "\nERROR: plugin exists but is not executable - $dir_name/$name\n";
167
+ delete $plugins->{$pkey};
168
+ }
169
+ } #endif plugin is executable
170
+
171
+ } else {
172
+ delete $plugins->{$pkey};
173
+ } #endif plugin exists
174
+
175
+ }
176
+ }
177
+ }
178
+
179
+ #destroys the plugin dialog
180
+ $plugin_dialog->response('ok');
181
+
182
+ return TRUE;
183
+ }
184
+
185
+ # Ask a plugin executable for a piece of information ("name", "sort",
186
+ # "tip", ...) by running "$plugin $info"; returns the decoded output.
187
+ sub plugin_get_info {
188
+ my ($plugin, $info) = @_;
189
+
190
+ my $plugin_info = `$plugin $info`;
191
+ utf8::decode $plugin_info;
192
+
193
+ return $plugin_info;
194
+ }
195
+
196
+ # Scan the upload plugin directory and (re)populate the accounts hash.
197
+ sub check_installed_upload_plugins {
198
+ my ($d, $shf, $window, $shutter_root, $pathclass, $accounts, $update_gui) = @_;
199
+
200
+ my $upload_plugin_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'info', 'close', $d->get("Updating upload plugin information"));
201
+ $upload_plugin_dialog->{destroyed} = FALSE;
202
+
203
+ $upload_plugin_dialog->set_title("Shutter");
204
+
205
+ $upload_plugin_dialog->set('secondary-text' => $d->get("Please wait while Shutter updates the upload plugin information") . ".");
206
+
207
+ $upload_plugin_dialog->signal_connect(response => sub { $upload_plugin_dialog->{destroyed} = TRUE; $_[0]->destroy; });
208
+
209
+ $upload_plugin_dialog->set_resizable(TRUE);
210
+
211
+ my $upload_plugin_progress = Gtk3::ProgressBar->new;
212
+ $upload_plugin_progress->set_no_show_all(TRUE);
213
+ $upload_plugin_progress->set_ellipsize('middle');
214
+ $upload_plugin_progress->set_orientation('horizontal');
215
+ $upload_plugin_progress->set_fraction(0);
216
+
217
+ $upload_plugin_dialog->get_child->add($upload_plugin_progress);
218
+ my $current_plugin = Gtk3::Label->new("");
219
+ $current_plugin->set_line_wrap(TRUE);
220
+ $upload_plugin_dialog->get_child->add($current_plugin);
221
+
222
+ #plugins in user-home not supported yet FIXME
223
+ my @upload_plugin_paths = ("$shutter_root/share/shutter/resources/system/upload_plugins/upload/*");
224
+
225
+ foreach my $upload_plugin_path (@upload_plugin_paths) {
226
+ my @upload_plugins = bsd_glob($upload_plugin_path);
227
+ foreach my $ukey (@upload_plugins) {
228
+
229
+ #Checking if file exists
230
+ if ($shf->file_exists("$ukey")) {
231
+
232
+ #file is executable
233
+ if ($shf->file_executable("$ukey")) {
234
+
235
+ #parse filename
236
+ my ($name, $folder, $type) = fileparse($ukey, qr/\.[^.]*/);
237
+
238
+ #skip Imgur if optional dependency Path::Class not met
239
+ next if ($name eq 'Imgur' && !$pathclass);
240
+
241
+ #~ print $name, $folder, $type, "\n";
242
+
243
+ if ( !exists $accounts->{$name}
244
+ || !exists $accounts->{$name}->{module}
245
+ || $accounts->{$name}->{supports_anonymous_upload} ne upload_plugin_get_info($ukey, 'supports_anonymous_upload')
246
+ || $accounts->{$name}->{supports_authorized_upload} ne upload_plugin_get_info($ukey, 'supports_authorized_upload')
247
+ || $accounts->{$name}->{supports_oauth_upload} ne upload_plugin_get_info($ukey, 'supports_oauth_upload'))
248
+ {
249
+
250
+ #show dialog and progress bar
251
+ if ( !$upload_plugin_dialog->get_window
252
+ && !$upload_plugin_dialog->{destroyed})
253
+ {
254
+ $upload_plugin_progress->show;
255
+ $upload_plugin_dialog->show_all;
256
+ }
257
+
258
+ print "\nINFO: new upload-plugin information detected - $folder$name\n";
259
+
260
+ if (upload_plugin_get_info($ukey, 'module')) {
261
+
262
+ # Path
263
+ $accounts->{$name}->{path} = $ukey;
264
+
265
+ # Module Name
266
+ $accounts->{$name}->{module} = upload_plugin_get_info($ukey, 'module');
267
+
268
+ # URL
269
+ $accounts->{$name}->{host} = upload_plugin_get_info($ukey, 'url');
270
+
271
+ # Folder
272
+ $accounts->{$name}->{folder} = $folder;
273
+
274
+ # Description
275
+ $accounts->{$name}->{description} = plugin_get_info($ukey, 'description');
276
+
277
+ # Username
278
+ $accounts->{$name}->{username} = ""
279
+ unless defined $accounts->{$name}->{username};
280
+
281
+ # Password
282
+ $accounts->{$name}->{password} = ""
283
+ unless defined $accounts->{$name}->{password};
284
+
285
+ # Register Color
286
+ $accounts->{$name}->{register_color} = "blue";
287
+
288
+ # Register Text
289
+ $accounts->{$name}->{register_text} = upload_plugin_get_info($ukey, 'registration');
290
+
291
+ # Upload Features
292
+ $accounts->{$name}->{supports_anonymous_upload} = upload_plugin_get_info($ukey, 'supports_anonymous_upload');
293
+ $accounts->{$name}->{supports_authorized_upload} = upload_plugin_get_info($ukey, 'supports_authorized_upload');
294
+ $accounts->{$name}->{supports_oauth_upload} = upload_plugin_get_info($ukey, 'supports_oauth_upload');
295
+
296
+ #refresh the progressbar
297
+ $upload_plugin_progress->pulse;
298
+ $current_plugin->set_text($accounts->{$name}->{path});
299
+
300
+ } else {
301
+ print "\nERROR: upload-plugin exists but does not work properly - $folder$name\n";
302
+ delete $accounts->{$name};
303
+ }
304
+
305
+ #refresh gui
306
+ $update_gui->();
307
+
308
+ }
309
+
310
+ } else {
311
+ my $changed = chmod(0755, "$ukey");
312
+ unless ($changed) {
313
+
314
+ #parse filename
315
+ my ($name, $folder, $type) = fileparse($ukey, qr/\.[^.]*/);
316
+ print "\nERROR: upload-plugin exists but is not executable - $ukey\n";
317
+ delete $accounts->{$name};
318
+ } #endif plugin is executable
319
+ }
320
+ }
321
+ }
322
+
323
+ }
324
+
325
+ #destroys the upload_plugin dialog
326
+ $upload_plugin_dialog->response('ok');
327
+
328
+ return TRUE;
329
+ }
330
+
331
+ # Same as plugin_get_info, for upload plugins.
332
+ sub upload_plugin_get_info {
333
+ my ($upload_plugin, $info) = @_;
334
+
335
+ my $upload_plugin_info = `$upload_plugin $info`;
336
+ utf8::decode $upload_plugin_info;
337
+
338
+ return $upload_plugin_info;
339
+ }
340
+
341
+ 1;
t/Shutter/App/13_plugins.t ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/perl
2
+ use 5.010;
3
+ use utf8;
4
+ use strict;
5
+ use warnings;
6
+
7
+ use Test::More;
8
+ use Glib qw/ TRUE FALSE /;
9
+ use File::Temp qw/ tempdir /;
10
+ use File::Path qw/ make_path /;
11
+
12
+ require_ok("Shutter::App::PluginRegistry");
13
+ can_ok("Shutter::App::PluginRegistry", qw(check_installed_plugins plugin_get_info check_installed_upload_plugins upload_plugin_get_info));
14
+
15
+ # Write an executable shell script that answers "<script> <key>" with the
16
+ # value from %answers (empty for unknown keys).
17
+ sub write_plugin {
18
+ my ($path, %answers) = @_;
19
+ open my $fh, '>:encoding(UTF-8)', $path or die "cannot write $path: $!";
20
+ print {$fh} "#!/bin/sh\ncase \"\$1\" in\n";
21
+ for my $key (sort keys %answers) {
22
+ print {$fh} " $key) printf '%s\\n' '$answers{$key}' ;;\n";
23
+ }
24
+ print {$fh} "esac\n";
25
+ close $fh;
26
+ chmod 0755, $path or die "chmod $path: $!";
27
+ return $path;
28
+ }
29
+
30
+ # Minimal stand-in for the Shutter objects the registry needs ($sc, $d,
31
+ # $shf, $lp): a single object blessed into main implements all of them.
32
+ sub new_stub { return bless {}, __PACKAGE__ }
33
+ sub get_debug { return 0 }
34
+ sub get { return $_[1] }
35
+ sub file_exists { my ($self, $f) = @_; return defined $f && -f $f }
36
+ sub file_executable { my ($self, $f) = @_; return -x $f }
37
+ sub icon_size { return (16, 16) }
38
+ sub load { my ($self, $f) = @_; return "pixbuf:$f" }
39
+
40
+ my $root = tempdir(CLEANUP => 1);
41
+ local $ENV{HOME} = tempdir(CLEANUP => 1);
42
+
43
+ subtest "plugin_get_info / upload_plugin_get_info" => sub {
44
+ my $bin = write_plugin("$root/info", name => 'Bläh', sort => 'effect');
45
+
46
+ my $name = Shutter::App::PluginRegistry::plugin_get_info($bin, 'name');
47
+ is($name, "Bläh\n", "output is returned verbatim (incl. newline)");
48
+ ok(utf8::is_utf8($name), "output is decoded as UTF-8");
49
+ is(Shutter::App::PluginRegistry::plugin_get_info($bin, 'sort'), "effect\n", "other key");
50
+ is(Shutter::App::PluginRegistry::plugin_get_info($bin, 'unknown'), "", "unknown key gives empty string");
51
+ is(Shutter::App::PluginRegistry::upload_plugin_get_info($bin, 'name'), "Bläh\n", "upload variant behaves the same");
52
+ };
53
+
54
+ SKIP: {
55
+ skip "no display available", 2 unless Gtk3::init_check();
56
+
57
+ subtest "check_installed_plugins" => sub {
58
+ my $dir = "$root/share/shutter/resources/system/plugins/shell/myplug";
59
+ make_path($dir);
60
+ write_plugin("$dir/myplug", name => 'My Plugin', sort => 'effect', tip => 'Does things');
61
+
62
+ my $user_dir = "$ENV{HOME}/.shutter/plugins/perl/userplug";
63
+ make_path($user_dir);
64
+ write_plugin("$user_dir/userplug", name => 'User Plugin', sort => 'misc', tip => 'Tip');
65
+ open my $fh, '>', "$user_dir/userplug.png" or die;
66
+ close $fh;
67
+
68
+ # directory without the plugin binary: stale entries are removed
69
+ my $broken = "$root/share/shutter/resources/system/plugins/shell/broken";
70
+ make_path($broken);
71
+
72
+ my %plugins = ($broken => {binary => "$broken/broken"});
73
+ my $lp = new_stub();
74
+ my $gui = 0;
75
+
76
+ my $ret = Shutter::App::PluginRegistry::check_installed_plugins(new_stub(), new_stub(), $lp, new_stub(), undef, $root, \%plugins, sub { $gui++ });
77
+
78
+ is($ret, TRUE, "returns TRUE");
79
+ ok(!exists $plugins{$broken}, "plugin without binary removed");
80
+ is_deeply(
81
+ $plugins{$dir},
82
+ {
83
+ binary => "$dir/myplug",
84
+ name => 'My Plugin',
85
+ category => 'effect',
86
+ tooltip => 'Does things',
87
+ lang => 'shell',
88
+ pixbuf => "$root/share/shutter/resources/icons/executable.svg",
89
+ pixbuf_object => "pixbuf:$root/share/shutter/resources/icons/executable.svg",
90
+ },
91
+ "system plugin registered with fallback icon"
92
+ );
93
+ is($plugins{$user_dir}{lang}, 'perl', "language taken from parent dir");
94
+ is($plugins{$user_dir}{pixbuf}, "$user_dir/userplug.png", "custom icon used");
95
+ is($plugins{$user_dir}{pixbuf_object}, "pixbuf:$user_dir/userplug.png", "custom icon loaded");
96
+ is($gui, 2, "gui refreshed once per new plugin");
97
+
98
+ # already known plugins are not queried again
99
+ $plugins{$dir}{name} = "Cached\n";
100
+ Shutter::App::PluginRegistry::check_installed_plugins(new_stub(), new_stub(), $lp, new_stub(), undef, $root, \%plugins, sub { $gui++ });
101
+ is($plugins{$dir}{name}, 'Cached', "cached info kept (and chomped)");
102
+ is($gui, 2, "no gui refresh without new plugins");
103
+ };
104
+
105
+ subtest "check_installed_upload_plugins" => sub {
106
+ my $dir = "$root/share/shutter/resources/system/upload_plugins/upload";
107
+ make_path($dir);
108
+ my %features = (
109
+ supports_anonymous_upload => 1,
110
+ supports_authorized_upload => 0,
111
+ supports_oauth_upload => 0
112
+ );
113
+ write_plugin("$dir/Foo.pm", module => 'Foo', url => 'https://foo.example', description => 'Foo host', registration => 'Sign up', %features);
114
+ write_plugin("$dir/Imgur.pm", module => 'Imgur', %features);
115
+ write_plugin("$dir/Bad.pm", %features);
116
+
117
+ my %accounts = (Foo => {username => 'me'});
118
+ my $gui = 0;
119
+ my $ret = Shutter::App::PluginRegistry::check_installed_upload_plugins(new_stub(), new_stub(), undef, $root, FALSE, \%accounts, sub { $gui++ });
120
+
121
+ is($ret, TRUE, "returns TRUE");
122
+ ok(!exists $accounts{Imgur}, "Imgur skipped without Path::Class");
123
+ ok(!exists $accounts{Bad}, "plugin without module removed");
124
+ is($accounts{Foo}{module}, "Foo\n", "module");
125
+ is($accounts{Foo}{host}, "https://foo.example\n", "host");
126
+ is($accounts{Foo}{description}, "Foo host\n", "description");
127
+ is($accounts{Foo}{register_text}, "Sign up\n", "registration text");
128
+ is($accounts{Foo}{register_color}, "blue", "register colour");
129
+ is($accounts{Foo}{username}, 'me', "existing username kept");
130
+ is($accounts{Foo}{password}, '', "password defaulted");
131
+ is($accounts{Foo}{path}, "$dir/Foo.pm", "path");
132
+ is($accounts{Foo}{folder}, "$dir/", "folder");
133
+ is($accounts{Foo}{supports_anonymous_upload}, "1\n", "feature flag");
134
+ is($gui, 2, "gui refreshed for each new upload plugin");
135
+
136
+ Shutter::App::PluginRegistry::check_installed_upload_plugins(new_stub(), new_stub(), undef, $root, TRUE, \%accounts, sub { $gui++ });
137
+ is($accounts{Imgur}{module}, "Imgur\n", "Imgur registered with Path::Class");
138
+ };
139
+ }
140
+
141
+ done_testing();