blib/lib/FreeDesktop/Icons.pm

filter: Statementstmt 86.7% 301 / 347 Branchbran 71.6% 116 / 162 Conditioncond 90.0% 27 / 30 MC/DCmcdc 85.0% 17 / 20 Subroutinesub 80.0% 28 / 35 Podpod 56.0% 14 / 25 totaltotal 81.2% 503 / 619 CC 118 SCAR 25.0
CC
118
Cov
82%
availableContexts
22.0
availableSizes
21.0
getGeneral
20.0
CRAP 12.1
↑ Summary
1 package FreeDesktop::Icons;
2  
3 =head1 NAME
4  
5 FreeDesktop::Icons - Use icon libraries quick & easy
6  
7 =cut
8  
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 $VERSION="0.04";
15 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 my $mswin = $Config{'osname'} eq 'MSWin32';
18  
19 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 my @extensions = (
22 '.jpg',
23 '.jpeg',
24 '.png',
25 '.gif',
26 '.xbm',
27 '.xpm',
28 '.svg',
29 );
30  
31 my %regsubs = (
32 52 ▶ 1 => sub { return shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 46 ▶ 2 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 46 my $test = shift;
35 46 $test =~ s/\-/_/g;
36 46 return $test;
37 },
38 43 ▶ 3 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 43 my $test = shift;
40 43 $test =~ s/\-//g;
41 43 return $test;
42 },
43 2 );
44  
45 2 my @defaulticonpath = ();
46 2 ▶if ($mswin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mswin) { } 0 2 2
47 0 push @defaulticonpath, $ENV{ALLUSERSPROFILE} . '\Icons'
48 } else {
49 2 my $local = $ENV{HOME} . '/.local/share/icons';
50 2 ▶ push @defaulticonpath, $local if -e $local;
Branch: 1 branch
BranchTrueFalseTotal
if -e $local 0 2 2
51 2 my $xdgpath = $ENV{XDG_DATA_DIRS};
52 2 ▶ if (defined $xdgpath) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xdgpath) 0 2 2
53 0 my @xdgdirs = split /\:/, $xdgpath;
54 0 for (@xdgdirs) {
55 0 push @defaulticonpath, "$_/icons";
56 }
57 }
58 }
59  
60 =head1 SYNOPSIS
61  
62 my $iconlib = new FreeDeskTop::Icons;
63 $iconlib->theme('Oxygen');
64 $iconlib->size('16');
65 my $imagefile = $iconlib->get('edit-copy');
66  
67 =head1 DESCRIPTION
68  
69 This module gives access to icon libraries on your system. It more
70 ore less conforms to the Free Desktop specifications here:
71 L<https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html>
72  
73 Furthermore it allows you to add your own icon folders through the B<rawpath> method.
74  
75 We have made provisions to make it work on Windows as well.
76  
77 The constructor takes a list of folders where it finds the icons
78 libraries. If you specify nothing, it will assign default values for:
79  
80 Windows: $ENV{ALLUSERSPROFILE} . '\Icons'. This package will not create
81 the folder if it does not exist. See also the README.md included in this distribution.
82  
83 Others: $ENV{HOME} . '/.local/share/icons', and the folder 'icons' in $ENV{XDG_DATA_DIRS}.
84  
85 =head1 METHODS
86  
87 =over 4
88  
89 =cut
90  
91 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
92 2 my $class = shift;
93 2 my $self = { };
94 2 bless $self, $class;
95  
96 2 $self->{CONTEXT} = undef;
97 2 $self->{ICONSIZE} = undef;
98 2 $self->{THEME} = undef;
99 2 $self->{THEMEPOOL} = {};
100 2 $self->{THEMES} = {};
101 2 $self->{RAWPATH} = [];
102 2 $self->{SIZE} = undef;
103  
104 2 my @iconpath = @_;
105 2 ▶ @iconpath = @defaulticonpath unless @iconpath;
Branch: 1 branch
BranchTrueFalseTotal
unless @iconpath 0 2 2
106 2 $self->CollectThemes(@iconpath);
107  
108 2 return $self;
109 }
110  
111  
112 =item B<availableContexts>I<($theme, >[ I<$name, $size> ] I<);>
113  
114 Returns a list of available contexts. If you set $name to undef if will return
115 all contexts of size $size. If you set $size to undef it will return all
116 contexts associated with icon $name. If you set $name and $size to undef it
117 will return all known contexts in the theme. out $size it returns a list
118 of all contexts found in $theme.
119  
120 =cut
121  
122 32 ▶sub availableContexts {
Subroutine availableContexts: called CC=22 SCAR=30.9
Pod documented
123 32 my ($self, $theme, $name, $size) = @_;
124 32 my $t = $self->getTheme($theme);
125 32 my %found = ();
126 32 ▶ if ((not defined $name) and (not defined $size)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $name || defined $size) { } 4 28 32
elsif (defined $name and not defined $size) { } 8 20 28
elsif (not defined $name and defined $size) { } 8 12 20
Condition100%3/3
defined $name || defined $size
l!l&&r!l&&!r
2084
MC/DC100%2/2
defined $name || defined $size
defined $name defined $size
Truth table100%3/3
A || B
A= defined $nameB= defined $size
ABresult
1X1
011
000
127 4 my @names = keys %$t;
128 4 for (@names) {
129 32 my $si = $t->{$_};
130 32 my @sizes = keys %$si;
131 32 for (@sizes) {
132 32 my $ci = $si->{$_};
133 32 for (keys %$ci) {
134 32 $found{$_} = 1;
135 }
136 }
137 }
138 ▶ } elsif ((defined $name) and (not defined $size)) {
Condition100%3/3
defined $name and not defined $size
!ll&&!rl&&r
8128
MC/DC100%2/2
defined $name and not defined $size
defined $name not defined $size
Truth table100%3/3
A and not(B)
A= defined $nameB= not defined $size
ABresult
0X0
100
111
139 8 for (1 .. 3) {
140 24 my $sub = $regsubs{$_};
141 24 my $test = &$sub($name);
142 24 ▶ if (exists $t->{$test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$test}) 2 22 24
143 2 my $si = $t->{$test};
144 2 my @sizes = keys %$si;
145 2 for (@sizes) {
146 2 my $ci = $si->{$_};
147 2 for (keys %$ci) {
148 2 $found{$_} = 1;
149 }
150 }
151 }
152 }
153 ▶ } elsif ((not defined $name) and (defined $size)) {
Condition66%2/3
not defined $name and defined $size
!ll&&!rl&&r
1208
MC/DC50%1/2
not defined $name and defined $size
not defined $name defined $size
Truth table66%2/3
not(A) and B
A= not defined $nameB= defined $size
ABresult
0X0
100
111
154 8 my @names = keys %$t;
155 8 for (@names) {
156 64 ▶ if (exists $t->{$_}->{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$_}{$size}) 16 48 64
157 16 my $ci = $t->{$_}->{$size};
158 16 for (keys %$ci) {
159 16 $found{$_} = 1;
160 }
161 }
162 }
163 } else {
164 12 for (1 .. 3) {
165 36 my $sub = $regsubs{$_};
166 36 my $test = &$sub($name);
167 36 ▶ if (exists $t->{$test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$test}) 4 32 36
168 4 my $si = $t->{$test};
169 4 ▶ if (exists $si->{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $si->{$size}) 2 2 4
170 2 my $ci = $si->{$size};
171 2 %found = %$ci;
172 }
173 }
174 }
175 }
176 32 my $parent = $self->parentTheme($theme);
177 32 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 16 16 32
178 16 my @contexts = $self->availableContexts($parent, $name, $size);
179 16 for (@contexts) {
180 8 $found{$_} = 1
181 }
182 }
183 32 return sort keys %found
184 }
185  
186 =item B<availableIcons>I<($theme, >[ I<$size, $context> ] I<);>
187  
188 Returns a list of available icons. If you set $size to undef the list will
189 contain names it found in all sizes. If you set $context to undef it will return
190 names it found in all contexts. If you leave out both then
191 you get a list of all available icons. Watch out, it might be pretty long.
192  
193 =cut
194  
195 32 ▶sub availableIcons {
Subroutine availableIcons: called CC=17 SCAR=28.3
Pod documented
196 32 my ($self, $theme, $size, $context) = @_;
197 32 my $t = $self->getTheme($theme);
198  
199 32 my @names = keys %$t;
200 32 my %matches = ();
201 32 ▶ if ((not defined $size) and (not defined $context)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $size || defined $context) { } 4 28 32
elsif (defined $size and not defined $context) { } 8 20 28
elsif (not defined $size and defined $context) { } 8 12 20
Condition100%3/3
defined $size || defined $context
l!l&&r!l&&!r
2084
MC/DC100%2/2
defined $size || defined $context
defined $size defined $context
Truth table100%3/3
A || B
A= defined $sizeB= defined $context
ABresult
1X1
011
000
202 4 %matches = %$t
203 ▶ } elsif ((defined $size) and (not defined $context)) {
Condition100%3/3
defined $size and not defined $context
!ll&&!rl&&r
8128
MC/DC100%2/2
defined $size and not defined $context
defined $size not defined $context
Truth table100%3/3
A and not(B)
A= defined $sizeB= not defined $context
ABresult
0X0
100
111
204 8 for (@names) {
205 64 ▶ if (exists $t->{$_}->{$size}) { $matches{$_} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$_}{$size}) 16 48 64
206 }
207 ▶ } elsif ((not defined $size) and (defined $context)) {
Condition66%2/3
not defined $size and defined $context
!ll&&!rl&&r
1208
MC/DC50%1/2
not defined $size and defined $context
not defined $size defined $context
Truth table66%2/3
not(A) and B
A= not defined $sizeB= defined $context
ABresult
0X0
100
111
208 8 for (@names) {
209 64 my $name = $_;
210 64 my $si = $t->{$name};
211 64 my @sizes = keys %$si;
212 64 for (@sizes) {
213 64 ▶ if (exists $t->{$name}->{$_}->{$context}) { $matches{$name} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$name}{$_}{$context}) 16 48 64
214 }
215 }
216 } else {
217 12 for (@names) {
218 96 ▶ if (exists $t->{$_}->{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$_}{$size}) 32 64 96
219 32 my $c = $t->{$_}->{$size};
220 32 ▶ if (exists $c->{$context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->{$context}) 8 24 32
221 8 $matches{$_} = 1
222 }
223 }
224 }
225 }
226 32 my $parent = $self->parentTheme($theme);
227 32 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 16 16 32
228 16 my @icons = $self->availableIcons($parent, $size, $context);
229 16 for (@icons) {
230 36 $matches{$_} = 1
231 }
232 }
233 32 return sort keys %matches
234 }
235  
236  
237 =item B<availableSizes>I<($theme, >[ I<$name, $context> ] I<);>
238  
239 Returns a list of available contexts. If you leave out $size it returns a list
240 of all contexts found in $theme.
241  
242 =cut
243  
244 32 ▶sub availableSizes {
Subroutine availableSizes: called CC=21 SCAR=30.5
Pod documented
245 32 my ($self, $theme, $name, $context) = @_;
246 32 my $t = $self->getTheme($theme);
247 32 ▶ return () unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 32 32
248  
249 32 my %found = ();
250 32 ▶ if ((not defined $name) and (not defined $context)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $name || defined $context) { } 4 28 32
elsif (defined $name and not defined $context) { } 8 20 28
elsif (not defined $name and defined $context) { } 8 12 20
Condition100%3/3
defined $name || defined $context
l!l&&r!l&&!r
2084
MC/DC100%2/2
defined $name || defined $context
defined $name defined $context
Truth table100%3/3
A || B
A= defined $nameB= defined $context
ABresult
1X1
011
000
251 4 my @names = keys %$t;
252 4 for (@names) {
253 32 my $si = $t->{$_};
254 32 my @sizes = keys %$si;
255 32 for (@sizes) {
256 32 $found{$_} = 1
257 }
258 }
259 ▶ } elsif ((defined $name) and (not defined $context)) {
Condition100%3/3
defined $name and not defined $context
!ll&&!rl&&r
8128
MC/DC100%2/2
defined $name and not defined $context
defined $name not defined $context
Truth table100%3/3
A and not(B)
A= defined $nameB= not defined $context
ABresult
0X0
100
111
260 8 for (1 .. 3) {
261 24 my $sub = $regsubs{$_};
262 24 my $test = &$sub($name);
263 24 ▶ if (exists $t->{$test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$test}) 2 22 24
264 2 my $si = $t->{$test};
265 2 %found = %$si;
266 }
267 }
268 ▶ } elsif ((not defined $name) and (defined $context)) {
Condition66%2/3
not defined $name and defined $context
!ll&&!rl&&r
1208
MC/DC50%1/2
not defined $name and defined $context
not defined $name defined $context
Truth table66%2/3
not(A) and B
A= not defined $nameB= defined $context
ABresult
0X0
100
111
269 8 my @names = keys %$t;
270 8 for (@names) {
271 64 my $n = $_;
272 64 my $si = $t->{$n};
273 64 my @sizes = keys %$si;
274 64 for (@sizes) {
275 64 ▶ if (exists $t->{$n}->{$_}->{$context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$n}{$_}{$context}) 16 48 64
276 16 $found{$_} = 1
277 }
278 }
279 }
280 } else {
281 12 for (1 .. 3) {
282 36 my $sub = $regsubs{$_};
283 36 my $test = &$sub($name);
284 36 ▶ if (exists $t->{$test}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$test}) 4 32 36
285 4 my $si = $t->{$test};
286 4 my @sizes = keys %$si;
287 4 for (@sizes) {
288 4 ▶ if (exists $t->{$test}->{$_}->{$context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{$test}{$_}{$context}) 2 2 4
289 2 $found{$_} = 1
290 }
291 }
292 }
293 }
294 }
295 32 my $parent = $self->parentTheme($theme);
296 32 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 16 16 32
297 16 my @sizes = $self->availableSizes($parent, $name, $context);
298 16 for (@sizes) {
299 8 $found{$_} = 1
300 }
301 }
302 32 delete $found{'unknown'};
303 32 return sort keys %found
304 }
305  
306 =item B<availableThemes>
307  
308 Returns a list of available themes it found while initiating the module.
309  
310 =cut
311  
312 2 ▶sub availableThemes {
Subroutine availableThemes: called CC=1 SCAR=0.0
Pod documented
313 2 my $self = shift;
314 2 my $k = $self->{THEMES};
315 2 return sort keys %$k
316 }
317  
318 0 ▶sub AvailableSizesCurrentTheme {
Subroutine AvailableSizesCurrentTheme: not called CC=1 SCAR=6.9
Pod undocumented
319 0 my $self = shift;
320 0 return $self->availableSizes($self->theme);
321 }
322  
323 2 ▶sub CollectThemes {
Subroutine CollectThemes: called CC=7 SCAR=19.6
Pod undocumented
324 2 my $self = shift;
325 2 my %themes = ();
326 2 for (@_) {
327 2 my $dir = $_;
328 2 ▶ if (opendir DIR, $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $dir) 2 0 2
329 2 ▶ while (my $entry = readdir(DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir DIR)) 10 2 12
330 10 my $fullname = "$dir/$entry";
331 10 ▶ if (-d $fullname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $fullname) 10 0 10
332 10 ▶ if (-e "$fullname/index.theme") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$fullname/index.theme") 6 4 10
333 6 my $index = $self->LoadThemeFile($fullname);
334 6 my $main = delete $index->{general};
335 6 my $name = $main->{'Name'};
336 6 ▶ if (%$index) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$index) 6 0 6
337 6 $themes{$name} = {
338 path => $fullname,
339 general => $main,
340 folders => $index,
341 }
342 }
343 }
344 }
345 }
346 2 closedir DIR;
347 }
348 }
349 2 $self->{THEMES} = \%themes
350 }
351  
352 =item B<context>I<(?$context?)>
353  
354 Set and return the preferred context to search in.
355  
356 =cut
357  
358 0 ▶sub context {
Subroutine context: not called CC=2 SCAR=17.9
Pod documented
359 0 my $self = shift;
360 0 ▶ $self->{CONTEXT} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
361 return $self->{CONTEXT}
362 0 }
363  
364 4 ▶sub CreateIndex {
Subroutine CreateIndex: called CC=8 SCAR=21.5
Pod undocumented
365 4 my ($self, $tindex) = @_;
366 4 my %index = ();
367 4 my $base = $tindex->{path};
368 4 my $folders = $tindex->{folders};
369 4 foreach my $dir (keys %$folders) {
370 2 ▶ my @raw = <"$base/$dir/*">;
Subroutine BEGIN: called CC=1 SCAR=0.0
371 16 foreach my $file (@raw) {
372 32 ▶ if ($self->IsImageFile($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->IsImageFile($file)) 32 0 32
373 32 my ($name, $d, $e) = fileparse($file, @extensions);
374 32 ▶ unless (exists $index{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $index{$name}) 32 0 32
375 32 $index{$name} = {}
376 }
377 32 my $size = $folders->{$dir}->{Size};
378 32 ▶ unless (defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 0 32 32
379 0 $size = 'unknown';
380 }
381 32 ▶ unless (exists $index{$name}->{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $index{$name}{$size}) 32 0 32
382 32 $index{$name}->{$size} = {}
383 }
384 32 my $context = $folders->{$dir}->{Context};
385 32 ▶ unless (defined $context) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context) 0 32 32
386 0 $context = 'unknown';
387 }
388 32 $index{$name}->{$size}->{$context} = $file;
389 }
390 }
391 }
392 4 return \%index;
393 }
394  
395 8 ▶sub FindImageC {
Subroutine FindImageC: called CC=3 SCAR=11.2
Pod undocumented
396 8 my ($self, $si, $context) = @_;
397 8 ▶ if (exists $si->{$context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $si->{$context}) { } 6 2 8
398 6 return $si->{$context}
399 } else {
400 2 my @contexts = sort keys %$si;
401 2 ▶ if (@contexts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@contexts) 2 0 2
402 2 return $si->{$contexts[0]};
403 }
404 }
405 return undef
406 0 }
407  
408 8 ▶sub FindImageS {
Subroutine FindImageS: called CC=6 SCAR=18.3
Pod undocumented
409 8 my ($self, $nindex, $size, $context, $resize) = @_;
410 8 ▶ if (exists $nindex->{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $nindex->{$size}) { } 6 2 8
411 6 my $file = $self->FindImageC($nindex->{$size}, $context);
412 6 ▶ if (defined $file) { return $file }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 6 0 6
413 } else {
414 2 ▶ if (defined $resize) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resize) 2 0 2
415 2 $$resize = 1;
416 2 my @sizes = reverse sort keys %$nindex;
417 2 for (@sizes) {
418 2 my $si = $nindex->{$_};
419 2 my $file = $self->FindImageC($si, $context);
420 2 ▶ if (defined $file) { return $file }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 2 0 2
421 }
422 }
423 }
424 return undef
425 0 }
426  
427 32 ▶sub FindLibImage {
Subroutine FindLibImage: called CC=11 SCAR=25.2
Pod undocumented
428 32 my ($self, $name, $size, $context, $resize, $theme) = @_;
429
430 32 ▶ $size = $self->size unless (defined $size);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 32 32
431 32 ▶ $context = $self->context unless (defined $context);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 0 32 32
432 32 ▶ $context = 'unknown' unless defined $context;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $context 0 32 32
433 32 ▶ $theme = $self->theme unless defined $theme;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $theme 19 13 32
434 32 ▶ unless (defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 0 32 32
435 0 warn "you must specify a size";
436 return undef
437 0 }
438 32 ▶ unless (defined $theme) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $theme) 0 32 32
439 0 warn "you must specify a theme";
440 return undef
441 0 }
442  
443 32 my $index = $self->getTheme($theme);
444 32 my $file;
445 32 ▶ $file = $self->FindImageS($index->{$name}, $size, $context, $resize,) if exists $index->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{$name} 8 24 32
446 32 ▶ return $file if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 8 24 32
447  
448 24 my $parent = $self->parentTheme($theme);
449 24 ▶ $file = $self->FindLibImage($name, $size, $context, $resize, $parent) if defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent 13 11 24
450 24 ▶ return $file if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 2 22 24
451  
452 22 return undef;
453 }
454  
455 21 ▶sub FindRawImage {
Subroutine FindRawImage: called CC=5 SCAR=16.1
Pod undocumented
456 21 my ($self, $name) = @_;
457 21 my $path = $self->{RAWPATH};
458 21 for (@$path) {
459 21 my $folder = $_;
460 21 opendir(DIR, $folder);
461 21 ▶ while (my $item = readdir(DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = readdir DIR)) 63 19 82
462 63 my $full = "$folder/$item";
463 63 ▶ if ($self->IsImageFile($full) and ($item =~ /^$name/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->IsImageFile($full) and $item =~ /^$name/) 2 61 63
Condition100%3/3
$self->IsImageFile($full) and $item =~ /^$name/
!ll&&!rl&&r
42192
MC/DC100%2/2
$self->IsImageFile($full) and $item =~ /^$name/
$self->IsImageFile($full) $item =~ /^$name/
Truth table100%3/3
A and B
A= $self->IsImageFile($full)B= $item =~ /^$name/
ABresult
0X0
100
111
464 2 closedir(DIR);
465 2 return $full
466 }
467 }
468 19 closedir(DIR);
469 }
470 return undef
471 19 }
472  
473 =item B<get>I<($name, ?$size?, ?$context?, ?\$resize?)>
474  
475 Returns the full filename of an image in the library. Finds the best suitable
476 version of the image in the library according to $size and $context. If you specify
477 \$resize B<get> will attempt to return an icon of a different size if it cannot find
478 the requested size. If it eventually returns an image of another size, it sets $resize
479 to 1. This gives the opportunity to scale the image to the requested icon size.
480 All parameters except $name are optional.
481  
482 =cut
483  
484 12 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
485 12 my ($self, $name, $size, $context, $resize) = @_;
486  
487 12 for (1 .. 3) {
488 21 my $sub = $regsubs{$_};
489 21 my $test = &$sub($name);
490 21 my $img = $self->FindRawImage($test);
491 21 ▶ return $img if defined $img;
Branch: 1 branch
BranchTrueFalseTotal
if defined $img 2 19 21
492 19 $img = $self->FindLibImage($test, $size, $context, $resize);
493 19 ▶ return $img if defined $img;
Branch: 1 branch
BranchTrueFalseTotal
if defined $img 8 11 19
494 }
495 2 return undef;
496 }
497  
498 =item B<getFolders>I<($theme)>
499  
500 Returns a reference to the folders hash defined in the theme.index of $theme.
501  
502 =cut
503  
504 0 ▶sub getFolders {
Subroutine getFolders: not called CC=3 SCAR=24.8
Pod documented
505 0 my ($self, $theme) = @_;
506 0 ▶ carp "undefined theme" unless defined $theme;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $theme 0 0 0
507 0 my $t = $self->{THEMES}->{$theme};
508 0 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) { } 0 0 0
509 0 return $t->{'folders'};
510 } else {
511 0 carp "theme '$theme' not found"
512 }
513 }
514  
515 =item B<getGeneral>I<($theme, ?$key?)>
516  
517 Returns a reference to the folders hash defined in the theme.index of $theme.
518 If you specify $key it will return the value of that key in the hash.
519  
520 =cut
521  
522 0 ▶sub getGeneral {
Subroutine getGeneral: not called CC=4 SCAR=30.0
Pod documented
523 0 my ($self, $theme, $key) = @_;
524 0 ▶ carp "undefined theme" unless defined $theme;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $theme 0 0 0
525 0 my $t = $self->{THEMES}->{$theme};
526 0 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) { } 0 0 0
527 0 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 0 0 0
528 0 return $t->{'general'}->{$key}
529 } else {
530 0 return $t->{'general'}
531 }
532 } else {
533 0 carp "theme '$theme' not found"
534 }
535 }
536  
537 =item B<getPath>I<($theme)>
538  
539 Returns the full path of the theme folder of $theme.
540  
541 =cut
542  
543 0 ▶sub getPath {
Subroutine getPath: not called CC=3 SCAR=24.8
Pod documented
544 0 my ($self, $theme) = @_;
545 0 ▶ carp "undefined theme" unless defined $theme;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $theme 0 0 0
546 0 my $t = $self->{THEMES}->{$theme};
547 0 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) { } 0 0 0
548 0 return $t->{'path'}
549 } else {
550 0 carp "theme '$theme' not found"
551 }
552 }
553  
554 =item B<getTheme>I<($theme)>
555  
556 Returns the theme data hash of I<$theme>.
557 Returns undef if I<$theme> is not found.
558  
559 =cut
560  
561 128 ▶sub getTheme {
Subroutine getTheme: called CC=3 SCAR=11.1
Pod documented
562 128 my ($self, $name) = @_;
563 128 my $pool = $self->{THEMEPOOL};
564 128 ▶ if (exists $pool->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pool->{$name}) { } 124 4 128
565 124 return $pool->{$name}
566 } else {
567 4 my $themindex = $self->{THEMES}->{$name};
568 4 ▶ if (defined $themindex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $themindex) { } 4 0 4
569 4 my $index = $self->CreateIndex($themindex);
570 4 $pool->{$name} = $index;
571 4 return $index
572 } else {
573 return undef
574 0 }
575 }
576 }
577  
578 95 ▶sub IsImageFile {
Subroutine IsImageFile: called CC=3 SCAR=11.2
Pod undocumented
579 95 my ($self, $file) = @_;
580 95 ▶ unless (-f $file) { return 0 } #It must be a file
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 42 53 95
581 53 my ($d, $f, $e) = fileparse(lc($file), @extensions);
582 53 ▶ if ($e ne '') { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($e ne "") 53 0 53
583 0 return 0
584 }
585  
586 6 ▶sub LoadThemeFile {
Subroutine LoadThemeFile: called CC=8 SCAR=21.0
Pod undocumented
587 6 my ($self, $file) = @_;
588 6 $file = "$file/index.theme";
589 6 ▶ if (open(OFILE, "<", $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open OFILE, '<', $file) { } 6 0 6
590 6 my %index = ();
591 6 my $section;
592 6 my %inf = ();
593 6 my $firstline = <OFILE>;
594 6 ▶ unless ($firstline =~ /^\[.+\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstline =~ /^\[.+\]$/) { } 6 0 6
595 0 warn "Illegal file format $file";
596 } else {
597 6 ▶ while (<OFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline OFILE)) 134 6 140
598 134 my $line = $_;
599 134 chomp $line;
600 134 ▶ if ($line =~ /^\[([^\]]+)\]/) { #new section
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[([^\]]+)\]/) { } 24 110 134
elsif ($line =~ s/^([^=]+)=//) { } 80 30 110
601 24 ▶ if (defined $section) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $section) { } 18 6 24
602 18 $index{$section} = { %inf }
603 } else {
604 6 $index{general} = { %inf }
605 }
606 24 $section = $1;
607 24 %inf = ();
608 } elsif ($line =~ s/^([^=]+)=//) { #new key
609 80 $inf{$1} = $line;
610 }
611 }
612 6 ▶ if (defined $section) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $section) 6 0 6
613 6 $index{$section} = { %inf }
614 }
615 6 close OFILE;
616 }
617 6 return \%index;
618 } else {
619 0 warn "Cannot open theme index file: $file"
620 }
621 }
622  
623 120 ▶sub parentTheme {
Subroutine parentTheme: called CC=1 SCAR=0.0
Pod undocumented
624 120 my ($self, $theme) = @_;
625 120 return $self->{THEMES}->{$theme}->{'general'}->{'Inherits'};
626 }
627  
628 =item B<rawpath>I<(?\@folders?)>
629  
630 Sets and returns a reference to a list of folders where raw icons can
631 be found.
632  
633 =cut
634  
635 2 ▶sub rawpath {
Subroutine rawpath: called CC=2 SCAR=7.1
Pod documented
636 2 my $self = shift;
637 2 ▶ $self->{RAWPATH} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
638 return $self->{RAWPATH}
639 2 }
640  
641 =item B<size>I<(?$size?)>
642  
643 Sets and returns the preferred size to search for.
644  
645 =cut
646  
647 0 ▶sub size {
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
648 0 my $self = shift;
649 0 ▶ $self->{SIZE} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
650 return $self->{SIZE}
651 0 }
652  
653 =item B<themeExists>I<(?$theme?)>
654  
655 returns a boolean.
656  
657 =cut
658  
659 0 ▶sub themeExists {
Subroutine themeExists: not called CC=1 SCAR=6.9
Pod documented
660 0 my ($self, $theme) = @_;
661 0 return exists $self->{THEMES}->{$theme}
662 }
663  
664 =item B<theme>I<(?$theme?)>
665  
666 Sets and returns the theme to search in.
667  
668 =cut
669  
670 21 ▶sub theme {
Subroutine theme: called CC=2 SCAR=6.9
Pod documented
671 21 my $self = shift;
672 21 ▶ $self->{THEME} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 19 21
673 return $self->{THEME}
674 21 }
675  
676 =back
677  
678 =head1 LICENSE
679  
680 Same as Perl.
681  
682 =head1 AUTHOR
683  
684 Hans Jeuken (hanje at cpan dot org)
685  
686 =head1 BUGS
687  
688 Unknown. If you find any, please contact the author.
689  
690 =cut
691  
692 2 1;
693  
694  
695  
696  
697  
698  
↑ Summary