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