File: appdb.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (726 lines) | stat: -rw-r--r-- 13,649 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2008,2009,2010 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains code of the application database
 **************************************************************************
 *
 * LADI Session Handler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

#include "appdb.h"
#include "../log.h"
#include "../common/catdup.h"
#include "../assert.h"

void
lash_appdb_free_entry(
  struct lash_appdb_entry * entry_ptr);

#define MAP_TYPE_STRING  0
#define MAP_TYPE_BOOL    1

struct map
{
  const char * key;
  unsigned int type;
  size_t offset;
};

struct map g_appdb_entry_map[] = 
{
  {
    .key = "Name",
    .type = MAP_TYPE_STRING,
    .offset = offsetof(struct lash_appdb_entry, name)
  },
  {
    .key = "GenericName",
    .type = MAP_TYPE_STRING,
    .offset = offsetof(struct lash_appdb_entry, generic_name)
  },
  {
    .key = "Comment",
    .type = MAP_TYPE_STRING,
    .offset = offsetof(struct lash_appdb_entry, comment)
  },
  {
    .key = "Icon",
    .type = MAP_TYPE_STRING,
    .offset = offsetof(struct lash_appdb_entry, icon)
  },
  {
    .key = "Exec",
    .type = MAP_TYPE_STRING,
    .offset = offsetof(struct lash_appdb_entry, exec)
  },
  {
    .key = "Path",
    .type = MAP_TYPE_STRING,
    .offset = offsetof(struct lash_appdb_entry, path)
  },
  {
    .key = "Terminal",
    .type = MAP_TYPE_BOOL,
    .offset = offsetof(struct lash_appdb_entry, terminal)
  },
  {
    .key = NULL,
  }
};

struct entry
{
  const char * key;
  const char * value;
};

#define MAX_ENTRIES 1000

static
const char *
get_xdg_var(
  const char * var_name,
  const char * default_value)
{
  const char * value;

  value = getenv(var_name);

  /* Spec says that if variable is "either not set or empty", default should be used */
  if (value == NULL || strlen(value) == 0)
  {
    return default_value;
  }

  return value;
}

static
bool
suffix_match(
  const char * string,
  const char * suffix)
{
  size_t len;
  size_t len_suffix;

  len = strlen(string);
  len_suffix = strlen(suffix);

  if (len <= len_suffix)
  {
    return false;
  }

  if (memcmp(string + (len - len_suffix), ".desktop", len_suffix) != 0)
  {
    return false;
  }

  return true;
}

static
bool
load_file_data(
  const char * file_path,
  char ** data_ptr_ptr)
{
  FILE * file;
  long size;
  bool ret;
  char * data_ptr;

  ret = true;
  *data_ptr_ptr = NULL;

  file = fopen(file_path, "r");
  if (file == NULL)
  {
    log_error("Failed to open '%s' for reading", file_path);
    goto exit;
  }

  if (fseek(file, 0, SEEK_END) == -1)
  {
    log_error("fseek('%s') failed", file_path);
    goto exit_close;
  }

  size = ftell(file);
  if (size == -1)
  {
    log_error("ftell('%s') failed", file_path);
    goto exit_close;
  }

  data_ptr = malloc(size + 1);
  if (data_ptr == NULL)
  {
    log_error("Failed to allocate %ld bytes for data of file '%s'", size + 1, file_path);
    ret = false;
    goto exit_close;
  }

  if (fseek(file, 0, SEEK_SET) == -1)
  {
    log_error("fseek('%s') failed", file_path);
    goto exit_close;
  }

  if (fread(data_ptr, size, 1, file) != 1)
  {
    log_error("Failed to read %ld bytes of data from file '%s'", size, file_path);
    goto exit_free_data;
  }

  data_ptr[size] = 0;

  *data_ptr_ptr = data_ptr;
  goto exit_close;

exit_free_data:
  free(data_ptr);

exit_close:
  fclose(file);

exit:
  return ret;
}

char *
strlstrip(char * string)
{
  while (*string == ' ' || *string == '\t')
  {
    string++;
  }

  return string;
}

void
strrstrip(char * string)
{
  char * temp;

  temp = string + strlen(string);

  while (temp > string)
  {
    temp--;

    if (*temp == ' ' || *temp == '\t')
    {
      *temp = 0;
    }
  }
}

bool
lash_appdb_parse_file_data(
  char * data,
  struct entry * entries_array,
  size_t max_count,
  size_t * count_ptr)
{
  char * line;
  char * next_line;
  char * value;
  bool group_found;
  size_t count;

  group_found = false;
  line = data;
  count = 0;

  do
  {
    next_line = strchr(line, '\n');
    if (next_line != NULL)
    {
      //log_info("there is next line");
      *next_line = 0;
      next_line++;
    }

    //log_info("Line '%s'", line);

    /* skip comments (and empty lines) */
    if (*line == 0 || *line == '#')
    {
      continue;
    }

    if (!group_found)
    {
      /* first real line should be begining of "Desktop Entry" group */
      if (strcmp(line, "[Desktop Entry]") != 0)
      {
        return false;
      }

      group_found = true;
      continue;
    }

    value = strchr(line, '=');
    if (value == NULL)
    {
      goto exit;
    }

    *value = 0;
    value++;

    /* strip spaces */
    strrstrip(line);
    value = strlstrip(value);

    //log_info("Key=%s", line);
    //log_info("Value=%s", value);

    if (count + 1 == max_count)
    {
      log_error("failed to parse desktop entry with more than %u keys", (unsigned int)max_count);
      return false;
    }

    entries_array->key = line;
    entries_array->value = value;
    entries_array++;
    count++;
  }
  while ((line = next_line) != NULL);

exit:
  *count_ptr = count;

  return group_found;
}

const char *
lash_appdb_find_key(
  struct entry * entries,
  size_t count,
  const char * key)
{
  size_t i;

  for (i = 0 ; i < count ; i++)
  {
    if (strcmp(entries[i].key, key) == 0)
    {
      return entries[i].value;
    }
  }

  return NULL;
}

bool
lash_appdb_load_file(
  struct list_head * appdb,
  const char * file_path)
{
  char * data;
  bool ret;
  struct entry entries[MAX_ENTRIES];
  size_t entries_count;
  const char * value;
  const char * name;
  const char * xlash;
  struct list_head * node_ptr;
  struct lash_appdb_entry * entry_ptr;
  struct map * map_ptr;
  char ** str_ptr_ptr;
  bool * bool_ptr;

  //log_info("Desktop entry '%s'", file_path);

  ret = true;

  if (!load_file_data(file_path, &data))
  {
    ret = false;
    goto exit;
  }

  if (data == NULL)
  {
    goto exit;
  }

  if (!lash_appdb_parse_file_data(data, entries, MAX_ENTRIES, &entries_count))
  {
    goto exit_free_data;
  }

  //log_info("%llu entries", (unsigned long long)entries_count);

  /* check whether entry is of "Application" type */
  value = lash_appdb_find_key(entries, entries_count, "Type");
  if (value == NULL || strcmp(value, "Application") != 0)
  {
    goto exit_free_data;
  }

  /* check whether "Name" is preset, it is required */
  name = lash_appdb_find_key(entries, entries_count, "Name");
  if (name == NULL)
  {
    goto exit_free_data;
  }

  /* check whether entry has LIBLASH or LASHCLASS key */
  xlash = lash_appdb_find_key(entries, entries_count, "X-LASH");
  if (xlash == NULL)
  {
    goto exit_free_data;
  }

  /* check whether entry already exists (first found entries have priority according to XDG Base Directory Specification) */
  list_for_each(node_ptr, appdb)
  {
    entry_ptr = list_entry(node_ptr, struct lash_appdb_entry, siblings);

    if (strcmp(entry_ptr->name, name) == 0)
    {
      goto exit_free_data;
    }
  }

  //log_info("Application '%s' found", name);

  /* allocate new entry */
  entry_ptr = malloc(sizeof(struct lash_appdb_entry));
  if (entry_ptr == NULL)
  {
    log_error("malloc() failed");
    goto fail_free_data;
  }

  memset(entry_ptr, 0, sizeof(struct lash_appdb_entry));

  /* fill the entry */
  map_ptr = g_appdb_entry_map;
  while (map_ptr->key != NULL)
  {
    value = lash_appdb_find_key(entries, entries_count, map_ptr->key);
    if (value == NULL)
    {
      ASSERT(strcmp(map_ptr->key, "Name") != 0); /* name is required and we already checked this */
      map_ptr++;  
      continue;
    }

    //log_info("mapping key '%s' to '%s'", map_ptr->key, value);

    if (map_ptr->type == MAP_TYPE_STRING)
    {
      str_ptr_ptr = (char **)((char *)entry_ptr + map_ptr->offset);
      *str_ptr_ptr = strdup(value);
      if (*str_ptr_ptr == NULL)
      {
        log_error("strdup() failed");
        goto fail_free_entry;
      }
    }
    else if (map_ptr->type == MAP_TYPE_BOOL)
    {
      bool_ptr = (bool *)((char *)entry_ptr + map_ptr->offset);
      if (strcmp(value, "true") == 0)
      {
        *bool_ptr = true;
      }
      else if (strcmp(value, "false") == 0)
      {
        *bool_ptr = false;
      }
      else
      {
        log_error("Ignoring %s:%s bool with wrong value '%s'", name, map_ptr->key, value);
      }
    }
    else
    {
      ASSERT_NO_PASS;
      goto fail_free_entry;
    }

    map_ptr++;
  }

  /* add entry to appdb list */
  list_add_tail(&entry_ptr->siblings, appdb);

  goto exit_free_data;

fail_free_entry:
  lash_appdb_free_entry(entry_ptr);

fail_free_data:
  ret = false;

exit_free_data:
  free(data);

exit:
  return ret;
}

bool
lash_appdb_load_dir(
  struct list_head * appdb,
  const char * base_directory)
{
  char * directory_path;
  bool ret;
  DIR * dir;
  struct dirent * dentry_ptr;
  char * file_path;

  //log_info("lash_appdb_load_dir() called for '%s'.", base_directory);

  ret = false;

  directory_path = catdup(base_directory, "/applications/");
  if (directory_path == NULL)
  {
    log_error("catdup() failed to compose the appdb dir path");
    goto fail;
  }

  //log_info("Scanning directory '%s'", directory_path);

  dir = opendir(directory_path);
  if (dir != NULL)
  {
    while ((dentry_ptr = readdir(dir)) != NULL)
    {
      if (dentry_ptr->d_type != DT_REG)
      {
        continue;
      }

      if (!suffix_match(dentry_ptr->d_name, ".desktop"))
      {
        continue;
      }

      file_path = catdup(directory_path, dentry_ptr->d_name);
      if (file_path == NULL)
      {
        log_error("catdup() failed to compose the appdb dir file");
      }
      else
      {
        if (!lash_appdb_load_file(appdb, file_path))
        {
          free(file_path);
          goto fail_free_path;
        }

        free(file_path);
      }
    }

    closedir(dir);
  }
  else
  {
    //log_info("failed to open directory '%s'", directory_path);
  }

  ret = true;

fail_free_path:
  free(directory_path);

fail:
  return ret;
}

bool
lash_appdb_load_dirs(
  struct list_head * appdb,
  const char * base_directories)
{
  char * limiter;
  char * directory;
  char * directories;

  directories = strdup(base_directories);
  if (directories == NULL)
  {
    log_error("strdup() failed");
    return false;
  }

  directory = directories;

  do
  {
    limiter = strchr(directory, ':');
    if (limiter != NULL)
    {
      *limiter = 0;
    }

    if (!lash_appdb_load_dir(appdb, directory))
    {
      free(directories);
      return false;
    }

    directory = limiter + 1;
  }
  while (limiter != NULL);

  free(directories);

  return true;
}

bool
lash_appdb_load(
  struct list_head * appdb)
{
  const char * data_home;
  char * data_home_default;
  const char * data_dirs;
  const char * home_dir;
  bool ret;

  ret = false;

  INIT_LIST_HEAD(appdb);

  //log_info("lash_appdb_load() called.");

  home_dir = getenv("HOME");
  if (home_dir == NULL)
  {
    log_error("HOME environment variable is not set.");
    goto fail;
  }

  data_home_default = catdup(home_dir, "/.local/share");
  if (data_home_default == NULL)
  {
    log_error("catdup failed to compose data_home_default");
    goto fail;
  }

  data_home = get_xdg_var("XDG_DATA_HOME", data_home_default);

  if (!lash_appdb_load_dir(appdb, data_home))
  {
    goto fail_free_data_home_default;
  }

  data_dirs = get_xdg_var("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/");

  if (!lash_appdb_load_dirs(appdb, data_dirs))
  {
    goto fail_free_data_home_default;
  }

  ret = true;

fail_free_data_home_default:
  free(data_home_default);

fail:
  if (!ret)
  {
    lash_appdb_free(appdb);
  }

  return ret;
}

void
lash_appdb_free_entry(
  struct lash_appdb_entry * entry_ptr)
{
  //log_info("lash_appdb_free_entry() called.");

  if (entry_ptr->name != NULL)
  {
    free(entry_ptr->name);
  }

  if (entry_ptr->generic_name != NULL)
  {
    free(entry_ptr->generic_name);
  }

  if (entry_ptr->comment != NULL)
  {
    free(entry_ptr->comment);
  }

  if (entry_ptr->icon != NULL)
  {
    free(entry_ptr->icon);
  }

  if (entry_ptr->exec != NULL)
  {
    free(entry_ptr->exec);
  }

  if (entry_ptr->path != NULL)
  {
    free(entry_ptr->path);
  }

  free(entry_ptr);
}

void
lash_appdb_free(
  struct list_head * appdb)
{
  struct list_head * node_ptr;
  struct lash_appdb_entry * entry_ptr;

  //log_info("lash_appdb_free() called.");

  while (!list_empty(appdb))
  {
    node_ptr = appdb->next;
    entry_ptr = list_entry(node_ptr, struct lash_appdb_entry, siblings);

    list_del(node_ptr);

    //log_info("Destroying appdb entry '%s'", entry_ptr->name);

    lash_appdb_free_entry(entry_ptr);
  }
}