blib/lib/Linux/DesktopFiles.pm

filter: Statementstmt 58.5% 58 / 99 Branchbran 30.6% 19 / 62 Conditioncond 21.2% 7 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 66.6% 4 / 6 Podpod 100% 5 / 5 totaltotal 41.3% 93 / 225 CC 51 SCAR 53.1
CC
51
Cov
43%
parse_desktop_file
291.4
get_desktop_files
90.0
parse_desktop_files
6.0
CRAP 202.7
↑ Summary Build »
1 package Linux::DesktopFiles;
2  
3 # This module is designed to be pretty fast.
4 # The best uses of this module is to generate real
5 # time menus, based on the content of desktop files.
6  
7 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #use strict;
10 #use warnings;
11  
12 2 our $VERSION = '0.25';
13  
14 2 our %TRUE_VALUES = (
15 'true' => 1,
16 'True' => 1,
17 '1' => 1
18 );
19  
20 sub new {
21 1 ▶ my ($class, %opt) = @_;
Subroutine new: called CC=4 SCAR=14.7
Pod documented
22  
23 my %data = (
24 keep_unknown_categories => 0,
25 unknown_category_key => 'Other',
26  
27 case_insensitive_cats => 0,
28  
29 skip_filename_re => undef,
30 skip_entry => undef,
31 substitutions => undef,
32  
33 1 ▶ terminal => (defined($opt{terminal}) ? undef : $ENV{TERM}),
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'terminal'} ? : 0 1 1
34  
35 terminalize => 0,
36 terminalization_format => q{%s -e '%s'},
37  
38 desktop_files_paths => [
39 qw(
40 /usr/local/share/applications
41 /usr/share/applications
42 )
43 ],
44  
45 keys_to_keep => [qw(Exec Name Icon)],
46  
47 categories => [
48 qw(
49 Utility
50 Development
51 Education
52 Game
53 Graphics
54 AudioVideo
55 Network
56 Office
57 Settings
58 System
59 )
60 ],
61  
62 %opt,
63 );
64  
65 1 $data{_file_keys_re} = do {
66 1 my %seen;
67 10 my @keys = map { quotemeta($_) } grep { !$seen{$_}++ }
68 1 ▶ (@{$data{keys_to_keep}}, qw(Hidden NoDisplay Categories), ($data{terminalize} ? qw(Terminal) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$data{'terminalize'} ? : 0 1 1
69  
70 1 local $" = q{|};
71 1 qr/^(@keys)=(.*\S)/m;
72 };
73  
74 1 ▶ if ($data{case_insensitive_cats}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'case_insensitive_cats'}) { } 0 1 1
75 0 @{$data{_categories}}{map { (lc $_) =~ tr/_a-z0-9/_/cr } @{$data{categories}}} = ();
76 }
77 else {
78 1 @{$data{_categories}}{@{$data{categories}}} = ();
79 }
80  
81 1 bless \%data, $class;
82 }
83  
84 sub get_desktop_files {
85 0 ▶ my ($self) = @_;
Subroutine get_desktop_files: not called CC=9 SCAR=45.0
Pod documented
86  
87 0 my %table;
88 0 foreach my $dir (@{$self->{desktop_files_paths}}) {
89 0 ▶ opendir(my $dir_h, $dir) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir_h, $dir or next 0 0 0
90  
91 #<<<
92 0 ▶ my $is_local = (
Condition0%0/3
index($dir, '/local/') != -1 || index($dir, '/.local/') != -1
l!l&&r!l&&!r
000
MC/DC0%0/2
index($dir, '/local/') != -1 || index($dir, '/.local/') != -1
index($dir, '/local/') != -1 index($dir, '/.local/') != -1
Truth table0%0/3
A || B
A= index($dir, '/local/') != -1B= index($dir, '/.local/') != -1
ABresult
1X1
011
000
93 index($dir, '/local/') != -1
94 or index($dir, '/.local/') != -1
95 );
96 #>>>
97  
98 0 foreach my $file (readdir $dir_h) {
99 0 ▶ if (substr($file, -8) eq '.desktop') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($file, -8) eq ".desktop") 0 0 0
100 0 ▶ if ($is_local or not exists($table{$file})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_local or not exists $table{$file}) 0 0 0
Condition0%0/3
$is_local or not exists $table{$file}
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_local or not exists $table{$file}
$is_local not exists $table{$file}
Truth table0%0/3
A or not(B)
A= $is_localB= not exists $table{$file}
ABresult
1X1
011
000
101 0 $table{$file} = "$dir/$file";
102 }
103 }
104 }
105 }
106  
107 0 ▶ wantarray ? values(%table) : [values(%table)];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
108 }
109  
110 # Used for unescaping strings
111 2 my %Chr = (s => ' ', n => "\n", r => "\r", t => "\t", '\\' => '\\');
112  
113 sub parse_desktop_file {
114 2 ▶ my ($self, $desktop_file) = @_;
Subroutine parse_desktop_file: called CC=36 SCAR=56.7
Pod documented
115  
116 # Check the filename and skip it if it matches `skip_filename_re`
117 2 ▶ if (defined $self->{skip_filename_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"skip_filename_re"}) 0 2 2
118 0 ▶ substr($desktop_file, rindex($desktop_file, '/') + 1) =~ /$self->{skip_filename_re}/ && return;
Branch: 1 branch
BranchTrueFalseTotal
substr($desktop_file, rindex($desktop_file, "/") + 1) =~ /$self->{"skip_filename_re"}/u and return 0 0 0
119 }
120  
121 # Open and read the desktop file
122 2 ▶ sysopen my $desktop_fh, $desktop_file, 0 or return;
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $desktop_fh, $desktop_file, 0 or return 0 2 2
123 2 sysread $desktop_fh, (my $file), -s $desktop_file;
124  
125 # Locate the "[Desktop Entry]" section
126 2 ▶ if ((my $index = index($file, "]\n", index($file, "[Desktop Entry]") + 15)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $index = index($file, "]\n", index($file, "[Desktop Entry]") + 15)) != -1) 2 0 2
127 2 $file = substr($file, 0, $index);
128 }
129  
130 # Parse the entry data
131 2 my %info = $file =~ /$self->{_file_keys_re}/g;
132  
133 # Ignore the file when `NoDisplay` is true
134 2 ▶ if (exists $info{NoDisplay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info{"NoDisplay"}) 0 2 2
135 0 ▶ return if exists $TRUE_VALUES{$info{NoDisplay}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Linux::DesktopFiles::TRUE_VALUES{$info{"NoDisplay"}} 0 0 0
136 }
137  
138 # Ignore the file when `Hidden` is true
139 2 ▶ if (exists $info{Hidden}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info{"Hidden"}) 0 2 2
140 0 ▶ return if exists $TRUE_VALUES{$info{Hidden}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Linux::DesktopFiles::TRUE_VALUES{$info{"Hidden"}} 0 0 0
141 }
142  
143 # If no 'Name' entry is defined, create one with the name of the file
144 2 ▶ $info{Name} //= substr($desktop_file, rindex($desktop_file, '/') + 1, -8);
Condition33%1/3
$info{"Name"} //= substr($desktop_file, rindex($desktop_file, "/") + 1, -8)
l!l&&r!l&&!r
200
MC/DC0%0/2
$info{"Name"} //= substr($desktop_file, rindex($desktop_file, "/") + 1, -8)
$info{"Name"} substr($desktop_file, rindex($desktop_file, "/") + 1, -8)
Truth table33%1/3
A //= B
A= $info{"Name"}B= substr($desktop_file, rindex($desktop_file, "/") + 1, -8)
ABresult
1X1
011
000
145  
146 # Unescape string escapes (\n, \t, etc.)
147 2 ▶ $info{$_} =~ s{\\(.)}{ $Chr{$1} // $1 }eg for (keys %info);
Condition0%0/3
$Chr{$1} // $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$Chr{$1} // $1
$Chr{$1} $1
Truth table0%0/3
A // B
A= $Chr{$1}B= $1
ABresult
1X1
011
000
148  
149 # Handle `skip_entry`
150 2 ▶ if (defined($self->{skip_entry}) and ref($self->{skip_entry}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"skip_entry"} and ref $self->{"skip_entry"} eq "ARRAY") 0 2 2
Condition33%1/3
defined $self->{"skip_entry"} and ref $self->{"skip_entry"} eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{"skip_entry"} and ref $self->{"skip_entry"} eq "ARRAY"
defined $self->{"skip_entry"} ref $self->{"skip_entry"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"skip_entry"}B= ref $self->{"skip_entry"} eq "ARRAY"
ABresult
0X0
100
111
151 0 foreach my $pair_ref (@{$self->{skip_entry}}) {
152 0 ▶ if (exists($info{$pair_ref->{key}}) and $info{$pair_ref->{key}} =~ /$pair_ref->{re}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info{$pair_ref->{"key"}} and $info{$pair_ref->{"key"}} =~ /$pair_ref->{"re"}/u) 0 0 0
Condition0%0/3
exists $info{$pair_ref->{"key"}} and $info{$pair_ref->{"key"}} =~ /$pair_ref->{"re"}/u
!ll&&!rl&&r
000
MC/DC0%0/2
exists $info{$pair_ref->{"key"}} and $info{$pair_ref->{"key"}} =~ /$pair_ref->{"re"}/u
exists $info{$pair_ref->{"key"}} $info{$pair_ref->{"key"}} =~ /$pair_ref->{"re"}/u
Truth table0%0/3
A and B
A= exists $info{$pair_ref->{"key"}}B= $info{$pair_ref->{"key"}} =~ /$pair_ref->{"re"}/u
ABresult
0X0
100
111
153 0 return;
154 }
155 }
156 }
157  
158 # Make user-defined substitutions
159 2 ▶ if (defined($self->{substitutions}) and ref($self->{substitutions}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"substitutions"} and ref $self->{"substitutions"} eq "ARRAY") 0 2 2
Condition33%1/3
defined $self->{"substitutions"} and ref $self->{"substitutions"} eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{"substitutions"} and ref $self->{"substitutions"} eq "ARRAY"
defined $self->{"substitutions"} ref $self->{"substitutions"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"substitutions"}B= ref $self->{"substitutions"} eq "ARRAY"
ABresult
0X0
100
111
160 0 foreach my $pair_ref (@{$self->{substitutions}}) {
161 0 ▶ if (exists $info{$pair_ref->{key}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info{$pair_ref->{"key"}}) 0 0 0
162 0 ▶ if ($pair_ref->{global}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair_ref->{'global'}) { } 0 0 0
163 0 $info{$pair_ref->{key}} =~ s/$pair_ref->{re}/$pair_ref->{value}/g;
164 }
165 else {
166 0 $info{$pair_ref->{key}} =~ s/$pair_ref->{re}/$pair_ref->{value}/;
167 }
168 }
169 }
170 }
171  
172 # Parse categories (and remove any duplicates)
173 2 my %categories;
174  
175 #<<<
176 @categories{
177 8 ▶ grep { exists $self->{_categories}{$_} } (
Branch: 1 branch
BranchTrueFalseTotal
$self->{'case_insensitive_cats'} ? : 0 2 2
178 $self->{case_insensitive_cats}
179 0 ▶ ? (map { lc($_) =~ tr/_a-z0-9/_/cr } split(/;/, $info{Categories} // ''))
Condition25%1/4
$info{"Categories"} // ""
l!l
00
$info{"Categories"} // ""
l!l
20
MC/DC0%0/1
$info{"Categories"} // ""
$info{"Categories"}
MC/DC0%0/1
$info{"Categories"} // ""
$info{"Categories"}
Truth table0%0/2
A
A= $info{"Categories"}
Aresult
11
00
Truth table50%1/2
A
A= $info{"Categories"}
Aresult
11
00
180 2 : (split(/;/, $info{Categories} // ''))
181 )
182 } = ();
183 #>>>
184  
185 2 my @cats = keys %categories;
186  
187 # Skip entry when there are no categories and `keep_unknown_categories` is false
188 # When `keep_unknown_categories` is true, set `@cats` to `unknown_category_key`.
189 2 ▶ if (!@cats) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cats) 0 2 2
190 0 ▶ if ($self->{keep_unknown_categories}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'keep_unknown_categories'}) { } 0 0 0
191 0 push @cats, $self->{unknown_category_key};
192 }
193 else {
194 0 return;
195 }
196 }
197  
198 # Store the categories
199 2 $info{Categories} = \@cats;
200  
201 # Remove `% ...` from the value of `Exec`
202 2 ▶ index($info{Exec}, ' %') != -1 and $info{Exec} =~ s/ +%.*//s;
Branch: 1 branch
BranchTrueFalseTotal
index($info{"Exec"}, " %") != -1 and $info{"Exec"} =~ s/ +%.*//su 2 0 2
203  
204 # Terminalize
205 2 ▶ if ( $self->{terminalize}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"terminalize"} and defined $info{"Terminal"} and exists $Linux::DesktopFiles::TRUE_VALUES{$info{"Terminal"}}) 0 2 2
Condition33%2/6
$self->{"terminalize"} and defined $info{"Terminal"} and exists $Linux::DesktopFiles::TRUE_VALUES{$info{"Terminal"}}
!ll&&!rl&&r
200
$self->{"terminalize"} and defined $info{"Terminal"}
!ll&&!rl&&r
200
MC/DC0%0/3
$self->{"terminalize"} and defined $info{"Terminal"} and exists $Linux::DesktopFiles::TRUE_VALUES{$info{"Terminal"}}
$self->{"terminalize"} defined $info{"Terminal"} exists $Linux::DesktopFiles::TRUE_VALUES{$info{"Terminal"}}
Truth table25%1/4
A and B and C
A= $self->{"terminalize"}B= defined $info{"Terminal"}C= exists $Linux::DesktopFiles::TRUE_VALUES{$info{"Terminal"}}
ABCresult
0XX0
10X0
1100
1111
206 and defined($info{Terminal})
207 and exists($TRUE_VALUES{$info{Terminal}})) {
208 0 $info{Exec} = sprintf($self->{terminalization_format}, $self->{terminal}, $info{Exec});
209 }
210  
211 # Check and clean the icon name
212 2 ▶ if (exists $info{Icon}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info{"Icon"}) 2 0 2
213 2 my $icon = $info{Icon};
214  
215 2 my $abs;
216 2 ▶ if (substr($icon, 0, 1) eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($icon, 0, 1) eq "/") 0 2 2
217 0 ▶ if (-f $icon) { # icon is specified as an absolute path
Branch: 1 branch
BranchTrueFalseTotal
if (-f $icon) { } 0 0 0
218 0 $abs = 1;
219 }
220 else { # otherwise, take its basename
221 0 $icon = substr($icon, 1 + rindex($icon, '/'));
222 }
223 }
224  
225 # Remove any icon extension
226 2 ▶ if (!$abs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($abs) 2 0 2
227 2 $icon =~ s/\.(?:png|jpe?g|svg|xpm)\z//i;
228 }
229  
230 # Store the icon back into `%info`
231 2 $info{Icon} = $icon;
232 }
233  
234 2 ▶ wantarray ? (%info) : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
235 }
236  
237 sub parse {
238 1 ▶ my ($self, $hash_ref, @desktop_files) = @_;
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
239  
240 1 foreach my $desktop_file (@desktop_files) {
241 1 ▶ my $entry = $self->parse_desktop_file($desktop_file) // next;
Condition50%1/2
$self->parse_desktop_file($desktop_file) // (next)
l!l
10
MC/DC0%0/1
$self->parse_desktop_file($desktop_file) // (next)
$self->parse_desktop_file($desktop_file)
Truth table50%1/2
A
A= $self->parse_desktop_file($desktop_file)
Aresult
11
00
242  
243 # Push the entry into its belonging categories
244 1 foreach my $category (@{$entry->{Categories}}) {
245 1 push @{$hash_ref->{$category}}, $entry;
246 }
247 }
248  
249 1 $hash_ref;
250 }
251  
252 sub parse_desktop_files {
253 0 ▶ my ($self) = @_;
Subroutine parse_desktop_files: not called CC=2 SCAR=17.9
Pod documented
254 0 my %categories;
255 0 $self->parse(\%categories, $self->get_desktop_files);
256 0 ▶ wantarray ? (%categories) : \%categories;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
257 }
258  
259 2 1;
260  
261 __END__
↑ Summary Build »