2007-06-18 03:40:41 +00:00
|
|
|
/*
|
2008-01-01 17:20:20 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-06-18 03:40:41 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2007-06-18 03:40:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/makemeta.h>
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
#include "hig.h"
|
|
|
|
#include "makemeta-ui.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define UPDATE_INTERVAL_MSEC 200
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkWidget * size_lb;
|
|
|
|
GtkWidget * pieces_lb;
|
|
|
|
GtkWidget * announce_entry;
|
|
|
|
GtkWidget * comment_entry;
|
|
|
|
GtkWidget * progressbar;
|
|
|
|
GtkWidget * private_check;
|
|
|
|
GtkWidget * dialog;
|
2008-01-28 04:27:22 +00:00
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder * builder;
|
|
|
|
tr_handle * handle;
|
2008-01-28 04:27:22 +00:00
|
|
|
|
|
|
|
gboolean isBuilding;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
MakeMetaUI;
|
|
|
|
|
|
|
|
static void
|
|
|
|
freeMetaUI( gpointer p )
|
|
|
|
{
|
|
|
|
MakeMetaUI * ui = (MakeMetaUI *) p;
|
|
|
|
tr_metaInfoBuilderFree( ui->builder );
|
|
|
|
memset( ui, ~0, sizeof(MakeMetaUI) );
|
|
|
|
g_free( ui );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-01-28 04:27:22 +00:00
|
|
|
refreshButtons( MakeMetaUI * ui )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-01-28 04:27:22 +00:00
|
|
|
GtkDialog * d = GTK_DIALOG( ui->dialog );
|
|
|
|
gtk_dialog_set_response_sensitive( d, GTK_RESPONSE_ACCEPT, !ui->isBuilding && ( ui->builder!=NULL ) );
|
|
|
|
gtk_dialog_set_response_sensitive( d, GTK_RESPONSE_CLOSE, !ui->isBuilding );
|
|
|
|
gtk_dialog_set_response_sensitive( d, GTK_RESPONSE_CANCEL, ui->isBuilding );
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-01-28 04:27:22 +00:00
|
|
|
static void
|
|
|
|
setIsBuilding( MakeMetaUI * ui, gboolean isBuilding )
|
|
|
|
{
|
|
|
|
ui->isBuilding = isBuilding;
|
|
|
|
|
|
|
|
if( ui->builder != NULL )
|
2008-03-24 17:18:08 +00:00
|
|
|
ui->builder->result = TR_MAKEMETA_OK;
|
2008-01-28 04:27:22 +00:00
|
|
|
|
|
|
|
if( !isBuilding )
|
|
|
|
gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR( ui->progressbar ), 0 );
|
|
|
|
|
|
|
|
refreshButtons( ui );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
refresh_cb ( gpointer user_data )
|
|
|
|
{
|
2007-07-06 18:24:54 +00:00
|
|
|
int denom;
|
2007-06-18 03:40:41 +00:00
|
|
|
char buf[1024];
|
|
|
|
double fraction;
|
|
|
|
MakeMetaUI * ui = (MakeMetaUI *) user_data;
|
|
|
|
GtkProgressBar * p = GTK_PROGRESS_BAR( ui->progressbar );
|
|
|
|
|
2007-07-06 18:24:54 +00:00
|
|
|
denom = ui->builder->pieceCount ? ui->builder->pieceCount : 1;
|
|
|
|
fraction = (double)ui->builder->pieceIndex / denom;
|
2007-06-18 03:40:41 +00:00
|
|
|
gtk_progress_bar_set_fraction( p, fraction );
|
2008-01-28 04:27:22 +00:00
|
|
|
g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", ui->builder->top, (int)(fraction*100) );
|
2007-06-18 03:40:41 +00:00
|
|
|
gtk_progress_bar_set_text( p, buf );
|
|
|
|
|
|
|
|
if( ui->builder->isDone )
|
|
|
|
{
|
2008-03-24 17:18:08 +00:00
|
|
|
char * txt = NULL;
|
|
|
|
|
|
|
|
switch( ui->builder->result )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-03-24 17:18:08 +00:00
|
|
|
case TR_MAKEMETA_OK:
|
|
|
|
txt = g_strdup( _( "Torrent created" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_MAKEMETA_URL:
|
|
|
|
txt = g_strdup_printf( _( "Torrent creation failed: %s" ), _( "Invalid URL" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_MAKEMETA_CANCELLED:
|
|
|
|
txt = g_strdup_printf( _( "Torrent creation cancelled" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_MAKEMETA_IO_READ: {
|
|
|
|
char * tmp = g_strdup_printf( _( "Couldn't read \"%s\": %s" ), ui->builder->errfile, g_strerror( ui->builder->my_errno ) );
|
|
|
|
txt = g_strdup_printf( _( "Torrent creation failed: %s" ), tmp );
|
|
|
|
g_free( tmp );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TR_MAKEMETA_IO_WRITE: {
|
|
|
|
char * tmp = g_strdup_printf( _( "Couldn't write \"%s\": %s" ), ui->builder->errfile, g_strerror( ui->builder->my_errno ) );
|
|
|
|
txt = g_strdup_printf( _( "Torrent creation failed: %s" ), tmp );
|
|
|
|
g_free( tmp );
|
|
|
|
break;
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2008-01-28 04:27:22 +00:00
|
|
|
|
2008-03-24 17:18:08 +00:00
|
|
|
gtk_progress_bar_set_fraction( p, ui->builder->result==TR_MAKEMETA_OK ? 1.0 : 0.0 );
|
|
|
|
gtk_progress_bar_set_text( p, txt );
|
2008-01-28 04:27:22 +00:00
|
|
|
setIsBuilding( ui, FALSE );
|
2008-03-24 17:18:08 +00:00
|
|
|
g_free( txt );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return !ui->builder->isDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_tag (gpointer tag)
|
|
|
|
{
|
|
|
|
g_source_remove (GPOINTER_TO_UINT(tag)); /* stop the periodic refresh */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
response_cb( GtkDialog* d, int response, gpointer user_data )
|
|
|
|
{
|
|
|
|
MakeMetaUI * ui = (MakeMetaUI*) user_data;
|
|
|
|
char *tmp;
|
|
|
|
char buf[1024];
|
|
|
|
guint tag;
|
|
|
|
|
|
|
|
if( response != GTK_RESPONSE_ACCEPT )
|
|
|
|
{
|
2008-01-28 04:27:22 +00:00
|
|
|
if( ui->builder == NULL )
|
|
|
|
{
|
|
|
|
gtk_widget_destroy( GTK_WIDGET( d ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ui->builder->isDone || !ui->isBuilding )
|
|
|
|
{
|
|
|
|
gtk_widget_destroy( ui->dialog );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->builder->abortFlag = TRUE;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-28 04:27:22 +00:00
|
|
|
if( ui->builder == NULL || ui->isBuilding )
|
2007-10-19 21:49:18 +00:00
|
|
|
return;
|
|
|
|
|
2008-01-28 04:27:22 +00:00
|
|
|
setIsBuilding( ui, TRUE );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
tmp = g_path_get_basename (ui->builder->top);
|
2008-01-28 04:27:22 +00:00
|
|
|
g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", ui->builder->top, 0 );
|
|
|
|
|
|
|
|
gtk_progress_bar_set_text( GTK_PROGRESS_BAR(ui->progressbar), buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
g_free( tmp );
|
|
|
|
|
|
|
|
tr_makeMetaInfo( ui->builder,
|
|
|
|
NULL,
|
|
|
|
gtk_entry_get_text( GTK_ENTRY( ui->announce_entry ) ),
|
|
|
|
gtk_entry_get_text( GTK_ENTRY( ui->comment_entry ) ),
|
|
|
|
gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ui->private_check ) ) );
|
|
|
|
|
|
|
|
tag = g_timeout_add (UPDATE_INTERVAL_MSEC, refresh_cb, ui);
|
2008-01-28 04:27:22 +00:00
|
|
|
g_object_set_data_full (G_OBJECT(d), "tag", GUINT_TO_POINTER(tag), remove_tag);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2008-03-19 19:23:54 +00:00
|
|
|
onSelectionChanged( GtkFileChooser *chooser, gpointer user_data )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
MakeMetaUI * ui = (MakeMetaUI *) user_data;
|
|
|
|
char * filename;
|
2007-12-19 02:46:30 +00:00
|
|
|
char sizeStr[128];
|
2008-01-28 04:27:22 +00:00
|
|
|
char buf[MAX_PATH_LENGTH];
|
2007-06-18 13:51:41 +00:00
|
|
|
uint64_t totalSize=0;
|
|
|
|
int fileCount=0, pieceCount=0, pieceSize=0;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
if( ui->builder ) {
|
|
|
|
tr_metaInfoBuilderFree( ui->builder );
|
|
|
|
ui->builder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = gtk_file_chooser_get_filename( chooser );
|
2008-01-28 04:27:22 +00:00
|
|
|
if( !filename )
|
2008-03-19 20:07:27 +00:00
|
|
|
g_snprintf( buf, sizeof( buf ), _( "No files selected" ) );
|
2008-01-28 04:27:22 +00:00
|
|
|
else {
|
2007-06-18 03:40:41 +00:00
|
|
|
ui->builder = tr_metaInfoBuilderCreate( ui->handle, filename );
|
2008-01-28 04:27:22 +00:00
|
|
|
g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", filename, 0 );
|
2007-06-18 03:40:41 +00:00
|
|
|
g_free( filename );
|
|
|
|
fileCount = ui->builder->fileCount;
|
|
|
|
totalSize = ui->builder->totalSize;
|
|
|
|
pieceCount = ui->builder->pieceCount;
|
|
|
|
pieceSize = ui->builder->pieceSize;
|
|
|
|
}
|
2008-01-28 04:27:22 +00:00
|
|
|
gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar ), buf );
|
|
|
|
refreshButtons( ui );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-03-19 19:23:54 +00:00
|
|
|
if( !filename )
|
|
|
|
g_snprintf( buf, sizeof( buf ), _( "<i>No files selected</i>" ) );
|
|
|
|
else {
|
|
|
|
tr_strlsize( sizeStr, totalSize, sizeof(sizeStr) );
|
|
|
|
g_snprintf( buf, sizeof( buf ),
|
2008-03-19 20:07:27 +00:00
|
|
|
/* %1$s is the torrent size
|
|
|
|
%2$'d is its number of files */
|
|
|
|
ngettext( "<i>%1$s; %2$'d File</i>",
|
|
|
|
"<i>%1$s; %2$'d Files</i>", fileCount ),
|
2008-03-19 19:23:54 +00:00
|
|
|
sizeStr, fileCount );
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
gtk_label_set_markup ( GTK_LABEL(ui->size_lb), buf );
|
|
|
|
|
2008-03-19 19:23:54 +00:00
|
|
|
if( !filename )
|
|
|
|
*buf = '\0';
|
|
|
|
else {
|
|
|
|
tr_strlsize( sizeStr, pieceSize, sizeof(sizeStr) );
|
|
|
|
g_snprintf( buf, sizeof( buf ),
|
2008-03-19 20:07:27 +00:00
|
|
|
/* %1$'s is number of pieces;
|
|
|
|
%2$s is how big each piece is */
|
|
|
|
ngettext( "<i>%1$'d Piece @ %2$s</i>",
|
|
|
|
"<i>%1$'d Pieces @ %2$s</i>",
|
2008-03-19 19:23:54 +00:00
|
|
|
pieceCount ),
|
|
|
|
pieceCount, sizeStr );
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
gtk_label_set_markup ( GTK_LABEL(ui->pieces_lb), buf );
|
|
|
|
}
|
|
|
|
|
2007-06-18 04:52:03 +00:00
|
|
|
static void
|
2008-03-19 19:23:54 +00:00
|
|
|
onFileModeToggled( GtkToggleButton * t, gpointer w )
|
2007-06-18 04:52:03 +00:00
|
|
|
{
|
2008-03-19 19:23:54 +00:00
|
|
|
const gboolean active = gtk_toggle_button_get_active( t );
|
|
|
|
gtk_widget_set_sensitive( w, active );
|
|
|
|
if( active )
|
|
|
|
g_signal_emit_by_name( w, "selection-changed", NULL );
|
2007-06-18 04:52:03 +00:00
|
|
|
}
|
2008-03-19 19:23:54 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
GtkWidget*
|
2007-09-20 20:14:13 +00:00
|
|
|
make_meta_ui( GtkWindow * parent, tr_handle * handle )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-03-19 19:23:54 +00:00
|
|
|
GSList * group;
|
2007-06-18 03:40:41 +00:00
|
|
|
int row = 0;
|
2008-03-19 19:23:54 +00:00
|
|
|
GtkWidget *l, *d, *t, *w, *h;
|
2008-01-28 04:27:22 +00:00
|
|
|
GtkBox * main_vbox;
|
2007-06-18 03:40:41 +00:00
|
|
|
MakeMetaUI * ui = g_new0 ( MakeMetaUI, 1 );
|
|
|
|
ui->handle = handle;
|
|
|
|
|
2008-02-03 17:10:03 +00:00
|
|
|
d = gtk_dialog_new_with_buttons( _("New Torrent"),
|
2007-06-18 03:40:41 +00:00
|
|
|
parent,
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
2008-01-28 04:27:22 +00:00
|
|
|
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
|
2007-06-18 03:40:41 +00:00
|
|
|
GTK_STOCK_NEW, GTK_RESPONSE_ACCEPT,
|
|
|
|
NULL );
|
|
|
|
g_signal_connect( d, "response", G_CALLBACK(response_cb), ui );
|
|
|
|
g_object_set_data_full( G_OBJECT(d), "ui", ui, freeMetaUI );
|
|
|
|
ui->dialog = d;
|
2008-01-28 04:27:22 +00:00
|
|
|
main_vbox = GTK_BOX( GTK_DIALOG( d )->vbox );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
t = hig_workarea_create ();
|
|
|
|
|
2008-03-19 19:23:54 +00:00
|
|
|
hig_workarea_add_section_title (t, &row, _( "Content" ));
|
|
|
|
|
|
|
|
l = gtk_radio_button_new_with_mnemonic( NULL, _( "_Single File:" ) );
|
|
|
|
w = gtk_file_chooser_button_new( NULL, GTK_FILE_CHOOSER_ACTION_OPEN );
|
|
|
|
hig_workarea_add_row_w( t, &row, l, w, NULL );
|
|
|
|
group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( l ) );
|
|
|
|
g_signal_connect( l, "toggled", G_CALLBACK(onFileModeToggled), w );
|
|
|
|
g_signal_connect( w, "selection-changed", G_CALLBACK(onSelectionChanged), ui );
|
|
|
|
|
|
|
|
l = gtk_radio_button_new_with_mnemonic( group, _( "_Folder:" ) );
|
|
|
|
w = gtk_file_chooser_button_new( NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
|
|
|
|
hig_workarea_add_row_w( t, &row, l, w, NULL );
|
|
|
|
g_signal_connect( l, "toggled", G_CALLBACK(onFileModeToggled), w );
|
|
|
|
g_signal_connect( w, "selection-changed", G_CALLBACK(onSelectionChanged), ui );
|
|
|
|
gtk_widget_set_sensitive( w, FALSE );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
h = gtk_hbox_new( FALSE, GUI_PAD_SMALL );
|
|
|
|
w = ui->size_lb = gtk_label_new (NULL);
|
2008-03-19 19:23:54 +00:00
|
|
|
gtk_label_set_markup ( GTK_LABEL(w), _( "<i>No files selected</i>" ) );
|
2007-06-18 03:40:41 +00:00
|
|
|
gtk_box_pack_start( GTK_BOX(h), w, FALSE, FALSE, 0 );
|
|
|
|
w = ui->pieces_lb = gtk_label_new (NULL);
|
|
|
|
gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, 0 );
|
|
|
|
w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
|
2008-01-27 18:50:37 +00:00
|
|
|
gtk_widget_set_size_request (w, 2 * GUI_PAD_BIG, 0);
|
2007-06-18 03:40:41 +00:00
|
|
|
gtk_box_pack_start_defaults ( GTK_BOX(h), w );
|
|
|
|
hig_workarea_add_row (t, &row, "", h, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
hig_workarea_add_section_divider( t, &row );
|
2008-03-19 20:34:35 +00:00
|
|
|
hig_workarea_add_section_title (t, &row, _("Details"));
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-03-19 19:23:54 +00:00
|
|
|
w = ui->private_check = hig_workarea_add_wide_checkbutton( t, &row, _( "_Private to this tracker" ), FALSE );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
w = ui->announce_entry = gtk_entry_new( );
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(w), "http://");
|
2008-03-19 19:23:54 +00:00
|
|
|
hig_workarea_add_row (t, &row, _( "Announce _URL:" ), w, NULL );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
w = ui->comment_entry = gtk_entry_new( );
|
2008-03-19 19:23:54 +00:00
|
|
|
hig_workarea_add_row (t, &row, _( "Commen_t:" ), w, NULL );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-01-28 04:27:22 +00:00
|
|
|
hig_workarea_finish( t, &row );
|
|
|
|
gtk_box_pack_start_defaults( main_vbox, t );
|
|
|
|
|
|
|
|
w = gtk_frame_new( NULL );
|
|
|
|
gtk_frame_set_shadow_type( GTK_FRAME( w ), GTK_SHADOW_NONE );
|
|
|
|
gtk_container_set_border_width( GTK_CONTAINER( w ), GUI_PAD_BIG );
|
|
|
|
|
|
|
|
ui->progressbar = gtk_progress_bar_new( );
|
2008-03-07 20:48:36 +00:00
|
|
|
gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar), _( "No files selected" ) );
|
2008-01-28 04:27:22 +00:00
|
|
|
gtk_container_add( GTK_CONTAINER( w ), ui->progressbar );
|
|
|
|
|
|
|
|
gtk_box_pack_start( main_vbox, w, FALSE, FALSE, GUI_PAD_BIG );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
gtk_window_set_default_size( GTK_WINDOW(d), 400u, 0u );
|
|
|
|
gtk_widget_show_all( GTK_DIALOG(d)->vbox );
|
2008-01-28 04:27:22 +00:00
|
|
|
setIsBuilding( ui, FALSE );
|
2007-06-18 03:40:41 +00:00
|
|
|
return d;
|
|
|
|
}
|