/usr/share/perl5/Padre/PluginHandle.pm is in padre 1.00+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | package Padre::PluginHandle;
use 5.008;
use strict;
use warnings;
use Carp ();
use Params::Util ();
use Padre::Util ();
use Padre::Current ();
use Padre::Locale::T;
our $VERSION = '1.00';
use Class::XSAccessor {
getters => {
class => 'class',
db => 'db',
plugin => 'plugin',
},
};
my %STATUS = (
error => _T('Error'),
unloaded => _T('Unloaded'),
loaded => _T('Loaded'),
incompatible => _T('Incompatible'),
disabled => _T('Disabled'),
enabled => _T('Enabled'),
);
#####################################################################
# Constructor and Accessors
sub new {
my $class = shift;
my $self = bless {
@_,
status => 'unloaded',
errstr => [''],
}, $class;
# Check params
if ( exists $self->{name} ) {
Carp::confess("PluginHandle->name should no longer be used (foo)");
}
my $module = $self->class;
my $plugin = $self->plugin;
unless ( Params::Util::_CLASS($module) ) {
Carp::croak("Missing or invalid class param for Padre::PluginHandle");
}
if ( defined $plugin and not Params::Util::_INSTANCE( $plugin, $module ) ) {
Carp::croak("Invalid plugin param for Padre::PluginHandle");
}
unless ( _STATUS( $self->status ) ) {
Carp::croak("Missing or invalid status param for Padre::PluginHandle");
}
# Load or create the database configuration for the plugin
unless ( Params::Util::_INSTANCE( $self->db, 'Padre::DB::Plugin' ) ) {
local $@;
require Padre::DB;
$self->{db} = eval { Padre::DB::Plugin->load($module); };
$self->{db} ||= Padre::DB::Plugin->create(
name => $module,
# Track the last version of the plugin that we were
# able to successfully enable (nothing to start with)
version => undef,
# Having undef here means no preference yet
enabled => undef,
config => undef,
);
}
return $self;
}
#####################################################################
# Status Methods
sub locale_prefix {
my $self = shift;
my $string = $self->class;
$string =~ s/::/__/g;
return $string;
}
sub status {
my $self = shift;
if (@_) {
unless ( _STATUS( $_[0] ) ) {
Carp::croak("Invalid PluginHandle status '$_[0]'");
}
$self->{status} = $_[0];
}
return $self->{status};
}
sub status_localized {
my $self = shift;
my $text = $STATUS{ $self->{status} } or return;
return Wx::gettext($text);
}
sub error {
$_[0]->{status} eq 'error';
}
sub unloaded {
$_[0]->{status} eq 'unloaded';
}
sub loaded {
$_[0]->{status} eq 'loaded';
}
sub incompatible {
$_[0]->{status} eq 'incompatible';
}
sub disabled {
$_[0]->{status} eq 'disabled';
}
sub enabled {
$_[0]->{status} eq 'enabled';
}
sub can_enable {
$_[0]->{status} eq 'loaded'
or $_[0]->{status} eq 'disabled';
}
sub can_disable {
$_[0]->{status} eq 'enabled';
}
sub can_editor {
$_[0]->{status} eq 'enabled'
and $_[0]->{plugin}->can('editor_enable');
}
sub can_context {
$_[0]->{status} eq 'enabled'
and $_[0]->{plugin}->can('event_on_context_menu');
}
sub errstr {
my $self = shift;
# Set the error string
if (@_) {
$self->{errstr} = [@_];
return 1;
}
# Delay the translating sprintf and rerun each time,
# so that plugin errors can appear in the currently active language
# instead of the language at the time of the error.
my @copy = @{ $self->{errstr} };
my $text = Wx::gettext( shift @copy );
return sprintf( $text, @copy );
}
######################################################################
# Interface Methods
# Wrap any can call in an eval as the plugin might have a custom
# can method and we need to be paranoid around plugins.
sub plugin_can {
my $self = shift;
my $plugin = $self->{plugin} or return undef;
# Ignore errors and flatten to a boolean
local $@;
return !!eval { $plugin->can(shift) };
}
sub plugin_icon {
my $self = shift;
my $icon = eval { $self->class->plugin_icon; };
if ( Params::Util::_INSTANCE( $icon, 'Wx::Bitmap' ) ) {
return $icon;
} else {
return;
}
}
sub plugin_name {
my $self = shift;
if ( $self->plugin_can('plugin_name') ) {
local $@;
return scalar eval { $self->plugin->plugin_name };
} else {
return $self->class;
}
}
sub plugin_version {
my $self = shift;
# Prefer the version from the loaded plugin
if ( $self->plugin_can('VERSION') ) {
local $@;
my $rv = eval { $self->plugin->VERSION; };
return $rv;
}
# Intuit the version by reading the actual file
require Class::Inspector;
my $file = Class::Inspector->resolved_filename( $self->class );
if ($file) {
require Padre::Util;
my $version = Padre::Util::parse_variable( $file, 'VERSION' );
return $version if $version;
}
return '???';
}
# Wrapper over the void context call to preferences
sub plugin_preferences {
my $self = shift;
if ( $self->plugin_can('plugin_preferences') ) {
local $@;
eval { $self->plugin->plugin_preferences };
}
}
######################################################################
# Pass-Through Methods
sub enable {
my $self = shift;
unless ( $self->can_enable ) {
Carp::croak("Cannot enable plug-in '$self'");
}
# Add the plugin catalog to the locale
require Padre::Locale;
my $prefix = $self->locale_prefix;
my $code = Padre::Locale::rfc4646();
my $current = $self->current;
my $main = $current->main;
$main->{locale}->AddCatalog("$prefix-$code");
# Call the enable method for the object
my $plugin_status;
eval { $plugin_status = $self->plugin->plugin_enable; };
if ($@) {
# Crashed during plugin enable
$self->status('error');
$self->errstr(
_T("Failed to enable plug-in '%s': %s"),
$self->class,
$@,
);
return 0;
} else {
if ( not $plugin_status ) {
# Prerequisites missing plug-in enable
$self->status('error');
$self->errstr(
_T("Prerequisites missing suggest you read the POD for '%s': %s"),
$self->class,
$@,
);
return 0;
}
}
# If the plugin defines document types, register them.
# Skip document registration on error.
my @documents = eval { $self->plugin->registered_documents; };
if ($@) {
# Crashed during document registration
$self->status('error');
$self->errstr(
_T("Failed to enable plug-in '%s': %s"),
$self->class,
$@,
);
return 0;
}
while (@documents) {
my $type = shift @documents;
my $class = shift @documents;
require Padre::MIME;
Padre::MIME->find($type)->plugin($class);
}
# If the plugin defines syntax highlighters, register them.
# Skip highlighter registration on error.
# TO DO remove these when plugin is disabled (and make sure files
# are not highlighted with this any more)
my @highlighters = eval { $self->plugin->registered_highlighters; };
if ($@) {
# Crashed during highlighter registration
$self->status('error');
$self->errstr(
_T("Failed to enable plug-in '%s': %s"),
$self->class,
$@,
);
return 0;
}
while (@highlighters) {
my $module = shift @highlighters;
my $params = shift @highlighters;
require Padre::Wx::Scintilla;
Padre::Wx::Scintilla->add_highlighter( $module, $params );
}
# Look for Padre hooks
if ( $self->plugin->can('padre_hooks') ) {
my $hooks = eval { $self->plugin->padre_hooks; };
if ( ref($hooks) ne 'HASH' ) {
$main->error(
sprintf(
Wx::gettext('Plugin %s returned %s instead of a hook list on ->padre_hooks'),
$self->class,
$hooks,
)
);
return;
}
my $manager = $current->ide->plugin_manager;
for my $hookname ( keys( %{$hooks} ) ) {
if ( !$Padre::PluginManager::PADRE_HOOKS{$hookname} ) {
$main->error(
sprintf( Wx::gettext('Plugin %s tried to register invalid hook %s'), $self->class, $hookname ) );
next;
}
for my $hook ( ( ref( $hooks->{$hookname} ) eq 'ARRAY' ) ? @{ $hooks->{$hookname} } : $hooks->{$hookname} )
{
if ( ref($hook) ne 'CODE' ) {
$main->error(
sprintf( Wx::gettext('Plugin %s tried to register non-CODE hook %s'), $self->class, $hookname )
);
next;
}
push @{ $manager->{hooks}->{$hookname} }, [ $self->plugin, $hook ];
}
}
}
# Update the last-enabled version each time it is enabled
$self->update( version => $self->plugin_version );
# Update the status
$self->status('enabled');
$self->errstr('');
return 1;
}
sub disable {
my $self = shift;
unless ( $self->can_disable ) {
Carp::croak("Cannot disable plug-in '$self'");
}
# If the plugin defines document types, deregister them
my @documents = $self->plugin->registered_documents;
while (@documents) {
my $type = shift @documents;
my $class = shift @documents;
Padre::MIME->find($type)->reset;
}
# Call the plugin's own disable method
eval { $self->plugin->plugin_disable; };
if ($@) {
# Crashed during plugin disable
$self->status('error');
$self->errstr(
_T("Failed to disable plug-in '%s': %s"),
$self->class,
$@,
);
return 1;
}
# Remove hooks
# The ->padre_hooks method may not return constant values, scanning the hook
# tree is much safer than removing the hooks reported _now_
# NOTE: Horribly violates encapsulation
my $manager = $self->current->ide->plugin_manager;
for my $hookname ( keys( %{ $manager->{hooks} } ) ) {
my @new_list;
for my $hook ( @{ $manager->{hooks}->{$hookname} } ) {
next if $hook->[0] eq $self->plugin;
push @new_list, $hook;
}
$manager->{hooks}->{$hookname} = \@new_list;
}
# Update the status
$self->status('disabled');
$self->errstr('');
return 0;
}
sub unload {
require Padre::Unload;
Padre::Unload::unload( $_[0]->class );
}
sub update {
shift->db->update(@_);
}
######################################################################
# Support Methods
sub current {
if ( $_[0]->{plugin} ) {
return $_[0]->{plugin}->current;
} else {
return Padre::Current->new;
}
}
sub _STATUS {
Params::Util::_STRING( $_[0] ) or return;
return {
error => 1,
unloaded => 1,
loaded => 1,
incompatible => 1,
disabled => 1,
enabled => 1,
}->{ $_[0] };
}
1;
# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
|