-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmime_types.pl
executable file
·76 lines (61 loc) · 1.75 KB
/
mime_types.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 10 January 2014
# https://trizenx.blogspot.com
# List the available categories and mime-types from .desktop files
# usage: perl mime_types.pl [Category]
use 5.016;
use strict;
use warnings;
my %opt;
if (@ARGV) {
require Getopt::Std;
Getopt::Std::getopts('hj', \%opt);
}
sub help {
print <<"EOF";
usage: $0 [options] [Category]
options:
-j : join the results with a semicolon (;)
-h : print this message and exit
example:
perl $0 # displays the available categories
perl $0 Audio # displays the Audio mime-types
perl $0 -j Video # displays the Video mime-types joined in one line
EOF
exit;
}
help() if $opt{h};
my @desktop_files = grep { /\.desktop\z/ }
glob('/usr/share/applications/*');
my %table;
foreach my $file (@desktop_files) {
sysopen(my $fh, $file, 0) || next;
sysread($fh, (my $content), -s $file);
if ((my $p = index($content, "\n[",
(my $i = index($content, '[Desktop Entry]') + 2**4))) != -1) {
$content = substr($content, $i, $p - $i);
}
my @cats = $content =~ /^Categories=(.+)/m ? split(/;/, $1) : ();
my @types = $content =~ /^MimeType=(.+)/m ? split(/;/, $1) : ();
foreach my $cat (@cats) {
@{$table{$cat}}{@types} = ();
}
}
{
{
local $\ = $opt{j} ? ';' : "\n";
if (@ARGV && exists $table{$ARGV[0]}) {
foreach my $type (sort keys %{$table{$ARGV[0]}}) {
print $type;
}
}
else {
foreach my $category (sort { fc($a) cmp fc($b) } keys %table) {
print $category;
}
}
}
$opt{j} && print "\n";
}