summaryrefslogtreecommitdiff
path: root/libnautilus/nautilus-icon-factory.c
blob: bf86db7bf72920747f8298716a61b084dd3cea29 (plain)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-icon-factory.c: Class for obtaining icons for files and other objects.
 
 * Copyright (C) 1999, 2000 Red Hat Inc.
   Copyright (C) 1999, 2000 Eazel, 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: John Sullivan <sullivan@eazel.com>
*/

#include <config.h>
#include "nautilus-icon-factory.h"

#include <string.h>
#include <stdio.h>

#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-mime-info.h>
#include <libgnome/gnome-util.h>

#include "nautilus-string.h"
#include "nautilus-default-file-icon.h"

#define ICON_CACHE_MAX_ENTRIES 10
#define ICON_CACHE_SWEEP_TIMEOUT 10

/* This allows us to do smarter caching */
static guint use_counter = 0;

typedef struct {
	guint ref_count;
        char *name;
        GdkPixbuf *plain, *symlink;
        guint last_use;
} IconSet;

typedef enum {
        ICON_SET_DIRECTORY,
        ICON_SET_DIRECTORY_CLOSED,
        ICON_SET_EXECUTABLE,
        ICON_SET_REGULAR,
        ICON_SET_CORE,
        ICON_SET_SOCKET,
        ICON_SET_FIFO,
        ICON_SET_CHARACTER_DEVICE,
        ICON_SET_BLOCK_DEVICE,
        ICON_SET_BROKEN_SYMBOLIC_LINK,
        ICON_SET_FALLBACK,
        ICON_SET_SPECIAL_LAST
} SpecialIconSetType;

typedef struct {
        char *theme_name;
        GHashTable *name_to_image;

        IconSet special_icon_sets[ICON_SET_SPECIAL_LAST];

        GdkPixbuf *symlink_overlay;
        
        guint sweep_timer;
} NautilusIconFactory;

struct _NautilusScalableIcon {
	guint ref_count;

	char *uri;
	IconSet *icon_set;
	gboolean is_symbolic_link;
};

/* forward declarations */
static NautilusIconFactory * nautilus_get_current_icon_factory (void);
static GdkPixbuf *           nautilus_icon_factory_scale       (NautilusIconFactory *factory,
								GdkPixbuf           *standard_sized_pixbuf,
								guint                size_in_pixels);
static NautilusScalableIcon *scalable_icon_get                 (const char          *uri,
								IconSet             *icon_set,
								gboolean             is_symbolic_link);

static IconSet *
icon_set_new (const gchar *name)
{
        IconSet *new;

        new = g_new0 (IconSet, 1);
        new->name = g_strdup (name);

        return new;
}

static void
icon_set_destroy (IconSet *icon_set, gboolean free_name)
{
        if (icon_set == NULL)
		return;

	if (free_name)
		g_free (icon_set->name);
	if (icon_set->plain != NULL)
		gdk_pixbuf_unref (icon_set->plain);
	if (icon_set->symlink != NULL)
		gdk_pixbuf_unref (icon_set->symlink);
}

static NautilusIconFactory *
nautilus_icon_factory_new (const char *theme_name)
{
        NautilusIconFactory *factory;
        
        factory = g_new0 (NautilusIconFactory, 1);

        factory->theme_name = g_strdup (theme_name);
        factory->name_to_image = g_hash_table_new (g_str_hash, g_str_equal);
        factory->special_icon_sets[ICON_SET_DIRECTORY].name = "i-directory.png"; 
        factory->special_icon_sets[ICON_SET_DIRECTORY_CLOSED].name = "i-dirclosed.png"; 
        factory->special_icon_sets[ICON_SET_EXECUTABLE].name = "i-executable.png"; 
        factory->special_icon_sets[ICON_SET_REGULAR].name = "i-regular.png"; 
        factory->special_icon_sets[ICON_SET_CORE].name = "i-core.png"; 
        factory->special_icon_sets[ICON_SET_SOCKET].name = "i-sock.png"; 
        factory->special_icon_sets[ICON_SET_FIFO].name = "i-fifo.png"; 
        factory->special_icon_sets[ICON_SET_CHARACTER_DEVICE].name = "i-chardev.png"; 
        factory->special_icon_sets[ICON_SET_BLOCK_DEVICE].name = "i-blockdev.png"; 
        factory->special_icon_sets[ICON_SET_BROKEN_SYMBOLIC_LINK].name = "i-brokenlink.png"; 
        factory->special_icon_sets[ICON_SET_FALLBACK].name = "";
        
        return factory;
}

static gboolean
nautilus_icon_factory_destroy_icon_sets (gpointer key, gpointer value, gpointer user_data)
{
        icon_set_destroy (value, TRUE);
        return TRUE;
}

static void
nautilus_icon_factory_invalidate (NautilusIconFactory *factory)
{
        int i;

        g_hash_table_foreach_remove (factory->name_to_image,
                                     nautilus_icon_factory_destroy_icon_sets,
                                     NULL);

        for (i = 0; i < ICON_SET_SPECIAL_LAST; i++)
                icon_set_destroy (&factory->special_icon_sets[i], FALSE);

        if (factory->symlink_overlay) {
                gdk_pixbuf_unref (factory->symlink_overlay);
                factory->symlink_overlay = NULL;
        }
}

#if 0

static void
nautilus_icon_factory_destroy (NautilusIconFactory *factory)
{
        nautilus_icon_factory_invalidate (factory);
        g_hash_table_destroy (factory->name_to_image);

        g_free (factory->theme_name);
        g_free (factory);
}

#endif

static gboolean
icon_set_possibly_free (gpointer key, gpointer value, gpointer user_data)
{
        IconSet *is = value;

        if (is->last_use > (use_counter - ICON_CACHE_MAX_ENTRIES))
                return FALSE;

        if (is->plain && is->plain->ref_count <= 1) {
                gdk_pixbuf_unref (is->plain);
                is->plain = NULL;
        }

        if (is->symlink && is->symlink->ref_count <= 1) {
                gdk_pixbuf_unref (is->symlink);
                is->symlink = NULL;
        }

        if (is->symlink == NULL && is->plain == NULL && is->ref_count == 0) {
                g_free (is->name);
                return TRUE;
        }

        return FALSE;
}

static gboolean
nautilus_icon_factory_sweep(gpointer data)
{
        NautilusIconFactory *factory;

	factory = data;

        g_hash_table_foreach_remove (factory->name_to_image, icon_set_possibly_free, NULL);
        factory->sweep_timer = 0;

        return FALSE;
}

static void
nautilus_icon_factory_setup_sweep(NautilusIconFactory *factory)
{
        if (factory->sweep_timer)
                return;

        if (g_hash_table_size (factory->name_to_image) < ICON_CACHE_MAX_ENTRIES)
                return;

        factory->sweep_timer = g_timeout_add (ICON_CACHE_SWEEP_TIMEOUT * 1000,
					      nautilus_icon_factory_sweep, factory);
}

void
nautilus_icon_factory_set_theme(const char *theme_name)
{
	NautilusIconFactory *factory;

	factory = nautilus_get_current_icon_factory ();
        nautilus_icon_factory_invalidate (factory);
        g_free (factory->theme_name);
        factory->theme_name = g_strdup (theme_name);
}

static IconSet *
nautilus_icon_factory_get_icon_set_for_file (NautilusIconFactory *factory, NautilusFile *file)
{
        IconSet *icon_set;
        const char *mime_type;
        const char *icon_name;

        mime_type = nautilus_file_get_mime_type (file);
        icon_name = NULL;
        if (mime_type)
                icon_name = gnome_mime_get_value (mime_type, "icon-filename");

        if (icon_name) {
                icon_set = g_hash_table_lookup (factory->name_to_image, icon_name);
                if (!icon_set) {
                        icon_set = icon_set_new (icon_name);
                        g_hash_table_insert (factory->name_to_image, icon_set->name, icon_set);
                }
        } else {
                /* We can't get a name, so we have to do some faking to figure out what set to load */
                if (nautilus_file_is_executable (file))
                        icon_set = &factory->special_icon_sets[ICON_SET_EXECUTABLE];
                else
                        icon_set = &factory->special_icon_sets[ICON_SET_REGULAR];
        }

        return icon_set;
}

static GdkPixbuf *
nautilus_icon_factory_load_file(NautilusIconFactory *factory, const char *fn)
{
        char *file_name = NULL;
        char cbuf[128];
        GdkPixbuf *image;

        if(*fn != '/') {
                if(factory->theme_name) {
                        g_snprintf(cbuf, sizeof(cbuf), "nautilus/%s/%s", factory->theme_name, fn);
                        
                        file_name = gnome_pixmap_file(cbuf);
                }
                
                if(!file_name) {
                        g_snprintf(cbuf, sizeof(cbuf), "nautilus/%s", fn);
                        file_name = gnome_pixmap_file(cbuf);
                }
        }
        
        image = gdk_pixbuf_new_from_file(file_name?file_name:fn);
        g_free(file_name);

        return image;
}

/* Splats one on top of the other, putting the src pixbuf in the lower left corner of the dest pixbuf */
static void
my_gdk_pixbuf_composite(GdkPixbuf *dest, GdkPixbuf *src)
{
        int dx, dy, dw, dh;

        dw = MIN(dest->art_pixbuf->width, src->art_pixbuf->width);
        dh = MIN(dest->art_pixbuf->width, src->art_pixbuf->width);
        dx = dw - src->art_pixbuf->width;
        dy = dh - src->art_pixbuf->height;

	gdk_pixbuf_composite(src, dest, dx, dy, dw, dh, 0, 0, 1, 1, ART_FILTER_BILINEAR, 255);
}

static GdkPixbuf *
nautilus_icon_factory_load_icon(NautilusIconFactory *factory, IconSet *is, gboolean is_symlink)
{
        GdkPixbuf *image;

        if(is_symlink)
                image = is->symlink;
        else
                image = is->plain;

        if (!image) {
                if (*is->name == '\0') {
                        /* This is the fallback icon set */
                        image = gdk_pixbuf_new_from_data ((guchar*)nautilus_default_file_icon,
                                                           ART_PIX_RGB,
                                                           nautilus_default_file_icon_has_alpha,
                                                           nautilus_default_file_icon_width,
                                                           nautilus_default_file_icon_height,
                                                           /* rowstride */
                                                           nautilus_default_file_icon_width*4,
                                                           NULL, /* don't destroy data */
                                                           NULL );
                } else {
                        /* need to load the file */
                        image = nautilus_icon_factory_load_file(factory, is->name);
                }
                if (is_symlink) {
                        if (!factory->symlink_overlay)
                                factory->symlink_overlay = nautilus_icon_factory_load_file(factory, "i-symlink.png");
                  
                        if(factory->symlink_overlay)
                                my_gdk_pixbuf_composite(image, factory->symlink_overlay);
                        is->symlink = image;
                } else
                        is->plain = image;
        }

        if (image)
                gdk_pixbuf_ref(image); /* Returned value is owned by caller */

        return image;
}

static NautilusScalableIcon *
scalable_icon_new (const char *uri,
		   IconSet *icon_set,
		   gboolean is_symbolic_link)
{
	NautilusScalableIcon *icon;

	g_return_val_if_fail (icon_set != NULL, NULL);

	icon = g_new (NautilusScalableIcon, 1);
	icon->ref_count = 1;
	icon->uri = g_strdup (uri);
	icon->icon_set = icon_set;
	icon->is_symbolic_link = is_symbolic_link;

	icon_set->ref_count++;

	return icon;
}

static NautilusScalableIcon *
scalable_icon_get (const char *uri,
		   IconSet *icon_set,
		   gboolean is_symbolic_link)
{
	/* FIXME: These should come from a hash table. */
	return scalable_icon_new (uri, icon_set, is_symbolic_link);
}

void
nautilus_scalable_icon_unref (NautilusScalableIcon *icon)
{
	g_return_if_fail (icon->ref_count != 0);

	if (--icon->ref_count != 0)
		return;

	g_free (icon->uri);

	g_assert (icon->icon_set->ref_count != 0);
	icon->icon_set->ref_count--;

	g_free (icon);
}

NautilusScalableIcon *
nautilus_icon_factory_get_icon_for_file (NautilusFile *file)
{
	NautilusIconFactory *factory;
        IconSet *set;
	char *uri, *custom_image;
        gboolean is_symbolic_link;
	NautilusScalableIcon *scalable_icon;

	if (file == NULL)
		return NULL;
	
 	factory = nautilus_get_current_icon_factory ();
	
	/* Get an icon set based on the file's type. */
        switch (nautilus_file_get_type (file)) {
        case GNOME_VFS_FILE_TYPE_UNKNOWN:
        case GNOME_VFS_FILE_TYPE_REGULAR:
        default:
                set = nautilus_icon_factory_get_icon_set_for_file (factory, file);
                break;
        case GNOME_VFS_FILE_TYPE_DIRECTORY:
                set = &factory->special_icon_sets[ICON_SET_DIRECTORY];
                break;
        case GNOME_VFS_FILE_TYPE_FIFO:
                set = &factory->special_icon_sets[ICON_SET_FIFO];
                break;
        case GNOME_VFS_FILE_TYPE_SOCKET:
                set = &factory->special_icon_sets[ICON_SET_SOCKET];
                break;
        case GNOME_VFS_FILE_TYPE_CHARDEVICE:
                set = &factory->special_icon_sets[ICON_SET_CHARACTER_DEVICE];
                break;
        case GNOME_VFS_FILE_TYPE_BLOCKDEVICE:
                set = &factory->special_icon_sets[ICON_SET_BLOCK_DEVICE];
                break;
        case GNOME_VFS_FILE_TYPE_BROKENSYMLINK:
                set = &factory->special_icon_sets[ICON_SET_BROKEN_SYMBOLIC_LINK];
                break;
        }
	
	/* Also record whether it's a symbolic link or not.
	 * Later, we'll probably use a separate icon badge for this,
	 * but for now, we'll keep it.
	 */
        is_symbolic_link = nautilus_file_is_symbolic_link (file);
	
	/* if there is a custom image in the metadata, use that instead */
	
    custom_image = nautilus_file_get_metadata(file, "image", NULL);
	if (custom_image != NULL)
	  uri = custom_image;	  	
	/* Use the image itself as a custom icon. */
	else if (nautilus_has_prefix (nautilus_file_get_mime_type (file), "image/")
	    && nautilus_file_get_size (file) < 16384)
		uri = nautilus_file_get_uri (file);
	else
		uri = NULL;
	
	/* Create the icon or find it in the cache if it's already there. */
	scalable_icon = scalable_icon_get (uri, set, is_symbolic_link);
	if (uri != NULL)
	  g_free (uri);
	
        nautilus_icon_factory_setup_sweep (factory);
	
	return scalable_icon;
}

GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_icon (NautilusScalableIcon *scalable_icon,
					   guint	         size_in_pixels)
{
	NautilusIconFactory *factory;
        IconSet *set;
        GdkPixbuf *image;

	factory = nautilus_get_current_icon_factory (); 

	/* FIXME: This works only with file:// images, because there's
	 * no convenience function for loading an image with gnome-vfs
	 * and gdk-pixbuf.
	 */
	image = NULL;
	if (nautilus_has_prefix (scalable_icon->uri, "file://"))
		image = gdk_pixbuf_new_from_file (scalable_icon->uri + 7);
	
	/* If there was no suitable custom icon URI, then use the icon set. */
	if (image == NULL) {
		set = scalable_icon->icon_set;
		set->last_use = use_counter++;
		image = nautilus_icon_factory_load_icon
			(factory, set, scalable_icon->is_symbolic_link);
	}
	
	/* If the icon set failed, then use the fallback set. */
        if (image == NULL) {
		g_warning ("failed to load icon, using fallback icon set");
                set = &factory->special_icon_sets[ICON_SET_FALLBACK];
                set->last_use = use_counter++;
                image = nautilus_icon_factory_load_icon
			(factory, set, scalable_icon->is_symbolic_link);
        }
	
        g_assert (image != NULL);

        /* First cut at handling multiple sizes. If size is other than standard,
         * scale the pixbuf here. Eventually we'll store icons at multiple sizes
         * rather than relying on scaling in every case (though we'll still need
         * scaling as a fallback). We'll also cache the scaled pixbufs.
         * For now, assume that the icon found so far is of standard size.
         */
        if (size_in_pixels != NAUTILUS_ICON_SIZE_STANDARD)
        {
        	GdkPixbuf *scaled_icon;

        	scaled_icon = nautilus_icon_factory_scale
			(factory, image, size_in_pixels);
        	gdk_pixbuf_unref (image);
        	image = scaled_icon;
        }
  
        nautilus_icon_factory_setup_sweep (factory);
  
        return image;
}

NautilusIconFactory *
nautilus_get_current_icon_factory (void)
{
        static NautilusIconFactory *global_icon_factory = NULL;
        if (global_icon_factory == NULL)
                global_icon_factory = nautilus_icon_factory_new (NULL);
        return global_icon_factory;
}

static GdkPixbuf *
nautilus_icon_factory_scale (NautilusIconFactory *factory,
		     GdkPixbuf *standard_sized_pixbuf,
		     guint size_in_pixels)
{
	GdkPixbuf *result;
	int old_width, old_height, new_width, new_height;
	
	g_return_val_if_fail (standard_sized_pixbuf != NULL, NULL);

	old_width = gdk_pixbuf_get_width (standard_sized_pixbuf);
	old_height = gdk_pixbuf_get_height (standard_sized_pixbuf);

	new_width = (old_width * size_in_pixels) / NAUTILUS_ICON_SIZE_STANDARD;
	new_height = (old_height * size_in_pixels) / NAUTILUS_ICON_SIZE_STANDARD;

	/* This creates scaled icon with ref. count of 1. */
	result = gdk_pixbuf_scale_simple (standard_sized_pixbuf, 
					  new_width, 
					  new_height, 
					  ART_FILTER_BILINEAR);

	return result;
}


/* 
 * Return nominal icon size for given zoom level.
 * @zoom_level: zoom level for which to find matching icon size.
 * 
 * Return value: icon size between NAUTILUS_ICON_SIZE_SMALLEST and
 * NAUTILUS_ICON_SIZE_LARGEST, inclusive.
 */
guint
nautilus_icon_size_for_zoom_level (NautilusZoomLevel zoom_level)
{
	switch (zoom_level)
	{
		case NAUTILUS_ZOOM_LEVEL_SMALLEST:
			return NAUTILUS_ICON_SIZE_SMALLEST;
		case NAUTILUS_ZOOM_LEVEL_SMALLER:
			return NAUTILUS_ICON_SIZE_SMALLER;
		case NAUTILUS_ZOOM_LEVEL_SMALL:
			return NAUTILUS_ICON_SIZE_SMALL;
		case NAUTILUS_ZOOM_LEVEL_STANDARD:
			return NAUTILUS_ICON_SIZE_STANDARD;
		case NAUTILUS_ZOOM_LEVEL_LARGE:
			return NAUTILUS_ICON_SIZE_LARGE;
		case NAUTILUS_ZOOM_LEVEL_LARGER:
			return NAUTILUS_ICON_SIZE_LARGER;
		case NAUTILUS_ZOOM_LEVEL_LARGEST:
			return NAUTILUS_ICON_SIZE_LARGEST;
		default:
			g_assert_not_reached();
			return NAUTILUS_ICON_SIZE_STANDARD;
	}
}