Wrangler/Wx/Sidebar.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 177 CC 13 SCAR 27.7
CC
13
Cov
0%
gtkbookmarks
42.0
new
20.0
Populate
20.0
CRAP 16.0
1 package Wrangler::Wx::Sidebar;
2  
3 use strict;
4 use warnings;
5  
6 use base qw(Wx::TreeCtrl);
7  
8 use Wx qw(:treectrl wxDefaultPosition wxDefaultSize);
9 use Wx::Event qw(EVT_TREE_SEL_CHANGING EVT_TREE_ITEM_RIGHT_CLICK EVT_RIGHT_UP EVT_MENU EVT_LEFT_UP);
10 use Encode;
11 use File::Basename ();
12 use File::Spec ();
13 use Sys::Filesystem ();
14  
15 sub new {
16 my $class = shift;
17 my $parent = shift;
18 my $self = $class->SUPER::new( $parent, -1, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT|wxTR_NO_LINES|wxTR_HAS_BUTTONS );
19  
20 # hook-up access to $wrangler
21 $self->{wrangler} = $parent->{wrangler};
22  
23 # treelist is set-up centrally in Main; link data strctures here
24 $self->SetImageList( ${ $parent->{imagelist} } );
25 $self->{images} = $parent->{images};
26  
27 # ## what other Sidebars look like:
28 # # Linux: http://upload.wikimedia.org/wikipedia/commons/2/28/Nautilus-3.10.0.png
29 # # Linux: http://upload.wikimedia.org/wikipedia/commons/6/64/4.2_Konqueror_Filebrowser.png
30 # # Mac: https://web.archive.org/web/20130329080038/http://osxhelp.com/customizing-the-finder-sidebar-in-os-x
31 # # Mac: http://upload.wikimedia.org/wikipedia/en/2/23/Finder_Lion.png
32 # # WinXP: http://upload.wikimedia.org/wikipedia/en/c/c0/Windows_Explorer_XP.png
33 # # Win7: http://upload.wikimedia.org/wikipedia/en/c/cb/Windows_Explorer_Windows_7.png
34  
35 $self->SetForegroundColour(Wx::Colour->new(@{ $self->{wrangler}->config()->{'ui.foreground_colour'} })) if $self->{wrangler}->config()->{'ui.foreground_colour'};
36 $self->SetBackgroundColour( $parent->GetBackgroundColour() );
37  
38 ## init TreeCtrl, add a hidden root node
39 $self->{root} = $self->AddRoot('Wrangler Sidebar');
40 $self->SetIndent(7);
41  
42 $self->Populate();
43  
44 ## hook up events
45 EVT_TREE_SEL_CHANGING( $self, $self, sub {
46 my $itemData = $_[0]->GetItemData( $_[1]->GetItem() );
47 unless($itemData){ # Category headings (Devices, Bookmarks,...) have no data associated
48 $_[1]->Veto();
49 return;
50 }
51 my $ref = $itemData->GetData();
52 Wrangler::debug("Sidebar::OnSelectionChanged: $ref->{path}");
53 # emit appropriate event
54 Wrangler::PubSub::publish('dir.activated', $ref->{path}) if $ref->{path};
55 });
56 EVT_LEFT_UP($self,sub { Wrangler::debug("OnLeftClick"); });
57 # todo EVT_TREE_ITEM_RIGHT_CLICK( $self, $self, sub { Wrangler::debug("Sidebar::OnItemRightClick: @_"); $self->PopupMenu( Wrangler::Sidebar::MenuRightClick->new( $self ), wxDefaultPosition ); } );
58 EVT_RIGHT_UP($self,sub { \&OnRightClick(@_); });
59  
60 return $self;
61 }
62  
63 sub Clear {
64 my ($sidebar) = @_;
65  
66 $sidebar->DeleteChildren($sidebar->{root});
67 }
68  
69 sub Populate {
70 my ($sidebar,$path) = @_;
71  
72 $sidebar->Clear();
73  
74 ## list mounts
75 my $devices_tree = $sidebar->AppendItem( $sidebar->{root}, 'Devices', -1, -1);
76 $sidebar->SetItemBold($devices_tree);
77 my $sf = Sys::Filesystem->new();
78 for( $sf->filesystems(regular => 1) ){
79 my $label = $sf->label($_);
80 my $mount_point = $sf->mount_point($_);
81 next if $mount_point eq '/dev';
82 my $id = $sidebar->AppendItem($devices_tree,
83 $label ? $label : $mount_point,
84 $sidebar->{images}->{device}, $sidebar->{images}->{device},
85 Wx::TreeItemData->new({
86 path => $mount_point,
87 label => $label ? $label : $mount_point,
88 })
89 );
90 }
91  
92 ## load .gtk-bookmarks
93 my $bookmarks_tree = $sidebar->AppendItem( $sidebar->{root}, 'Bookmarks', -1, -1);
94 $sidebar->SetItemBold($bookmarks_tree);
95 for ( $sidebar->gtkbookmarks() ){
96 my $itemId = $sidebar->AppendItem($bookmarks_tree,
97 $_->{label},
98 $sidebar->{images}->{folder}, $sidebar->{images}->{folder}, Wx::TreeItemData->new( $_ ) # we store the ref to this branch here
99 );
100 # $sidebar->SetItemImage( $itemId, 0, $sidebar->{images}->{folder});
101 }
102  
103 ## "places"
104 my $places_tree = $sidebar->AppendItem( $sidebar->{root}, 'Places', -1, -1);
105 $sidebar->SetItemBold($places_tree);
106 my @places = (
107 {
108 path => $ENV{HOME},
109 label => 'Home',
110 icon => 'go_home',
111 },
112 {
113 path => File::Spec->catfile($ENV{HOME}, '.local/share/Trash/files'),
114 label => 'Recycler',
115 icon => 'trash',
116 }
117 );
118 for(@places){
119 my $itemId = $sidebar->AppendItem($places_tree,
120 $_->{label},
121 $sidebar->{images}->{ $_->{icon} }, $sidebar->{images}->{ $_->{icon} },
122 Wx::TreeItemData->new($_)
123 );
124 }
125  
126 $sidebar->ExpandAll();
127 }
128  
129 sub OnRightClick {
130 my $sidebar = shift;
131 my $event = shift;
132  
133 my $itemId = $sidebar->GetSelection();
134  
135 my $menu = Wx::Menu->new();
136  
137 if( $itemId && $sidebar->GetItemData($itemId) ){
138 my $item = Wx::MenuItem->new($menu, -1, "Rename");
139 $menu->Append($item);
140 $menu->Enable($item->GetId(),0);
141 my $item_del = Wx::MenuItem->new($menu, -1, "Delete");
142 $menu->Append($item_del);
143 $menu->Enable($item_del->GetId(),0);
144 $menu->AppendSeparator();
145 }
146  
147 EVT_MENU( $sidebar, $menu->Append(-1, "Settings", 'Settings'), sub { Wrangler::PubSub::publish('show.settings', 0, 0); } );
148  
149 $sidebar->PopupMenu( $menu, wxDefaultPosition );
150 }
151  
152 sub gtkbookmarks {
153 my $self = shift;
154  
155 my $parent = Wx::wxTheApp->GetTopWindow;
156  
157 ## for now, simple checks, later we might add the dependency on File::HomeDir
158 return unless $ENV{HOME};
159  
160 my $path = File::Spec->catfile( $ENV{HOME}, '.config', 'gtk-3.0', 'bookmarks');
161 if( $parent->{wrangler}->{fs}->test('e', $path) ){
162 Wrangler::debug('Wx::Sidebar: found gtk-bookmarks file in gtk3 location');
163 }else{
164 $path = File::Spec->catfile($ENV{HOME}, '.gtk-bookmarks');
165 return unless $parent->{wrangler}->{fs}->test('e', $path);
166 Wrangler::debug('Wx::Sidebar: found gtk-bookmarks in legacy location');
167 }
168  
169 my @bookmarks;
170 open(my $fh, '<', $path) or die "Can't open gtk-bookmarks: $path: $!";
171 binmode($fh);
172 while(<$fh>) {
173 $_ = encode_utf8($_); # we assume the file to be in utf8
174 chomp;
175  
176 my ($path, $label) = split(/ /,$_, 2);
177 $path =~ s/file:\/\///;
178  
179 $path = url_decode($path);
180  
181 if(!$label){
182 $label = File::Basename::basename($path)
183 }
184  
185 push(@bookmarks, {
186 path => $path,
187 label => $label,
188 }) if $path && $parent->{wrangler}->{fs}->test('e', $path);
189 }
190 close $fh;
191  
192 return @bookmarks;
193 }
194  
195 ## borrowed from CGI_Lite
196 # sub url_encode {
197 # my $string = shift;
198 #
199 # $string =~ s/([\x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF])/
200 # sprintf ('%%%x', ord ($1))/eg;
201 #
202 # return $string;
203 # }
204 sub url_decode {
205 my $string = shift;
206  
207 $string =~ s/%([\da-fA-F]{2})/chr (hex ($1))/eg;
208  
209 return $string;
210 }
211  
212  
213 # package Wrangler::Sidebar::MenuRightClick;
214 #
215 # use strict;
216 # use base qw(Wx::Menu);
217 #
218 # use Wx::Event qw(EVT_MENU);
219 #
220 # sub new {
221 # my $class = shift;
222 # my $parent = shift;
223 # my $self = $class->SUPER::new();
224 #
225 # my $add_menu = Wx::Menu->new();
226 #
227 # ## define menu entries
228 # EVT_MENU( $parent, $self->Append(-1, "Add", 'Add place' ), sub { } );
229 # EVT_MENU( $parent, $self->Append(-1, "Delete\tDEL", 'Delete place' ), sub { } );
230 #
231 # return $self;
232 # }
233  
234 sub Destroy {
235 my $self = shift;
236  
237 Wrangler::PubSub::unsubscribe_owner(__PACKAGE__);
238  
239 $self->SUPER::Destroy();
240 }
241  
242 1;