Wrangler/Plugin/ColourLabels.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 102 CC 4 SCAR 15.1
CC
4
Cov
0%
file_context_menu
12.0
directory_listing
6.0
new
2.0
CRAP 4.5
1 package Wrangler::Plugin::ColourLabels;
2  
3 use strict;
4 use warnings;
5  
6 use Wx ('wxITEM_RADIO');
7  
8 our @osx_colours = ('None','Orange', 'Red', 'Yellow', 'Blue', 'Purple', 'Green', 'Gray');
9 our @osx_colour_values = (
10 [0,0,0], # "None"
11 [255,200,60], # ffc83c 2
12 [255,138,138], # ff8a8a 1
13 [255,240,40], # fff028 3
14 [150,210,255], # 96d2ff 5
15 [235,180,255], # ebb4ff 6
16 [190,240,40], # bef028 4
17 [200,200,200], # c8c8c8 7
18 );
19  
20 sub new { my $class = shift; return bless({ wrangler => $_[0] }, $class); }
21  
22 sub plugin_name { return 'ColourLabels'; }
23  
24 sub plugin_info { return "Adds colour labeling to FileBrowser. Colour labels are stored in the OS X compliant extended attribute (xattr) \"kMDItemFSLabel\" of files and directories."; }
25  
26 sub plugin_phases {
27 return { wrangler_startup => 1, file_context_menu => 1, directory_listing => 1 };
28 }
29  
30 sub wrangler_startup {
31 my $self = shift;
32  
33 $Wrangler::wishlist->{'Extended Attributes::kMDItemFSLabel'} = 1;
34  
35 return 1;
36 }
37  
38 sub file_context_menu {
39 my $self = shift;
40 my $menu = shift;
41 my $selections = shift;
42  
43 $self->{menu_entries} = [];
44  
45 my $menuItem = Wx::MenuItem->new($menu, -1, 'Label X (remove)', 'Label X (remove)', wxITEM_RADIO);
46 push(@{ $self->{menu_entries} }, [
47 $menuItem,
48 sub { $self->Delete($selections); },
49 sub { $menuItem->Check(1); }
50 ]);
51  
52 my $has_colour;
53 for(@$selections){
54 $has_colour = $_->{'Extended Attributes::kMDItemFSLabel'} if $_->{'Extended Attributes::kMDItemFSLabel'};
55 }
56  
57 for my $colourId (1..$#osx_colours){
58 my $menuItem = Wx::MenuItem->new($menu, -1, 'Label "'.$osx_colours[$colourId].'"', 'Label "'.$osx_colours[$colourId].'"', wxITEM_RADIO); # ."\t$colourId"
59  
60 push(@{ $self->{menu_entries} }, [
61 $menuItem,
62 sub { $self->Set($selections, $colourId); },
63 sub { $menuItem->Check(1) if $has_colour && $has_colour == $colourId; }
64 ]);
65 }
66  
67 return $self->{menu_entries};
68 }
69  
70 sub directory_listing {
71 my $self = shift;
72 my $filebrowser = shift;
73 my $itemId = shift;
74 my $richlist_item = shift;
75  
76 my $colourId = $richlist_item->{'Extended Attributes::kMDItemFSLabel'};
77 $filebrowser->SetItemBackgroundColour($itemId, Wx::Colour->new(@{ $osx_colour_values[$colourId] }) ) if $colourId;
78 }
79  
80 sub Set {
81 my $self = shift;
82 my $selections = shift;
83 my $colourId = shift;
84  
85 for(@$selections){
86 Wrangler::debug("Plugin::ColourLabels::Set: setting colourId:$colourId ($osx_colours[$colourId]) on $_->{'Filesystem::Path'}");
87 my $ok = $self->{wrangler}->{fs}->set_property($_->{'Filesystem::Path'}, 'Extended Attributes::kMDItemFSLabel', $colourId);
88 }
89 Wrangler::PubSub::publish('filebrowser.refresh');
90 }
91  
92 sub Delete {
93 my $self = shift;
94 my $selections = shift;
95 my $colourId = shift;
96  
97 for(@$selections){
98 Wrangler::debug("Plugin::ColourLabels::Set: deleting metadata-key 'Extended Attributes::kMDItemFSLabel'");
99 my $ok = $self->{wrangler}->{fs}->del_property($_->{'Filesystem::Path'}, 'Extended Attributes::kMDItemFSLabel');
100 }
101 Wrangler::PubSub::publish('filebrowser.refresh');
102 }
103  
104 1;
105  
106 __END__