################################################### # # Copyright (C) 2008-2013 Mario Kemper # # 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;