File size: 11,149 Bytes
bc44c86 | 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 | ###################################################
#
# Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
#
# This file is part of Shutter.
#
# Shutter is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Shutter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Shutter; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
###################################################
package Shutter::App::PluginRegistry;
use utf8;
use 5.010;
use strict;
use warnings;
use Exporter 'import';
use Gtk3;
use Glib qw/ TRUE FALSE /;
use File::Glob qw/ bsd_glob /;
use File::Basename qw/ fileparse dirname /;
our @EXPORT_OK = qw(
check_installed_plugins
plugin_get_info
check_installed_upload_plugins
upload_plugin_get_info
);
# Scan the system and user plugin directories and (re)populate the
# plugin hash. A progress dialog is shown while new plugin information
# is collected; $update_gui is called to let the main loop breathe.
sub check_installed_plugins {
my ($sc, $d, $lp, $shf, $window, $shutter_root, $plugins, $update_gui) = @_;
my $plugin_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'info', 'close', $d->get("Updating plugin information"));
$plugin_dialog->{destroyed} = FALSE;
$plugin_dialog->set_title("Shutter");
$plugin_dialog->set('secondary-text' => $d->get("Please wait while Shutter updates the plugin information") . ".");
$plugin_dialog->signal_connect(response => sub { $plugin_dialog->{destroyed} = TRUE; $_[0]->destroy; });
$plugin_dialog->set_resizable(TRUE);
my $plugin_progress = Gtk3::ProgressBar->new;
$plugin_progress->set_no_show_all(TRUE);
$plugin_progress->set_ellipsize('middle');
$plugin_progress->set_orientation('horizontal');
$plugin_progress->set_fraction(0);
$plugin_dialog->get_child->add($plugin_progress);
my $current_plugin = Gtk3::Label->new("");
$current_plugin->set_line_wrap(TRUE);
$plugin_dialog->get_child->add($current_plugin);
my @plugin_paths = ("$shutter_root/share/shutter/resources/system/plugins/*/*", "$ENV{'HOME'}/.shutter/plugins/*/*");
#fallback icon
# maybe the plugin
# does not provide a custom icon
my $fb_pixbuf_path = "$shutter_root/share/shutter/resources/icons/executable.svg";
my $fb_pixbuf = $lp->load($fb_pixbuf_path, $shf->icon_size('menu'));
foreach my $plugin_path (@plugin_paths) {
my @plugins = bsd_glob($plugin_path);
foreach my $pkey (@plugins) {
if (-d $pkey) {
my $dir_name = $pkey;
#parse filename
my ($name, $folder, $type) = fileparse($dir_name, qr/\.[^.]*/);
#file exists
if ($shf->file_exists("$dir_name/$name")) {
#file is executable
if ($shf->file_executable("$dir_name/$name")) {
#new plugin information?
unless ($plugins->{$pkey}->{'binary'}
&& $plugins->{$pkey}->{'name'}
&& $plugins->{$pkey}->{'category'}
&& $plugins->{$pkey}->{'tooltip'}
&& $plugins->{$pkey}->{'lang'})
{
#show dialog and progress bar
if ( !$plugin_dialog->get_window
&& !$plugin_dialog->{destroyed})
{
$plugin_progress->show;
$plugin_dialog->show_all;
}
print "\nINFO: new plugin information detected - $dir_name/$name\n";
#path to executable
$plugins->{$pkey}->{'binary'} = "$dir_name/$name";
#name
$plugins->{$pkey}->{'name'} = plugin_get_info($plugins->{$pkey}->{'binary'}, 'name');
#category
$plugins->{$pkey}->{'category'} = plugin_get_info($plugins->{$pkey}->{'binary'}, 'sort');
#tooltip
$plugins->{$pkey}->{'tooltip'} = plugin_get_info($plugins->{$pkey}->{'binary'}, 'tip');
#language (shell, perl etc.)
#=> directory name
my $folder_name = dirname($dir_name);
$folder_name =~ /.*\/(.*)/;
$plugins->{$pkey}->{'lang'} = $1;
#refresh the progressbar
$plugin_progress->pulse;
$current_plugin->set_text($plugins->{$pkey}->{'binary'});
#refresh gui
$update_gui->();
}
$plugins->{$pkey}->{'lang'} = "shell"
if $plugins->{$pkey}->{'lang'} eq "";
chomp($plugins->{$pkey}->{'name'});
chomp($plugins->{$pkey}->{'category'});
chomp($plugins->{$pkey}->{'tooltip'});
chomp($plugins->{$pkey}->{'lang'});
#pixbuf
$plugins->{$pkey}->{'pixbuf'} = $plugins->{$pkey}->{'binary'} . ".png"
if ($shf->file_exists($plugins->{$pkey}->{'binary'} . ".png"));
$plugins->{$pkey}->{'pixbuf'} = $plugins->{$pkey}->{'binary'} . ".svg"
if ($shf->file_exists($plugins->{$pkey}->{'binary'} . ".svg"));
if ($shf->file_exists($plugins->{$pkey}->{'pixbuf'})) {
$plugins->{$pkey}->{'pixbuf_object'} = $lp->load($plugins->{$pkey}->{'pixbuf'}, $shf->icon_size('menu'));
} else {
$plugins->{$pkey}->{'pixbuf'} = $fb_pixbuf_path;
$plugins->{$pkey}->{'pixbuf_object'} = $fb_pixbuf;
}
if ($sc->get_debug) {
print "$plugins->{$pkey}->{'name'} - $plugins->{$pkey}->{'binary'}\n";
}
} else {
my $changed = chmod(0755, "$dir_name/$name");
unless ($changed) {
print "\nERROR: plugin exists but is not executable - $dir_name/$name\n";
delete $plugins->{$pkey};
}
} #endif plugin is executable
} else {
delete $plugins->{$pkey};
} #endif plugin exists
}
}
}
#destroys the plugin dialog
$plugin_dialog->response('ok');
return TRUE;
}
# Ask a plugin executable for a piece of information ("name", "sort",
# "tip", ...) by running "$plugin $info"; returns the decoded output.
sub plugin_get_info {
my ($plugin, $info) = @_;
my $plugin_info = `$plugin $info`;
utf8::decode $plugin_info;
return $plugin_info;
}
# Scan the upload plugin directory and (re)populate the accounts hash.
sub check_installed_upload_plugins {
my ($d, $shf, $window, $shutter_root, $pathclass, $accounts, $update_gui) = @_;
my $upload_plugin_dialog = Gtk3::MessageDialog->new($window, [qw/modal destroy-with-parent/], 'info', 'close', $d->get("Updating upload plugin information"));
$upload_plugin_dialog->{destroyed} = FALSE;
$upload_plugin_dialog->set_title("Shutter");
$upload_plugin_dialog->set('secondary-text' => $d->get("Please wait while Shutter updates the upload plugin information") . ".");
$upload_plugin_dialog->signal_connect(response => sub { $upload_plugin_dialog->{destroyed} = TRUE; $_[0]->destroy; });
$upload_plugin_dialog->set_resizable(TRUE);
my $upload_plugin_progress = Gtk3::ProgressBar->new;
$upload_plugin_progress->set_no_show_all(TRUE);
$upload_plugin_progress->set_ellipsize('middle');
$upload_plugin_progress->set_orientation('horizontal');
$upload_plugin_progress->set_fraction(0);
$upload_plugin_dialog->get_child->add($upload_plugin_progress);
my $current_plugin = Gtk3::Label->new("");
$current_plugin->set_line_wrap(TRUE);
$upload_plugin_dialog->get_child->add($current_plugin);
#plugins in user-home not supported yet FIXME
my @upload_plugin_paths = ("$shutter_root/share/shutter/resources/system/upload_plugins/upload/*");
foreach my $upload_plugin_path (@upload_plugin_paths) {
my @upload_plugins = bsd_glob($upload_plugin_path);
foreach my $ukey (@upload_plugins) {
#Checking if file exists
if ($shf->file_exists("$ukey")) {
#file is executable
if ($shf->file_executable("$ukey")) {
#parse filename
my ($name, $folder, $type) = fileparse($ukey, qr/\.[^.]*/);
#skip Imgur if optional dependency Path::Class not met
next if ($name eq 'Imgur' && !$pathclass);
#~ print $name, $folder, $type, "\n";
if ( !exists $accounts->{$name}
|| !exists $accounts->{$name}->{module}
|| $accounts->{$name}->{supports_anonymous_upload} ne upload_plugin_get_info($ukey, 'supports_anonymous_upload')
|| $accounts->{$name}->{supports_authorized_upload} ne upload_plugin_get_info($ukey, 'supports_authorized_upload')
|| $accounts->{$name}->{supports_oauth_upload} ne upload_plugin_get_info($ukey, 'supports_oauth_upload'))
{
#show dialog and progress bar
if ( !$upload_plugin_dialog->get_window
&& !$upload_plugin_dialog->{destroyed})
{
$upload_plugin_progress->show;
$upload_plugin_dialog->show_all;
}
print "\nINFO: new upload-plugin information detected - $folder$name\n";
if (upload_plugin_get_info($ukey, 'module')) {
# Path
$accounts->{$name}->{path} = $ukey;
# Module Name
$accounts->{$name}->{module} = upload_plugin_get_info($ukey, 'module');
# URL
$accounts->{$name}->{host} = upload_plugin_get_info($ukey, 'url');
# Folder
$accounts->{$name}->{folder} = $folder;
# Description
$accounts->{$name}->{description} = plugin_get_info($ukey, 'description');
# Username
$accounts->{$name}->{username} = ""
unless defined $accounts->{$name}->{username};
# Password
$accounts->{$name}->{password} = ""
unless defined $accounts->{$name}->{password};
# Register Color
$accounts->{$name}->{register_color} = "blue";
# Register Text
$accounts->{$name}->{register_text} = upload_plugin_get_info($ukey, 'registration');
# Upload Features
$accounts->{$name}->{supports_anonymous_upload} = upload_plugin_get_info($ukey, 'supports_anonymous_upload');
$accounts->{$name}->{supports_authorized_upload} = upload_plugin_get_info($ukey, 'supports_authorized_upload');
$accounts->{$name}->{supports_oauth_upload} = upload_plugin_get_info($ukey, 'supports_oauth_upload');
#refresh the progressbar
$upload_plugin_progress->pulse;
$current_plugin->set_text($accounts->{$name}->{path});
} else {
print "\nERROR: upload-plugin exists but does not work properly - $folder$name\n";
delete $accounts->{$name};
}
#refresh gui
$update_gui->();
}
} else {
my $changed = chmod(0755, "$ukey");
unless ($changed) {
#parse filename
my ($name, $folder, $type) = fileparse($ukey, qr/\.[^.]*/);
print "\nERROR: upload-plugin exists but is not executable - $ukey\n";
delete $accounts->{$name};
} #endif plugin is executable
}
}
}
}
#destroys the upload_plugin dialog
$upload_plugin_dialog->response('ok');
return TRUE;
}
# Same as plugin_get_info, for upload plugins.
sub upload_plugin_get_info {
my ($upload_plugin, $info) = @_;
my $upload_plugin_info = `$upload_plugin $info`;
utf8::decode $upload_plugin_info;
return $upload_plugin_info;
}
1;
|