File: XDGDesktopEntry.cpp

package info (click to toggle)
goplay 0.9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 728 kB
  • ctags: 452
  • sloc: cpp: 3,433; ansic: 996; makefile: 85; sh: 35
file content (781 lines) | stat: -rw-r--r-- 21,997 bytes parent folder | download | duplicates (2)
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
 * Copyright (c) 2011 Paulo Zanoni
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */


#include <cassert>
#include <cstdio>
#include <cstring>
#include <cstdlib>

#include <iostream>

extern "C" {
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
}

#include "XDGDesktopEntry.h"
#include "XDGUtils.h"
#include "XDGMenu.h"

XDGDesktopEntry::XDGEntryDescription XDGDesktopEntry::registeredEntries[] = {
    // Note: "Type" is special and treated in a different way

    // name,           valueType,       reqrd, type,        dpctd, valdValFnc
    //{ "Type",         XDGString,       true,  All,         false, NULL },
    { "Version",        XDGString,       false, All,         false, NULL },
    { "Name",           XDGLocalestring, true,  All,         false, NULL },
    { "GenericName",    XDGLocalestring, false, All,         false, NULL },
    { "NoDisplay",      XDGBoolean,      false, All,         false, NULL },
    { "Comment",        XDGLocalestring, false, All,         false, NULL },
    { "Icon",           XDGLocalestring, false, All,         false, NULL },
    { "Hidden",         XDGBoolean,      false, All,         false, NULL },
    { "OnlyShowIn",     XDGStrings,      false, All,         false,
	XDGMenu::validEnvironments },
    { "NotShowIn",      XDGStrings,      false, All,         false,
	XDGMenu::validEnvironments },
    { "TryExec",        XDGString,       false, Application, false, NULL },
    { "Exec",           XDGString,       true,  Application, false,
	validExec },
    { "Path",           XDGString,       false, Application, false, NULL },
    { "Terminal",       XDGBoolean,      false, Application, false, NULL },
    { "MimeType",       XDGStrings,      false, Application, false, NULL },
    { "Categories",     XDGStrings,      false, Application, false,
	XDGMenu::validCategories },
    { "StartupNotify",  XDGBoolean,      false, Application, false, NULL },
    { "StartupWMClass", XDGString,       false, Application, false, NULL },
    { "URL",            XDGString,       true,  Link,        false, NULL },

    // Old KDE stuff:
    // XXX: their valueType is not specified in the spec!
    //      I'm not sure they're actually considered deprecated...
    { "ServiceTypes",      XDGLocalestring, false, All,         false, NULL },
    { "DocPath",           XDGLocalestring, false, All,         false, NULL },
    { "Keywords",          XDGLocalestring, false, All,         false, NULL },
    { "InitialPreference", XDGLocalestring, false, All,         false, NULL },
    { "Dev",               XDGString,       false, FSDevice,    false, NULL },
    { "FSType",            XDGString,       false, FSDevice,    false, NULL },
    { "MountPoint",        XDGString,       false, FSDevice,    false, NULL },
    { "ReadOnly",          XDGBoolean,      false, FSDevice,    false, NULL },
    { "UnmountIcon",       XDGLocalestring, false, FSDevice,    false, NULL },

    // Really deprecated items
    // valueType not specified...
    { "Patterns",        XDGLocalestring, false, MimeType,    true,  NULL },
    { "DefaultApp",      XDGLocalestring, false, MimeType,    true,  NULL },
    { "Encoding",        XDGLocalestring, false, All,         true,  NULL },
    { "MiniIcon",        XDGLocalestring, false, All,         true,  NULL },
    { "TerminalOptions", XDGLocalestring, false, All,         true,  NULL },
    { "Protocols",       XDGLocalestring, false, All,         true,  NULL },
    { "Extensions",      XDGLocalestring, false, All,         true,  NULL },
    { "BinaryPattern",   XDGLocalestring, false, All,         true,  NULL },
    { "MapNotify",       XDGLocalestring, false, All,         true,  NULL },
    { "SwallowTitle",    XDGLocalestring, false, All,         true,  NULL },
    { "SwallowExec",     XDGString,       false, All,         true,  NULL },
    { "SortOrder",       XDGStrings,      false, All,         true,  NULL },
    { "FilePattern",     XDGLocalestring, false, All,         true,  NULL },


    // End of list
    { NULL,             XDGStrings,      false, All,         false, NULL },
    //{ "",    XDGLocalestring, false, All, false, NULL },

    // XXX: Keys that have a specific set of values (so we need to implement
    //      validValueFunc):
    //      - Encoding (deprecated)
    // XXX: FilePattern actually had its own "regex" type
    // XXX: We're only 1.0-compliant, so why bother registering deprecated stuff
    //      here? Maybe we should just fail?
};


XDGDesktopEntry::XDGDesktopEntry(const char *fileName)
    : valid_(false),
      fileName_(NULL),
      fileContent_(NULL),
      entries_(NULL)
{
    struct stat statBuffer;
    int fd;

    fileName_ = strdup(fileName);
    assert(fileName_);

    if (stat(fileName_, &statBuffer) != 0) {
	std::cerr << "Error: can't stat " << fileName_ << "\n";
	return;
    }

    // We're going to slurp the whole file, add a few '\0's and point structs to
    // it.
    fileContent_ = (char *)malloc(statBuffer.st_size+1);
    assert(fileContent_);
    fileContent_[statBuffer.st_size] = '\0';

    fd = open(fileName_, O_RDONLY);
    if (fd == -1) {
	std::cerr << "Error: can't open " << fileName_ << "\n";
	return;
    }

    ssize_t ret, readed = 0;
    while (readed < statBuffer.st_size) {
	ret = read(fd, fileContent_, statBuffer.st_size - readed);
	if (ret == -1) {
	    std::cerr << "Error: can't read " << fileName_ << "\n";
	    return;
	}
	readed += ret;
    }

    if (close(fd) == -1) {
	std::cerr << "Error: can't close " << fileName_ << "\n";
	return;
    }

    XDGUtils::log(XDGUtils::Debug, "File:\n%s\n", fileContent_);

    char *line;
    int lineNumber = 1;
    bool desktopEntryGroupFound = false;
    for(int i = 0; fileContent_[i] != '\0'; i++, lineNumber++) {
	line = &fileContent_[i];

	if (line[0] == '\n') {
	    continue;
	}

	if (line[0] == '#') {
	    for(; fileContent_[i] != '\n'; i++)
		;
	    continue;
	}

	if (line[0] == '[') {
	    if (desktopEntryGroupFound) {
		// We don't care about reading other groups for now.
		// Later we might parse them and check for correct syntax
		break;
	    } else {
		if (strncmp(line, "[Desktop Entry]\n", 16) == 0) {
		    desktopEntryGroupFound = true;
		    for(; fileContent_[i] != '\n'; i++)
			;
		    continue;
		} else {
		    std::cerr << fileName_ << " error: first group must be "
			      << "[Desktop Entry]\n";
		    return;
		}
	    }
	}

	// processEntry will mess with the line, so we better advance now
	for(; fileContent_[i] != '\n'; i++)
	    ;
	if (!processEntry(line)) {
	    std::cerr << fileName_ << " error: invalid entry at line "
		      << lineNumber << "\n";
	    return;
	}
    }

    validateEntries();
}

XDGDesktopEntry::~XDGDesktopEntry()
{
    free(fileName_);
    free(fileContent_);
    struct XDGEntry *pEntry = entries_;
    struct XDGEntry *prev;

    while (pEntry != NULL) {
	prev = pEntry;
	pEntry = pEntry->next;
	free(prev);
    }
}

bool XDGDesktopEntry::processEntry(char *line)
{
    unsigned int i, keyEnd = 0, valueBegin = 0;
    unsigned int localeBegin = 0, localeEnd = 0;
    char lastChar;

    struct XDGEntry *entry = (struct XDGEntry*)malloc(sizeof(struct XDGEntry));
    assert(entry);

    // Look for the key
    for(i = 0; line[i] != '\n' && line[i] != '\0'; i++) {
	if (!isalnum(line[i]) && line[i] != '-') {
	    keyEnd = i;
	    break;
	}
    }

    if (keyEnd == 0) {
	free(entry);
	return false;
    }

    lastChar = line[keyEnd];
    line[keyEnd] = '\0';
    entry->key = &line[0];
    XDGUtils::log(XDGUtils::Debug, "key[%s]\n", entry->key);
    i++;

    // Check for localized entries
    if (lastChar == '[') {
	localeBegin = i;
	for(; line[i] != '\n' && line[i] != '\0'; i++) {
	    if (line[i] == ']') {
		localeEnd = i;
		i++;
		break;
	    }
	}
	if (localeEnd != 0) {
	    lastChar = line[localeEnd];
	    line[localeEnd] = '\0';
	    entry->locale = &line[localeBegin];
	} else {
	    free(entry);
	    return false;
	}
    } else {
	entry->locale = NULL;
    }

    if (entry->locale)
	XDGUtils::log(XDGUtils::Debug, "locale[%s]\n", entry->locale);
    else
	XDGUtils::log(XDGUtils::Debug, "locale[NULL]\n");

    // Now eat spaces until the '=' char
    if (lastChar != '=') {
	for(; line[i] != '\n' && line[i] != '\0'; i++) {
	    if (!isspace(line[i]))
		break;
	}
	if (line[i] != '=') {
	    std::cerr << fileName_ << " error: '=' expected, but found '"
		      << line[i] << "'\n";
	    free(entry);
	    return false;
	} else {
	    i++;
	}
    }

    // Now eat spaces until the value starts
    for(; line[i] != '\n' && line[i] != '\0'; i++) {
	if (!isspace(line[i])) {
	    valueBegin = i;
	    break;
	}
    }

    if (line[i] == '\n' || line[i] == '\0' ||
	line[valueBegin] == '\n' || line[valueBegin] == '\0') {
	// Although the spec doesn't mention, there are desktop files with
	// empty values
	entry->value = NULL;
    } else {
	entry->value = &line[valueBegin];
	for(; line[i] != '\n' && line[i] != '\0'; i++)
	    ;
	line[i] = '\0';
    }

    if (entry->value)
	XDGUtils::log(XDGUtils::Debug, "value[%s]\n", entry->value);
    else
	XDGUtils::log(XDGUtils::Debug, "value[NULL]\n");

    return storeEntry(entry);
}

bool XDGDesktopEntry::storeEntry(struct XDGEntry *entry)
{
    struct XDGEntry *pEntry;

    entry->next = NULL;

    if (entries_ == NULL) {
	entries_ = entry;
	return true;
    }
    pEntry = entries_;
    while (pEntry->next != NULL) {
	if (strcmp(entry->key, pEntry->key) == 0) {
	    if (entry->locale == NULL && pEntry->locale == NULL) {
		std::cerr << fileName_ << " error: key " << entry->key
			  << "already stored!\n";
		return false;
	    }

	    if (!(entry->locale == NULL || pEntry->locale == NULL)) {
		if (strcmp(entry->locale, pEntry->locale) == 0) {
		    std::cerr << fileName_ << " error: key " << entry->key
			      << "[" << entry->locale  << "] already stored!\n";
		    return false;
		}
	    }

	}
	pEntry = pEntry->next;
    }

    pEntry->next = entry;
    return true;
}

bool XDGDesktopEntry::getEntry(const char *key, const char *locale,
			       const char **value)
{
    struct XDGEntry *pEntry;

    pEntry = entries_;
    while (pEntry != NULL) {
	if (strcmp(pEntry->key, key) == 0) {
	    if ((pEntry->locale == NULL && locale == NULL) ||
		(strcmp(pEntry->locale, locale) == 0)) {
		*value = pEntry->value;
		return true;
	    }
	}
	pEntry = pEntry->next;
    }
    return false;
}

void XDGDesktopEntry::validateEntries()
{
    int i;
    char *key = NULL;
    char *locale;
    const char *value;
    XDGTypeMask type;

    valid_ = false;

    if (!getEntry("Type", NULL, &value)) {
	std::cerr << fileName_ << " error: \"Type\" key not found!\n";
	return;
    } else {
	if (strcmp(value, "Application") == 0)
	    type = Application;
	else if (strcmp(value, "Link") == 0)
	    type = Link;
	else if (strcmp(value, "Directory") == 0)
	    type = Directory;
	else if (strcmp(value, "ServiceType") == 0)
	    type = ServiceType;
	else if (strcmp(value, "Service") == 0)
	    type = Service;
	else if (strcmp(value, "FSDevice") == 0)
	    type = FSDevice;
	else if (strcmp(value, "MimeType") == 0) {
	    type = MimeType;
	    std::cerr << fileName_
		      << " error: type \"MimeType\" is deprecated!\n";
	} else {
	    std::cerr << fileName_ << " error: invalid type!\n";
	    return;
	}
    }

    // For each key we have, compare against the registered ones
    struct XDGEntry *pEntry;
    for(pEntry = entries_; pEntry != NULL; pEntry = pEntry->next) {
	key = pEntry->key;
	locale = pEntry->locale;
	value = pEntry->value;

	if ((key[0] == 'X' && key[1] == '-') || (strcmp(key, "Type") == 0))
	    continue;

	bool found = false;
	for(i = 0; registeredEntries[i].name != NULL; i++) {
	    if (strcmp(registeredEntries[i].name, key) != 0)
		continue;

	    found = true;

	    if (!(registeredEntries[i].type & type)) {
		std::cerr << fileName_ << " error: key " << key
			  << " invalid for this type\n";
		return;
	    }

	    if (locale != NULL &&
		registeredEntries[i].valueType != XDGLocalestring) {
		std::cerr << fileName_ << " error: only localestring can "
			  << "have localized values\n";
		return;
	    }

	    if (!checkValueType(value, registeredEntries[i].valueType)) {
		std::cerr << fileName_ << " error: invalid value "
			  << value << " for type\n";
		return;
	    }

	    if (registeredEntries[i].deprecated)
		std::cerr << fileName_ << " warning: key " << key
			  << " is deprecated\n";

	    if (registeredEntries[i].validValueFunc != NULL) {
		if (!(registeredEntries[i].validValueFunc)(value)) {
		    std::cerr << fileName_ << " error: invalid value " << value
			      << "\n";
		    return;
		}
	    }

	    break;
	}
	if (!found) {
	    std::cerr << fileName_ << " error: key " << key
		      << " is not registered\n";
	    return;
	}

    }

    // Now find missing required keys
    for(i = 0; registeredEntries[i].name != NULL; i++) {
	if (!registeredEntries[i].type & type)
	    continue;

	if (registeredEntries[i].required) {
	    if (!getEntry(key, NULL, &value)) {
		std::cerr << fileName_ << " error: key " << key
			  << " entry required but not found\n";
		return;
	    }
	}
    }


    if (getEntry("OnlyShowIn", NULL, &value) &&
	getEntry("NotShowIn", NULL, &value)) {
	std::cerr << fileName_ << " error: only one of OnlyShowIn or "
		  << "NotShowIn is allowed\n";
	return;
    }

    // XXX: this way, we only check for the "defined" entries. Anything else is
    //      just ignored! Shouldn't we fail for unknown entries?


    valid_ = true;
}

bool XDGDesktopEntry::checkValueType(const char *value, XDGEntryValueType type)
{
    unsigned int i;

    // Allow empty values for now
    if (!value)
	return true;

    if (type == XDGString) {
	// "Values of type string may contain all ASCII characters except for
	// control characters."
	for(i = 0; value[i] != '\0'; i++)
	    if (!isascii(value[i]) || iscntrl(value[i]))
		return false;
	return true;

    } else if (type == XDGStrings) {
	char *s, *tokPtr, *tokStr;
	if (!XDGUtils::validSemicolonList(value))
	    return false;

	s = strdup(value);
	ITERATE_ESCAPED_STRTOK_R(s, ";", &tokPtr, tokStr) {
	    if (!checkValueType(tokStr, XDGString)) {
		free(s);
		return false;
	    }
	}
	free(s);
	return true;

    } else if (type == XDGLocalestring) {
	// "Values of type localestring are user displayable, and are encoded in
	// UTF-8."
	// XXX: implement! check for displayability and utf-8!
	return true;

    } else if (type == XDGBoolean) {
	// "Values of type boolean must either be the string true or false."
	if (strcmp(value, "0") == 0 || strcmp(value, "1") == 0) {
	    std::cerr << fileName_ << " warning: boolean values \"0\" and "
		      << "\"1\" are deprecated!\n";
	    return true;
	}
	if ((strcmp(value, "true") != 0) && (strcmp(value, "false") != 0))
	    return false;
	else
	    return true;

    } else if (type == XDGNumeric) {
	// "Values of type numeric must be a valid floating point number as
	// recognized by the %f specifier for scanf in the C locale."
	float tmp;
	if (sscanf(value, "%f", &tmp) != 1)
	    return false;
	else
	    return true;

    } else {
	std::cerr << fileName_ << " error: invalid XDGEntryType\n";
	return false;
    }
}

bool XDGDesktopEntry::valid()
{
    return valid_;
}

bool XDGDesktopEntry::showInEnvironment(const char *environment)
{
    if (!valid_) {
	XDGUtils::log(XDGUtils::Verbose,
		      "%s error: invalid desktop file\n", fileName_);
	return false;
    }

    const char *value;
    char *s, *tokPtr, *tokStr;
    if (getEntry("OnlyShowIn", NULL, &value)) {
	s = strdup(value);
	ITERATE_ESCAPED_STRTOK_R(s, ";", &tokPtr, tokStr) {
	    if (strcmp(tokStr, environment) == 0) {
		free(s);
		return true;
	    }
	}
	XDGUtils::log(XDGUtils::Verbose, "%s not in OnlyShowIn list\n",
		      fileName_);
	free(s);
	return false;
    } else if (getEntry("NotShowIn", NULL, &value)) {
	s = strdup(value);
	ITERATE_ESCAPED_STRTOK_R(s, ";", &tokPtr, tokStr) {
	    if (strcmp(tokStr, environment) == 0) {
		XDGUtils::log(XDGUtils::Verbose,
			      "%s present in NotShowIn list\n", fileName_);
		free(s);
		return false;

	    }
	}
	free(s);
	return true;
    }

    return true;
}

int XDGDesktopEntry::run(const char *args)
{
    const char *value;

    if (!valid_)
	return 0;

    XDGUtils::log(XDGUtils::Verbose, "%s: --> Running entry!\n", fileName_);
    std::string executablePath;

    // Test TryExec:
    if (getEntry("TryExec", NULL, &value)) {
	XDGUtils::log(XDGUtils::Verbose, "%s: TryExec: %s\n", fileName_, value);

	if (value[0] == '/' || value[0] == '.') {
	    if (!XDGUtils::fileExists(value)) {
		XDGUtils::log(XDGUtils::Verbose,
			      "%s error: TryExec failed: file %s not found\n",
			      fileName_, value);
		return 0;
	    }
	    executablePath = value;
	} else {
	    if (!XDGUtils::findFileInPath(value, executablePath)) {
		XDGUtils::log(XDGUtils::Verbose,
			      "%s error: TryExec failed: file %s not found\n",
			      fileName_, value);
		return 0;
	    }
	}
	if (access(executablePath.c_str(), X_OK) == -1) {
	    XDGUtils::log(XDGUtils::Verbose,
			  "%s TryExec failed: file %s not executable\n",
			  fileName_, value);
	    return 0;
	}
	XDGUtils::log(XDGUtils::Verbose, "%s: -- TryExec success\n",
		      fileName_);
    }

    // Now run the desktop file
    getEntry("Exec", NULL, &value);

    // XXX: For now, just remove the field codes...
    std::string commandString;
    for(unsigned int i = 0; value[i] != '\0'; i++) {
	if (value[i] == '%') {
	    i++;
	    if (value[i] == 'i') {
		// "The Icon key of the desktop entry expanded as two arguments,
		// first --icon and then the value of the Icon key. Should not
		// expand to any arguments if the Icon key is empty or missing."
		const char *iconValue;
		if (getEntry("Icon", NULL, &iconValue)) {
		    commandString += std::string("-- icon ") + iconValue;
		}
	    } else if (value[i] == 'c') {
		// "The translated name of the application as listed in the
		// appropriate Name key in the desktop entry."
		// XXX: implement
	    } else if (value[i] == 'k') {
		// "The location of the desktop file as either a URI (if for
		// example gotten from the vfolder system) or a local filename
		// or empty if no location is known."
		// XXX: implement
	    } else {
		continue;
	    }
	}
	commandString += value[i];
    }

    commandString += std::string(" ") + args;

    return runCommand(commandString.c_str());
}

int XDGDesktopEntry::runCommand(const char *commandString)
{
    // XXX: use alloca instead of new?
    unsigned int i, buf_i, j;
    bool lookingForQuote = false;
    pid_t pid;

    // "buffer" contains consecutive strings separated by '\0's
    char *buffer = (char *) alloca(strlen(commandString) * sizeof(char));
    char *arg = &buffer[0];
    unsigned int arguments = 0;

    for(i = 0, buf_i = 0; commandString[i] != '\0'; i++) {
	if (commandString[i] == '\\') {
	    i++;
	} else if (commandString[i] == ' ' && (!lookingForQuote)) {
	    if (!(arg == &buffer[buf_i])) {
		// Not two consecutive spaces
		buffer[buf_i] = '\0';
		arguments++;
		buf_i++;
		arg = &buffer[buf_i];
	    }
	} else if (commandString[i] == '"') {
	    lookingForQuote = !lookingForQuote;
	} else {
	    buffer[buf_i] = commandString[i];
	    buf_i++;
	}
    }
    if (arg != &buffer[buf_i]) {
	buffer[buf_i] = '\0';
	arguments++;
    }

    if (lookingForQuote) {
        std::cerr << "Error: missing quotes\n";
        return 0;
    }

    if (arguments == 0) {
        std::cerr << "Error: empty command\n";
        return 0;
    }

    char **c_args = (char **) alloca(sizeof(char *) * arguments+1);
    char *c_command;

    c_command = buffer;
    for(i = 0, j = 0; i < arguments; i++) {
	c_args[i] = &buffer[j];
	// Skip to the next argument:
	for(; buffer[j] != '\0'; j++)
	    ;
	j++;
    }
    c_args[i] = NULL;

    pid = fork();
    if (pid == 0) {
        execvp(c_command, c_args);
        exit(0);
    }

    return pid;
}


bool XDGDesktopEntry::validExec(const char *execString)
{
    bool exclusiveFieldFound = false;
    for(unsigned int i = 0; execString[i] != '\0'; i++) {
	if (execString[i] == '%') {
	    i++;
	    if (execString[i] == 'f' || execString[i] == 'F' ||
		execString[i] == 'u' || execString[i] == 'U') {
		if (exclusiveFieldFound) {
		    std::cerr << "Error: Exec fields %f, %F, %u "
			         "or %U are mutually exclusive\n";
		    return false;
		} else {
		    exclusiveFieldFound = true;
		}
	    } else if (execString[i] == 'd' || execString[i] == 'D' ||
		       execString[i] == 'n' || execString[i] == 'N' ||
		       execString[i] == 'v' || execString[i] == 'm' ) {
		std::cerr << "Warning: deprecated Exec field code %"
			  << execString[i] << "\n";
	    } else if (execString[i] != 'i' && execString[i] != 'c' &&
		       execString[i] != 'k') {
		std::cerr << "Error: invalid Exec field %"
			  << execString[i] << "\n";
		return false;
	    }
	}
    }
    return true;
}