blib/lib/Curses/UI/Dialog/Filebrowser.pm untested

filter: Statementstmt 0.0% 0 / 200 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 323 CC 30 SCAR 43.8
CC
30
Cov
0%
get_dir
210.0
new
72.0
goto_homedirectory
12.0
CRAP 79.7
1 # ----------------------------------------------------------------------
2 # Curses::UI::Dialog::Filebrowser
3 #
4 # (c) 2001-2002 by Maurice Makaay. All rights reserved.
5 # This file is part of Curses::UI. Curses::UI is free software.
6 # You can redistribute it and/or modify it under the same terms
7 # as perl itself.
8 #
9 # Currently maintained by Marcus Thiesen
10 # e-mail: marcus@cpan.thiesenweb.de
11 # ----------------------------------------------------------------------
12  
13 package Curses::UI::Dialog::Filebrowser;
14  
15 use strict;
16 use Curses;
17 use Curses::UI::Window;
18 use Curses::UI::Common;
19 use Cwd;
20  
21 use vars qw(
22 $VERSION
23 @ISA
24 );
25  
26 @ISA = qw(
27 Curses::UI::Window
28 Curses::UI::Common
29 );
30  
31 $VERSION = '1.10';
32  
33 sub new ()
34 {
35 my $class = shift;
36  
37 my %userargs = @_;
38 keys_to_lowercase(\%userargs);
39  
40 my %args = (
41 -title => undef,
42 -path => undef,
43 -file => '',
44 -show_hidden => 0,
45 -mask => undef,
46 -mask_selected => 0,
47 -editfilename => 0,
48 -bg => -1,
49 -fg => -1,
50  
51 %userargs,
52  
53 -border => 1,
54 -centered => 1,
55 -titleinverse => 0,
56 -ipad => 1,
57 -selected_cache => {},
58 );
59  
60 # Does -file contain a path? Then do some splitting.
61 if (defined $args{-file} and $args{-file} =~ m|/|)
62 {
63 my $file = "";
64 my $path = "";
65  
66 my @path = split /\//, $args{-file};
67 $file = pop @path;
68 if (@path) {
69 $path = join "/", @path;
70 }
71 $args{-path} = $path;
72 $args{-file} = $file;
73 }
74  
75 # Does -path not contain a path? Then use the
76 # current working directory.
77 if (not defined $args{-path} or $args{-path} =~ /^\s*$/) {
78 $args{-path} = cwd;
79 }
80  
81 my $this = $class->SUPER::new(%args);
82 $this->layout();
83
84 my $l = $this->root->lang;
85  
86 # Start at home? Goto the homedirectory of the current user
87 # if the -path is not defined.
88 $this->goto_homedirectory unless defined $this->{-path};
89  
90 my $buttons = $this->add(
91 'buttons', 'Buttonbox',
92 -y => -1,
93 -x => 0,
94 -width => undef,
95 -buttonalignment => 'right',
96 -buttons => [ 'ok', 'cancel' ],
97 -bg => $this->{-bg},
98 -fg => $this->{-fg},
99 );
100  
101 # Let the window in which the buttons are loose focus
102 # if a button is pressed.
103 $buttons->set_routine( 'press-button', \&press_button_callback );
104  
105 my $one_up = $l->get('file_dirup');
106 my $dirbrowser = $this->add(
107 'dirbrowser', 'Listbox',
108 -y => 0,
109 -border => 1,
110 -width => int(($this->canvaswidth - 3)/2),
111 -padbottom => 6,
112 -values => [],
113 -vscrollbar => 1,
114 -labels => { '..' => ".. ($one_up)" },
115 -bg => $this->{-bg},
116 -fg => $this->{-fg},
117 -bbg => $this->{-bg},
118 -bfg => $this->{-fg},
119  
120 );
121  
122 $dirbrowser->set_routine('option-select',\&dirselect);
123 $dirbrowser->set_routine('goto-homedirectory',\&select_homedirectory);
124 $dirbrowser->set_binding('goto-homedirectory', '~');
125
126 my $filebrowser = $this->add(
127 'filebrowser', 'Listbox',
128 -y => 0,
129 -x => $this->getobj('dirbrowser')->width + 1,
130 -border => 1,
131 -padbottom => 6,
132 -vscrollbar => 1,
133 -values => ["info.txt","passwd"],
134 -bg => $this->{-bg},
135 -fg => $this->{-fg},
136 -bbg => $this->{-bg},
137 -bfg => $this->{-fg},
138 );
139  
140 $filebrowser->set_routine('option-select', \&fileselect);
141 $filebrowser->set_routine('goto-homedirectory',\&select_homedirectory);
142 $filebrowser->set_binding('goto-homedirectory', '~');
143  
144 # Get language specific data.
145 my $l_path = $l->get('file_path');
146 my $l_mask = $l->get('file_mask');
147 my $l_file = $l->get('file_file');
148 my $l_len = $l->get('file_labelsize');
149  
150 my $labeloffset = 1;
151 my $textoffset = $l_len + 2;
152  
153 $this->add(
154 'pathlabel', 'Label',
155 -x => $labeloffset,
156 -y => $this->canvasheight - 5,
157 -text => $l_path,
158 -bg => $this->{-bg},
159 -fg => $this->{-fg},
160 );
161 $this->add(
162 'pathvalue', 'Label',
163 -x => $textoffset,
164 -y => $this->canvasheight - 5,
165 -width => $this->canvaswidth - 6,
166 -text => $this->{-path},
167 -bg => $this->{-bg},
168 -fg => $this->{-fg},
169 );
170  
171 $this->add(
172 'filelabel', 'Label',
173 -x => $labeloffset,
174 -y => $this->canvasheight - 4,
175 -text => $l_file,
176 -bg => $this->{-bg},
177 -fg => $this->{-fg},
178 );
179
180 if ($this->{-editfilename})
181 {
182 $this->add(
183 'filevalue', 'TextEntry',
184 -x => $textoffset,
185 -y => $this->canvasheight - 4,
186 -text => $this->{-file},
187 -width => 32,
188 -showlines => 1,
189 -border => 0,
190 -sbborder => 0,
191 -regexp => '/^[^\/]*$/',
192 -bg => $this->{-bg},
193 -fg => $this->{-fg},
194  
195 );
196 } else {
197 $this->add(
198 'filevalue', 'Label',
199 -x => $textoffset,
200 -y => $this->canvasheight - 4,
201 -text => $this->{-file},
202 -width => $this->canvaswidth - 6,
203 -bg => $this->{-bg},
204 -fg => $this->{-fg},
205 );
206 }
207  
208 if (defined $this->{-mask} and ref $this->{-mask} eq 'ARRAY')
209 {
210 $this->add(
211 'masklabel', 'Label',
212 -x => $labeloffset,
213 -y => $this->canvasheight - 2,
214 -text => $l_mask,
215 -bg => $this->{-bg},
216 -fg => $this->{-fg},
217  
218 );
219  
220 my @values = ();
221 my %labels = ();
222 my $i =0;
223 foreach my $mask (@{$this->{-mask}})
224 {
225 push @values, $mask->[0];
226 $labels{$mask->[0]} = $mask->[1];
227 }
228  
229 my $maskbox = $this->add(
230 'maskbox', 'Popupmenu',
231 -x => $textoffset,
232 -y => $this->canvasheight - 2,
233 -values => \@values,
234 -labels => \%labels,
235 -selected => $this->{-mask_selected},
236 -onchange => \&maskbox_onchange,
237 -bg => $this->{-bg},
238 -fg => $this->{-fg},
239  
240 );
241 $this->{-activemask} = $maskbox->get;
242 }
243  
244 $this->set_binding(sub{
245 my $this = shift;
246 $this->getobj('buttons')->{-selected} = 1;
247 $this->loose_focus;
248 }, CUI_ESCAPE);
249  
250 $this->layout();
251 $this->get_dir;
252  
253 if ($this->{-editfilename}) {
254 $this->getobj('filevalue')->focus;
255 } else {
256 $this->getobj('filebrowser')->focus;
257 }
258  
259 return bless $this, $class;
260 }
261  
262 sub layout()
263 {
264 my $this = shift;
265  
266 my $w = 60;
267 my $h = 18;
268 $h += 2 if defined $this->{-mask};
269 $this->{-width} = $w,
270 $this->{-height} = $h,
271  
272 $this->SUPER::layout() or return;
273  
274 return $this;
275 }
276  
277 sub get_dir()
278 {
279 my $this = shift;
280  
281 # Get pathvalue, filevalue, dirbrowser and filebrowser objects.
282 my $pv = $this->getobj('pathvalue');
283 my $db = $this->getobj('dirbrowser');
284 my $fb = $this->getobj('filebrowser');
285  
286 my $path = $pv->text;
287  
288 # Resolve path.
289 $path =~ s|/+|/|g;
290 my @path = split /\//, $path;
291 my @resolved = ();
292 foreach my $dir (@path)
293 {
294 if ($dir eq '.') { next }
295 elsif ($dir eq '..') { pop @resolved if @resolved }
296 else { push @resolved, $dir }
297 }
298 $path = join "/", @resolved;
299
300 # Catch totally bogus paths.
301 if (not -d $path) { $path = "/" }
302
303 $pv->text($path);
304
305 my @dirs = ();
306 my @files = ();
307 unless (opendir D, $path)
308 {
309 my $l = $this->root->lang();
310 my $error = $l->get('file_err_opendir_pre')
311 . $path
312 . $l->get('file_err_opendir_post')
313 . ":\n$!";
314 $this->root->error($error);
315 return;
316 }
317 foreach my $f (sort readdir D)
318 {
319 next if $f =~ /^\.$|^\.\.$/;
320 next if $f =~ /^\./ and not $this->{-show_hidden};
321 push @dirs, $f if -d "$path/$f";
322 if (-f "$path/$f")
323 {
324 $this->{-activemask} = '.'
325 unless defined $this->{-activemask};
326 push @files, $f if $f =~ /$this->{-activemask}/i;
327 }
328 }
329 closedir D;
330  
331 unshift @dirs, ".." if $path ne '/';
332
333 $db->values(\@dirs);
334 $db->{-ypos} = $this->{-selected_cache}->{$path};
335 $db->{-ypos} = 0 unless defined $db->{-ypos};
336 $db->{-selected} = undef;
337 $db->layout_content->draw(1);
338  
339 $fb->values(\@files);
340 $fb->{-ypos} = $fb->{-yscrpos} = 0;
341 $fb->layout_content->draw(1);
342
343 return $this;
344 }
345  
346 # Set $this->{-path} to the homedirectory of the current user.
347 sub goto_homedirectory()
348 {
349 my $this = shift;
350  
351 my @pw = getpwuid($>);
352 if (@pw) {
353 if (-d $pw[7]) {
354 $this->{-path} = $pw[7];
355 } else {
356 $this->{-path} = '/';
357 $this->root->error("Homedirectory $pw[7] not found");
358 return;
359 }
360 } else {
361 $this->{-path} = '/';
362 $this->root->error("Can't find a passwd entry for uid $>");
363 return;
364 }
365  
366 return $this;
367 }
368  
369 sub select_homedirectory()
370 {
371 my $b = shift; # dir-/filebrowser
372 my $this = $b->parent;
373 my $pv = $this->getobj('pathvalue');
374  
375 $this->goto_homedirectory or return $b;
376 $pv->text($this->{-path});
377 $this->get_dir;
378  
379 return $b;
380 }
381  
382 sub dirselect()
383 {
384 my $db = shift; # dirbrowser
385 my $this = $db->parent;
386 my $fv = $this->getobj('filevalue');
387 my $pv = $this->getobj('pathvalue');
388  
389 # Find the new path.
390 my $add = $db->values->[$db->{-ypos}];
391 my $savepath = $pv->text;
392 $this->{-selected_cache}->{$savepath} = $db->{-ypos};
393 $pv->text("/$savepath/$add");
394  
395 # Clear the filename field if the filename
396 # may not be edited.
397 $fv->text('') unless $this->{-editfilename};
398  
399 # Get the selected directory.
400 unless ($this->get_dir) {
401 $pv->text($savepath);
402 }
403  
404 return $db;
405 }
406  
407 sub fileselect()
408 {
409 my $filebrowser = shift;
410 my $this = $filebrowser->parent;
411  
412 my $selected = $filebrowser->{-ypos};
413
414 my $file = $filebrowser->values->[$selected];
415  
416 if (defined $file) {
417 $this->{-file} = $file;
418 $this->getobj('filevalue')->text($file);
419 }
420 # TODO: find out if it is done by mouseclick. If yes, then do
421 # not change focus.
422 # Doubleclick may also select the file.
423 # $this->getobj('buttons')->focus;
424 }
425  
426 sub maskbox_onchange()
427 {
428 my $maskbox = shift;
429 my $this = $maskbox->parent;
430 $this->{-activemask} = $maskbox->get;
431 $this->get_dir;
432 return $maskbox;
433 }
434  
435 sub draw(;$)
436 {
437 my $this = shift;
438 my $no_doupdate = shift || 0;
439  
440 # Draw Window
441 $this->SUPER::draw(1) or return $this;
442  
443 $this->{-canvasscr}->noutrefresh();
444 doupdate() unless $no_doupdate;
445  
446 return $this;
447 }
448  
449 sub get()
450 {
451 my $this = shift;
452 if ($this->getobj('buttons')->get) {
453 my $file = $this->getobj('pathvalue')->get
454 . "/"
455 . $this->getobj('filevalue')->get;
456 $file =~ s|/+|/|g;
457 return $file;
458 } else {
459 return;
460 }
461 }
462  
463 sub press_button_callback()
464 {
465 my $buttons = shift;
466 my $this = $buttons->parent;
467 my $file = $this->get;
468  
469 my $ok_pressed = $buttons->get;
470 if ($ok_pressed and $file =~ m|/$|) {
471 my $l = $this->root->lang;
472 $this->root->error($l->get('file_err_nofileselected'));
473 return;
474 } else {
475 $this->loose_focus;
476 }
477 }
478  
479 1;
480  
481  
482 =pod
483  
484 =head1 NAME
485  
486 Curses::UI::Dialog::Filebrowser - Create and manipulate filebrowser dialogs
487  
488 =head1 CLASS HIERARCHY
489  
490 Curses::UI::Widget
491 |
492 +----Curses::UI::Container
493 |
494 +----Curses::UI::Window
495 |
496 +----Curses::UI::Dialog::Filebrowser
497  
498  
499  
500 =head1 SYNOPSIS
501  
502 use Curses::UI;
503 my $cui = new Curses::UI;
504 my $win = $cui->add('window_id', 'Window');
505  
506 # The hard way.
507 # -------------
508 my $dialog = $win->add(
509 'mydialog', 'Dialog::Filebrowser'
510 );
511 $dialog->focus;
512 my $file = $dialog->get();
513 $win->delete('mydialog');
514  
515 # The easy way (see Curses::UI documentation).
516 # --------------------------------------------
517 $file = $cui->filebrowser();
518 $file = $cui->loadfilebrowser();
519 $file = $cui->savefilebrowser();
520  
521  
522  
523  
524 =head1 DESCRIPTION
525  
526 Curses::UI::Dialog::Filebrowser is a filebrowser dialog.
527 This type of dialog can be used to select a file, anywhere
528 on the filesystem.
529  
530 See exampes/demo-Curses::UI::Dialog::Filebrowser in the
531 distribution for a short demo.
532  
533  
534  
535 =head1 OPTIONS
536  
537 =over 4
538  
539 =item * B<-title> < TEXT >
540  
541 Set the title of the dialog window to TEXT.
542  
543 =item * B<-path> < PATH >
544  
545 Set the path to start with to PATH. If this path
546 does not exist, the filebrowser will start in the
547 rootdirectory.
548  
549 =item * B<-file> < FILE >
550  
551 Set the filename to start with to FILE.
552  
553 =item * B<-editfilename> < BOOLEAN >
554  
555 If BOOLEAN has a true value, the user may edit
556 the filename. This is for example useful for a
557 filebrowser that is used to select a filename to
558 save to. By default this option is set to false.
559  
560 =item * B<-show_hidden> < BOOLEAN >
561  
562 If BOOLEAN has a true value, hidden files (the filename
563 starts with a dot) will also be shown. By default this
564 option is set to false.
565  
566 =item * B<-mask> < ARRAYREF >
567  
568 If B<-mask> is defined, a filemask popupbox will be added
569 to the filebrowser dialog window. This popupbox will filter
570 the list of files that is displayed, using a regular expression
571 (case insensitive). The ARRAYREF contains a list of array
572 references. Each array reference has two elements: a regexp and
573 a description. Here's an example B<-mask>:
574  
575 my $mask = [
576 [ '.', 'All files (*)' ],
577 [ '\.txt$', 'Text files (*.txt)' ]
578 [ 'howto', 'HOWTO documentation' ],
579 [ 'core', 'Core files' ],
580 ];
581  
582 =item * B<-mask_selected> < INDEX >
583  
584 Normally the first mask in the list of masks will be made
585 active upon creation of the filebrowser. If you want
586 another mask to be active, use the B<-mask_selected>
587 option. Set this value to the index of the mask you want
588 to be active. For example: if you would want the "howto"
589 mask in the above example to be active, you would use
590 the value 2 for B<-mask_selected>.
591  
592 =back
593  
594  
595  
596  
597 =head1 METHODS
598  
599 =over 4
600  
601 =item * B<new> ( OPTIONS )
602  
603 =item * B<layout> ( )
604  
605 =item * B<draw> ( BOOLEAN )
606  
607 =item * B<focus> ( )
608  
609 These are standard methods. See L<Curses::UI::Container|Curses::UI::Container>
610 for an explanation of these.
611  
612 =item * B<get> ( )
613  
614 This method will return the complete path to the file that was
615 selected using the filebrowser. If no file was selected, this
616 method will return an undefined value.
617  
618 =back
619  
620  
621  
622 =head1 SPECIAL BINDINGS
623  
624 =over 4
625  
626 =item * B<escape>
627  
628 This will invoke the cancel button, so the filebrowser widget
629 returns without selecting any file.
630  
631 =item * B<~>
632  
633 If the directory- or filelistbox of the dialog window has the
634 focus and the tilde (~) button is pressed, the filebrowser
635 will chdir to the homedirectory of the current user.
636  
637 =back
638  
639  
640  
641 =head1 SEE ALSO
642  
643 L<Curses::UI|Curses::UI>,
644 L<Curses::UI::Container|Curses::UI::Container>,
645 L<Curses::UI::Buttonbox|Curses::UI::Buttonbox>
646  
647  
648  
649  
650 =head1 AUTHOR
651  
652 Copyright (c) 2001-2002 Maurice Makaay. All rights reserved.
653  
654 Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)
655  
656  
657 This package is free software and is provided "as is" without express
658 or implied warranty. It may be used, redistributed and/or modified
659 under the same terms as perl itself.
660