Debian Codegraph @

debian/menu-stuff/generate-flwm-menu-from-desktop-files

raw
1 #!/usr/bin/perl
2 #
3 # Script to generate an FLWM menu based on XDG .desktop files
4 #
5 # Copyright: 2017-2018 Axel Beckert <[email protected]>
6 # License: GPL-2+
7 # (See debian/copyright or /usr/share/doc/flwm/copyright for details.)
8
9 use strict;
10 use warnings;
11 use 5.010;
12 use autodie;
13
14 use File::DesktopEntry;
15 use Path::Tiny;
16 use File::Which;
17 use Set::Scalar;
18
19 my $desktop_dir = path('/usr/share/applications/');
20 my $target_dir = path('/var/lib/flwm/wmx/Free Desktop/');
21
22 # c.f. https://standards.freedesktop.org/menu-spec/latest/apa.html
23 my $main_categories = Set::Scalar->new(
24 qw(
25 AudioVideo
26 Audio
27 Video
28 Development
29 Education
30 Game
31 Graphics
32 Network
33 Office
34 Science
35 Settings
36 System
37 Utility
38 ));
39
40 my $desktop_categories = Set::Scalar->new(
41 qw(
42 KDE
43 GNOME
44 LXQt
45 XFCE
46 ));
47
48 my %useless_combination = (
49 'KDE/Qt' => 1,
50 'LXQt/Qt' => 1,
51 'GNOME/GTK' => 1,
52 'XFCE/GTK' => 1,
53 );
54
55 # Add /usr/games to $PATH because it's usually not in root's $PATH,
56 # but we need it for File::Which finding games under /usr/games/.
57 $ENV{PATH} = $ENV{PATH}.':/usr/games';
58
59 # Virginify target directory
60 $target_dir->remove_tree;
61 $target_dir->mkpath;
62
63 # Just exit gracefully if /usr/share/applications/ doesn't exist.
64 exit(0) unless $desktop_dir->exists;
65
66 # Get all .desktop files
67 my @desktop_files = $desktop_dir->children( qr{ \.desktop $ }x );
68
69 foreach my $desktop_file (@desktop_files) {
70 my $desktop_pt = path($desktop_file);
71
72 # Weed out broken symbolic links
73 next unless $desktop_pt->exists;
74
75 # Filter out localized non-UTF-8 values as they tend to make
76 # File::DesktopEntry to barf and we don't need them anyways.
77 my $desktop_raw = join('',
78 grep { !/\.(CP1251|KOI8-)/ }
79 $desktop_pt->lines);
80
81 # Finally parse the file
82 my $desktop_entry = File::DesktopEntry->new(\$desktop_raw);
83
84 # Skip files without Exec, Name or Type entry
85 next unless defined $desktop_entry->get('Exec');
86 next unless defined $desktop_entry->get('Type');
87 next unless defined $desktop_entry->get('Name');
88
89 # Skip files which aren't of Type=Application or have NoDisplay=true
90 next unless $desktop_entry->get('Type') eq 'Application';
91 my $nodisplay = $desktop_entry->get('NoDisplay');
92 next if defined($nodisplay) and $nodisplay eq 'true';
93
94 # Skip files where Exec wants a single file, URI or
95 # directory. (It's actually unclear in the specification if %f, %u
96 # and %u need exactly one parameter or only at most one
97 # parameter. Assuming the former and hence skipping such files.)
98 next if $desktop_entry->get('Exec') =~ m( \% [fud] )x;
99
100 my $program = $desktop_entry->parse_Exec();
101 my $name = $desktop_entry->get('Name');
102 my $categories = $desktop_entry->get('Categories') || 'Uncategorized';
103 my $terminal = $desktop_entry->get('Terminal') || 'false';
104
105 # Check name for suitability as file name and fix it if necessary.
106 $name =~ s{/}{-}g;
107
108 # The ConsoleOnly category is special and equivalent to Terminal=true
109 if ($categories =~ m/ \b ConsoleOnly \b /x or
110 $terminal eq 'true') {
111 $program = "x-terminal-emulator -e '$program'";
112 }
113
114 # Check if it's more than just a path, e.g. contains parameters
115 if ($program =~ m( ['"] )x or $program =~ m( \s )x) {
116 write_shell_script($categories, $name, $program, $desktop_file)
117 }
118
119 # Check if the program is already an absolute path or not
120 elsif ($program =~ m( ^ / )x) {
121 create_symlink($categories, $name, $program, $desktop_file);
122 } else {
123 my $path = which($program);
124 unless (defined $path) {
125 warn "$program from $desktop_file cannot be found in \$PATH, skipping";
126 next
127 }
128 create_symlink($categories, $name, $path, $desktop_file);
129 }
130 }
131
132 sub write_shell_script {
133 my ($categories, $name, $script, $desktop_file) = @_;
134
135 foreach my $cat (extract_categories($categories)) {
136 my $cat_dir = subdir($target_dir, $cat);
137 my $path = check_duplicates($cat_dir->child($name), $desktop_file);
138 $path->spew("#!/bin/sh\n", "exec $script\n");
139 $path->chmod(0755);
140 }
141 }
142
143 sub create_symlink {
144 my ($categories, $name, $target, $desktop_file) = @_;
145
146 foreach my $cat (extract_categories($categories)) {
147 my $cat_dir = subdir($target_dir, $cat);
148 symlink($target, check_duplicates($cat_dir->child($name),
149 $desktop_file));
150 }
151 }
152
153 sub extract_categories {
154 my $categories = shift;
155 my $result_categories = Set::Scalar->new();
156
157 # Condense multiple semicolons into one
158 $categories =~ s/ ;;+ /;/x;
159
160 # Remove trailing semicolons
161 $categories =~ s/ ; $ //x;
162
163 # Generate array of categories and filter them
164 my $all_categories = Set::Scalar->new(
165 # Blacklist any category starting with X-
166 grep { ! /^X-/ }
167 split(/;/, $categories)
168 );
169
170 # Check if a at least one main section is defined
171 my $included_main_categories =
172 $all_categories * ( $main_categories + $desktop_categories );
173 unless ($included_main_categories->is_empty) {
174 my $remaining_categories = $all_categories - $main_categories;
175 foreach my $main_cat ($included_main_categories->members) {
176 my $prefixed_categories = Set::Scalar->new(
177 map { "$main_cat/$_" }
178 grep {
179 $main_cat ne $_ and
180 !exists($useless_combination{"$main_cat/$_"})
181 }
182 $remaining_categories->members
183 );
184 $result_categories += $prefixed_categories;
185 }
186 } else {
187 $result_categories += $all_categories;
188 }
189
190 return $result_categories->members;
191 }
192
193 sub subdir {
194 my ($target_dir, $cat) = @_;
195
196 my $cat_dir = $target_dir->child($cat);
197 $cat_dir->mkpath;
198
199 return $cat_dir;
200 }
201
202 sub check_duplicates {
203 my ($path, $desktop_file) = @_;
204 if (-e $path) {
205 $path = $path->sibling($path->basename .
206 ' (' .
207 $desktop_file->basename('.desktop') .
208 ')');
209 }
210
211 return $path;
212 }