Changeset 11512


Ignore:
Timestamp:
Dec 10, 2010, 6:51:05 PM (11 years ago)
Author:
charles
Message:

(trunk) #3817 "use the OS' proxy support" -- implemented for libtransmission, transmission-gtk

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/main.c

    r11368 r11512  
    3636#include <glib/gstdio.h>
    3737
     38#include <curl/curl.h>
     39
    3840#include <libtransmission/transmission.h>
    3941#include <libtransmission/rpcimpl.h>
    4042#include <libtransmission/utils.h>
    4143#include <libtransmission/version.h>
     44#include <libtransmission/web.h>
    4245
    4346#include "actions.h"
     
    529532}
    530533
     534
     535#ifdef HAVE_GCONF2
     536static void
     537applyDesktopProxySettings( CURL * easy, GConfClient * client, const char * host_key, const char * port_key )
     538{
     539    int port;
     540    GConfValue * value;
     541
     542    if(( value = gconf_client_get( client, host_key, NULL )))
     543    {
     544        const char * url = gconf_value_get_string( value );
     545        if( url && *url )
     546        {
     547            char * scheme = NULL;
     548
     549            if( !tr_urlParse( url, strlen( url ), &scheme, NULL, NULL, NULL ) )
     550            {
     551                if( !gtr_strcmp0( scheme, "socks4" ) )
     552                    curl_easy_setopt( easy, CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS4 );
     553                else if( !gtr_strcmp0( scheme, "socks5" ) )
     554                    curl_easy_setopt( easy, CURLOPT_PROXYTYPE, (long)CURLPROXY_SOCKS5 );
     555                else if( !gtr_strcmp0( scheme, "http" ) )
     556                    curl_easy_setopt( easy, CURLOPT_PROXYTYPE, (long)CURLPROXY_HTTP );
     557            }
     558
     559            curl_easy_setopt( easy, CURLOPT_PROXY, url );
     560
     561            if( port_key != NULL )
     562                if(( value = gconf_client_get( client, port_key, NULL )))
     563                    if(( port = gconf_value_get_int( value )))
     564                        curl_easy_setopt( easy, CURLOPT_PROXYPORT, (long)port );
     565
     566            tr_free( scheme );
     567        }
     568    }
     569}
     570#endif
     571
     572static void
     573curlConfigFunc( tr_session * session UNUSED, void * vcurl UNUSED, const char * destination )
     574{
     575#ifdef HAVE_GCONF2
     576    const char * str;
     577    GConfValue * value;
     578    CURL * easy = vcurl;
     579    gboolean use_http_proxy = TRUE;
     580    GConfClient * client = gconf_client_get_default( );
     581
     582    /* get GNOME's proxy mode */
     583    if(( value = gconf_client_get( client, "/system/proxy/mode", NULL )))
     584    {
     585        char * mode = g_strdup( gconf_value_get_string( value ) );
     586
     587        if( !gtr_strcmp0( mode, "auto" ) )
     588        {
     589            applyDesktopProxySettings( easy, client, "/system/proxy/autoconfig_url", NULL );
     590            use_http_proxy = FALSE;
     591        }
     592        else if( !gtr_strcmp0( mode, "manual" ))
     593        {
     594            char * scheme = NULL;
     595            if( !tr_urlParse( destination, strlen( destination ), &scheme, NULL, NULL, NULL ) )
     596            {
     597                if( !gtr_strcmp0( scheme, "ftp" ) )
     598                {
     599                    applyDesktopProxySettings( easy, client, "/system/proxy/ftp_host", "/system/proxy/ftp_port" );
     600                    use_http_proxy = FALSE;
     601                }
     602                else if( !gtr_strcmp0( scheme, "https" ) )
     603                {
     604                    applyDesktopProxySettings( easy, client, "/system/proxy/secure_host", "/system/proxy/secure_port" );
     605                    use_http_proxy = FALSE;
     606                }
     607            }
     608            tr_free( scheme );
     609        }
     610
     611        g_free( mode );
     612    }
     613
     614    /* if this the proxy hasn't been handled yet and "use_http_proxy" is disabled, then don't use a proxy */
     615    if( use_http_proxy )
     616        if(( value = gconf_client_get( client, "/system/http_proxy/use_http_proxy", NULL )))
     617            use_http_proxy = gconf_value_get_bool( value ) != 0;
     618
     619    if( use_http_proxy )
     620    {
     621        applyDesktopProxySettings( easy, client, "/system/http_proxy/host", "/system/http_proxy/port" );
     622
     623        if((( value = gconf_client_get( client, "/system/http_proxy/use_authentication", NULL ))) &&  gconf_value_get_bool( value ))
     624        {
     625            const char * user = NULL;
     626            const char * pass = NULL;
     627
     628            if(( value = gconf_client_get( client, "/system/http_proxy/authentication_user", NULL )))
     629                user = str = gconf_value_get_string( value );
     630            if(( value = gconf_client_get( client, "/system/http_proxy/authentication_password", NULL )))
     631                pass = str = gconf_value_get_string( value );
     632
     633           if( ( user != NULL ) && ( pass != NULL ) )
     634           {
     635               char * userpass = g_strdup_printf( "%s:%s", user, pass );
     636               curl_easy_setopt( easy, CURLOPT_PROXYUSERPWD, userpass );
     637               g_free( userpass );
     638           }
     639       }
     640    }
     641#endif
     642}
     643
    531644int
    532645main( int argc, char ** argv )
     
    661774        /* initialize the libtransmission session */
    662775        session = tr_sessionInit( "gtk", configDir, TRUE, pref_get_all( ) );
     776        tr_sessionSetWebConfigFunc( session, curlConfigFunc );
     777
    663778        pref_flag_set( TR_PREFS_KEY_ALT_SPEED_ENABLED, tr_sessionUsesAltSpeed( session ) );
    664779        pref_int_set( TR_PREFS_KEY_PEER_PORT, tr_sessionGetPeerPort( session ) );
  • trunk/gtk/tr-prefs.c

    r11401 r11512  
    11131113
    11141114static void
    1115 peerPageDestroyed( gpointer gdata, GObject * dead UNUSED )
     1115networkPageDestroyed( gpointer gdata, GObject * dead UNUSED )
    11161116{
    11171117    struct network_page_data * data = gdata;
     
    11471147}
    11481148
    1149 static GtkWidget*
    1150 peerPage( GObject * core )
     1149static void
     1150onGNOMEClicked( GtkButton * button, gpointer vdata UNUSED )
     1151{
     1152    GError * err = NULL;
     1153
     1154    if( !g_spawn_command_line_async( "gnome-network-properties", &err ) )
     1155    {
     1156        GtkWidget * d = gtk_message_dialog_new( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( button ) ) ),
     1157                                                GTK_DIALOG_DESTROY_WITH_PARENT,
     1158                                                GTK_MESSAGE_ERROR,
     1159                                                GTK_BUTTONS_CLOSE,
     1160                                                "%s", err->message );
     1161        g_signal_connect_swapped( d, "response", G_CALLBACK( gtk_widget_destroy ), d );
     1162        gtk_widget_show( d );
     1163        g_clear_error( &err );
     1164    }
     1165}
     1166
     1167static GtkWidget*
     1168networkPage( GObject * core )
    11511169{
    11521170    int                        row = 0;
     
    11791197    hig_workarea_add_row( t, &row, NULL, h, NULL );
    11801198    data->prefsTag = g_signal_connect( TR_CORE( core ), "prefs-changed", G_CALLBACK( onCorePrefsChanged ), data );
    1181     g_object_weak_ref( G_OBJECT( t ), peerPageDestroyed, data );
     1199    g_object_weak_ref( G_OBJECT( t ), networkPageDestroyed, data );
    11821200
    11831201    s = _( "Pick a _random port every time Transmission is started" );
     
    11961214    w = new_spin_button( TR_PREFS_KEY_PEER_LIMIT_GLOBAL, core, 1, 3000, 5 );
    11971215    hig_workarea_add_row( t, &row, _( "Maximum peers _overall:" ), w, NULL );
     1216
     1217    hig_workarea_add_section_divider( t, &row );
     1218    hig_workarea_add_section_title( t, &row, _( "Options" ) );
     1219
     1220    w = gtk_button_new_with_mnemonic( _( "Launch GNOME Network Preferences" ) );
     1221    g_signal_connect( w, "clicked", G_CALLBACK( onGNOMEClicked ), data );
     1222    hig_workarea_add_row( t, &row, _( "Pro_xies:" ), w, NULL );
    11981223
    11991224    hig_workarea_finish( t, &row );
     
    12351260                              gtk_label_new ( _( "Privacy" ) ) );
    12361261    gtk_notebook_append_page( GTK_NOTEBOOK( n ),
    1237                               peerPage( core ),
     1262                              networkPage( core ),
    12381263                              gtk_label_new ( _( "Network" ) ) );
    12391264    gtk_notebook_append_page( GTK_NOTEBOOK( n ),
  • trunk/libtransmission/session.c

    r11496 r11512  
    24722472    }
    24732473}
     2474
     2475/***
     2476****
     2477***/
     2478
     2479void
     2480tr_sessionSetWebConfigFunc( tr_session * session, void (*func)(tr_session*, void*, const char* ) )
     2481{
     2482    session->curl_easy_config_func = func;
     2483}
  • trunk/libtransmission/session.h

    r11368 r11512  
    4444struct tr_cache;
    4545struct tr_fdInfo;
     46
     47typedef void ( tr_web_config_func )( tr_session * session, void * curl_pointer, const char * url );
    4648
    4749struct tr_turtle_info
     
    191193
    192194    tr_bool bufferInUse;
     195
     196    tr_web_config_func          * curl_easy_config_func;
    193197};
    194198
  • trunk/libtransmission/web.c

    r11398 r11512  
    172172    if( task->range )
    173173        curl_easy_setopt( e, CURLOPT_RANGE, task->range );
     174
     175    if( s->curl_easy_config_func != NULL )
     176        s->curl_easy_config_func( s, e, task->url );
    174177
    175178    tr_free( cookie_filename );
  • trunk/libtransmission/web.h

    r10535 r11512  
    2525tr_web_close_mode;
    2626
     27/**
     28 * This is a mechanism for adjusting your CURL* object to match
     29 * the host OS's platform-dependent settings.
     30 *
     31 * A use case for this function is to call curl_easy_setopt() on curl_pointer.
     32 *
     33 * Examples of curl_easy_setopt() can be found at
     34 * http://curl.haxx.se/libcurl/c/curl_easy_setopt.html()
     35 */
     36void tr_sessionSetWebConfigFunc( tr_session * session, void (*config)(tr_session * session, void * curl_pointer, const char * url ) );
     37
     38
    2739void tr_webClose( tr_session * session, tr_web_close_mode close_mode );
    2840
Note: See TracChangeset for help on using the changeset viewer.