xdgmimeicon.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* -*- mode: C; c-file-style: "gnu" -*- */
  2. /* xdgmimeicon.c: Private file. Datastructure for storing the aliases.
  3. *
  4. * More info can be found at http://www.freedesktop.org/standards/
  5. *
  6. * Copyright (C) 2008 Red Hat, Inc.
  7. *
  8. * Licensed under the Academic Free License version 2.0
  9. * Or under the following terms:
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 02111-1307, USA.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include "xdgmimeicon.h"
  30. #include "xdgmimeint.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <assert.h>
  34. #include <string.h>
  35. #include <fnmatch.h>
  36. #ifndef FALSE
  37. #define FALSE (0)
  38. #endif
  39. #ifndef TRUE
  40. #define TRUE (!FALSE)
  41. #endif
  42. typedef struct XdgIcon XdgIcon;
  43. struct XdgIcon
  44. {
  45. char *mime_type;
  46. char *icon_name;
  47. };
  48. struct XdgIconList
  49. {
  50. struct XdgIcon *icons;
  51. int n_icons;
  52. };
  53. XdgIconList *
  54. _xdg_mime_icon_list_new (void)
  55. {
  56. XdgIconList *list;
  57. list = malloc (sizeof (XdgIconList));
  58. list->icons = NULL;
  59. list->n_icons = 0;
  60. return list;
  61. }
  62. void
  63. _xdg_mime_icon_list_free (XdgIconList *list)
  64. {
  65. int i;
  66. if (list->icons)
  67. {
  68. for (i = 0; i < list->n_icons; i++)
  69. {
  70. free (list->icons[i].mime_type);
  71. free (list->icons[i].icon_name);
  72. }
  73. free (list->icons);
  74. }
  75. free (list);
  76. }
  77. static int
  78. icon_entry_cmp (const void *v1, const void *v2)
  79. {
  80. return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type);
  81. }
  82. const char *
  83. _xdg_mime_icon_list_lookup (XdgIconList *list,
  84. const char *mime_type)
  85. {
  86. XdgIcon *entry;
  87. XdgIcon key;
  88. if (list->n_icons > 0)
  89. {
  90. key.mime_type = (char *)mime_type;
  91. key.icon_name = NULL;
  92. entry = bsearch (&key, list->icons, list->n_icons,
  93. sizeof (XdgIcon), icon_entry_cmp);
  94. if (entry)
  95. return entry->icon_name;
  96. }
  97. return NULL;
  98. }
  99. void
  100. _xdg_mime_icon_read_from_file (XdgIconList *list,
  101. const char *file_name)
  102. {
  103. FILE *file;
  104. char line[255];
  105. int alloc;
  106. file = fopen (file_name, "r");
  107. if (file == NULL)
  108. return;
  109. /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
  110. * Blah */
  111. alloc = list->n_icons + 16;
  112. list->icons = realloc (list->icons, alloc * sizeof (XdgIcon));
  113. while (fgets (line, 255, file) != NULL)
  114. {
  115. char *sep;
  116. if (line[0] == '#')
  117. continue;
  118. sep = strchr (line, ':');
  119. if (sep == NULL)
  120. continue;
  121. *(sep++) = '\000';
  122. sep[strlen (sep) -1] = '\000';
  123. if (list->n_icons == alloc)
  124. {
  125. alloc <<= 1;
  126. list->icons = realloc (list->icons,
  127. alloc * sizeof (XdgIcon));
  128. }
  129. list->icons[list->n_icons].mime_type = strdup (line);
  130. list->icons[list->n_icons].icon_name = strdup (sep);
  131. list->n_icons++;
  132. }
  133. list->icons = realloc (list->icons,
  134. list->n_icons * sizeof (XdgIcon));
  135. fclose (file);
  136. if (list->n_icons > 1)
  137. qsort (list->icons, list->n_icons,
  138. sizeof (XdgIcon), icon_entry_cmp);
  139. }
  140. void
  141. _xdg_mime_icon_list_dump (XdgIconList *list)
  142. {
  143. int i;
  144. if (list->icons)
  145. {
  146. for (i = 0; i < list->n_icons; i++)
  147. {
  148. printf ("%s %s\n",
  149. list->icons[i].mime_type,
  150. list->icons[i].icon_name);
  151. }
  152. }
  153. }