source: trunk/gtk/favicon.c @ 10385

Last change on this file since 10385 was 10385, checked in by charles, 12 years ago

(trunk gtk) get the new filterbar working on older versions of GTK+

File size: 3.3 KB
Line 
1/*
2 * This file Copyright (C) 2010 Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id:$
11 */
12
13#include <glib/gstdio.h> /* g_remove() */
14#include <gtk/gtk.h>
15
16#include <libtransmission/transmission.h>
17#include <libtransmission/web.h> /* tr_webRun() */
18
19#include "favicon.h"
20#include "util.h" /* gtr_mkdir_with_parents(), gtr_idle_add() */
21
22struct favicon_data
23{
24    GFunc func;
25    gpointer data;
26    char * host;
27    char * contents;
28    size_t len;
29};
30
31static char*
32favicon_get_cache_dir( void )
33{
34    static char * dir = NULL;
35
36    if( dir == NULL )
37    {
38        dir = g_build_filename( g_get_user_cache_dir(),
39                                "transmission",
40                                "favicons",
41                                NULL );
42        gtr_mkdir_with_parents( dir, 0777 );
43    }
44
45    return dir;
46}
47
48static char*
49favicon_get_cache_filename( const char * host )
50{
51    return g_build_filename( favicon_get_cache_dir(), host, NULL );
52}
53
54static void
55favicon_save_cache_file( const char * host, const void * data, size_t len )
56{
57    char * filename = favicon_get_cache_filename( host );
58    g_file_set_contents( filename, data, len, NULL );
59    g_free( filename );
60}
61
62static GdkPixbuf*
63favicon_load_from_file( const char * host )
64{
65    char * filename = favicon_get_cache_filename( host );
66    GdkPixbuf * pixbuf = gdk_pixbuf_new_from_file_at_size( filename, 16, 16, NULL );
67    if( pixbuf == NULL ) /* bad file */
68        g_remove( filename );
69    g_free( filename );
70    return pixbuf;
71}
72
73static gboolean
74favicon_web_done_idle_cb( gpointer vfav )
75{
76    GdkPixbuf * pixbuf = NULL;
77    struct favicon_data * fav = vfav;
78
79    if( fav->len > 0 )
80    {
81        favicon_save_cache_file( fav->host, fav->contents, fav->len );
82        pixbuf = favicon_load_from_file( fav->host );
83    }
84
85    fav->func( pixbuf, fav->data );
86
87    g_free( fav->host );
88    g_free( fav->contents );
89    g_free( fav );
90    return FALSE;
91}
92
93static void
94favicon_web_done_cb( tr_session    * session UNUSED,
95                     long            code UNUSED,
96                     const void    * data,
97                     size_t          len,
98                     void          * vfav )
99{
100    struct favicon_data * fav = vfav;
101    fav->contents = g_memdup( data, len );
102    fav->len = len;
103
104    gtr_idle_add( favicon_web_done_idle_cb, fav );
105}
106   
107void
108gtr_get_favicon( tr_session  * session,
109                 const char  * host, 
110                 GFunc         pixbuf_ready_func, 
111                 gpointer      pixbuf_ready_func_data )
112{
113    GdkPixbuf * pixbuf = favicon_load_from_file( host );
114
115    if( pixbuf != NULL )
116    {
117        pixbuf_ready_func( pixbuf, pixbuf_ready_func_data );
118    }
119    else
120    {
121        struct favicon_data * data;
122        char * url = g_strdup_printf( "http://%s/favicon.ico", host );
123
124        g_debug( "trying favicon from \"%s\"", url );
125        data  = g_new( struct favicon_data, 1 );
126        data->func = pixbuf_ready_func;
127        data->data = pixbuf_ready_func_data;
128        data->host = g_strdup( host );
129        tr_webRun( session, url, NULL, favicon_web_done_cb, data );
130
131        g_free( url );
132    }
133}
Note: See TracBrowser for help on using the repository browser.