From 7d0cd8201453092479d76edcfa660c7eb1e462dc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 29 Nov 2009 20:35:48 +0000 Subject: [PATCH] (trunk gtk) #2096 "magnet links" -- if a user (or, more likely, web browser) starts up a second copy of Transmission to add a magnet link via its command line, delegate that magnet link back to the already-running copy of Transmission via DBus. --- gtk/tr-core.c | 11 ++++++++--- gtk/util.c | 36 ++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/gtk/tr-core.c b/gtk/tr-core.c index cd6f26a7e..723a630df 100644 --- a/gtk/tr-core.c +++ b/gtk/tr-core.c @@ -954,7 +954,7 @@ tr_core_add_ctor( TrCore * core, tr_ctor * ctor ) /* invoked remotely via dbus. */ gboolean tr_core_add_metainfo( TrCore * core, - const char * base64_metainfo, + const char * payload, gboolean * setme_success, GError ** gerr UNUSED ) { @@ -964,7 +964,12 @@ tr_core_add_metainfo( TrCore * core, { *setme_success = FALSE; } - else + else if( gtr_is_supported_url( payload ) || gtr_is_magnet_link( payload ) ) + { + tr_core_add_from_url( core, payload ); + *setme_success = TRUE; + } + else /* base64-encoded metainfo */ { int err; int file_length; @@ -975,7 +980,7 @@ tr_core_add_metainfo( TrCore * core, ctor = tr_ctorNew( session ); tr_core_apply_defaults( ctor ); - file_contents = tr_base64_decode( base64_metainfo, -1, &file_length ); + file_contents = tr_base64_decode( payload, -1, &file_length ); err = tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length ); if( !err ) diff --git a/gtk/util.c b/gtk/util.c index 993eeab49..6767d4102 100644 --- a/gtk/util.c +++ b/gtk/util.c @@ -375,7 +375,7 @@ checkfilenames( int argc, char **argv ) ret = g_slist_prepend( ret, filename ); else { if( gtr_is_hex_hashcode( argv[i] ) ) - ret = g_slist_prepend( ret, g_strdup( argv[i] ) ); + ret = g_slist_prepend( ret, g_strdup_printf( "magnet:?xt=urn:btih:%s", argv[i] ) ); g_free( filename ); } } @@ -564,25 +564,37 @@ gtr_dbus_add_torrent( const char * filename ) { /* FIXME: why is this static? */ static gboolean success = FALSE; -#ifdef HAVE_DBUS_GLIB - DBusGProxy * proxy = NULL; - GError * err = NULL; - DBusGConnection * conn; - char * file_contents; - gsize file_length; +#ifdef HAVE_DBUS_GLIB + char * payload; + gsize file_length; + char * file_contents = NULL; + + /* If it's a file, load its contents and send them over the wire... + * it might be a temporary file that's going to disappear. */ if( g_file_get_contents( filename, &file_contents, &file_length, NULL ) ) + payload = tr_base64_encode( file_contents, file_length, NULL ); + else if( gtr_is_supported_url( filename ) || gtr_is_magnet_link( filename ) ) + payload = tr_strdup( filename ); + else + payload = NULL; + + if( payload != NULL ) { - char * b64 = tr_base64_encode( file_contents, file_length, NULL ); + GError * err = NULL; + DBusGConnection * conn; + DBusGProxy * proxy = NULL; + if(( conn = dbus_g_bus_get( DBUS_BUS_SESSION, &err ))) proxy = dbus_g_proxy_new_for_name (conn, VALUE_SERVICE_NAME, VALUE_SERVICE_OBJECT_PATH, VALUE_SERVICE_INTERFACE ); else if( err ) g_message( "err: %s", err->message ); + if( proxy ) dbus_g_proxy_call( proxy, "AddMetainfo", &err, - G_TYPE_STRING, b64, + G_TYPE_STRING, payload, G_TYPE_INVALID, G_TYPE_BOOLEAN, &success, G_TYPE_INVALID ); @@ -594,10 +606,10 @@ gtr_dbus_add_torrent( const char * filename ) if( conn ) dbus_g_connection_unref( conn ); - tr_free( b64 ); - g_free( file_contents ); + tr_free( payload ); } - else g_message( "couldn't read %s", filename ); + + g_free( file_contents ); #endif return success;