1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimeicon.c: Private file. Datastructure for storing the aliases.
4 * More info can be found at http://www.freedesktop.org/standards/
6 * Copyright (C) 2008 Red Hat, Inc.
8 * Licensed under the Academic Free License version 2.0
9 * Or under the following terms:
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.
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.
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/>.
27 #include "xdgmimeicon.h"
28 #include "xdgmimeint.h"
43 typedef struct XdgIcon XdgIcon
;
53 struct XdgIcon
*icons
;
58 _xdg_mime_icon_list_new (void)
62 list
= malloc (sizeof (XdgIconList
));
71 _xdg_mime_icon_list_free (XdgIconList
*list
)
77 for (i
= 0; i
< list
->n_icons
; i
++)
79 free (list
->icons
[i
].mime_type
);
80 free (list
->icons
[i
].icon_name
);
88 icon_entry_cmp (const void *v1
, const void *v2
)
90 return strcmp (((XdgIcon
*)v1
)->mime_type
, ((XdgIcon
*)v2
)->mime_type
);
94 _xdg_mime_icon_list_lookup (XdgIconList
*list
,
95 const char *mime_type
)
100 if (list
->n_icons
> 0)
102 key
.mime_type
= (char *)mime_type
;
103 key
.icon_name
= NULL
;
105 entry
= bsearch (&key
, list
->icons
, list
->n_icons
,
106 sizeof (XdgIcon
), icon_entry_cmp
);
108 return entry
->icon_name
;
115 _xdg_mime_icon_read_from_file (XdgIconList
*list
,
116 const char *file_name
)
122 file
= fopen (file_name
, "r");
127 /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars.
129 alloc
= list
->n_icons
+ 16;
130 list
->icons
= realloc (list
->icons
, alloc
* sizeof (XdgIcon
));
131 while (fgets (line
, 255, file
) != NULL
)
137 sep
= strchr (line
, ':');
141 sep
[strlen (sep
) -1] = '\000';
142 if (list
->n_icons
== alloc
)
145 list
->icons
= realloc (list
->icons
,
146 alloc
* sizeof (XdgIcon
));
148 list
->icons
[list
->n_icons
].mime_type
= strdup (line
);
149 list
->icons
[list
->n_icons
].icon_name
= strdup (sep
);
152 list
->icons
= realloc (list
->icons
,
153 list
->n_icons
* sizeof (XdgIcon
));
157 if (list
->n_icons
> 1)
158 qsort (list
->icons
, list
->n_icons
,
159 sizeof (XdgIcon
), icon_entry_cmp
);
162 #ifdef NOT_USED_IN_GIO
165 _xdg_mime_icon_list_dump (XdgIconList
*list
)
171 for (i
= 0; i
< list
->n_icons
; i
++)
174 list
->icons
[i
].mime_type
,
175 list
->icons
[i
].icon_name
);