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

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 238 CC 19 SCAR 38.5
CC
19
Cov
0%
get_dir
132.0
new
12.0
goto_homedirectory
12.0
CRAP 46.8
1 # ----------------------------------------------------------------------
2 # Curses::UI::Dialog::Dirbrowser
3 # (C) 2003 by Roberto De Leo based on work
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::Dirbrowser;
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.0';
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 -bg => -1,
44 -fg => -1,
45  
46 %userargs,
47  
48 -border => 1,
49 -centered => 1,
50 -titleinverse => 0,
51 -ipad => 1,
52 -selected_cache => {},
53 );
54  
55 # Does -path not contain a path? Then use the
56 # current working directory.
57 if (not defined $args{-path} or $args{-path} =~ /^\s*$/) {
58 $args{-path} = cwd;
59 }
60  
61 my $this = $class->SUPER::new(%args);
62 $this->layout();
63  
64 my $l = $this->root->lang;
65  
66 # Start at home? Goto the homedirectory of the current user
67 # if the -path is not defined.
68 $this->goto_homedirectory unless defined $this->{-path};
69  
70 my $buttons = $this->add(
71 'buttons', 'Buttonbox',
72 -y => -1,
73 -x => 0,
74 -width => undef,
75 -buttonalignment => 'middle',
76 -buttons => [ 'ok', 'cancel' ],
77 -bg => $this->{-bg},
78 -fg => $this->{-fg},
79 );
80  
81 # Let the window in which the buttons are loose focus
82 # if a button is pressed.
83 $buttons->set_routine( 'press-button', \&press_button_callback );
84  
85 my $one_up = $l->get('file_dirup');
86 my $dirbrowser = $this->add(
87 'dirbrowser', 'Listbox',
88 -y => 0,
89 -border => 1,
90 -width => $this->canvaswidth - 2,
91 -padbottom => 6,
92 -values => [],
93 -vscrollbar => 1,
94 -labels => { '..' => ".. ($one_up)" },
95 -bg => $this->{-bg},
96 -fg => $this->{-fg},
97 -bbg => $this->{-bg},
98 -bfg => $this->{-fg},
99  
100 );
101  
102 $dirbrowser->set_routine('option-select',\&dirselect);
103 $dirbrowser->set_routine('goto-homedirectory',\&select_homedirectory);
104 $dirbrowser->set_binding('goto-homedirectory', '~');
105  
106 # Get language specific data.
107 my $l_path = $l->get('file_path');
108 my $l_mask = $l->get('file_mask');
109 my $l_file = $l->get('file_file');
110 my $l_len = $l->get('file_labelsize');
111  
112 my $labeloffset = 1;
113 my $textoffset = $l_len + 2;
114  
115 $this->add(
116 'pathlabel', 'Label',
117 -x => $labeloffset,
118 -y => $this->canvasheight - 5,
119 -text => $l_path,
120 -bg => $this->{-bg},
121 -fg => $this->{-fg},
122 );
123 $this->add(
124 'pathvalue', 'Label',
125 -x => $textoffset,
126 -y => $this->canvasheight - 5,
127 -width => $this->canvaswidth - 6,
128 -text => $this->{-path},
129 -bg => $this->{-bg},
130 -fg => $this->{-fg},
131 );
132  
133 $this->set_binding(sub{
134 my $this = shift;
135 $this->getobj('buttons')->{-selected} = 1;
136 $this->loose_focus;
137 }, CUI_ESCAPE);
138  
139 $this->layout();
140 $this->get_dir;
141  
142 return bless $this, $class;
143 }
144  
145 sub layout()
146 {
147 my $this = shift;
148  
149 my $w = 50;
150 my $h = 18;
151 $this->{-width} = $w,
152 $this->{-height} = $h,
153  
154 $this->SUPER::layout() or return;
155  
156 return $this;
157 }
158  
159 sub get_dir()
160 {
161 my $this = shift;
162  
163 # Get pathvalue, filevalue, dirbrowser and filebrowser objects.
164 my $pv = $this->getobj('pathvalue');
165 my $db = $this->getobj('dirbrowser');
166  
167 my $path = $pv->text;
168  
169 # Resolve path.
170 $path =~ s|/+|/|g;
171 my @path = split /\//, $path;
172 my @resolved = ();
173 foreach my $dir (@path)
174 {
175 if ($dir eq '.') { next }
176 elsif ($dir eq '..') { pop @resolved if @resolved }
177 else { push @resolved, $dir }
178 }
179 $path = join "/", @resolved;
180
181 # Catch totally bogus paths.
182 if (not -d $path) { $path = "/" }
183
184 $pv->text($path);
185
186 my @dirs = ();
187 unless (opendir D, $path)
188 {
189 my $l = $this->root->lang();
190 my $error = $l->get('file_err_opendir_pre')
191 . $path
192 . $l->get('file_err_opendir_post')
193 . ":\n$!";
194 $this->root->error($error);
195 return;
196 }
197 foreach my $f (sort readdir D)
198 {
199 next if $f =~ /^\.$|^\.\.$/;
200 next if $f =~ /^\./ and not $this->{-show_hidden};
201 push @dirs, $f if -d "$path/$f";
202 }
203 closedir D;
204  
205 unshift @dirs, ".." if $path ne '/';
206  
207 $db->values(\@dirs);
208 $db->{-ypos} = $this->{-selected_cache}->{$path};
209 $db->{-ypos} = 0 unless defined $db->{-ypos};
210 $db->{-selected} = undef;
211 $db->layout_content->draw(1);
212  
213 return $this;
214 }
215  
216 # Set $this->{-path} to the homedirectory of the current user.
217 sub goto_homedirectory()
218 {
219 my $this = shift;
220  
221 my @pw = getpwuid($>);
222 if (@pw) {
223 if (-d $pw[7]) {
224 $this->{-path} = $pw[7];
225 } else {
226 $this->{-path} = '/';
227 $this->root->error("Homedirectory $pw[7] not found");
228 return;
229 }
230 } else {
231 $this->{-path} = '/';
232 $this->root->error("Can't find a passwd entry for uid $>");
233 return;
234 }
235  
236 return $this;
237 }
238  
239 sub select_homedirectory()
240 {
241 my $b = shift; # dir-/filebrowser
242 my $this = $b->parent;
243 my $pv = $this->getobj('pathvalue');
244  
245 $this->goto_homedirectory or return $b;
246 $pv->text($this->{-path});
247 $this->get_dir;
248  
249 return $b;
250 }
251  
252 sub dirselect()
253 {
254 my $db = shift; # dirbrowser
255 my $this = $db->parent;
256 my $pv = $this->getobj('pathvalue');
257  
258 # Find the new path.
259 my $add = $db->values->[$db->{-ypos}];
260 my $savepath = $pv->text;
261 $this->{-selected_cache}->{$savepath} = $db->{-ypos};
262 $pv->text("/$savepath/$add");
263  
264 # Get the selected directory.
265 unless ($this->get_dir) {
266 $pv->text($savepath);
267 }
268  
269 return $db;
270 }
271  
272 sub maskbox_onchange()
273 {
274 my $maskbox = shift;
275 my $this = $maskbox->parent;
276 $this->{-activemask} = $maskbox->get;
277 $this->get_dir;
278 return $maskbox;
279 }
280  
281 sub draw(;$)
282 {
283 my $this = shift;
284 my $no_doupdate = shift || 0;
285  
286 # Draw Window
287 $this->SUPER::draw(1) or return $this;
288  
289 $this->{-canvasscr}->noutrefresh();
290 doupdate() unless $no_doupdate;
291  
292 return $this;
293 }
294  
295 sub get()
296 {
297 my $this = shift;
298 if ($this->getobj('buttons')->get) {
299 my $file = $this->getobj('pathvalue')->get;
300 $file =~ s|/+|/|g;
301 return $file;
302 } else {
303 return;
304 }
305 }
306  
307 sub press_button_callback()
308 {
309 my $buttons = shift;
310 my $this = $buttons->parent;
311 my $file = $this->get;
312  
313 my $ok_pressed = $buttons->get;
314 if ($ok_pressed and $file =~ m|/$|) {
315 my $l = $this->root->lang;
316 $this->root->error($l->get('file_err_nofileselected'));
317 return;
318 } else {
319 $this->loose_focus;
320 }
321 }
322  
323 1;
324  
325  
326 =pod
327  
328 =head1 NAME
329  
330 Curses::UI::Dialog::Dirbrowser - Create and manipulate filebrowser dialogs
331  
332 =head1 CLASS HIERARCHY
333  
334 Curses::UI::Widget
335 |
336 +----Curses::UI::Container
337 |
338 +----Curses::UI::Window
339 |
340 +----Curses::UI::Dialog::Dirbrowser
341  
342  
343  
344 =head1 SYNOPSIS
345  
346 use Curses::UI;
347 my $cui = new Curses::UI;
348 my $win = $cui->add('window_id', 'Window');
349  
350 # The hard way.
351 # -------------
352 my $dialog = $win->add(
353 'mydialog', 'Dialog::Dirbrowser'
354 );
355 $dialog->focus;
356 my $file = $dialog->get();
357 $win->delete('mydialog');
358  
359 # The easy way (see Curses::UI documentation).
360 # --------------------------------------------
361 $file = $cui->filebrowser();
362 $file = $cui->loadfilebrowser();
363 $file = $cui->savefilebrowser();
364  
365  
366  
367  
368 =head1 DESCRIPTION
369  
370 Curses::UI::Dialog::Dirbrowser is a dirbrowser dialog.
371 This type of dialog can be used to select a directory, anywhere
372 on the filesystem.
373  
374 See exampes/demo-Curses::UI::Dialog::Dirbrowser in the
375 distribution for a short demo.
376  
377  
378  
379 =head1 OPTIONS
380  
381 =over 4
382  
383 =item * B<-title> < TEXT >
384  
385 Set the title of the dialog window to TEXT.
386  
387 =item * B<-path> < PATH >
388  
389 Set the path to start with to PATH. If this path
390 does not exist, the filebrowser will start in the
391 rootdirectory.
392  
393 =item * B<-show_hidden> < BOOLEAN >
394  
395 If BOOLEAN has a true value, hidden files (the filename
396 starts with a dot) will also be shown. By default this
397 option is set to false.
398  
399 =back
400  
401  
402  
403  
404 =head1 METHODS
405  
406 =over 4
407  
408 =item * B<new> ( OPTIONS )
409  
410 =item * B<layout> ( )
411  
412 =item * B<draw> ( BOOLEAN )
413  
414 =item * B<focus> ( )
415  
416 These are standard methods. See L<Curses::UI::Container|Curses::UI::Container>
417 for an explanation of these.
418  
419 =item * B<get> ( )
420  
421 This method will return the complete path to the file that was
422 selected using the filebrowser. If no file was selected, this
423 method will return an undefined value.
424  
425 =back
426  
427  
428  
429 =head1 SPECIAL BINDINGS
430  
431 =over 4
432  
433 =item * B<escape>
434  
435 This will invoke the cancel button, so the filebrowser widget
436 returns without selecting any file.
437  
438 =item * B<~>
439  
440 If the directory- or filelistbox of the dialog window has the
441 focus and the tilde (~) button is pressed, the filebrowser
442 will chdir to the homedirectory of the current user.
443  
444 =back
445  
446  
447  
448 =head1 SEE ALSO
449  
450 L<Curses::UI|Curses::UI>,
451 L<Curses::UI::Container|Curses::UI::Container>,
452 L<Curses::UI::Buttonbox|Curses::UI::Buttonbox>
453  
454  
455  
456  
457 =head1 AUTHOR
458  
459 Copyright (c) 2001-2002 Maurice Makaay. All rights reserved.
460  
461 Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)
462  
463  
464 This package is free software and is provided "as is" without express
465 or implied warranty. It may be used, redistributed and/or modified
466 under the same terms as perl itself.
467