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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
nautilus-find-icon-image.c: Functions that locate icon image files,
used internally by the icon factory.
Copyright (C) 1999, 2000 Red Hat Inc.
Copyright (C) 1999, 2000 Eazel, Inc.
Copyright (C) 2001 Free Software Foundation, Inc.
This program 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.
This program 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 this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Darin Adler <darin@bentspoon.com>
*/
#include <config.h>
#include "nautilus-find-icon-image.h"
#include "nautilus-file-utilities.h"
#include <eel/eel-glib-extensions.h>
#include <eel/eel-string.h>
#include <eel/eel-xml-extensions.h>
#include <libgnome/gnome-util.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <string.h>
/* List of suffixes to search when looking for an icon file. */
static const char *icon_file_name_suffixes[] =
{
".svg",
".png",
".jpg",
};
static char *
make_full_icon_path (const char *path,
const char *suffix,
gboolean theme_is_in_user_directory)
{
char *partial_path, *full_path;
char *user_directory, *themes_directory;
partial_path = g_strconcat (path, suffix, NULL);
if (path[0] == '/') {
return partial_path;
}
/* Build a path for this icon, depending on the theme_is_in_user_directory boolean. */
if (theme_is_in_user_directory) {
user_directory = nautilus_get_user_directory ();
themes_directory = nautilus_make_path (user_directory, "themes");
full_path = nautilus_make_path (themes_directory, partial_path);
g_free (user_directory);
g_free (themes_directory);
if (!g_file_exists (full_path)) {
g_free (full_path);
full_path = NULL;
}
} else {
full_path = nautilus_pixmap_file (partial_path);
}
if (full_path == NULL) {
full_path = gnome_vfs_icon_path_from_filename (partial_path);
}
g_free (partial_path);
return full_path;
}
/* utility routine to parse the attach points string to set up the array in icon_info */
static void
parse_attach_points (NautilusEmblemAttachPoints *attach_points,
const char *attach_point_string)
{
char **point_array;
char c;
int i, x_offset, y_offset;
attach_points->num_points = 0;
if (attach_point_string == NULL) {
return;
}
/* Split the attach point string into a string array, then process
* each point with sscanf in a loop.
*/
point_array = g_strsplit (attach_point_string, "|", MAX_ATTACH_POINTS);
for (i = 0; point_array[i] != NULL; i++) {
if (sscanf (point_array[i], " %d , %d %c", &x_offset, &y_offset, &c) == 2) {
attach_points->points[attach_points->num_points].x = x_offset;
attach_points->points[attach_points->num_points].y = y_offset;
attach_points->num_points++;
} else {
g_warning ("bad attach point specification: %s", point_array[i]);
}
}
g_strfreev (point_array);
}
static void
read_details (const char *path,
guint icon_size,
gboolean for_aa,
NautilusIconDetails *details)
{
xmlDocPtr doc;
xmlNodePtr node;
char *size_as_string, *property;
ArtIRect parsed_rect;
memset (&details->text_rect, 0, sizeof (details->text_rect));
doc = xmlParseFile (path);
size_as_string = g_strdup_printf ("%u", icon_size);
node = eel_xml_get_root_child_by_name_and_property
(doc, "icon", "size", size_as_string);
g_free (size_as_string);
property = NULL;
if (for_aa) {
property = xmlGetProp (node, "embedded_text_rectangle_aa");
}
if (property == NULL) {
property = xmlGetProp (node, "embedded_text_rectangle");
}
if (property != NULL) {
if (sscanf (property,
" %d , %d , %d , %d %*s",
&parsed_rect.x0,
&parsed_rect.y0,
&parsed_rect.x1,
&parsed_rect.y1) == 4) {
details->text_rect = parsed_rect;
}
xmlFree (property);
}
property = NULL;
if (for_aa) {
property = xmlGetProp (node, "attach_points_aa");
}
if (property == NULL) {
property = xmlGetProp (node, "attach_points");
}
parse_attach_points (&details->attach_points, property);
xmlFree (property);
xmlFreeDoc (doc);
}
/* Pick a particular icon to use, trying all the various suffixes.
* Return the path of the icon or NULL if no icon is found.
*/
static char *
get_themed_icon_file_path (const NautilusIconTheme *icon_theme,
const char *icon_name,
guint icon_size,
gboolean for_aa,
NautilusIconDetails *details)
{
guint i;
gboolean include_size, in_user_directory;
char *themed_icon_name, *partial_path, *path, *aa_path, *xml_path;
g_assert (icon_name != NULL);
if (icon_theme == NULL || icon_theme->name == NULL || icon_name[0] == '/') {
themed_icon_name = g_strdup (icon_name);
in_user_directory = FALSE;
} else {
themed_icon_name = g_strconcat (icon_theme->name, "/", icon_name, NULL);
in_user_directory = icon_theme->is_in_user_directory;
}
include_size = icon_size != NAUTILUS_ICON_SIZE_STANDARD;
/* Try each suffix. */
for (i = 0; i < G_N_ELEMENTS (icon_file_name_suffixes); i++) {
if (include_size && g_strcasecmp (icon_file_name_suffixes[i], ".svg") != 0) {
/* Build a path for this icon. */
partial_path = g_strdup_printf ("%s-%u",
themed_icon_name,
icon_size);
} else {
partial_path = g_strdup (themed_icon_name);
}
/* if we're in anti-aliased mode, try for an optimized one first */
if (for_aa) {
aa_path = g_strconcat (partial_path, "-aa", NULL);
path = make_full_icon_path (aa_path,
icon_file_name_suffixes[i],
in_user_directory);
g_free (aa_path);
/* Return the path if the file exists. */
if (path != NULL) {
g_free (partial_path);
break;
}
g_free (path);
path = NULL;
}
path = make_full_icon_path (partial_path,
icon_file_name_suffixes[i],
in_user_directory);
g_free (partial_path);
/* Return the path if the file exists. */
if (path != NULL) {
break;
}
g_free (path);
path = NULL;
}
/* Open the XML file to get the text rectangle and emblem attach points */
if (path != NULL && details != NULL) {
xml_path = make_full_icon_path (themed_icon_name,
".xml",
in_user_directory);
read_details (xml_path, icon_size, for_aa, details);
g_free (xml_path);
}
g_free (themed_icon_name);
return path;
}
static gboolean
theme_has_icon (const NautilusIconTheme *theme,
const char *name)
{
char *path;
if (theme == NULL || theme->name == NULL) {
return FALSE;
}
path = get_themed_icon_file_path (theme, name,
NAUTILUS_ICON_SIZE_STANDARD,
FALSE, NULL);
g_free (path);
return path != NULL;
}
/* Check and see if there is a theme icon to use. If there's a
* fallback theme specified, try it, too. This decision is based on
* whether there's a non-size-specific theme icon.
*/
static const NautilusIconTheme *
choose_theme (const NautilusIconThemeSpecifications *theme_specs,
const char *name)
{
if (name[0] == '/') {
return NULL;
}
if (theme_has_icon (&theme_specs->current, name)) {
return &theme_specs->current;
}
if (theme_has_icon (&theme_specs->fallback, name)) {
return &theme_specs->fallback;
}
return NULL;
}
/* Sick hack. If we still haven't found anything, and we're looking
* for an emblem, check out the emblems area in the user's home
* directory, since it might be an emblem that they've added there.
*/
static char *
get_user_emblem_path (const char *name, guint icon_size)
{
char *path, *user_directory;
guint i;
if (icon_size != NAUTILUS_ICON_SIZE_STANDARD) {
return FALSE;
}
if (!eel_str_has_prefix (name, NAUTILUS_EMBLEM_NAME_PREFIX)) {
return FALSE;
}
user_directory = nautilus_get_user_directory ();
path = NULL;
for (i = 0; i < G_N_ELEMENTS (icon_file_name_suffixes); i++) {
path = g_strdup_printf ("%s/emblems/%s%s",
user_directory,
name + strlen (NAUTILUS_EMBLEM_NAME_PREFIX),
icon_file_name_suffixes[i]);
if (g_file_exists (path)) {
break;
}
g_free (path);
path = NULL;
}
g_free (user_directory);
return path;
}
/* Choose the file name to load, taking into account theme
* vs. non-theme icons. Also fill in info in the icon structure based
* on what's found in the XML file.
*/
char *
nautilus_get_icon_file_name (const NautilusIconThemeSpecifications *theme_specs,
const char *name,
const char *modifier,
guint size,
gboolean for_aa,
NautilusIconDetails *details)
{
const NautilusIconTheme *theme;
char *path;
char *name_with_modifier;
if (details != NULL) {
memset (details, 0, sizeof (*details));
}
if (name == NULL) {
return NULL;
}
theme = choose_theme (theme_specs, name);
/* If there's a modifier, try the modified icon first. */
if (modifier != NULL && modifier[0] != '\0') {
name_with_modifier = g_strconcat (name, "-", modifier, NULL);
path = get_themed_icon_file_path (theme, name_with_modifier, size, for_aa, details);
g_free (name_with_modifier);
if (path != NULL) {
return path;
}
}
/* Check for a normal icon. */
path = get_themed_icon_file_path (theme, name, size, for_aa, details);
if (path != NULL) {
return path;
}
/* Check for a user emblem. */
path = get_user_emblem_path (name, size);
if (path != NULL) {
return path;
}
return NULL;
}
char *
nautilus_remove_icon_file_name_suffix (const char *icon_name)
{
guint i;
const char *suffix;
for (i = 0; i < G_N_ELEMENTS (icon_file_name_suffixes); i++) {
suffix = icon_file_name_suffixes[i];
if (eel_str_has_suffix (icon_name, suffix)) {
return eel_str_strip_trailing_str (icon_name, suffix);
}
}
return g_strdup (icon_name);
}
|