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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
#include <config.h>
#include <glib.h>
#include <popt.h>
#include "desktop_file.h"
#include "validate.h"
#include <libintl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#define _(x) gettext ((x))
#define N_(x) x
static gboolean delete_original = FALSE;
static gboolean copy_generic_name_to_name = FALSE;
static gboolean copy_name_to_generic_name = FALSE;
static char *vendor_name = NULL;
static char *target_dir = NULL;
static GSList *added_categories = NULL;
static GSList *removed_categories = NULL;
static GSList *added_only_show_in = NULL;
static GSList *removed_only_show_in = NULL;
static GSList *removed_keys = NULL;
static mode_t permissions = 0644;
static gboolean
str_has_prefix (const char *str,
const char *prefix)
{
int str_len;
int prefix_len;
g_return_val_if_fail (str != NULL, FALSE);
g_return_val_if_fail (prefix != NULL, FALSE);
str_len = strlen (str);
prefix_len = strlen (prefix);
if (str_len < prefix_len)
return FALSE;
return strncmp (str, prefix, prefix_len) == 0;
}
static gboolean
files_are_the_same (const char *first,
const char *second)
{
/* This check gets confused by hard links.
* but it's too annoying to check if two
* paths are the same (though I'm sure there's a
* "path canonicalizer" I should be using...)
*/
struct stat first_sb;
struct stat second_sb;
if (stat (first, &first_sb) < 0)
{
g_printerr (_("Could not stat \"%s\": %s\n"), first, g_strerror (errno));
exit (1);
}
if (stat (second, &second_sb) < 0)
{
g_printerr (_("Could not stat \"%s\": %s\n"), first, g_strerror (errno));
exit (1);
}
return ((first_sb.st_dev == second_sb.st_dev) &&
(first_sb.st_ino == second_sb.st_ino) &&
/* Broken paranoia in case an OS doesn't use inodes or something */
(first_sb.st_size == second_sb.st_size) &&
(first_sb.st_mtime == second_sb.st_mtime));
}
static void
process_one_file (const char *filename,
GError **err)
{
char *new_filename;
char *dirname;
char *basename;
GnomeDesktopFile *df = NULL;
GSList *tmp;
g_assert (vendor_name);
dirname = g_path_get_dirname (filename);
basename = g_path_get_basename (filename);
if (!str_has_prefix (basename, vendor_name))
{
char *new_base;
new_base = g_strconcat (vendor_name, "-", basename, NULL);
new_filename = g_build_filename (target_dir, new_base, NULL);
g_free (new_base);
}
else
{
new_filename = g_build_filename (target_dir, basename, NULL);
}
g_free (dirname);
g_free (basename);
df = gnome_desktop_file_load (filename, err);
if (df == NULL)
goto cleanup;
if (!desktop_file_fixup (df, filename))
exit (1);
if (copy_name_to_generic_name)
gnome_desktop_file_copy_key (df, NULL, "Name", "GenericName");
if (copy_generic_name_to_name)
gnome_desktop_file_copy_key (df, NULL, "GenericName", "Name");
/* Mark file as having been processed by us, so automated
* tools can check that desktop files went through our
* munging
*/
gnome_desktop_file_set_raw (df, NULL, "X-Desktop-File-Install-Version", NULL, VERSION);
/* Add categories */
tmp = added_categories;
while (tmp != NULL)
{
gnome_desktop_file_merge_string_into_list (df, NULL, "Categories",
NULL, tmp->data);
tmp = tmp->next;
}
/* Remove categories */
tmp = removed_categories;
while (tmp != NULL)
{
gnome_desktop_file_remove_string_from_list (df, NULL, "Categories",
NULL, tmp->data);
tmp = tmp->next;
}
/* Add onlyshowin */
tmp = added_only_show_in;
while (tmp != NULL)
{
gnome_desktop_file_merge_string_into_list (df, NULL, "OnlyShowIn",
NULL, tmp->data);
tmp = tmp->next;
}
/* Remove onlyshowin */
tmp = removed_only_show_in;
while (tmp != NULL)
{
gnome_desktop_file_remove_string_from_list (df, NULL, "OnlyShowIn",
NULL, tmp->data);
tmp = tmp->next;
}
/* Remove keys */
tmp = removed_keys;
while (tmp != NULL)
{
gnome_desktop_file_unset (df, NULL, tmp->data);
tmp = tmp->next;
}
if (!gnome_desktop_file_save (df, new_filename,
permissions, err))
goto cleanup;
if (delete_original &&
!files_are_the_same (filename, new_filename))
{
if (unlink (filename) < 0)
g_printerr (_("Error removing original file \"%s\": %s\n"),
filename, g_strerror (errno));
}
gnome_desktop_file_free (df);
/* Load and validate the file we just wrote */
df = gnome_desktop_file_load (new_filename, err);
if (df == NULL)
goto cleanup;
if (!desktop_file_validate (df, new_filename))
{
g_printerr (_("desktop-file-install created an invalid desktop file!\n"));
exit (1);
}
cleanup:
g_free (new_filename);
if (df)
gnome_desktop_file_free (df);
}
static void parse_options_callback (poptContext ctx,
enum poptCallbackReason reason,
const struct poptOption *opt,
const char *arg,
void *data);
enum {
OPTION_VENDOR = 1,
OPTION_DIR,
OPTION_ADD_CATEGORY,
OPTION_REMOVE_CATEGORY,
OPTION_ADD_ONLY_SHOW_IN,
OPTION_REMOVE_ONLY_SHOW_IN,
OPTION_DELETE_ORIGINAL,
OPTION_MODE,
OPTION_COPY_NAME_TO_GENERIC_NAME,
OPTION_COPY_GENERIC_NAME_TO_NAME,
OPTION_REMOVE_KEY,
OPTION_LAST
};
struct poptOption options[] = {
{
NULL,
'\0',
POPT_ARG_CALLBACK,
parse_options_callback,
0,
NULL,
NULL
},
{
NULL,
'\0',
POPT_ARG_INCLUDE_TABLE,
poptHelpOptions,
0,
N_("Help options"),
NULL
},
{
"vendor",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_VENDOR,
N_("Specify the vendor prefix to be applied to the desktop file. If the file already has this prefix, nothing happens."),
NULL
},
{
"dir",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_DIR,
N_("Specify the directory where files should be installed."),
NULL
},
{
"add-category",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_ADD_CATEGORY,
N_("Specify a category to be added to the Categories field."),
NULL
},
{
"remove-category",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_REMOVE_CATEGORY,
N_("Specify a category to be removed from the Categories field."),
NULL
},
{
"add-only-show-in",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_ADD_ONLY_SHOW_IN,
N_("Specify a product name to be added to the OnlyShowIn field."),
NULL
},
{
"remove-only-show-in",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_REMOVE_ONLY_SHOW_IN,
N_("Specify a product name to be removed from the OnlyShowIn field."),
NULL
},
{
"delete-original",
'\0',
POPT_ARG_NONE,
&delete_original,
OPTION_DELETE_ORIGINAL,
N_("Delete the source desktop file, leaving only the target file. Effectively \"renames\" a desktop file."),
NULL
},
{
"mode",
'm',
POPT_ARG_STRING,
NULL,
OPTION_MODE,
N_("Set the given permissions on the destination file."),
NULL
},
{
"copy-name-to-generic-name",
'\0',
POPT_ARG_NONE,
©_name_to_generic_name,
OPTION_COPY_NAME_TO_GENERIC_NAME,
N_("Copy the contents of the \"Name\" field to the \"GenericName\" field."),
NULL
},
{
"copy-generic-name-to-name",
'\0',
POPT_ARG_NONE,
©_generic_name_to_name,
OPTION_COPY_GENERIC_NAME_TO_NAME,
N_("Copy the contents of the \"GenericName\" field to the \"Name\" field."),
NULL
},
{
"remove-key",
'\0',
POPT_ARG_STRING,
NULL,
OPTION_REMOVE_KEY,
N_("Specify a field to be removed from the desktop file."),
NULL
},
{
NULL,
'\0',
0,
NULL,
0,
NULL,
NULL
}
};
static void
parse_options_callback (poptContext ctx,
enum poptCallbackReason reason,
const struct poptOption *opt,
const char *arg,
void *data)
{
const char *str;
if (reason != POPT_CALLBACK_REASON_OPTION)
return;
switch (opt->val & POPT_ARG_MASK)
{
case OPTION_VENDOR:
if (vendor_name)
{
g_printerr (_("Can only specify --vendor once\n"));
exit (1);
}
str = poptGetOptArg (ctx);
vendor_name = g_strdup (str);
break;
case OPTION_DIR:
if (target_dir)
{
g_printerr (_("Can only specify --dir once\n"));
exit (1);
}
str = poptGetOptArg (ctx);
target_dir = g_strdup (str);
break;
case OPTION_ADD_CATEGORY:
str = poptGetOptArg (ctx);
added_categories = g_slist_prepend (added_categories,
g_strdup (str));
break;
case OPTION_REMOVE_CATEGORY:
str = poptGetOptArg (ctx);
removed_categories = g_slist_prepend (removed_categories,
g_strdup (str));
break;
case OPTION_ADD_ONLY_SHOW_IN:
str = poptGetOptArg (ctx);
added_only_show_in = g_slist_prepend (added_only_show_in,
g_strdup (str));
break;
case OPTION_REMOVE_ONLY_SHOW_IN:
str = poptGetOptArg (ctx);
removed_only_show_in = g_slist_prepend (removed_only_show_in,
g_strdup (str));
break;
case OPTION_MODE:
{
unsigned long ul;
char *end;
str = poptGetOptArg (ctx);
end = NULL;
ul = strtoul (str, &end, 8);
if (*str && end && *end == '\0')
permissions = ul;
else
{
g_printerr (_("Could not parse mode string \"%s\"\n"),
str);
exit (1);
}
}
break;
case OPTION_REMOVE_KEY:
str = poptGetOptArg (ctx);
removed_keys = g_slist_prepend (removed_keys,
g_strdup (str));
break;
default:
break;
}
}
int
main (int argc, char **argv)
{
poptContext ctx;
int nextopt;
GError* err = NULL;
const char** args;
int i;
setlocale (LC_ALL, "");
ctx = poptGetContext ("desktop-file-install", argc, (const char **) argv, options, 0);
poptReadDefaultConfig (ctx, TRUE);
while ((nextopt = poptGetNextOpt (ctx)) > 0)
/*nothing*/;
if (nextopt != -1)
{
g_printerr (_("Error on option %s: %s.\nRun '%s --help' to see a full list of available command line options.\n"),
poptBadOption (ctx, 0),
poptStrerror (nextopt),
argv[0]);
return 1;
}
if (vendor_name == NULL)
vendor_name = g_strdup (g_getenv ("DESKTOP_FILE_VENDOR"));
if (vendor_name == NULL)
{
g_printerr (_("Must specify the vendor namespace for these files with --vendor\n"));
return 1;
}
if (copy_generic_name_to_name && copy_name_to_generic_name)
{
g_printerr (_("Specifying both --copy-name-to-generic-name and --copy-generic-name-to-name at once doesn't make much sense.\n"));
return 1;
}
if (target_dir == NULL)
target_dir = g_strdup (g_getenv ("DESKTOP_FILE_INSTALL_DIR"));
if (target_dir == NULL)
target_dir = g_build_filename (DATADIR, "applications", NULL);
args = poptGetArgs (ctx);
i = 0;
while (args && args[i])
{
err = NULL;
process_one_file (args[i], &err);
if (err != NULL)
{
g_printerr (_("Error on file \"%s\": %s\n"),
args[i], err->message);
g_error_free (err);
return 1;
}
++i;
}
if (i == 0)
{
g_printerr (_("Must specify one or more desktop files to install\n"));
return 1;
}
poptFreeContext (ctx);
return 0;
}
|