2010-03-15 02:53:31 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2010 Mnemosyne LLC
|
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2010-03-15 02:53:31 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-03-16 00:45:12 +00:00
|
|
|
* $Id$
|
2010-03-15 02:53:31 +00:00
|
|
|
*/
|
|
|
|
|
2010-03-15 14:35:43 +00:00
|
|
|
#include <glib/gstdio.h> /* g_remove() */
|
2010-03-15 02:53:31 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2010-03-15 14:35:43 +00:00
|
|
|
#include <libtransmission/web.h> /* tr_webRun() */
|
2010-03-15 02:53:31 +00:00
|
|
|
|
|
|
|
#include "favicon.h"
|
|
|
|
#include "util.h" /* gtr_mkdir_with_parents(), gtr_idle_add() */
|
|
|
|
|
2010-03-17 20:30:04 +00:00
|
|
|
#define IMAGE_TYPES 4
|
|
|
|
static const char * image_types[IMAGE_TYPES] = { "ico", "png", "gif", "jpg" };
|
|
|
|
|
2010-03-15 02:53:31 +00:00
|
|
|
struct favicon_data
|
|
|
|
{
|
2010-03-17 20:30:04 +00:00
|
|
|
tr_session * session;
|
2010-03-15 02:53:31 +00:00
|
|
|
GFunc func;
|
|
|
|
gpointer data;
|
|
|
|
char * host;
|
|
|
|
char * contents;
|
|
|
|
size_t len;
|
2010-03-17 20:30:04 +00:00
|
|
|
int type;
|
2010-03-15 02:53:31 +00:00
|
|
|
};
|
|
|
|
|
2010-03-17 20:30:04 +00:00
|
|
|
static char*
|
|
|
|
get_url( const char * host, int image_type )
|
|
|
|
{
|
|
|
|
return g_strdup_printf( "http://%s/favicon.%s", host, image_types[image_type] );
|
|
|
|
}
|
|
|
|
|
2010-03-15 02:53:31 +00:00
|
|
|
static char*
|
|
|
|
favicon_get_cache_dir( void )
|
|
|
|
{
|
|
|
|
static char * dir = NULL;
|
|
|
|
|
|
|
|
if( dir == NULL )
|
|
|
|
{
|
2010-03-15 14:35:43 +00:00
|
|
|
dir = g_build_filename( g_get_user_cache_dir(),
|
|
|
|
"transmission",
|
|
|
|
"favicons",
|
|
|
|
NULL );
|
2010-03-15 02:53:31 +00:00
|
|
|
gtr_mkdir_with_parents( dir, 0777 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
favicon_get_cache_filename( const char * host )
|
|
|
|
{
|
|
|
|
return g_build_filename( favicon_get_cache_dir(), host, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-03-16 00:45:12 +00:00
|
|
|
favicon_save_to_cache( const char * host, const void * data, size_t len )
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
|
|
|
char * filename = favicon_get_cache_filename( host );
|
|
|
|
g_file_set_contents( filename, data, len, NULL );
|
|
|
|
g_free( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkPixbuf*
|
2010-03-16 00:45:12 +00:00
|
|
|
favicon_load_from_cache( const char * host )
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2010-03-15 15:07:23 +00:00
|
|
|
char * filename = favicon_get_cache_filename( host );
|
|
|
|
GdkPixbuf * pixbuf = gdk_pixbuf_new_from_file_at_size( filename, 16, 16, NULL );
|
|
|
|
if( pixbuf == NULL ) /* bad file */
|
|
|
|
g_remove( filename );
|
|
|
|
g_free( filename );
|
2010-03-15 02:53:31 +00:00
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
2010-03-17 20:30:04 +00:00
|
|
|
static void favicon_web_done_cb( tr_session*, long, const void*, size_t, void* );
|
|
|
|
|
2010-03-15 02:53:31 +00:00
|
|
|
static gboolean
|
|
|
|
favicon_web_done_idle_cb( gpointer vfav )
|
|
|
|
{
|
2010-03-15 15:07:23 +00:00
|
|
|
GdkPixbuf * pixbuf = NULL;
|
2010-03-17 20:30:04 +00:00
|
|
|
gboolean finished = FALSE;
|
2010-03-15 02:53:31 +00:00
|
|
|
struct favicon_data * fav = vfav;
|
|
|
|
|
2010-03-17 20:30:04 +00:00
|
|
|
if( fav->len > 0 ) /* we got something... try to make a pixbuf from it */
|
2010-03-15 15:07:23 +00:00
|
|
|
{
|
2010-03-16 00:45:12 +00:00
|
|
|
favicon_save_to_cache( fav->host, fav->contents, fav->len );
|
|
|
|
pixbuf = favicon_load_from_cache( fav->host );
|
2010-03-17 20:30:04 +00:00
|
|
|
finished = pixbuf != NULL;
|
2010-03-15 15:07:23 +00:00
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2010-03-17 20:30:04 +00:00
|
|
|
if( !finished ) /* no pixbuf yet... */
|
|
|
|
{
|
|
|
|
if( ++fav->type == IMAGE_TYPES ) /* failure */
|
|
|
|
{
|
|
|
|
finished = TRUE;
|
|
|
|
}
|
|
|
|
else /* keep trying */
|
|
|
|
{
|
|
|
|
char * url = get_url( fav->host, fav->type );
|
|
|
|
|
|
|
|
g_free( fav->contents );
|
|
|
|
fav->contents = NULL;
|
|
|
|
fav->len = 0;
|
|
|
|
|
|
|
|
tr_webRun( fav->session, url, NULL, favicon_web_done_cb, fav );
|
|
|
|
g_free( url );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( finished )
|
|
|
|
{
|
|
|
|
fav->func( pixbuf, fav->data );
|
|
|
|
g_free( fav->host );
|
|
|
|
g_free( fav->contents );
|
|
|
|
g_free( fav );
|
|
|
|
}
|
2010-03-15 02:53:31 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
favicon_web_done_cb( tr_session * session UNUSED,
|
|
|
|
long code UNUSED,
|
|
|
|
const void * data,
|
|
|
|
size_t len,
|
|
|
|
void * vfav )
|
|
|
|
{
|
|
|
|
struct favicon_data * fav = vfav;
|
|
|
|
fav->contents = g_memdup( data, len );
|
|
|
|
fav->len = len;
|
|
|
|
|
|
|
|
gtr_idle_add( favicon_web_done_idle_cb, fav );
|
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2010-03-15 02:53:31 +00:00
|
|
|
void
|
2010-03-15 14:35:43 +00:00
|
|
|
gtr_get_favicon( tr_session * session,
|
2010-03-17 17:07:40 +00:00
|
|
|
const char * host,
|
|
|
|
GFunc pixbuf_ready_func,
|
2010-03-15 14:35:43 +00:00
|
|
|
gpointer pixbuf_ready_func_data )
|
2010-03-15 02:53:31 +00:00
|
|
|
{
|
2010-03-16 00:45:12 +00:00
|
|
|
GdkPixbuf * pixbuf = favicon_load_from_cache( host );
|
2010-03-15 02:53:31 +00:00
|
|
|
|
|
|
|
if( pixbuf != NULL )
|
|
|
|
{
|
|
|
|
pixbuf_ready_func( pixbuf, pixbuf_ready_func_data );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct favicon_data * data;
|
2010-03-17 20:30:04 +00:00
|
|
|
char * url = get_url( host, 0 );
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2010-03-16 00:45:12 +00:00
|
|
|
data = g_new( struct favicon_data, 1 );
|
2010-03-17 20:30:04 +00:00
|
|
|
data->session = session;
|
2010-03-15 02:53:31 +00:00
|
|
|
data->func = pixbuf_ready_func;
|
|
|
|
data->data = pixbuf_ready_func_data;
|
|
|
|
data->host = g_strdup( host );
|
2010-03-17 20:30:04 +00:00
|
|
|
data->type = 0;
|
2010-03-15 02:53:31 +00:00
|
|
|
|
2010-03-17 20:30:04 +00:00
|
|
|
tr_webRun( session, url, NULL, favicon_web_done_cb, data );
|
2010-03-15 02:53:31 +00:00
|
|
|
g_free( url );
|
|
|
|
}
|
|
|
|
}
|
2010-04-23 01:46:02 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gtr_get_favicon_from_url( tr_session * session,
|
|
|
|
const char * url,
|
|
|
|
GFunc pixbuf_ready_func,
|
|
|
|
gpointer pixbuf_ready_func_data )
|
|
|
|
{
|
|
|
|
char * host = gtr_get_host_from_url( url );
|
|
|
|
gtr_get_favicon( session, host, pixbuf_ready_func, pixbuf_ready_func_data );
|
|
|
|
g_free( host );
|
|
|
|
}
|