(gtk) #989: add a checkbox to auto-update the blocklist once a week

This commit is contained in:
Charles Kerr 2008-09-02 20:59:00 +00:00
parent f53b33526b
commit c3c8006c85
13 changed files with 275 additions and 145 deletions

View File

@ -23,6 +23,7 @@ AM_CFLAGS = \
noinst_HEADERS = \
actions.h \
add-dialog.h \
blocklist.h \
conf.h \
details.h \
dialogs.h \
@ -54,6 +55,7 @@ dbus_generated_sources = tr-core-dbus.h
transmission_SOURCES = \
actions.c \
add-dialog.c \
blocklist.c \
conf.c \
details.c \
dialogs.c \

158
gtk/blocklist.c Normal file
View File

@ -0,0 +1,158 @@
#include <stdarg.h>
#include <stdlib.h> /* getenv() */
#include <unistd.h> /* write() */
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gi18n.h>
#include <curl/curl.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include <libtransmission/version.h>
#include "blocklist.h"
#include "tr-core.h"
#include "tr-prefs.h"
#include "util.h"
#define BLOCKLIST_DATE "blocklist-date"
/***
****
***/
struct idle_data
{
TrCore * core;
gboolean isDone;
char * str;
};
static gboolean
emitProgressIdle( gpointer gdata )
{
struct idle_data * data = gdata;
tr_core_blocksig( data->core, data->isDone, data->str );
g_free( data->str );
g_free( data );
return FALSE;
}
static void
emitProgress( TrCore * core, gboolean isDone, const char * fmt, ... )
{
struct idle_data * data = tr_new0( struct idle_data, 1 );
va_list args;
data->core = core;
data->isDone = isDone;
va_start( args, fmt );
g_vasprintf( &data->str, fmt, args );
va_end( args );
tr_inf( "%s", data->str );
g_idle_add( emitProgressIdle, data );
}
/***
****
***/
static size_t
writeFunc( void * ptr, size_t size, size_t nmemb, void * fd )
{
const size_t byteCount = size * nmemb;
return write( *(int*)fd, ptr, byteCount );
}
static gpointer
blocklistThreadFunc( gpointer gcore )
{
TrCore * core = TR_CORE( gcore );
const char * url = "http://download.m0k.org/transmission/files/level1.gz";
gboolean ok = TRUE;
char * filename = NULL;
char * filename2 = NULL;
int fd;
int rules;
emitProgress( core, FALSE, _( "Retrieving blocklist..." ) );
if( ok )
{
GError * err = NULL;
fd = g_file_open_tmp( "transmission-blockfile-XXXXXX", &filename, &err );
if( err ) {
emitProgress( core, TRUE, _( "Unable to get blocklist: %s" ), err->message );
g_clear_error( &err );
ok = FALSE;
}
}
if( ok )
{
CURL * curl = curl_easy_init( );
curl_easy_setopt( curl, CURLOPT_URL, url );
curl_easy_setopt( curl, CURLOPT_ENCODING, "deflate" );
curl_easy_setopt( curl, CURLOPT_USERAGENT, "Transmission/" LONG_VERSION_STRING );
curl_easy_setopt( curl, CURLOPT_VERBOSE, getenv( "TR_CURL_VERBOSE" ) != NULL );
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, &fd );
curl_easy_setopt( curl, CURLOPT_NOPROGRESS, 1 );
ok = !curl_easy_perform( curl );
curl_easy_cleanup( curl );
close( fd );
}
if( !ok )
{
emitProgress( core, TRUE, _( "Unable to get blocklist." ) );
}
if( ok )
{
char * cmd;
emitProgress( core, FALSE, _( "Uncompressing blocklist..." ) );
filename2 = g_strdup_printf( "%s.txt", filename );
cmd = g_strdup_printf( "zcat %s > %s ", filename, filename2 );
tr_dbg( "%s", cmd );
system( cmd );
g_free( cmd );
}
if( ok )
{
emitProgress( core, FALSE, _( "Parsing blocklist..." ) );
rules = tr_blocklistSetContent( tr_core_handle( core ), filename2 );
}
if( ok )
{
emitProgress( core, TRUE, _( "Blocklist updated with %'d entries" ), rules );
pref_int_set( BLOCKLIST_DATE, time( NULL ) );
}
g_free( filename2 );
g_free( filename );
return NULL;
}
/***
****
***/
void
gtr_blocklist_update( TrCore * core )
{
g_thread_create( blocklistThreadFunc, core, TRUE, NULL );
}
void
gtr_blocklist_maybe_autoupdate( TrCore * core )
{
if( pref_flag_get( PREF_KEY_BLOCKLIST_UPDATES_ENABLED )
&& ( time( NULL ) - pref_int_get( BLOCKLIST_DATE ) > (60*60*24*7) ) )
gtr_blocklist_update( core );
}

10
gtk/blocklist.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef TR_GTK_BLOCKLIST_H
#define TR_GTK_BLOCKLIST_H
#include "tr-core.h"
void gtr_blocklist_update( TrCore * core );
void gtr_blocklist_maybe_autoupdate( TrCore * core );
#endif

View File

@ -160,22 +160,22 @@ getPrefs( void )
****
***/
int
int64_t
pref_int_get( const char * key )
{
int64_t i;
int64_t i = 0;
tr_bencDictFindInt( getPrefs( ), key, &i );
return i;
}
void
pref_int_set( const char * key, int value )
pref_int_set( const char * key, int64_t value )
{
tr_benc * d = getPrefs( );
tr_bencDictRemove( d, key );
tr_bencDictAddInt( d, key, value );
}
void
pref_int_set_default( const char * key, int value )
pref_int_set_default( const char * key, int64_t value )
{
if( !tr_bencDictFind( getPrefs( ), key ) )
pref_int_set( key, value );

View File

@ -29,9 +29,9 @@
#ifndef TG_CONF_H
#define TG_CONF_H
int pref_int_get ( const char * key );
void pref_int_set ( const char * key, int value );
void pref_int_set_default ( const char * key, int value );
int64_t pref_int_get ( const char * key );
void pref_int_set ( const char * key, int64_t value );
void pref_int_set_default ( const char * key, int64_t value );
gboolean pref_flag_get ( const char * key );
void pref_flag_set ( const char * key, gboolean value );

View File

@ -47,6 +47,7 @@
#include "actions.h"
#include "add-dialog.h"
#include "blocklist.h"
#include "conf.h"
#include "details.h"
#include "dialogs.h"
@ -464,6 +465,7 @@ main( int argc, char ** argv )
appsetup( win, argfiles, cbdata, startpaused, startminimized );
tr_sessionSetRPCCallback( h, onRPCChanged, cbdata );
gtr_blocklist_maybe_autoupdate( cbdata->core );
gtk_main();
}

View File

@ -91,6 +91,30 @@ tr_core_marshal_err( GClosure * closure, GValue * ret UNUSED,
callback( inst, errcode, errstr, gdata );
}
static void
tr_core_marshal_blocklist( GClosure * closure, GValue * ret UNUSED,
guint count, const GValue * vals,
gpointer hint UNUSED, gpointer marshal )
{
typedef void (*TRMarshalErr)
( gpointer, enum tr_core_err, const char *, gpointer );
TRMarshalErr callback;
GCClosure * cclosure = (GCClosure*) closure;
gboolean flag;
const char * str;
gpointer inst, gdata;
g_return_if_fail( count == 3 );
inst = g_value_peek_pointer( vals );
flag = g_value_get_boolean( vals + 1 );
str = g_value_get_string( vals + 2 );
gdata = closure->data;
callback = (TRMarshalErr)( marshal ? marshal : cclosure->callback );
callback( inst, flag, str, gdata );
}
static void
tr_core_marshal_prompt( GClosure * closure, GValue * ret UNUSED,
guint count, const GValue * vals,
@ -148,6 +172,11 @@ tr_core_class_init( gpointer g_class, gpointer g_class_data UNUSED )
core_class = TR_CORE_CLASS( g_class );
core_class->blocksig = g_signal_new( "blocklist-status",
G_TYPE_FROM_CLASS( g_class ),
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
tr_core_marshal_blocklist, G_TYPE_NONE,
2, G_TYPE_BOOLEAN, G_TYPE_STRING );
core_class->errsig = g_signal_new( "error", G_TYPE_FROM_CLASS( g_class ),
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
tr_core_marshal_err, G_TYPE_NONE,
@ -744,6 +773,12 @@ tr_core_load( TrCore * self, gboolean forcePaused )
return count;
}
void
tr_core_blocksig( TrCore * core, gboolean isDone, const char * status )
{
g_signal_emit( core, TR_CORE_GET_CLASS(core)->blocksig, 0, isDone, status );
}
static void
tr_core_errsig( TrCore * core, enum tr_core_err type, const char * msg )
{

View File

@ -60,6 +60,10 @@ typedef struct TrCoreClass
{
GObjectClass parent;
/* "blocklist" signal:
void handler( TrCore *, const char *, gpointer userData ); */
int blocksig;
/* "error" signal:
void handler( TrCore *, enum tr_core_err, const char *, gpointer ) */
int errsig;
@ -159,6 +163,9 @@ void tr_core_update( TrCore * self );
/* emit the "quit" signal */
void tr_core_quit( TrCore * self );
/* emit the "blocklist changed" signal */
void tr_core_blocksig( TrCore * core, gboolean isDone, const char * status );
/* Set a preference value, save the prefs file, and emit the
"prefs-changed" signal */
void tr_core_set_pref( TrCore * self, const char * key, const char * val );
@ -171,6 +178,7 @@ void tr_core_set_pref_bool( TrCore * self, const char * key, gboolean val );
"prefs-changed" signal */
void tr_core_set_pref_int( TrCore * self, const char * key, int val );
/**
***
**/

View File

@ -21,6 +21,7 @@
#include <libtransmission/utils.h>
#include <libtransmission/version.h>
#include <libtransmission/web.h>
#include "blocklist.h"
#include "conf.h"
#include "hig.h"
#include "tr-core.h"
@ -54,7 +55,8 @@ tr_prefs_init_global( void )
pref_int_set_default ( PREF_KEY_PEER_SOCKET_TOS, TR_DEFAULT_PEER_SOCKET_TOS );
pref_flag_set_default ( PREF_KEY_ALLOW_HIBERNATION, FALSE );
pref_flag_set_default ( PREF_KEY_BLOCKLIST_ENABLED, TR_DEFAULT_BLOCKLIST_ENABLED );
pref_flag_set_default ( PREF_KEY_BLOCKLIST_ENABLED, TRUE );
pref_flag_set_default ( PREF_KEY_BLOCKLIST_UPDATES_ENABLED, TRUE );
pref_string_set_default ( PREF_KEY_OPEN_DIALOG_FOLDER, g_get_home_dir( ) );
@ -349,6 +351,7 @@ struct blocklist_data
GtkWidget * check;
GtkWidget * dialog;
TrCore * core;
gulong id;
int abortFlag;
char secondary[256];
};
@ -363,122 +366,27 @@ updateBlocklistText( GtkWidget * w, TrCore * core )
"Ignore the %'d _blocklisted peers", n ), n );
gtk_button_set_label( GTK_BUTTON( w ), buf );
}
static gboolean
updateBlocklistTextFromData( gpointer gdata )
static void
onBlocklistDialogResponse( GtkDialog * d, int response UNUSED, gpointer gdata )
{
struct blocklist_data * data = gdata;
updateBlocklistText( data->check, data->core );
return FALSE;
}
static gboolean
blocklistDialogSetSecondary( gpointer gdata )
{
struct blocklist_data * data = gdata;
GtkMessageDialog * md = GTK_MESSAGE_DIALOG( data->dialog );
gtk_message_dialog_format_secondary_text( md, data->secondary );
return FALSE;
}
static gboolean
blocklistDialogAllowClose( gpointer dialog )
{
GtkDialog * d = GTK_DIALOG( dialog );
gtk_dialog_set_response_sensitive( GTK_DIALOG( d ), GTK_RESPONSE_CANCEL, FALSE );
gtk_dialog_set_response_sensitive( GTK_DIALOG( d ), GTK_RESPONSE_CLOSE, TRUE );
return FALSE;
g_signal_handler_disconnect( data->core, data->id );
gtk_widget_destroy( GTK_WIDGET( d ) );
}
static void
got_blocklist( tr_handle * handle UNUSED,
long response_code UNUSED,
const void * response,
size_t response_len,
void * gdata )
onBlocklistStatus( TrCore * core UNUSED, gboolean isDone, const char * status, gpointer gdata )
{
struct blocklist_data * data = gdata;
const char * text = response;
int size = response_len;
int rules = 0;
gchar * filename = NULL;
gchar * filename2 = NULL;
int fd = -1;
int ok = 1;
if( !data->abortFlag && ( !text || !size ) )
{
ok = FALSE;
g_snprintf( data->secondary, sizeof( data->secondary ),
_( "Unable to get blocklist." ) );
g_message( data->secondary );
g_idle_add( blocklistDialogSetSecondary, data );
}
if( ok && !data->abortFlag )
{
GError * err = NULL;
fd = g_file_open_tmp( "transmission-blockfile-XXXXXX", &filename, &err );
if( err ) {
g_snprintf( data->secondary, sizeof( data->secondary ),
_( "Unable to get blocklist: %s" ), err->message );
g_warning( data->secondary );
g_idle_add( blocklistDialogSetSecondary, data );
g_clear_error( &err );
ok = FALSE;
} else {
write( fd, text, size );
close( fd );
}
}
if( ok && !data->abortFlag )
{
char * cmd;
filename2 = g_strdup_printf( "%s.txt", filename );
g_snprintf( data->secondary, sizeof( data->secondary ),
_( "Uncompressing blocklist..." ) );
g_idle_add( blocklistDialogSetSecondary, data );
cmd = g_strdup_printf( "zcat %s > %s ", filename, filename2 );
tr_dbg( "%s", cmd );
system( cmd );
g_free( cmd );
}
if( ok && !data->abortFlag )
{
g_snprintf( data->secondary, sizeof( data->secondary ),
_( "Parsing blocklist..." ) );
g_idle_add( blocklistDialogSetSecondary, data );
rules = tr_blocklistSetContent( tr_core_handle( data->core ), filename2 );
}
if( ok && !data->abortFlag )
{
g_snprintf( data->secondary, sizeof( data->secondary ),
_( "Blocklist updated with %'d entries" ), rules );
g_idle_add( blocklistDialogSetSecondary, data );
g_idle_add( blocklistDialogAllowClose, data->dialog );
g_idle_add( updateBlocklistTextFromData, data );
}
/* g_free( data ); */
if( filename2 ) {
unlink( filename2 );
g_free( filename2 );
}
if( filename ) {
unlink( filename );
g_free( filename );
}
}
static void
onUpdateBlocklistResponseCB( GtkDialog * dialog, int response, gpointer vdata )
{
struct blocklist_data * data = vdata;
if( response == GTK_RESPONSE_CANCEL )
data->abortFlag = 1;
data->dialog = NULL;
gtk_widget_destroy( GTK_WIDGET( dialog ) );
gdk_threads_enter( );
gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( data->dialog ), status );
gtk_dialog_set_response_sensitive( GTK_DIALOG( data->dialog ), GTK_RESPONSE_CANCEL, !isDone );
gtk_dialog_set_response_sensitive( GTK_DIALOG( data->dialog ), GTK_RESPONSE_CLOSE, isDone );
if( isDone )
updateBlocklistText( data->check, core );
gdk_threads_leave( );
}
static void
@ -486,28 +394,26 @@ onUpdateBlocklistCB( GtkButton * w, gpointer gdata )
{
GtkWidget * d;
struct blocklist_data * data = gdata;
tr_handle * handle = g_object_get_data( G_OBJECT( w ), "handle" );
const char * url = "http://download.m0k.org/transmission/files/level1.gz";
d = gtk_message_dialog_new( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( w ) ) ),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_NONE,
_( "Updating Blocklist" ) );
gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( d ),
_( "Retrieving blocklist..." ) );
data->dialog = d;
data->id = g_signal_connect( data->core, "blocklist-status", G_CALLBACK( onBlocklistStatus ), data );
gtk_dialog_add_buttons( GTK_DIALOG( d ),
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
NULL );
gtk_dialog_set_response_sensitive( GTK_DIALOG( d ), GTK_RESPONSE_CLOSE, FALSE );
data->dialog = d;
g_signal_connect( d, "response", G_CALLBACK(onUpdateBlocklistResponseCB), data );
g_signal_connect( d, "response", G_CALLBACK(onBlocklistDialogResponse), data );
gtk_widget_show( d );
tr_webRun( handle, url, NULL, got_blocklist, data );
gtr_blocklist_update( data->core );
}
static void
@ -532,26 +438,12 @@ peerPage( GObject * core, gboolean * alive )
GtkWidget * l;
struct blocklist_data * data;
data = g_new0( struct blocklist_data, 1 );
data->core = TR_CORE( core );
t = hig_workarea_create( );
hig_workarea_add_section_title (t, &row, _("Options"));
w = new_check_button( "", PREF_KEY_BLOCKLIST_ENABLED, core );
updateBlocklistText( w, TR_CORE( core ) );
h = gtk_hbox_new( FALSE, GUI_PAD_BIG );
gtk_box_pack_start_defaults( GTK_BOX(h), w );
b = gtk_button_new_with_mnemonic( _( "_Update Blocklist" ) );
data = g_new0( struct blocklist_data, 1 );
data->core = TR_CORE( core );
data->check = w;
g_object_set_data( G_OBJECT( b ), "handle", tr_core_handle( TR_CORE( core ) ) );
g_signal_connect( b, "clicked", G_CALLBACK(onUpdateBlocklistCB), data );
gtk_box_pack_start( GTK_BOX(h), b, FALSE, FALSE, 0 );
g_signal_connect( w, "toggled", G_CALLBACK(target_cb), b );
target_cb( w, b );
hig_workarea_add_wide_control( t, &row, h );
s = _("_Ignore unencrypted peers");
w = gtk_check_button_new_with_mnemonic( s );
gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(w),
@ -577,6 +469,28 @@ peerPage( GObject * core, gboolean * alive )
testing_port_cb( NULL, l );
g_signal_connect( w2, "value-changed", G_CALLBACK(testing_port_cb), l );
hig_workarea_add_section_divider( t, &row );
hig_workarea_add_section_title( t, &row, _( "Blocklist" ) );
w = new_check_button( "", PREF_KEY_BLOCKLIST_ENABLED, core );
updateBlocklistText( w, TR_CORE( core ) );
h = gtk_hbox_new( FALSE, GUI_PAD_BIG );
gtk_box_pack_start_defaults( GTK_BOX(h), w );
b = gtk_button_new_with_mnemonic( _( "_Update Blocklist" ) );
data->check = w;
g_object_set_data( G_OBJECT( b ), "handle", tr_core_handle( TR_CORE( core ) ) );
g_signal_connect( b, "clicked", G_CALLBACK(onUpdateBlocklistCB), data );
gtk_box_pack_start( GTK_BOX(h), b, FALSE, FALSE, 0 );
g_signal_connect( w, "toggled", G_CALLBACK(target_cb), b );
target_cb( w, b );
hig_workarea_add_wide_control( t, &row, h );
s = _( "_Enable automatic updates" );
w = new_check_button( s, PREF_KEY_BLOCKLIST_UPDATES_ENABLED, core );
hig_workarea_add_wide_control( t, &row, w );
g_signal_connect( data->check, "toggled", G_CALLBACK(target_cb), w );
target_cb( data->check, w );
hig_workarea_add_section_divider( t, &row );
hig_workarea_add_section_title( t, &row, _( "Limits" ) );

View File

@ -56,6 +56,7 @@ GtkWidget * tr_prefs_dialog_new( GObject * core, GtkWindow * parent );
#define PREF_KEY_MAX_PEERS_GLOBAL "max-peers-global"
#define PREF_KEY_MAX_PEERS_PER_TORRENT "max-peers-per-torrent"
#define PREF_KEY_BLOCKLIST_ENABLED "blocklist-enabled"
#define PREF_KEY_BLOCKLIST_UPDATES_ENABLED "blocklist-updates-enabled"
#define PREF_KEY_MAIN_WINDOW_HEIGHT "main-window-height"
#define PREF_KEY_MAIN_WINDOW_WIDTH "main-window-width"
#define PREF_KEY_MAIN_WINDOW_X "main-window-x"

View File

@ -172,7 +172,7 @@ gtr_localtime( time_t time )
return g_locale_to_utf8( buf, -1, NULL, NULL, NULL );
}
gboolean
int
mkdir_p( const char * path, mode_t mode )
{
#if GLIB_CHECK_VERSION( 2, 8, 0)

View File

@ -40,7 +40,7 @@
typedef void (*callbackfunc_t)(void*);
/* return a human-readable string for the size given in bytes. */
char* tr_strlsize( char * buf, guint64 size, size_t buflen );
char* tr_strlsize( char * buf, uint64_t size, size_t buflen );
/* return a human-readable string for the transfer rate given in bytes. */
char* tr_strlspeed (char * buf, double KiBps, size_t buflen );
@ -54,8 +54,7 @@ char* tr_strltime( char * buf, int secs, size_t buflen );
char* gtr_localtime( time_t time );
/* create a directory and any missing parent directories */
gboolean
mkdir_p(const char *name, mode_t mode);
int mkdir_p(const char *name, mode_t mode);
/* create a copy of a GSList of strings, this dups the actual strings too */
GSList *

View File

@ -1,6 +1,7 @@
[encoding: UTF-8]
gtk/actions.c
gtk/add-dialog.c
gtk/blocklist.c
gtk/conf.c
gtk/details.c
gtk/dialogs.c