aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-icon-stream.c
blob: 7fc69dbaa64823c44a937f08deabd5092a984ad0 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Authors: Jeffrey Stedfast <fejj@ximian.com>
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright 2003 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gdk-pixbuf-loader.h>
#ifdef HAVE_LIBGNOMEUI_GNOME_THUMBNAIL_H
#include <libgnomeui/gnome-thumbnail.h>
#endif
#include <gtk/gtkimage.h>
#include "em-icon-stream.h"

#include "libedataserver/e-msgport.h"

#define d(x) 

/* fixed-point scale factor for scaled images in cache */
#define EMIS_SCALE (1024)

struct _emis_cache_node {
    EMCacheNode node;

    GdkPixbuf *pixbuf;
};

static void em_icon_stream_class_init (EMIconStreamClass *klass);
static void em_icon_stream_init (CamelObject *object);
static void em_icon_stream_finalize (CamelObject *object);

static ssize_t emis_sync_write(CamelStream *stream, const char *buffer, size_t n);
static int emis_sync_close(CamelStream *stream);
static int emis_sync_flush(CamelStream *stream);

static EMSyncStreamClass *parent_class = NULL;
static EMCache *emis_cache;

static void
emis_cache_free(void *data)
{
    struct _emis_cache_node *node = data;

    g_object_unref(node->pixbuf);
}

CamelType
em_icon_stream_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        parent_class = (EMSyncStreamClass *)em_sync_stream_get_type();
        type = camel_type_register (em_sync_stream_get_type(),
                        "EMIconStream",
                        sizeof (EMIconStream),
                        sizeof (EMIconStreamClass),
                        (CamelObjectClassInitFunc) em_icon_stream_class_init,
                        NULL,
                        (CamelObjectInitFunc) em_icon_stream_init,
                        (CamelObjectFinalizeFunc) em_icon_stream_finalize);

        emis_cache = em_cache_new(60, sizeof(struct _emis_cache_node), emis_cache_free);
    }
    
    return type;
}

static void
em_icon_stream_class_init (EMIconStreamClass *klass)
{
    ((EMSyncStreamClass *)klass)->sync_write = emis_sync_write;
    ((EMSyncStreamClass *)klass)->sync_flush = emis_sync_flush;
    ((EMSyncStreamClass *)klass)->sync_close = emis_sync_close;
}

static void
em_icon_stream_init (CamelObject *object)
{
    EMIconStream *emis = (EMIconStream *)object;

    emis = emis;
}

static void
emis_cleanup(EMIconStream *emis)
{
    if (emis->loader) {
        gdk_pixbuf_loader_close(emis->loader, NULL);
        g_object_unref(emis->loader);
        emis->loader = NULL;
    }

    if (emis->destroy_id) {
        g_signal_handler_disconnect(emis->image, emis->destroy_id);
        emis->destroy_id = 0;
    }

    g_free(emis->key);
    emis->key = NULL;

    emis->image = NULL;
    emis->sync.cancel = TRUE;
}

static void
em_icon_stream_finalize(CamelObject *object)
{
    EMIconStream *emis = (EMIconStream *)object;

    emis_cleanup(emis);
}

static ssize_t
emis_sync_write(CamelStream *stream, const char *buffer, size_t n)
{
    EMIconStream *emis = EM_ICON_STREAM (stream);

    if (emis->loader == NULL)
        return -1;

    if (!gdk_pixbuf_loader_write(emis->loader, buffer, n, NULL)) {
        emis_cleanup(emis);
        return -1;
    }

    return (ssize_t) n;
}

static int
emis_sync_flush(CamelStream *stream)
{
    return 0;
}

static GdkPixbuf *
emis_fit(GdkPixbuf *pixbuf, int maxwidth, int maxheight, int *scale)
{
    GdkPixbuf *mini = NULL;
    int width, height;

    width = gdk_pixbuf_get_width(pixbuf);
    height = gdk_pixbuf_get_height(pixbuf);

    if ((maxwidth && width > maxwidth)
        || (maxheight && height > maxheight)) {
        if (width >= height || maxheight == 0) {
            if (scale)
                *scale = maxwidth * EMIS_SCALE / width;
            height = height * maxwidth / width;
            width = maxwidth;
        } else {
            if (scale)
                *scale = maxheight * EMIS_SCALE / height;
            width = width * maxheight / height;
            height = maxheight;
        }

#ifdef HAVE_LIBGNOMEUI_GNOME_THUMBNAIL_H
        mini = gnome_thumbnail_scale_down_pixbuf(pixbuf, width, height);
#else
        mini = gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_BILINEAR);
#endif
    }

    return mini;
}

static int
emis_sync_close(CamelStream *stream)
{
    EMIconStream *emis = (EMIconStream *)stream;
    GdkPixbuf *pixbuf, *mini;
    struct _emis_cache_node *node;
    char *scalekey;
    int scale;

    if (emis->loader == NULL)
        return -1;

    gdk_pixbuf_loader_close(emis->loader, NULL);

    pixbuf = gdk_pixbuf_loader_get_pixbuf(emis->loader);
    if (pixbuf == NULL) {
        d(printf("couldn't get pixbuf from loader\n"));
        emis_cleanup(emis);
        return -1;
    }

    mini = emis_fit(pixbuf, emis->width, emis->height, &scale);
    gtk_image_set_from_pixbuf(emis->image, mini?mini:pixbuf);

    if (emis->keep) {
        node = (struct _emis_cache_node *)em_cache_node_new(emis_cache, emis->key);
        node->pixbuf = g_object_ref(pixbuf);
        em_cache_add(emis_cache, (EMCacheNode *)node);
    }

    if (!emis->keep || mini) {
        scalekey = g_alloca(strlen(emis->key) + 20);
        sprintf(scalekey, "%s.%x", emis->key, scale);
        node = (struct _emis_cache_node *)em_cache_node_new(emis_cache, scalekey);
        node->pixbuf = mini?mini:g_object_ref(pixbuf);
        em_cache_add(emis_cache, (EMCacheNode *)node);
    }

    g_object_unref(emis->loader);
    emis->loader = NULL;

    g_signal_handler_disconnect(emis->image, emis->destroy_id);
    emis->destroy_id = 0;

    return 0;
}

static void
emis_image_destroy(struct _GtkImage *image, EMIconStream *emis)
{
    emis_cleanup(emis);
}

CamelStream *
em_icon_stream_new(GtkImage *image, const char *key, unsigned int maxwidth, unsigned int maxheight, int keep)
{
    EMIconStream *new;

    new = EM_ICON_STREAM(camel_object_new(EM_ICON_STREAM_TYPE));
    new->width = maxwidth;
    new->height = maxheight;
    new->image = image;
    new->keep = keep;
    new->destroy_id = g_signal_connect(image, "destroy", G_CALLBACK(emis_image_destroy), new);
    new->loader = gdk_pixbuf_loader_new();
    new->key = g_strdup(key);

    return (CamelStream *)new;
}

GdkPixbuf *
em_icon_stream_get_image(const char *key, unsigned int maxwidth, unsigned int maxheight)
{
    struct _emis_cache_node *node;
    GdkPixbuf *pb = NULL;

    /* forces the cache to be setup if not */
    em_icon_stream_get_type();  

    node = (struct _emis_cache_node *)em_cache_lookup(emis_cache, key);
    if (node) {
        int width, height;

        pb = node->pixbuf;
        g_object_ref(pb);
        em_cache_node_unref(emis_cache, (EMCacheNode *)node);

        width = gdk_pixbuf_get_width(pb);
        height = gdk_pixbuf_get_height(pb);

        if ((maxwidth && width > maxwidth)
            || (maxheight && height > maxheight)) {
            unsigned int scale;
            char *realkey;

            if (maxheight == 0 || width >= height)
                scale = width * EMIS_SCALE / maxwidth;
            else
                scale = height * EMIS_SCALE / maxheight;

            realkey = g_alloca(strlen(key)+20);
            sprintf(realkey, "%s.%x", key, scale);
            node = (struct _emis_cache_node *)em_cache_lookup(emis_cache, realkey);
            if (node) {
                g_object_unref(pb);
                pb = node->pixbuf;
                g_object_ref(pb);
                em_cache_node_unref(emis_cache, (EMCacheNode *)node);
            } else {
                GdkPixbuf *mini = emis_fit(pb, maxwidth, maxheight, NULL);

                g_object_unref(pb);
                pb = mini;
                node = (struct _emis_cache_node *)em_cache_node_new(emis_cache, realkey);
                node->pixbuf = pb;
                g_object_ref(pb);
                em_cache_add(emis_cache, (EMCacheNode *)node);
            }
        }
    }

    return pb;
}

int
em_icon_stream_is_resized(const char *key, unsigned int maxwidth, unsigned int maxheight)
{
    int res = FALSE;
    struct _emis_cache_node *node;
    
    /* forces the cache to be setup if not */
    em_icon_stream_get_type();

    node = (struct _emis_cache_node *)em_cache_lookup(emis_cache, key);
    if (node) {
        res = (maxwidth && gdk_pixbuf_get_width(node->pixbuf) > maxwidth)
            || (maxheight && gdk_pixbuf_get_width(node->pixbuf) > maxheight);

        em_cache_node_unref(emis_cache, (EMCacheNode *)node);
    }

    return res;
}

void
em_icon_stream_clear_cache(void)
{
    em_cache_clear(emis_cache);
}