| 1 | package Tk::AppWindow::Ext::Plugins; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::AppWindow::Ext::Plugins - load and unload plugins | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Carp; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | $VERSION="0.20"; | ||
| 14 | use Tk; | ||
| 15 | use Pod::Usage; | ||
| 16 | use File::Basename; | ||
| 17 | require Tk::YADialog; | ||
| 18 | require Tk::AppWindow::PluginsForm; | ||
| 19 | |||
| 20 | use base qw( Tk::AppWindow::BaseClasses::Extension ); | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | my $app = new Tk::AppWindow(@options, | ||
| 25 | -extensions => ['Plugins'], | ||
| 26 | ); | ||
| 27 | $app->MainLoop; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Gives your user the opportunity to tune the application to his wishes, | ||
| 32 | by loading and unloading plugins. | ||
| 33 | |||
| 34 | Plugins are kind of like extensions, they add functionality. However, | ||
| 35 | a plugin cannot define configvariables. It can issue commands though. | ||
| 36 | |||
| 37 | This extension will load the extension B<ConfigFolder> if it is not loaded already. | ||
| 38 | |||
| 39 | =head1 CONFIG VARIABLES | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item Switch: B<-noplugins> | ||
| 44 | |||
| 45 | Boolean flag, no plugins will be loaded at startup. The list of plugins in the config file will | ||
| 46 | be ignored if this option is set. Default value 0. | ||
| 47 | |||
| 48 | =item Switch: B<-plugins> | ||
| 49 | |||
| 50 | List of plugins that will be loaded at startup. The list of plugins in the config file will | ||
| 51 | be ignored if this option is set. | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | =head1 COMMANDS | ||
| 56 | |||
| 57 | =over 4 | ||
| 58 | |||
| 59 | =item B<plugsdialog> | ||
| 60 | |||
| 61 | Creates a dialog window in which the user can select and unselect plugins | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub new { | ||
| 68 | my $class = shift; | ||
| 69 | my $self = $class->SUPER::new(@_); | ||
| 70 | $self->{PLUGINS} = {}; | ||
| 71 | $self->Require('ConfigFolder'); | ||
| 72 | $self->addPreConfig( | ||
| 73 | -noplugins => ['PASSIVE', undef, undef, 0], | ||
| 74 | -plugins => ['PASSIVE'], | ||
| 75 | ); | ||
| 76 | |||
| 77 | $self->addPostConfig('DoPostConfig', $self); | ||
| 78 | |||
| 79 | $self->cmdConfig( | ||
| 80 | plugsdialog => ['PopPlugsDialog', $self], | ||
| 81 | ); | ||
| 82 | |||
| 83 | return $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head1 METHODS | ||
| 87 | |||
| 88 | =over 4 | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | |||
| 93 | sub AvailablePlugins { | ||
| 94 | my $self = shift; | ||
| 95 | |||
| 96 | my $space = $self->NameSpace; | ||
| 97 | unless (defined $space) { | ||
| 98 | croak "You must set the '-namespace' option"; | ||
| 99 | return (); | ||
| 100 | } | ||
| 101 | |||
| 102 | $space =~ s/\:\:/\//; | ||
| 103 | my %plugins = (); | ||
| 104 | for (@INC) { | ||
| 105 | my $dir = "$_/$space/Plugins"; | ||
| 106 | if ((-e $dir) and (-d $dir)) { | ||
| 107 | my @pm = <$dir/*.pm>; | ||
| 108 | for (@pm) { | ||
| 109 | my $plugin = basename($_, '.pm'); | ||
| 110 | $plugins{$plugin} = 1; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | return sort keys %plugins | ||
| 115 | } | ||
| 116 | |||
| 117 | sub CanQuit { | ||
| 118 | my $self = shift; | ||
| 119 | my @plugs = $self->plugList; | ||
| 120 | my $close = 1; | ||
| 121 | for (@plugs) { | ||
| 122 | $close = 0 unless $self->plugGet($_)->CanQuit | ||
| 123 | } | ||
| 124 | return $close | ||
| 125 | } | ||
| 126 | |||
| 127 | sub ConfigureBars { | ||
| 128 | my ($self, $plug) = @_; | ||
| 129 | my $menu = $self->extGet('MenuBar'); | ||
| 130 | if (defined $menu) { | ||
| 131 | my @items = $plug->MenuItems; | ||
| 132 | $menu->ReConfigure unless @items eq 0; | ||
| 133 | } | ||
| 134 | my $tool = $self->extGet('ToolBar'); | ||
| 135 | if (defined $tool) { | ||
| 136 | my @items = $plug->ToolItems; | ||
| 137 | $tool->ReConfigure unless @items eq 0; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub DoPostConfig { | ||
| 142 | my $self = shift; | ||
| 143 | return if $self->configGet('-noplugins'); | ||
| 144 | my $plugins = $self->configGet('-plugins'); | ||
| 145 | if (defined $plugins) { | ||
| 146 | for (@$plugins) { | ||
| 147 | $self->plugLoad($_); | ||
| 148 | } | ||
| 149 | } else { | ||
| 150 | my $cnf = $self->extGet('ConfigFolder'); | ||
| 151 | my @list = $cnf->loadList('plugins', 'aw_pluglist'); | ||
| 152 | for (@list) { | ||
| 153 | $self->plugLoad($_); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | sub MenuItems { | ||
| 159 | my $self = shift; | ||
| 160 | my @items = (); | ||
| 161 | my @l = $self->plugList; | ||
| 162 | for (@l) { | ||
| 163 | push @items, $self->plugGet($_)->MenuItems | ||
| 164 | } | ||
| 165 | unless ($self->extExists('Settings')) { | ||
| 166 | push @items, ( | ||
| 167 | [ 'menu_normal', 'appname::Quit', '~Plugins', 'plugsdialog', 'configure', 'F10', ], | ||
| 168 | [ 'menu_separator', 'appname::Quit', 'h2'], | ||
| 169 | ) | ||
| 170 | } | ||
| 171 | return @items; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub plugDescription { | ||
| 175 | my ($self, $plug) = @_; | ||
| 176 | my @path = ("Tk/AppWindow/Plugins/"); | ||
| 177 | my $ns = $self->NameSpace; | ||
| 178 | if (defined $ns) { | ||
| 179 | $ns =~ s/\:\:/\//g; | ||
| 180 | push @path, "$ns/Plugins"; | ||
| 181 | } | ||
| 182 | my $file; | ||
| 183 | for (@path) { | ||
| 184 | my $p = $_; | ||
| 185 | $file = Tk::findINC("$p/$plug.pm"); | ||
| 186 | last if defined $file; | ||
| 187 | } | ||
| 188 | open my $fi, "<", $file or die $!; | ||
| 189 | open my $fh, '>', \my $str or die $!; | ||
| 190 | pod2usage( | ||
| 191 | -exitval => 'NOEXIT', | ||
| 192 | -verbose => 99, | ||
| 193 | -input => $fi, | ||
| 194 | -output => $fh, | ||
| 195 | -sections => ['DESCRIPTION'], | ||
| 196 | ); | ||
| 197 | close $fh; | ||
| 198 | close $fi; | ||
| 199 | $str =~ s/^Description:\n//; | ||
| 200 | $str =~ s/\n+$//; | ||
| 201 | return $str; | ||
| 202 | } | ||
| 203 | |||
| 204 | =item B<plugExists(I<$name>)> | ||
| 205 | |||
| 206 | returns the requested plugin object. | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | sub plugExists { | ||
| 211 | my ($self, $plug) = @_; | ||
| 212 | return exists $self->{PLUGINS}->{$plug} | ||
| 213 | } | ||
| 214 | |||
| 215 | =item B<plugGet>(I<$name>) | ||
| 216 | |||
| 217 | returns the requested plugin object. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub plugGet { | ||
| 222 | my ($self, $plug) = @_; | ||
| 223 | return $self->{PLUGINS}->{$plug} | ||
| 224 | } | ||
| 225 | |||
| 226 | =item B<plugList> | ||
| 227 | |||
| 228 | returns a sorted list of loaded plugins. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub plugList { | ||
| 233 | my $plugs = $_[0]->{PLUGINS}; | ||
| 234 | return sort keys %$plugs | ||
| 235 | } | ||
| 236 | |||
| 237 | =item B<plugLoad>(I<$name>) | ||
| 238 | |||
| 239 | Loads the plugin; returns 1 if succesfull; | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | sub plugLoad { | ||
| 244 | my ($self, $plug) = @_; | ||
| 245 | return if $self->plugExists($plug); | ||
| 246 | my $namespace = $self->NameSpace; | ||
| 247 | if (defined $namespace) { | ||
| 248 | $namespace = $namespace . '::Plugins'; | ||
| 249 | my $obj; | ||
| 250 | my $modname = $namespace . "::$plug"; | ||
| 251 | my $app = $self->GetAppWindow; | ||
| 252 | eval "use $modname; \$obj = new $modname(\$app);"; | ||
| 253 | if (defined($obj)) { | ||
| 254 | $self->{PLUGINS}->{$plug} = $obj; | ||
| 255 | $self->ConfigureBars($obj); | ||
| 256 | return 1 | ||
| 257 | } | ||
| 258 | } | ||
| 259 | warn "unable to load plugin $plug\n"; | ||
| 260 | return 0 | ||
| 261 | } | ||
| 262 | |||
| 263 | =item B<plugUnload>(I<$name>) | ||
| 264 | |||
| 265 | Unloads the plugin; returns 1 if succesfull; | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | sub plugUnload { | ||
| 270 | my ($self, $plug) = @_; | ||
| 271 | return unless $self->plugExists($plug); | ||
| 272 | my $obj = $self->plugGet($plug); | ||
| 273 | if ($obj->Unload) { | ||
| 274 | delete $self->{PLUGINS}->{$plug}; | ||
| 275 | $self->ConfigureBars($obj); | ||
| 276 | return 1 | ||
| 277 | } | ||
| 278 | return 0; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub PopPlugsDialog { | ||
| 282 | my $self = shift; | ||
| 283 | my $dialog = $self->YADialog( | ||
| 284 | -title => 'Configure plugins', | ||
| 285 | -buttons => ['Close'], | ||
| 286 | ); | ||
| 287 | $dialog->PluginsForm( | ||
| 288 | -pluginsext => $self, | ||
| 289 | )->pack(-expand => 1, -fill => 'both'); | ||
| 290 | $dialog->Show(-popover => $self->GetAppWindow); | ||
| 291 | $dialog->destroy; | ||
| 292 | } | ||
| 293 | |||
| 294 | =item B<ReConfigure> | ||
| 295 | |||
| 296 | Calls ReConfigure on all loaded plugins. Returns 1 if all succesfull; | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | sub ReConfigure { | ||
| 301 | my $self = shift; | ||
| 302 | my @plugs = $self->plugList; | ||
| 303 | my $succes = 1; | ||
| 304 | for (@plugs) { | ||
| 305 | $succes = 0 unless $self->plugGet($_)->ReConfigure | ||
| 306 | } | ||
| 307 | return $succes | ||
| 308 | } | ||
| 309 | |||
| 310 | sub SavePlugList { | ||
| 311 | my $self = shift; | ||
| 312 | my $id = 'aw_pluglist'; | ||
| 313 | my $cnf = $self->extGet('ConfigFolder'); | ||
| 314 | $cnf->saveList('plugins', $id, @_); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub SettingsPage { | ||
| 318 | my $self = shift; | ||
| 319 | my @pages = ('Plugins' => ['PluginsForm', -pluginsext => $self ]); | ||
| 320 | for ($self->plugList) { | ||
| 321 | my $plug = $self->plugGet($_); | ||
| 322 | push @pages, $plug->SettingsPage; | ||
| 323 | } | ||
| 324 | return @pages | ||
| 325 | } | ||
| 326 | |||
| 327 | sub ToolItems { | ||
| 328 | my $self = shift; | ||
| 329 | my @items = (); | ||
| 330 | my @l = $self->plugList; | ||
| 331 | for (@l) { | ||
| 332 | push @items, $self->plugGet($_)->ToolItems | ||
| 333 | } | ||
| 334 | return @items; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub Quit { | ||
| 338 | my $self = shift; | ||
| 339 | my @plugs = $self->plugList; | ||
| 340 | $self->SavePlugList(@plugs); | ||
| 341 | for (@plugs) { | ||
| 342 | $self->plugGet($_)->Quit | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | =back | ||
| 347 | |||
| 348 | =head1 AUTHOR | ||
| 349 | |||
| 350 | Hans Jeuken (hanje at cpan dot org) | ||
| 351 | |||
| 352 | =head1 BUGS | ||
| 353 | |||
| 354 | Unknown. Probably plenty. If you find any, please contact the author. | ||
| 355 | |||
| 356 | =head1 SEE ALSO | ||
| 357 | |||
| 358 | =over 4 | ||
| 359 | |||
| 360 | =item L<Tk::AppWindow> | ||
| 361 | |||
| 362 | =item L<Tk::AppWindow::BaseClasses::Extension> | ||
| 363 | |||
| 364 | =item L<Tk::AppWindow::BaseClasses::Plugin> | ||
| 365 | |||
| 366 | =back | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | 1; | ||
| 371 | |||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | |||
| 378 |