cookutils view fix-desktop-file @ rev 1019

cook: add fix() to use '--as-needed' linker flag in compile_rules(); cookit(): make QA fail on empty vars / bad values; remove_already_packed(): fix bug when $PACKAGE not listed in $SPLIT and we use this function for the default set. lighttpd/index.cgi: sort orphans. modules/precheck: separate error message by empty lines.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Dec 07 14:31:28 2017 +0200 (2017-12-07)
parents 5afd5252a5e7
children
line source
1 #!/bin/sh
2 #
3 # Description:
4 # -----------
5 # There is a utility that allows you to check a lot of mistakes in the
6 # '.desktop' files: `desktop-file-validate`.
7 # This utility, `fix-desktop-file`, allows you to correct most errors
8 # automatically.
9 #
10 # Using:
11 # -----
12 # fix-desktop-file /path/to/desktop-file.desktop
13 # All the changes are made at the place with the replacement of the original
14 # file.
15 #
16 # License:
17 # -------
18 # `fix-desktop-file` is a part of Cookutils suite for SliTaz GNU/Linux
19 # and distributed under the same license as the Cookutils.
20 #
21 # Useful links:
22 # ------------
23 # * <http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html>
24 # * <http://standards.freedesktop.org/menu-spec/menu-spec-1.0.html>
25 #
26 # Author:
27 # ------
28 # Aleksej Bobylev <al.bobylev@gmail.com>, November 2015.
31 desktop="$1"
33 busybox awk '
34 BEGIN {
35 FS = "=";
36 in_section = 0;
37 Name = "";
38 Comment = "";
39 Icon = "";
40 }
42 function handle_cats(x, y, yy) {
43 split(y, yy, ";");
44 for(i in yy) {
45 if (cats[x] && ! cats[yy[i]]) {
46 printf "%s;", yy[i];
47 cats[yy[i]] = "printed";
48 }
49 }
50 }
52 in_section == 1 {
53 # remove Version and deprecated keys
54 if ($0 ~ /^(Version|Protocols|Extensions|BinaryPattern|MapNotify|Patterns|DefaultApp|MiniIcon|TerminalOptions|Encoding|SwallowTitle|SwallowExec|SortOrder|FilePattern|MultipleArgs)[[:space:]]*=/)
55 next;
57 # process Icon
58 if ($0 ~ /^Icon[[:space:]]*=/) {
59 if (Icon) next; # Icon already printed
60 Icon = gensub(/^[^=]+=[[:space:]]*/, "", "");
61 # remove icon extension for non-absolute path
62 if ($0 ~ /^Icon[[:space:]]*=[^\/]*$/)
63 sub(/\.(png|xpm|svg)$/, "");
64 }
66 # fix boolean values
67 if ($0 ~ /^(NoDisplay|Hidden|DBusActivatable|Terminal|StartupNotify)[[:space:]]*=/) {
68 sub(/0/, "false"); sub(/False/, "false");
69 sub(/1/, "true"); sub(/True/, "true");
70 }
72 # process Name
73 if ($0 ~ /^Name[[:space:]]*=/) {
74 if (Name) next; # Name already printed
75 Name = gensub(/^[^=]+=[[:space:]]*/, "", "");
76 print;
77 next;
78 }
80 # process localized Names
81 if ($0 ~ /^Name\[[^\]+\][[:space:]]*=/) {
82 locale = gensub(/^[^\[]*\[([^\]+)\].*$/, "\\1", "");
83 value = gensub(/^[^=]*=[[:space:]]*(.*)$/, "\\1", "");
84 if (name[locale]) next; # Name[locale] already printed
85 name[locale] = value;
86 if (Name &&
87 Name != value) { # skip redundant
88 print;
89 }
90 next;
91 }
93 # process Comment
94 if ($0 ~ /^Comment[[:space:]]*=/) {
95 if (Comment) next; # Comment already printed
96 Comment = gensub(/^[^=]+=[[:space:]]*/, "", "");
97 if (Comment == Name) {
98 printf "%s (*)\n", $0; # Forgive redundant Comment: Comment[xx] required it
99 } else {
100 print;
101 }
102 next;
103 }
105 # process localized Comments
106 if ($0 ~ /^Comment\[[^\]+\][[:space:]]*=/) {
107 locale = gensub(/^[^\[]*\[([^\]+)\].*$/, "\\1", "");
108 locomm = gensub(/^[^=]*=[[:space:]]*(.*)$/, "\\1", "");
109 if (comment[locale]) next; # Comment[locale] already printed
110 comment[locale] = locomm;
111 if (tolower(locomm) != tolower(Comment) &&
112 tolower(locomm) != tolower(name[locale])) { # skip redundant
113 comment[locale] = locomm;
114 print;
115 }
116 next;
117 }
119 # process Categories list
120 if ($0 ~ /^Categories[[:space:]]*=/) {
121 value = gensub(/^[^=]*=[[:space:]]*(.*)$/, "\\1", "");
122 split(value, categories, ";");
123 printf "Categories=";
124 for (i in categories) {
125 # skip empty (;;), Application(s), and repeated categories
126 if (categories[i] &&
127 categories[i] != "Application" &&
128 categories[i] != "Applications" &&
129 ! cats[categories[i]]) {
130 if (categories[i] == "Terminal") categories[i] = "ConsoleOnly" # Mistake
131 if (categories[i] == "Multimedia") categories[i] = "AudioVideo" # Mistake
132 gsub(/ /, "", categories[i]);
133 printf "%s;", categories[i];
134 cats[categories[i]] = "printed";
135 }
136 }
137 # add main category if needed (http://standards.freedesktop.org/menu-spec/latest/apas02.html)
138 handle_cats("Audio", "AudioVideo");
139 handle_cats("Video", "AudioVideo");
140 handle_cats("Building", "Development");
141 handle_cats("Debugger", "Development");
142 handle_cats("IDE", "Development");
143 handle_cats("GUIDesigner", "Development");
144 handle_cats("Profiling", "Development");
145 handle_cats("RevisionControl", "Development");
146 handle_cats("Translation", "Development");
147 handle_cats("Calendar", "Office");
148 handle_cats("ContactManagement", "Office");
149 handle_cats("Chart", "Office");
150 handle_cats("Finance", "Office");
151 handle_cats("FlowChart", "Office");
152 handle_cats("PDA", "Office");
153 handle_cats("Presentation", "Office");
154 handle_cats("Spreadsheet", "Office");
155 handle_cats("WordProcessor", "Office");
156 handle_cats("2DGraphics", "Graphics");
157 handle_cats("VectorGraphics", "Graphics;2DGraphics");
158 handle_cats("RasterGraphics", "Graphics;2DGraphics");
159 handle_cats("3DGraphics", "Graphics");
160 handle_cats("Scanning", "Graphics");
161 handle_cats("OCR", "Graphics;Scanning");
162 handle_cats("TextTools", "Utility");
163 handle_cats("DesktopSettings", "Settings");
164 handle_cats("HardwareSettings", "Settings");
165 handle_cats("Printing", "HardwareSettings;Settings");
166 handle_cats("PackageManager", "Settings");
167 handle_cats("Dialup", "Network");
168 handle_cats("InstantMessaging", "Network");
169 handle_cats("Chat", "Network");
170 handle_cats("IRCClient", "Network");
171 handle_cats("Feed", "Network");
172 handle_cats("FileTransfer", "Network");
173 handle_cats("News", "Network");
174 handle_cats("P2P", "Network");
175 handle_cats("RemoteAccess", "Network");
176 handle_cats("Telephony", "Network");
177 handle_cats("TelephonyTools", "Utility");
178 handle_cats("VideoConference", "Network");
179 handle_cats("WebBrowser", "Network");
180 handle_cats("Midi", "AudioVideo;Audio");
181 handle_cats("Mixer", "AudioVideo;Audio");
182 handle_cats("Sequencer", "AudioVideo;Audio");
183 handle_cats("Tuner", "AudioVideo;Audio");
184 handle_cats("TV", "AudioVideo;Video");
185 handle_cats("DiscBurning", "AudioVideo");
186 handle_cats("ActionGame", "Game");
187 handle_cats("AdventureGame", "Game");
188 handle_cats("ArcadeGame", "Game");
189 handle_cats("BoardGame", "Game");
190 handle_cats("BlocksGame", "Game");
191 handle_cats("CardGame", "Game");
192 handle_cats("KidsGame", "Game");
193 handle_cats("LogicGame", "Game");
194 handle_cats("RolePlaying", "Game");
195 handle_cats("Shooter", "Game");
196 handle_cats("Simulation", "Game");
197 handle_cats("SportsGame", "Game");
198 handle_cats("StrategyGame", "Game");
199 handle_cats("Archiving", "Utility");
200 handle_cats("Compression", "Utility;Archiving");
201 handle_cats("FileManager", "System;FileTools");
202 handle_cats("TerminalEmulator", "System");
203 handle_cats("Filesystem", "System");
204 handle_cats("Calculator", "Utility");
205 handle_cats("Clock", "Utility");
206 handle_cats("TextEditor", "Utility");
207 handle_cats("KDE", "Qt");
208 handle_cats("GNOME", "GTK");
209 handle_cats("XFCE", "GTK");
211 printf "\n";
212 next;
213 }
215 # process MimeType list
216 if ($0 ~ /^MimeType[[:space:]]*=/) {
217 value = gensub(/^[^=]*=[[:space:]]*(.*)$/, "\\1", "");
218 value = gensub(/x-directory\/gnome-default-handler/, "inode/directory", "g", value);
219 value = gensub(/x-directory\/normal/, "inode/directory", "g", value);
220 gsub(/ /, "", value);
221 split(value, mimetype, ";");
222 printf "MimeType=";
223 for (i in mimetype) {
224 # skip empty (;;), and repeated mimetypes
225 if (mimetype[i] &&
226 ! mimes[mimetype[i]]) {
227 printf "%s;", mimetype[i];
228 mimes[mimetype[i]] = "printed";
229 }
230 }
231 printf "\n";
232 next;
233 }
235 # process Keywords
236 if ($0 ~ /^Keywords[[:space:]]*=/) {
237 value = gensub(/^[^=]*=[[:space:]]*(.*)$/, "\\1", "");
238 split(value, keywords, ";");
239 printf "Keywords=";
240 for (i in keywords) {
241 # skip empty (;;), and repeated keywords
242 if (keywords[i] &&
243 ! keys[keywords[i]]) {
244 printf "%s;", keywords[i];
245 keys[keywords[i]] = "printed";
246 }
247 }
248 printf "\n";
249 delete keys;
250 next;
251 }
253 # process localized Keywords
254 if ($0 ~ /^Keywords\[[^\]+\][[:space:]]*=/) {
255 locale = gensub(/^[^\[]*\[([^\]+)\].*$/, "\\1", "");
256 lokeys = gensub(/^[^=]*=[[:space:]]*(.*)$/, "\\1", "");
257 split(lokeys, keywords, ";");
258 printf "Keywords[%s]=", locale;
259 for (i in keywords) {
260 # skip empty (;;), and repeated keywords
261 if (keywords[i] &&
262 ! keys[keywords[i]]) {
263 printf "%s;", keywords[i];
264 keys[keywords[i]] = "printed";
265 }
266 }
267 printf "\n";
268 delete keys;
269 next;
270 }
272 }
274 /^\[/ {
275 in_section = 0;
276 }
277 /^\[Desktop Entry\]/ {
278 in_section = 1;
279 }
280 {
281 print;
282 }
283 ' "$desktop" > "$desktop.$$"
285 mv -f "$desktop.$$" "$desktop"