File: MenuBase.pm

package info (click to toggle)
perlpanel 1%3A0.9.1%2Bcvs20051225-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 1,624 kB
  • ctags: 547
  • sloc: perl: 7,376; sh: 140; makefile: 125
file content (450 lines) | stat: -rw-r--r-- 11,602 bytes parent folder | download | duplicates (4)
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
# $Id: MenuBase.pm,v 1.39 2005/01/10 10:10:16 jodrell Exp $
# This file is part of PerlPanel.
# 
# PerlPanel 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 2 of the License, or
# (at your option) any later version.
# 
# PerlPanel 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 PerlPanel; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright: (C) 2003-2004 Gavin Brown <gavin.brown@uk.com>
#
package PerlPanel::MenuBase;
use PerlPanel::Applet::Commander;
use Gtk2::SimpleList;
use vars qw($COMMANDER);
use strict;

=pod

=head1 NAME

PerlPanel::MenuBase - a base class for PerlPanel menu applets.

=head1 SYNOPSIS

	package PerlPanel::Applet::MyMenu;
	use base 'PerlPanel::MenuBase';
	use strict;

	sub create_menu {

		my $self = shift;

		$self->menu->append($self->menu_item(
			'Hello World!',
			$icon,
			sub { print "Hello World!\n" }
		));
		return 1;
	}

	1;

=head1 DESCRIPTION

C<PerlPanel::MenuBase> is a base class that does as much as possible to
abstract the nuts-and-bolts details of building a PerlPanel menu applet. If you
use C<PerlPanel::MenuBase> to write a menu applet, you don't need to worry
about menu hierarchies or icons - all that's done for you. Instead to can
concentrate on building your menu backend.

=head1 USAGE

C<PerlPanel::MenuBase> is a base class - that means, you must write a Perl
module that inherits from it. The C<use base> line in the example above is one
way you can do this. Then you simply override the C<configure()> and
C<create_menu()> methods with your own.

=cut

our $COMMANDER = PerlPanel::Applet::Commander->new;
$COMMANDER->configure;

sub new {
	my $self		= {};
	$self->{package}	= shift;
	my $multi;
	eval(sprintf('$multi = $%s::MULTI;', $self->{package}));
	if (defined($multi)) {
		$self->{id}	= shift();
	}
	bless($self, $self->{package});
	return $self;
}

sub configure {
	my $self = shift;

	$self->{widget}	= Gtk2::Button->new;
	$self->{menu}	= Gtk2::Menu->new;

	$self->widget->signal_connect('clicked', sub { $self->popup });

	$self->create_menu;

	$self->add_control_items if ($self->show_control_items);

	return 1;
}

sub widget {
	return $_[0]->{widget};
}

sub menu {
	return $_[0]->{menu};
}

sub expand {
	return 0;
}

sub fill {
	return 0;
}

sub end {
	return 'start';
}

=pod

=head1 STANDARD METHODS

	$self->add_control_items;

This method appends the standard set of PerlPanel control options to the
menu. The menu will subsequently look like this:

	|				|
	| ----------------------------- |
	| Lock Screen			|
	| Run Program...		|
	| Take Screenshot...		|
	| ----------------------------- |
	| Shut Down...			|
	| Reboot...			|
	| ----------------------------- |
	| Configure...			|
	| Close Panel			|
	| Add To Panel                > |
	| ----------------------------- |
	| About...			|
	+-------------------------------+

=cut

sub add_control_items {
	my $self = shift;
	my %params = @_;

	if (scalar($self->menu->get_children) > 0) {
		$self->menu->append(Gtk2::SeparatorMenuItem->new);
	}

	### this currently does nothing:
	if ((defined($params{menu_data}) && defined($params{menu_edit_callback})) || defined($params{menu_edit_command})) {
		my $callback;
		if (defined($params{menu_data}) && defined($params{menu_edit_callback})) {
			$callback = sub { $self->run_menu_editor($params{menu_data}, $params{menu_edit_callback}) };
		} elsif (defined($params{menu_edit_command})) {
			$callback = sub { system("$params{menu_edit_command} &") };
		}
		my $item = $self->menu_item(
			_('Edit Menu...'),
			'gtk-properties',
			$callback,
		);
		$item->set_sensitive(0) unless (defined($params{menu_edit_command}));
		$self->menu->append($item);
		$self->menu->append(Gtk2::SeparatorMenuItem->new);
	}

	chomp(my $xscreensaver = `which xscreensaver-command 2> /dev/null`);
	if (-x $xscreensaver) {
		my $lock_item = $self->menu_item(_('Lock Screen'), PerlPanel::get_applet_pbf_filename('lock'), sub { system("$xscreensaver -lock &") });
		$lock_item->signal_connect('expose-event', sub {
			chomp(my $line = `pidof xscreensaver 2> /dev/null`);
			my @pids = split(/[\s\t]+/, $line);
			my $pid = shift(@pids);
			if (int($pid) < 1) {
				$lock_item->set_sensitive(0);
			} else {
				if (-e "/proc/$pid") {
					if ((stat("/proc/$pid"))[4] ne $<) {
						$lock_item->set_sensitive(0);
					} else {
						$lock_item->set_sensitive(1);
					}
				}
			}
			return undef;
		});
		$self->menu->append($lock_item);
	}
	$self->menu->append($self->menu_item(_('Run Program...'), PerlPanel::get_applet_pbf_filename('commander'), sub {
		$COMMANDER->run;
	}));
	$self->menu->append($self->menu_item(_('Take Screenshot...'), PerlPanel::get_applet_pbf_filename('screenshot'), sub {
		require('ScreenShot.pm');
		my $screenshot = PerlPanel::Applet::ScreenShot->new;
		$screenshot->configure;
		$screenshot->prompt;
	}));
	$self->menu->append(Gtk2::SeparatorMenuItem->new);

	# here we callously assume that the presence of this file means that bog-standard users can poweroff and reboot:	

	if (-e '/etc/pam.d/poweroff') {
		$self->menu->append($self->menu_item(
			_('Shut Down...'),
			PerlPanel::lookup_icon(sprintf('%s-action-shutdown', lc($PerlPanel::NAME))),
			sub {
				PerlPanel::question(
					_('Are you sure you want to shut down?'),
					sub { system("poweroff") },
					sub { },
				);
			},
		));

	}

	if (-e '/etc/pam.d/reboot') {
		$self->menu->append($self->menu_item(
			_('Reboot...'),
			PerlPanel::lookup_icon(sprintf('%s-action-reboot', lc($PerlPanel::NAME))),
			sub {
				PerlPanel::question(
					_('Are you sure you want to reboot?'),
					sub { system("reboot") },
					sub { },
				);
			},
		));
	}
	$self->menu->append(Gtk2::SeparatorMenuItem->new);
	$self->menu->append($self->menu_item(_('Configure...'), PerlPanel::get_applet_pbf_filename('configurator'), sub {
		require('Configurator.pm');
		my $configurator = PerlPanel::Applet::Configurator->new;
		$configurator->configure;
		$configurator->init;
	}));

	if ($PerlPanel::OBJECT_REF->{config}->{panel}->{show_quit_button} eq 'true') {
		$self->menu->append(
			$self->menu_item(_('Close Panel'),
			'gtk-close',
			sub { PerlPanel::shutdown }
		));
	}

	my $item = $self->menu_item(_('Add To Panel'), 'gtk-add');
	my $applet_menu = Gtk2::Menu->new;
	$item->set_submenu($applet_menu);

	my $registry = PerlPanel::load_appletregistry;

	foreach my $category (@PerlPanel::APPLET_CATEGORIES) {
		my $icon = PerlPanel::lookup_icon(sprintf('%s-applets-%s', lc($PerlPanel::NAME), lc($category)));
		unless (-e $icon) {
			$icon = PerlPanel::lookup_icon(sprintf('%s-applets', lc($PerlPanel::NAME)));
		}
		my $item = $self->menu_item(
			_($category),
			$icon,
		);
		my $submenu = Gtk2::Menu->new;
		$item->set_submenu($submenu);
		$applet_menu->append($item);

		foreach my $applet (sort @{$registry->{_categories}->{$category}}) {
			my $item = $self->menu_item(
				$applet,
				PerlPanel::get_applet_pbf($applet, PerlPanel::menu_icon_size),
				sub {$self->add_applet_dialog($applet)},
			);
			PerlPanel::tips->set_tip($item, $registry->{$applet});
			$submenu->append($item);
		}
	}

	if (defined($registry->{_categories}->{''})) {
		foreach my $applet (sort @{$registry->{_categories}->{''}}) {
			my $item = $self->menu_item(
				$applet,
				PerlPanel::get_applet_pbf($applet, PerlPanel::menu_icon_size),
				sub {$self->add_applet_dialog($applet)},
			);
			PerlPanel::tips->set_tip($item, $registry->{$applet});
			$applet_menu->append($item);
		}

		$applet_menu->append(Gtk2::SeparatorMenuItem->new);
	}

	my $add_item = $self->menu_item(
		_('Install Applet...'),
		'gtk-add',
		sub { PerlPanel::install_applet_dialog() },
	);
	PerlPanel::tips->set_tip($add_item, _('Install a New applet'), sub { $self->create_menu if ($_[0] == 1) });
	$applet_menu->append($add_item);

	$self->menu->append($item);

	$self->menu->append(Gtk2::SeparatorMenuItem->new);

	$self->menu->append($self->menu_item(_('About...'), PerlPanel::get_applet_pbf_filename('about'), sub {
		require('About.pm');
		my $about = PerlPanel::Applet::About->new;
		$about->configure;
		$about->about;
	}));
	return 1;
}

=pod

	my $item = $self->menu_item($label, $icon, $callback);

This returns a ready-prepared Gtk2::ImageMenuItem. This method does a lot of
hard work for you - C<$label> is set as the text label for the item, and if
defined, C<$callback> is connected to the C<'activate'> signal.

C<$icon> can be either a file, a C<Gtk::Gdk::Pixbuf>, or a stock ID.
C<menu_item> will automagically resize the icon to fit in with the rest of
the menu.

=cut

sub menu_item {
	my ($self, $label, $icon, $callback) = @_;
	my $item;
	my $pbf;
	if (-f $icon) {
		# it's a file:
		$pbf = Gtk2::Gdk::Pixbuf->new_from_file_at_size($icon, PerlPanel::menu_icon_size, PerlPanel::menu_icon_size);

	} elsif (ref($icon) eq 'Gtk2::Gdk::Pixbuf') {
		# it's a pixbuf:
		$pbf = $icon;

	} elsif ($icon =~ /^gtk-/) {
		# assume it's a stock ID:
		$pbf = $self->widget->render_icon($icon, PerlPanel::menu_icon_size_name);

	} else {
		$pbf = $self->widget->render_icon('gtk-new', PerlPanel::menu_icon_size_name);

	}

	if (ref($pbf) ne 'Gtk2::Gdk::Pixbuf') {
		$item = Gtk2::MenuItem->new_with_label($label);

	} else {
		$item = Gtk2::ImageMenuItem->new_with_label($label);
		$item->set_image(Gtk2::Image->new_from_pixbuf($pbf));
	}

	if (defined($callback)) {
		$item->signal_connect('activate', $callback);
	}
	return $item;
}

sub popup {
	my $self = shift;
	$self->menu->show_all;
	$self->menu->popup(undef, undef, sub { return $self->popup_position(@_) }, undef, undef, 0);
	return 1;
}

sub popup_position {
	my $self = shift;
	my ($x, undef) = PerlPanel::get_widget_position($self->widget);
	$x = 0 if ($x < 5);
	if (PerlPanel::position eq 'top') {
		return ($x, PerlPanel::panel->allocation->height);
	} else {
		$self->menu->realize;
		return ($x, PerlPanel::screen_height() - $self->menu->allocation->height - PerlPanel::panel->allocation->height);
	}
}

=pod

	my $icon = $self->get_icon($string, $is_submenu_parent);

This method is deprecated and is no longer available. Use PerlPanel::lookup_icon() instead.

=cut

sub add_applet_dialog {
	my ($self, $applet) = @_;
	# place the new applet next to the menu:
	my $idx = 0;
	foreach my $applet ($PerlPanel::OBJECT_REF->{hbox}->get_children) {
		last if ($applet eq $self->widget);
		$idx++;
	}
	if ($idx >= 0) {
		splice(@{$PerlPanel::OBJECT_REF->{config}{applets}}, $idx+1, 0, $applet);
		$PerlPanel::OBJECT_REF->load_applet($applet, $idx+1);
		PerlPanel::save_config();
	}
	return 1;
}

sub run_menu_editor {
	my ($self, $data, $callback) = @_;

	my $glade = PerlPanel::load_glade('menu-editor');
	my $dialog = $glade->get_widget('main_window');

	$dialog->set_icon(PerlPanel::icon);

	# this will be a Gtk2::Simple::Tree one day:
	my $list = Gtk2::SimpleList->new_from_treeview(
		$glade->get_widget('menu_tree'),
		'entry'	=> 'text',
	);

	$dialog->signal_connect('response', sub {
		my $data = $list->{data},
		$dialog->destroy;
		&{$callback}($data);
	});
}

sub file_age {
	my $self = shift;
	return (stat($self->{file}))[9];
}

=pod

=head1 SEE ALSO

=over

=item * L<perlpanel>

=item * L<perlpanel-applet-howto>

=item * L<Gtk2>

=back

=cut

1;