MimeInfo/Applications.pm

filter: Statementstmt 51.5% 65 / 126 Branchbran 25.0% 15 / 60 Conditioncond 33.3% 11 / 33 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 64.7% 11 / 17 Podpod 100% 4 / 4 totaltotal 41.7% 106 / 254 CC 45 SCAR 31.4
CC
45
Cov
42%
_write_list
56.0
_default
44.5
mime_applications_set_default
30.0
CRAP 23.1
« MimeInfo/Rox.pm ↑ Summary MimeInfo.pm »
1 package File::MimeInfo::Applications;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::BaseDir qw/config_home config_dirs data_home data_dirs data_files/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::MimeInfo qw/mimetype_canon mimetype_isa/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::DesktopEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 require Exporter;
11  
12 3 our $VERSION = '0.37';
13  
14 3 our @ISA = qw(Exporter);
15 3 our @EXPORT = qw(
16 mime_applications mime_applications_all
17 mime_applications_set_default mime_applications_set_custom
18 );
19  
20 3 ▶print STDERR << 'EOT' unless data_files(qw/applications mimeinfo.cache/);
Branch: 1 branch
BranchTrueFalseTotal
unless data_files(("applications", "mimeinfo.cache")) 2 1 3
21 WARNING: You don't seem to have any mimeinfo.cache files.
22 Try running the update-desktop-database command. If you
23 don't have this command you should install the
24 desktop-file-utils package. This package is available from
25 http://freedesktop.org/wiki/Software/desktop-file-utils/
26 EOT
27  
28 2 ▶sub mime_applications {
Subroutine mime_applications: called CC=3 SCAR=11.4
Pod documented
29 2 ▶ croak "usage: mime_applications(MIMETYPE)" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 2 2
30 2 my $mime = mimetype_canon(shift @_);
31 2 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
32 2 ▶ return wantarray ? (_default($mime), _others($mime)) : _default($mime);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
33 }
34  
35 0 ▶sub mime_applications_all {
Subroutine mime_applications_all: not called CC=2 SCAR=17.9
Pod documented
36 0 ▶ croak "usage: mime_applications(MIMETYPE)" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
37 0 my $mime = shift;
38 0 return mime_applications($mime),
39 grep defined($_), map mime_applications($_), mimetype_isa($mime);
40 }
41  
42 0 ▶sub mime_applications_set_default {
Subroutine mime_applications_set_default: not called CC=5 SCAR=34.0
Pod documented
43 0 ▶ croak "usage: mime_applications_set_default(MIMETYPE, APPLICATION)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
44 unless @_ == 2;
45 0 my ($mimetype, $desktop_file) = @_;
46 (undef, undef, $desktop_file) =
47 File::Spec->splitpath($desktop_file->{file})
48 0 ▶ if ref $desktop_file;
Branch: 1 branch
BranchTrueFalseTotal
if ref $desktop_file 0 0 0
49 0 ▶ croak "missing desktop entry filename for application"
Branch: 1 branch
BranchTrueFalseTotal
unless length $desktop_file 0 0 0
50 unless length $desktop_file;
51 0 ▶ $desktop_file .= '.desktop' unless $desktop_file =~ /\.desktop$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $desktop_file =~ /\.desktop$/ 0 0 0
52 0 _write_list($mimetype, $desktop_file);
53 }
54  
55 0 ▶sub mime_applications_set_custom {
Subroutine mime_applications_set_custom: not called CC=4 SCAR=30.0
Pod documented
56 0 ▶ croak "usage: mime_applications_set_custom(MIMETYPE, COMMAND)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
57 unless @_ == 2;
58 0 my ($mimetype, $command) = @_;
59 0 $command =~ /(\w+)/;
60 0 ▶ my $word = $1 or croak "COMMAND does not contain a word !?";
Branch: 1 branch
BranchTrueFalseTotal
my $word = $1 or croak("COMMAND does not contain a word !?") 0 0 0
61  
62 # Algorithm to generate name copied from other implementations
63 0 my $i = 1;
64 0 my $desktop_file =
65 data_home('applications', $word.'-usercreated-'.$i.'.desktop');
66 0 ▶ while (-e $desktop_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $desktop_file) 0 0 0
67 0 $i++;
68 0 $desktop_file =
69 data_home('applications', $word.'-usercreated-'.$i.'.desktop');
70 }
71  
72 0 my $object = File::DesktopEntry->new();
73 0 $object->set(
74 Type => 'Application',
75 Name => $word,
76 NoDisplay => 'true',
77 Exec => $command,
78 );
79 0 my (undef, undef, $df) = File::Spec->splitpath($desktop_file);
80 0 _write_list($mimetype, $df); # creates dir if needed
81 0 $object->write($desktop_file);
82 0 return $object;
83 }
84  
85 2 ▶sub _default {
Subroutine _default: called CC=12 SCAR=37.9
86 2 my $mimetype = shift;
87  
88 2 my $user = config_home(qw/mimeapps.list/);
89 2 my $system = config_dirs(qw/mimeapps.list/);
90 2 my $deprecated = data_home(qw/applications mimeapps.list/);
91 2 my $distro = data_dirs(qw/applications mimeapps.list/);
92 2 my $legacy = data_home(qw/applications defaults.list/);
93  
94 2 ▶ unless ( ( -f $user
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $user || $system && -f $system || $deprecated && -f $deprecated || $distro && -f $distro || $legacy && -f $legacy and -r _) 2 0 2
Condition33%9/27
-f $user || $system && -f $system || $deprecated && -f $deprecated || $distro && -f $distro || $legacy && -f $legacy and -r _
!ll&&!rl&&r
200
-f $user or $system and -f $system or $deprecated and -f $deprecated or $distro and -f $distro or $legacy and -f $legacy
l!l&&r!l&&!r
002
-f $user or $system and -f $system or $deprecated and -f $deprecated or $distro and -f $distro
l!l&&r!l&&!r
002
-f $user or $system and -f $system or $deprecated and -f $deprecated
l!l&&r!l&&!r
002
-f $user or $system and -f $system
l!l&&r!l&&!r
002
$system and -f $system
!ll&&!rl&&r
200
$deprecated and -f $deprecated
!ll&&!rl&&r
020
$distro and -f $distro
!ll&&!rl&&r
200
$legacy and -f $legacy
!ll&&!rl&&r
020
MC/DC0%0/10
-f $user || $system && -f $system || $deprecated && -f $deprecated || $distro && -f $distro || $legacy && -f $legacy and -r _
-f $user $system -f $system $deprecated -f $deprecated $distro -f $distro $legacy -f $legacy -r _
Truth table2%1/48
A or B and C or D and E or F and G or H and I and J
A= -f $userB= $systemC= -f $systemD= $deprecatedE= -f $deprecatedF= $distroG= -f $distroH= $legacyI= -f $legacyJ= -r _
ABCDEFGHIJresult
00X0X0X0XX0
00X0X0X10X0
00X0X100XX0
00X0X1010X0
00X100X0XX0
00X100X10X0
00X10100XX0
00X101010X0
0100X0X0XX0
0100X0X10X0
0100X100XX0
0100X1010X0
010100X0XX0
010100X10X0
01010100XX0
010101010X0
1XXXXXXXX00
011XXXXXX00
00X11XXXX00
01011XXXX00
00X0X11XX00
00X1011XX00
0100X11XX00
0101011XX00
00X0X0X1100
00X0X101100
00X100X1100
00X10101100
0100X0X1100
0100X101100
010100X1100
01010101100
1XXXXXXXX11
011XXXXXX11
00X11XXXX11
01011XXXX11
00X0X11XX11
00X1011XX11
0100X11XX11
0101011XX11
00X0X0X1111
00X0X101111
00X100X1111
00X10101111
0100X0X1111
0100X101111
010100X1111
01010101111
95 || ($system && -f $system)
96 || ($deprecated && -f $deprecated)
97 || ($distro && -f $distro)
98 || ($legacy && -f $legacy) )
99 && -r _ ) {
100 2 return undef;
101 }
102  
103 0 $Carp::CarpLevel++;
104 0 my @paths = grep defined, ($mimetype, $user, $system, $deprecated, $distro, $legacy);
105 0 my @list = _read_list(@paths);
106 0 ▶ return undef if @list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 0 0 0 0
107 0 my $desktop_file = _find_file(reverse @list);
108 0 $Carp::CarpLevel--;
109  
110 0 return $desktop_file;
111 }
112  
113 2 ▶sub _others {
Subroutine _others: called CC=8 SCAR=21.6
114 2 my $mimetype = shift;
115  
116 2 $Carp::CarpLevel++;
117 2 my (@list, @done);
118 2 for my $dir (data_dirs('applications')) {
119 4 my $cache = File::Spec->catfile($dir, 'mimeinfo.cache');
120 4 ▶ next if grep {$_ eq $cache} @done;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $cache;} @done 2 2 4
121 2 push @done, $cache;
122 2 ▶ next unless -f $cache and -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $cache and -r _ 0 2 2
Condition33%1/3
-f $cache and -r _
!ll&&!rl&&r
002
MC/DC0%0/2
-f $cache and -r _
-f $cache -r _
Truth table33%1/3
A and B
A= -f $cacheB= -r _
ABresult
0X0
100
111
123 2 for (_read_list($mimetype, $cache)) {
124 2 my $file = File::Spec->catfile($dir, $_);
125 2 ▶ next unless -f $file and -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file and -r _ 0 2 2
Condition33%1/3
-f $file and -r _
!ll&&!rl&&r
002
MC/DC0%0/2
-f $file and -r _
-f $file -r _
Truth table33%1/3
A and B
A= -f $fileB= -r _
ABresult
0X0
100
111
126 2 push @list, File::DesktopEntry->new($file);
127 }
128 }
129 2 $Carp::CarpLevel--;
130  
131 2 return @list;
132 }
133  
134 2 ▶sub _read_list { # read list with "mime/type=foo.desktop;bar.desktop" format
Subroutine _read_list: called CC=6 SCAR=18.1
135 2 my $mimetype = shift;
136  
137 2 my @list;
138 my $succeeded;
139  
140 2 for my $file (@_) {
141 2 ▶ if (open LIST, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open LIST, "<", $file) 2 0 2
142 2 $succeeded = 1;
143 2 ▶ while (<LIST>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline LIST)) 6 2 8
144 6 ▶ /^\Q$mimetype\E=(.*)$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$mimetype\E=(.*)$/ or next 2 4 6
145 2 push @list, grep defined($_), split ';', $1;
146 }
147 2 close LIST;
148 }
149 }
150  
151 2 ▶ unless ($succeeded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($succeeded) 0 2 2
152 0 croak "Could not read any defaults, tried:\n" . join("\t\n", @_);
153 }
154  
155 2 return @list;
156 }
157  
158 0 ▶sub _write_list {
Subroutine _write_list: not called CC=7 SCAR=40.3
159 0 my ($mimetype, $desktop_file) = @_;
160 0 my $file = config_home(qw/mimeapps.list/);
161 0 my $text;
162 0 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
163 0 ▶ open LIST, '<', $file or croak "Could not read file: $file";
Branch: 1 branch
BranchTrueFalseTotal
open LIST, "<", $file or croak("Could not read file: $file") 0 0 0
164 0 ▶ while (<LIST>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline LIST)) 0 0 0
165 0 ▶ $text .= $_ unless /^\Q$mimetype\E=/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\Q$mimetype\E=/ 0 0 0
166 }
167 0 close LIST;
168 0 $text =~ s/[\n\r]?$/\n/; # just to be sure
169 }
170 else {
171 0 _mkdir($file);
172 0 $text = "[Default Applications]\n";
173 }
174  
175 0 ▶ open LIST, '>', $file or croak "Could not write file: $file";
Branch: 1 branch
BranchTrueFalseTotal
open LIST, ">", $file or croak("Could not write file: $file") 0 0 0
176 0 print LIST $text;
177 0 print LIST "$mimetype=$desktop_file;\n";
178 0 ▶ close LIST or croak "Could not write file: $file";
Branch: 1 branch
BranchTrueFalseTotal
close LIST or croak("Could not write file: $file") 0 0 0
179 }
180  
181 0 ▶sub _find_file {
Subroutine _find_file: not called CC=3 SCAR=24.8
182 0 my @list = @_;
183 0 for (@list) {
184 0 my $file = data_files('applications', $_);
185 0 ▶ return File::DesktopEntry->new($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 0 0
186 }
187 0 return undef;
188 }
189  
190 0 ▶sub _mkdir {
Subroutine _mkdir: not called CC=4 SCAR=30.0
191 0 my $dir = shift;
192 0 ▶ return if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 0 0 0
193  
194 0 my ($vol, $dirs, undef) = File::Spec->splitpath($dir);
195 0 my @dirs = File::Spec->splitdir($dirs);
196 0 my $path = File::Spec->catpath($vol, shift @dirs);
197 0 ▶ while (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 0 0 0
198 0 mkdir $path; # fails silently
199 0 $path = File::Spec->catdir($path, shift @dirs);
200 }
201  
202 0 ▶ die "Could not create dir: $path\n" unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 0 0
203 }
204  
205 3 1;
206  
207 __END__
« MimeInfo/Rox.pm ↑ Summary MimeInfo.pm »