/src/glib/gio/xdgmime/xdgmimeicon.c
Line | Count | Source (jump to first uncovered line) |
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.1 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, see <http://www.gnu.org/licenses/>. |
23 | | */ |
24 | | |
25 | | #include "config.h" |
26 | | |
27 | | #include "xdgmimeicon.h" |
28 | | #include "xdgmimeint.h" |
29 | | #include <stdlib.h> |
30 | | #include <stdio.h> |
31 | | #include <assert.h> |
32 | | #include <string.h> |
33 | | #include <fnmatch.h> |
34 | | |
35 | | #ifndef FALSE |
36 | | #define FALSE (0) |
37 | | #endif |
38 | | |
39 | | #ifndef TRUE |
40 | | #define TRUE (!FALSE) |
41 | | #endif |
42 | | |
43 | | typedef struct XdgIcon XdgIcon; |
44 | | |
45 | | struct XdgIcon |
46 | | { |
47 | | char *mime_type; |
48 | | char *icon_name; |
49 | | }; |
50 | | |
51 | | struct XdgIconList |
52 | | { |
53 | | struct XdgIcon *icons; |
54 | | int n_icons; |
55 | | }; |
56 | | |
57 | | XdgIconList * |
58 | | _xdg_mime_icon_list_new (void) |
59 | 0 | { |
60 | 0 | XdgIconList *list; |
61 | |
|
62 | 0 | list = malloc (sizeof (XdgIconList)); |
63 | |
|
64 | 0 | list->icons = NULL; |
65 | 0 | list->n_icons = 0; |
66 | |
|
67 | 0 | return list; |
68 | 0 | } |
69 | | |
70 | | void |
71 | | _xdg_mime_icon_list_free (XdgIconList *list) |
72 | 0 | { |
73 | 0 | int i; |
74 | |
|
75 | 0 | if (list->icons) |
76 | 0 | { |
77 | 0 | for (i = 0; i < list->n_icons; i++) |
78 | 0 | { |
79 | 0 | free (list->icons[i].mime_type); |
80 | 0 | free (list->icons[i].icon_name); |
81 | 0 | } |
82 | 0 | free (list->icons); |
83 | 0 | } |
84 | 0 | free (list); |
85 | 0 | } |
86 | | |
87 | | static int |
88 | | icon_entry_cmp (const void *v1, const void *v2) |
89 | 0 | { |
90 | 0 | return strcmp (((XdgIcon *)v1)->mime_type, ((XdgIcon *)v2)->mime_type); |
91 | 0 | } |
92 | | |
93 | | const char * |
94 | | _xdg_mime_icon_list_lookup (XdgIconList *list, |
95 | | const char *mime_type) |
96 | 0 | { |
97 | 0 | XdgIcon *entry; |
98 | 0 | XdgIcon key; |
99 | |
|
100 | 0 | if (list->n_icons > 0) |
101 | 0 | { |
102 | 0 | key.mime_type = (char *)mime_type; |
103 | 0 | key.icon_name = NULL; |
104 | |
|
105 | 0 | entry = bsearch (&key, list->icons, list->n_icons, |
106 | 0 | sizeof (XdgIcon), icon_entry_cmp); |
107 | 0 | if (entry) |
108 | 0 | return entry->icon_name; |
109 | 0 | } |
110 | | |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | | void |
115 | | _xdg_mime_icon_read_from_file (XdgIconList *list, |
116 | | const char *file_name) |
117 | 0 | { |
118 | 0 | FILE *file; |
119 | 0 | char line[255]; |
120 | 0 | int alloc; |
121 | |
|
122 | 0 | file = fopen (file_name, "r"); |
123 | |
|
124 | 0 | if (file == NULL) |
125 | 0 | return; |
126 | | |
127 | | /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. |
128 | | * Blah */ |
129 | 0 | alloc = list->n_icons + 16; |
130 | 0 | list->icons = realloc (list->icons, alloc * sizeof (XdgIcon)); |
131 | 0 | while (fgets (line, 255, file) != NULL) |
132 | 0 | { |
133 | 0 | char *sep; |
134 | 0 | if (line[0] == '#') |
135 | 0 | continue; |
136 | | |
137 | 0 | sep = strchr (line, ':'); |
138 | 0 | if (sep == NULL) |
139 | 0 | continue; |
140 | 0 | *(sep++) = '\000'; |
141 | 0 | sep[strlen (sep) -1] = '\000'; |
142 | 0 | if (list->n_icons == alloc) |
143 | 0 | { |
144 | 0 | alloc <<= 1; |
145 | 0 | list->icons = realloc (list->icons, |
146 | 0 | alloc * sizeof (XdgIcon)); |
147 | 0 | } |
148 | 0 | list->icons[list->n_icons].mime_type = strdup (line); |
149 | 0 | list->icons[list->n_icons].icon_name = strdup (sep); |
150 | 0 | list->n_icons++; |
151 | 0 | } |
152 | 0 | list->icons = realloc (list->icons, |
153 | 0 | list->n_icons * sizeof (XdgIcon)); |
154 | |
|
155 | 0 | fclose (file); |
156 | | |
157 | 0 | if (list->n_icons > 1) |
158 | 0 | qsort (list->icons, list->n_icons, |
159 | 0 | sizeof (XdgIcon), icon_entry_cmp); |
160 | 0 | } |
161 | | |
162 | | #ifdef NOT_USED_IN_GIO |
163 | | |
164 | | void |
165 | | _xdg_mime_icon_list_dump (XdgIconList *list) |
166 | | { |
167 | | int i; |
168 | | |
169 | | if (list->icons) |
170 | | { |
171 | | for (i = 0; i < list->n_icons; i++) |
172 | | { |
173 | | printf ("%s %s\n", |
174 | | list->icons[i].mime_type, |
175 | | list->icons[i].icon_name); |
176 | | } |
177 | | } |
178 | | } |
179 | | |
180 | | #endif |