2008-02-13 03:00:21 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* 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.
|
|
|
|
*
|
2008-02-26 21:08:51 +00:00
|
|
|
* $Id$
|
2008-02-13 03:00:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
2008-04-03 02:11:15 +00:00
|
|
|
#include "add-dialog.h"
|
2008-03-18 20:41:27 +00:00
|
|
|
#include "conf.h"
|
2008-02-13 03:00:21 +00:00
|
|
|
#include "file-list.h"
|
|
|
|
#include "hig.h"
|
2008-03-18 20:41:27 +00:00
|
|
|
#include "tr-prefs.h"
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2008-04-03 02:11:15 +00:00
|
|
|
struct AddData
|
2008-02-13 03:00:21 +00:00
|
|
|
{
|
|
|
|
TrCore * core;
|
|
|
|
GtkWidget * list;
|
2008-03-18 16:18:22 +00:00
|
|
|
GtkWidget * run_check;
|
|
|
|
GtkWidget * trash_check;
|
2008-02-13 03:00:21 +00:00
|
|
|
char * filename;
|
|
|
|
char * destination;
|
|
|
|
TrTorrent * gtor;
|
|
|
|
tr_ctor * ctor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2008-04-03 02:11:15 +00:00
|
|
|
deleteOldTorrent( struct AddData * data )
|
2008-02-13 03:00:21 +00:00
|
|
|
{
|
|
|
|
if( data->gtor )
|
|
|
|
{
|
2008-02-15 18:20:56 +00:00
|
|
|
file_list_set_torrent( data->list, NULL );
|
|
|
|
|
2008-02-19 05:10:58 +00:00
|
|
|
tr_torrent_set_delete_flag( data->gtor, TRUE );
|
2008-02-13 03:00:21 +00:00
|
|
|
g_object_unref( G_OBJECT( data->gtor ) );
|
|
|
|
data->gtor = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-03 02:11:15 +00:00
|
|
|
addResponseCB( GtkDialog * dialog, gint response, gpointer gdata )
|
2008-02-13 03:00:21 +00:00
|
|
|
{
|
2008-04-03 02:11:15 +00:00
|
|
|
struct AddData * data = gdata;
|
2008-02-13 03:00:21 +00:00
|
|
|
|
|
|
|
if( data->gtor )
|
|
|
|
{
|
|
|
|
if( response != GTK_RESPONSE_ACCEPT )
|
|
|
|
deleteOldTorrent( data );
|
|
|
|
else {
|
2008-03-18 16:18:22 +00:00
|
|
|
if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( data->run_check ) ) )
|
2008-02-13 03:00:21 +00:00
|
|
|
tr_torrentStart( tr_torrent_handle( data->gtor ) );
|
|
|
|
tr_core_add_torrent( data->core, data->gtor );
|
2008-03-18 16:18:22 +00:00
|
|
|
if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( data->trash_check ) ) )
|
2008-03-09 15:27:08 +00:00
|
|
|
tr_file_trash_or_unlink( data->filename );
|
2008-02-13 03:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_ctorFree( data->ctor );
|
|
|
|
g_free( data->filename );
|
|
|
|
g_free( data->destination );
|
|
|
|
g_free( data );
|
|
|
|
gtk_widget_destroy( GTK_WIDGET( dialog ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-04-03 02:11:15 +00:00
|
|
|
updateTorrent( struct AddData * o )
|
2008-02-13 03:00:21 +00:00
|
|
|
{
|
2008-02-14 17:18:00 +00:00
|
|
|
if( o->gtor )
|
|
|
|
tr_torrentSetFolder( tr_torrent_handle( o->gtor ), o->destination );
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
file_list_set_torrent( o->list, o->gtor );
|
2008-02-13 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sourceChanged( GtkFileChooserButton * b, gpointer gdata )
|
|
|
|
{
|
2008-04-03 02:11:15 +00:00
|
|
|
struct AddData * data = gdata;
|
2008-02-13 03:00:21 +00:00
|
|
|
|
|
|
|
deleteOldTorrent( data );
|
|
|
|
|
|
|
|
g_free( data->filename );
|
|
|
|
data->filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( b ) );
|
|
|
|
|
|
|
|
/* maybe instantiate a torrent */
|
|
|
|
if( data->filename ) {
|
|
|
|
int err = 0;
|
|
|
|
tr_torrent * torrent;
|
|
|
|
tr_handle * handle = tr_core_handle( data->core );
|
|
|
|
tr_ctorSetMetainfoFromFile( data->ctor, data->filename );
|
|
|
|
tr_ctorSetDestination( data->ctor, TR_FORCE, data->destination );
|
2008-02-14 17:18:00 +00:00
|
|
|
tr_ctorSetPaused( data->ctor, TR_FORCE, TRUE );
|
|
|
|
tr_ctorSetDeleteSource( data->ctor, FALSE );
|
2008-02-13 03:00:21 +00:00
|
|
|
if(( torrent = tr_torrentNew( handle, data->ctor, &err )))
|
|
|
|
data->gtor = tr_torrent_new_preexisting( torrent );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTorrent( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
verifyRequested( GtkButton * button UNUSED, gpointer gdata )
|
|
|
|
{
|
2008-04-03 02:11:15 +00:00
|
|
|
struct AddData * data = gdata;
|
2008-02-13 03:00:21 +00:00
|
|
|
if( data->gtor )
|
2008-02-27 17:38:39 +00:00
|
|
|
tr_torrentVerify( tr_torrent_handle( data->gtor ) );
|
2008-02-13 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destinationChanged( GtkFileChooserButton * b, gpointer gdata )
|
|
|
|
{
|
2008-04-13 01:36:53 +00:00
|
|
|
char * fname = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( b ) );
|
|
|
|
if( fname )
|
|
|
|
{
|
|
|
|
struct AddData * data = gdata;
|
|
|
|
g_free( data->destination );
|
|
|
|
data->destination = fname;
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2008-04-13 01:36:53 +00:00
|
|
|
updateTorrent( data );
|
|
|
|
verifyRequested( NULL, data );
|
|
|
|
}
|
2008-02-13 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
2008-03-19 20:34:35 +00:00
|
|
|
static void
|
|
|
|
addTorrentFilters( GtkFileChooser * chooser )
|
|
|
|
{
|
|
|
|
GtkFileFilter * filter;
|
|
|
|
|
|
|
|
filter = gtk_file_filter_new( );
|
|
|
|
gtk_file_filter_set_name( filter, _( "Torrent files" ) );
|
|
|
|
gtk_file_filter_add_pattern( filter, "*.torrent" );
|
|
|
|
gtk_file_chooser_add_filter( chooser, filter );
|
|
|
|
|
|
|
|
filter = gtk_file_filter_new( );
|
|
|
|
gtk_file_filter_set_name( filter, _( "All files" ) );
|
|
|
|
gtk_file_filter_add_pattern( filter, "*" );
|
|
|
|
gtk_file_chooser_add_filter( chooser, filter );
|
|
|
|
}
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2008-02-19 05:52:37 +00:00
|
|
|
GtkWidget*
|
2008-04-03 02:11:15 +00:00
|
|
|
addSingleTorrentDialog( GtkWindow * parent,
|
|
|
|
TrCore * core,
|
|
|
|
tr_ctor * ctor )
|
2008-02-13 03:00:21 +00:00
|
|
|
{
|
|
|
|
int row;
|
|
|
|
int col;
|
|
|
|
const char * str;
|
|
|
|
GtkWidget * w;
|
|
|
|
GtkWidget * d;
|
|
|
|
GtkWidget * t;
|
|
|
|
GtkWidget * l;
|
2008-04-03 02:11:15 +00:00
|
|
|
struct AddData * data;
|
2008-02-13 03:00:21 +00:00
|
|
|
uint8_t flag;
|
|
|
|
|
|
|
|
/* make the dialog */
|
2008-03-19 01:49:07 +00:00
|
|
|
d = gtk_dialog_new_with_buttons( _( "Torrent Options" ), parent,
|
2008-02-14 17:18:00 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_NO_SEPARATOR,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
2008-04-03 02:11:15 +00:00
|
|
|
GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT,
|
2008-02-14 17:18:00 +00:00
|
|
|
NULL );
|
2008-02-28 14:48:23 +00:00
|
|
|
gtk_dialog_set_default_response( GTK_DIALOG( d ),
|
|
|
|
GTK_RESPONSE_ACCEPT );
|
|
|
|
gtk_dialog_set_alternative_button_order( GTK_DIALOG( d ),
|
|
|
|
GTK_RESPONSE_ACCEPT,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1 );
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2008-02-13 03:57:48 +00:00
|
|
|
if( tr_ctorGetDestination( ctor, TR_FORCE, &str ) )
|
|
|
|
g_assert_not_reached( );
|
2008-04-13 01:36:53 +00:00
|
|
|
g_assert( str );
|
2008-03-18 16:18:22 +00:00
|
|
|
|
2008-04-03 02:11:15 +00:00
|
|
|
data = g_new0( struct AddData, 1 );
|
2008-02-13 03:00:21 +00:00
|
|
|
data->core = core;
|
|
|
|
data->ctor = ctor;
|
|
|
|
data->filename = g_strdup( tr_ctorGetSourceFile( ctor ) );
|
|
|
|
data->destination = g_strdup( str );
|
2008-03-18 16:18:22 +00:00
|
|
|
data->list = file_list_new( NULL );
|
2008-03-18 19:14:21 +00:00
|
|
|
data->trash_check = gtk_check_button_new_with_mnemonic( _( "Mo_ve source file to Trash" ) );
|
2008-04-03 02:11:15 +00:00
|
|
|
data->run_check = gtk_check_button_new_with_mnemonic( _( "_Start when added" ) );
|
2008-03-18 16:18:22 +00:00
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
g_signal_connect( G_OBJECT( d ), "response",
|
2008-04-03 02:11:15 +00:00
|
|
|
G_CALLBACK( addResponseCB ), data );
|
2008-02-13 03:00:21 +00:00
|
|
|
|
|
|
|
t = gtk_table_new( 6, 2, FALSE );
|
|
|
|
gtk_container_set_border_width( GTK_CONTAINER( t ), GUI_PAD_BIG );
|
|
|
|
gtk_table_set_row_spacings( GTK_TABLE( t ), GUI_PAD );
|
|
|
|
gtk_table_set_col_spacings( GTK_TABLE( t ), GUI_PAD_BIG );
|
|
|
|
|
|
|
|
row = col = 0;
|
2008-03-18 19:14:21 +00:00
|
|
|
l = gtk_label_new_with_mnemonic( _( "_Source file:" ) );
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
|
|
|
|
gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
|
|
|
|
++col;
|
2008-03-19 20:34:35 +00:00
|
|
|
w = gtk_file_chooser_button_new( _( "Select Source File" ),
|
2008-02-14 17:18:00 +00:00
|
|
|
GTK_FILE_CHOOSER_ACTION_OPEN );
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
|
|
|
|
gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
|
2008-03-19 20:34:35 +00:00
|
|
|
addTorrentFilters( GTK_FILE_CHOOSER( w ) );
|
2008-02-14 17:18:00 +00:00
|
|
|
g_signal_connect( w, "selection-changed",
|
|
|
|
G_CALLBACK( sourceChanged ), data );
|
2008-02-13 03:00:21 +00:00
|
|
|
if( data->filename )
|
2008-02-13 04:34:51 +00:00
|
|
|
if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->filename ) )
|
|
|
|
g_warning( "couldn't select '%s'", data->filename );
|
2008-02-13 03:00:21 +00:00
|
|
|
|
|
|
|
++row;
|
|
|
|
col = 0;
|
2008-03-19 17:14:47 +00:00
|
|
|
l = gtk_label_new_with_mnemonic( _( "_Destination folder:" ) );
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
|
|
|
|
gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
|
|
|
|
++col;
|
2008-03-19 20:34:35 +00:00
|
|
|
w = gtk_file_chooser_button_new( _( "Select Destination Folder" ), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
|
2008-02-13 04:34:51 +00:00
|
|
|
if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->destination ) )
|
|
|
|
g_warning( "couldn't select '%s'", data->destination );
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
|
|
|
|
gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
|
|
|
|
g_signal_connect( w, "selection-changed", G_CALLBACK( destinationChanged ), data );
|
|
|
|
|
|
|
|
++row;
|
|
|
|
col = 0;
|
2008-03-18 16:18:22 +00:00
|
|
|
w = data->list;
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_widget_set_size_request ( w, 466u, 300u );
|
|
|
|
gtk_table_attach_defaults( GTK_TABLE( t ), w, col, col+2, row, row+1 );
|
|
|
|
|
|
|
|
++row;
|
|
|
|
col = 0;
|
|
|
|
w = gtk_button_new_with_mnemonic( _( "Verify Local Data" ) );
|
|
|
|
gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
|
|
|
|
g_signal_connect( w, "clicked", G_CALLBACK( verifyRequested ), data );
|
|
|
|
|
|
|
|
++row;
|
|
|
|
col = 0;
|
2008-03-18 19:14:21 +00:00
|
|
|
w = data->run_check;
|
|
|
|
if( tr_ctorGetPaused( ctor, TR_FORCE, &flag ) )
|
2008-03-09 15:27:08 +00:00
|
|
|
g_assert_not_reached( );
|
2008-03-18 19:14:21 +00:00
|
|
|
gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), !flag );
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
|
|
|
|
|
|
|
|
++row;
|
|
|
|
col = 0;
|
2008-03-18 19:14:21 +00:00
|
|
|
w = data->trash_check;
|
|
|
|
if( tr_ctorGetDeleteSource( ctor, &flag ) )
|
2008-02-13 03:57:48 +00:00
|
|
|
g_assert_not_reached( );
|
2008-03-18 19:14:21 +00:00
|
|
|
gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), flag );
|
2008-02-13 03:00:21 +00:00
|
|
|
gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
|
|
|
|
|
|
|
|
gtk_box_pack_start_defaults( GTK_BOX( GTK_DIALOG( d )->vbox ), t );
|
|
|
|
gtk_widget_show_all( d );
|
2008-02-19 05:52:37 +00:00
|
|
|
return d;
|
2008-02-13 03:00:21 +00:00
|
|
|
}
|
2008-03-18 01:22:11 +00:00
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static void
|
2008-04-03 02:13:13 +00:00
|
|
|
onAddDialogResponse( GtkDialog * dialog, int response, gpointer core )
|
2008-03-18 01:22:11 +00:00
|
|
|
{
|
2008-03-19 14:54:32 +00:00
|
|
|
char * folder;
|
|
|
|
|
|
|
|
/* remember this folder the next time we use this dialog */
|
|
|
|
folder = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER( dialog ) );
|
2008-03-18 20:41:27 +00:00
|
|
|
pref_string_set( PREF_KEY_OPEN_DIALOG_FOLDER, folder );
|
|
|
|
g_free( folder );
|
|
|
|
|
2008-03-19 14:54:32 +00:00
|
|
|
if( response == GTK_RESPONSE_ACCEPT )
|
|
|
|
{
|
|
|
|
GtkWidget * w = gtk_file_chooser_get_extra_widget( GTK_FILE_CHOOSER( dialog ) );
|
|
|
|
const gboolean showOptions = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON ( w ) );
|
|
|
|
const pref_flag_t start = PREF_FLAG_DEFAULT;
|
|
|
|
const pref_flag_t prompt = showOptions ? PREF_FLAG_TRUE : PREF_FLAG_FALSE;
|
2008-03-18 01:22:11 +00:00
|
|
|
GSList * l = gtk_file_chooser_get_filenames( GTK_FILE_CHOOSER( dialog ) );
|
2008-03-19 14:54:32 +00:00
|
|
|
tr_core_add_list( core, l, start, prompt );
|
2008-03-18 01:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy( GTK_WIDGET( dialog ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget*
|
2008-04-03 02:11:15 +00:00
|
|
|
addDialog( GtkWindow * parent,
|
|
|
|
TrCore * core )
|
2008-03-18 01:22:11 +00:00
|
|
|
{
|
|
|
|
GtkWidget * w;
|
2008-03-19 14:54:32 +00:00
|
|
|
GtkWidget * c;
|
2008-03-18 20:41:27 +00:00
|
|
|
char * folder;
|
2008-03-18 01:22:11 +00:00
|
|
|
|
2008-04-03 02:13:13 +00:00
|
|
|
w = gtk_file_chooser_dialog_new( _( "Add a Torrent" ), parent,
|
2008-03-18 01:22:11 +00:00
|
|
|
GTK_FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
2008-04-03 02:11:15 +00:00
|
|
|
GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT,
|
2008-03-18 01:22:11 +00:00
|
|
|
NULL );
|
|
|
|
gtk_dialog_set_alternative_button_order( GTK_DIALOG( w ),
|
|
|
|
GTK_RESPONSE_ACCEPT,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1 );
|
|
|
|
gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( w ), TRUE );
|
2008-03-19 20:34:35 +00:00
|
|
|
addTorrentFilters( GTK_FILE_CHOOSER( w ) );
|
2008-04-03 02:13:13 +00:00
|
|
|
g_signal_connect( w, "response", G_CALLBACK(onAddDialogResponse), core );
|
2008-03-18 01:22:11 +00:00
|
|
|
|
2008-03-18 20:41:27 +00:00
|
|
|
if(( folder = pref_string_get( PREF_KEY_OPEN_DIALOG_FOLDER ))) {
|
|
|
|
gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( w ), folder );
|
|
|
|
g_free( folder );
|
|
|
|
}
|
2008-03-19 14:54:32 +00:00
|
|
|
|
|
|
|
c = gtk_check_button_new_with_mnemonic( _( "Display _options dialog" ) );
|
|
|
|
gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( c ), pref_flag_get( PREF_KEY_OPTIONS_PROMPT ) );
|
|
|
|
gtk_file_chooser_set_extra_widget( GTK_FILE_CHOOSER( w ), c );
|
|
|
|
gtk_widget_show( c );
|
2008-03-18 20:41:27 +00:00
|
|
|
|
2008-03-18 01:22:11 +00:00
|
|
|
gtk_widget_show( w );
|
|
|
|
return w;
|
|
|
|
}
|