source: trunk/gtk/makemeta-ui.c @ 5636

Last change on this file since 5636 was 5636, checked in by charles, 14 years ago

fix a couple more minor leaks

  • Property svn:keywords set to Date Rev Author Id
File size: 11.0 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: makemeta-ui.c 5636 2008-04-17 19:54:22Z charles $
11 */
12
13#include <string.h>
14
15#include <glib/gi18n.h>
16#include <gtk/gtk.h>
17
18#include <libtransmission/transmission.h>
19#include <libtransmission/makemeta.h>
20
21#include "hig.h"
22#include "makemeta-ui.h"
23#include "util.h"
24
25#define UPDATE_INTERVAL_MSEC 200
26
27typedef struct
28{
29    GtkWidget * size_lb;
30    GtkWidget * pieces_lb;
31    GtkWidget * announce_entry;
32    GtkWidget * comment_entry;
33    GtkWidget * progressbar;
34    GtkWidget * private_check;
35    GtkWidget * dialog;
36
37    tr_metainfo_builder * builder;
38    tr_handle * handle;
39
40    gboolean isBuilding;
41}
42MakeMetaUI;
43
44static void
45freeMetaUI( gpointer p )
46{
47    MakeMetaUI * ui = p;
48    tr_metaInfoBuilderFree( ui->builder );
49    memset( ui, ~0, sizeof(MakeMetaUI) );
50    g_free( ui );
51}
52
53static void
54refreshButtons( MakeMetaUI * ui )
55{
56    GtkDialog * d = GTK_DIALOG( ui->dialog );
57    gtk_dialog_set_response_sensitive( d, GTK_RESPONSE_ACCEPT, !ui->isBuilding && ( ui->builder!=NULL ) );
58    gtk_dialog_set_response_sensitive( d, GTK_RESPONSE_CLOSE, !ui->isBuilding );
59    gtk_dialog_set_response_sensitive( d, GTK_RESPONSE_CANCEL, ui->isBuilding );
60}
61
62static void
63setIsBuilding( MakeMetaUI * ui, gboolean isBuilding )
64{
65    ui->isBuilding = isBuilding;
66
67    if( ui->builder != NULL )
68        ui->builder->result = TR_MAKEMETA_OK;
69
70    if( !isBuilding )
71        gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR( ui->progressbar ), 0 );
72
73    refreshButtons( ui );
74}
75
76static gboolean
77refresh_cb ( gpointer user_data )
78{
79    int denom;
80    char buf[1024];
81    double fraction;
82    MakeMetaUI * ui = (MakeMetaUI *) user_data;
83    GtkProgressBar * p = GTK_PROGRESS_BAR( ui->progressbar );
84
85    denom = ui->builder->pieceCount ? ui->builder->pieceCount : 1;
86    fraction = (double)ui->builder->pieceIndex / denom;
87    gtk_progress_bar_set_fraction( p, fraction );
88    g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", ui->builder->top, (int)(fraction*100) );
89    gtk_progress_bar_set_text( p, buf );
90
91    if( ui->builder->isDone )
92    {
93        char * txt = NULL;
94
95        switch( ui->builder->result )
96        {
97            case TR_MAKEMETA_OK:
98                txt = g_strdup( _( "Torrent created" ) );
99                break;
100
101            case TR_MAKEMETA_URL:
102                txt = g_strdup_printf( _( "Torrent creation failed: %s" ), _( "Invalid URL" ) );
103                break;
104
105            case TR_MAKEMETA_CANCELLED:
106                txt = g_strdup_printf( _( "Torrent creation cancelled" ) );
107                break;
108
109            case TR_MAKEMETA_IO_READ: {
110                char * tmp = g_strdup_printf( _( "Couldn't read \"%1$s\": %2$s" ), ui->builder->errfile, g_strerror( ui->builder->my_errno ) );
111                txt = g_strdup_printf( _( "Torrent creation failed: %s" ), tmp );
112                g_free( tmp  );
113                break;
114            }
115
116            case TR_MAKEMETA_IO_WRITE: {
117                char * tmp = g_strdup_printf( _( "Couldn't create \"%1$s\": %2$s" ), ui->builder->errfile, g_strerror( ui->builder->my_errno ) );
118                txt = g_strdup_printf( _( "Torrent creation failed: %s" ), tmp );
119                g_free( tmp  );
120                break;
121            }
122        }
123
124        gtk_progress_bar_set_fraction( p, ui->builder->result==TR_MAKEMETA_OK ? 1.0 : 0.0 );
125        gtk_progress_bar_set_text( p, txt );
126        setIsBuilding( ui, FALSE );
127        g_free( txt );
128    }
129
130    return !ui->builder->isDone;
131}
132
133static void
134remove_tag (gpointer tag)
135{
136  g_source_remove (GPOINTER_TO_UINT(tag)); /* stop the periodic refresh */
137}
138
139static void
140response_cb( GtkDialog* d, int response, gpointer user_data )
141{
142    MakeMetaUI * ui = (MakeMetaUI*) user_data;
143    char *tmp;
144    char buf[1024];
145    guint tag;
146
147    if( response != GTK_RESPONSE_ACCEPT )
148    {
149        if( ui->builder == NULL )
150        {
151            gtk_widget_destroy( GTK_WIDGET( d ) );
152            return;
153        }
154
155        if( ui->builder->isDone || !ui->isBuilding )
156        {
157            gtk_widget_destroy( ui->dialog );
158        }
159        else
160        {
161            ui->builder->abortFlag = TRUE;
162        }
163       
164        return;
165    }
166
167    if( ui->builder == NULL || ui->isBuilding )
168        return;
169
170    setIsBuilding( ui, TRUE );
171
172    tmp = g_path_get_basename (ui->builder->top);
173    g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", ui->builder->top, 0 );
174
175    gtk_progress_bar_set_text( GTK_PROGRESS_BAR(ui->progressbar), buf );
176    g_free( tmp );
177
178    tr_makeMetaInfo( ui->builder,
179                     NULL, 
180                     gtk_entry_get_text( GTK_ENTRY( ui->announce_entry ) ),
181                     gtk_entry_get_text( GTK_ENTRY( ui->comment_entry ) ),
182                     gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ui->private_check ) ) );
183
184    tag = g_timeout_add (UPDATE_INTERVAL_MSEC, refresh_cb, ui);
185    g_object_set_data_full (G_OBJECT(d), "tag", GUINT_TO_POINTER(tag), remove_tag);
186}
187
188/***
189****
190***/
191
192static void
193onSelectionChanged( GtkFileChooser *chooser, gpointer user_data )
194{
195    MakeMetaUI * ui = (MakeMetaUI *) user_data;
196    char * filename;
197    char sizeStr[128];
198    char buf[MAX_PATH_LENGTH];
199    uint64_t totalSize=0;
200    int fileCount=0, pieceCount=0, pieceSize=0;
201
202    if( ui->builder ) {
203        tr_metaInfoBuilderFree( ui->builder );
204        ui->builder = NULL;
205    }
206
207    filename = gtk_file_chooser_get_filename( chooser );
208    if( !filename )
209        g_snprintf( buf, sizeof( buf ), _( "No files selected" ) );
210    else {
211        ui->builder = tr_metaInfoBuilderCreate( ui->handle, filename );
212        g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", filename, 0 );
213        g_free( filename );
214        fileCount = ui->builder->fileCount;
215        totalSize = ui->builder->totalSize;
216        pieceCount = ui->builder->pieceCount;
217        pieceSize = ui->builder->pieceSize;
218    }
219    gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar ), buf );
220    refreshButtons( ui );
221
222    if( !filename )
223        g_snprintf( buf, sizeof( buf ), _( "<i>No files selected</i>" ) );
224    else {
225        tr_strlsize( sizeStr, totalSize, sizeof(sizeStr) );
226        g_snprintf( buf, sizeof( buf ),
227                    /* %1$s is the torrent size
228                       %2$'d is its number of files */
229                    ngettext( "<i>%1$s; %2$'d File</i>",
230                              "<i>%1$s; %2$'d Files</i>", fileCount ),
231                    sizeStr, fileCount );
232    }
233    gtk_label_set_markup ( GTK_LABEL(ui->size_lb), buf );
234
235    if( !filename )
236        *buf = '\0';
237    else {
238        tr_strlsize( sizeStr, pieceSize, sizeof(sizeStr) );
239        g_snprintf( buf, sizeof( buf ),
240                    /* %1$'s is number of pieces;
241                       %2$s is how big each piece is */
242                    ngettext( "<i>%1$'d Piece @ %2$s</i>",
243                              "<i>%1$'d Pieces @ %2$s</i>",
244                              pieceCount ),
245                    pieceCount, sizeStr );
246    }
247    gtk_label_set_markup ( GTK_LABEL(ui->pieces_lb), buf );
248}
249
250static void
251onFileModeToggled( GtkToggleButton * t, gpointer w )
252{
253    const gboolean active = gtk_toggle_button_get_active( t );
254    gtk_widget_set_sensitive( w, active );
255    if( active )
256        g_signal_emit_by_name( w, "selection-changed", NULL );
257}
258   
259GtkWidget*
260make_meta_ui( GtkWindow * parent, tr_handle * handle )
261{
262    GSList * group;
263    int row = 0;
264    GtkWidget *l, *d, *t, *w, *h;
265    GtkBox * main_vbox;
266    MakeMetaUI * ui = g_new0 ( MakeMetaUI, 1 );
267    ui->handle = handle;
268
269    d = gtk_dialog_new_with_buttons( _("New Torrent"),
270                                     parent,
271                                     GTK_DIALOG_DESTROY_WITH_PARENT,
272                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
273                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
274                                     GTK_STOCK_NEW, GTK_RESPONSE_ACCEPT,
275                                     NULL );
276    g_signal_connect( d, "response", G_CALLBACK(response_cb), ui );
277    g_object_set_data_full( G_OBJECT(d), "ui", ui, freeMetaUI );
278    ui->dialog = d;
279    main_vbox = GTK_BOX( GTK_DIALOG( d )->vbox );
280
281    t = hig_workarea_create ();
282
283    hig_workarea_add_section_title (t, &row, _( "Content" ));
284
285        l = gtk_radio_button_new_with_mnemonic( NULL, _( "_Single File:" ) );
286        w = gtk_file_chooser_button_new( NULL, GTK_FILE_CHOOSER_ACTION_OPEN );
287        hig_workarea_add_row_w( t, &row, l, w, NULL );
288        group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( l ) );
289        g_signal_connect( l, "toggled", G_CALLBACK(onFileModeToggled), w );
290        g_signal_connect( w, "selection-changed", G_CALLBACK(onSelectionChanged), ui );
291
292        l = gtk_radio_button_new_with_mnemonic( group, _( "_Folder:" ) );
293        w = gtk_file_chooser_button_new( NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
294        hig_workarea_add_row_w( t, &row, l, w, NULL );
295        g_signal_connect( l, "toggled", G_CALLBACK(onFileModeToggled), w );
296        g_signal_connect( w, "selection-changed", G_CALLBACK(onSelectionChanged), ui );
297        gtk_widget_set_sensitive( w, FALSE );
298
299        h = gtk_hbox_new( FALSE, GUI_PAD_SMALL );
300        w = ui->size_lb = gtk_label_new (NULL);
301        gtk_label_set_markup ( GTK_LABEL(w), _( "<i>No files selected</i>" ) );
302        gtk_box_pack_start( GTK_BOX(h), w, FALSE, FALSE, 0 );
303        w = ui->pieces_lb = gtk_label_new (NULL);
304        gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, 0 );
305        w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
306        gtk_widget_set_size_request (w, 2 * GUI_PAD_BIG, 0);
307        gtk_box_pack_start_defaults ( GTK_BOX(h), w );
308        hig_workarea_add_row (t, &row, "", h, NULL);
309       
310
311    hig_workarea_add_section_divider( t, &row );
312    hig_workarea_add_section_title (t, &row, _("Details"));
313
314        w = ui->private_check = hig_workarea_add_wide_checkbutton( t, &row, _( "_Private to this tracker" ), FALSE );
315
316        w = ui->announce_entry = gtk_entry_new( );
317        gtk_entry_set_text(GTK_ENTRY(w), "http://");
318        hig_workarea_add_row (t, &row, _( "Announce _URL:" ), w, NULL );
319
320        w = ui->comment_entry = gtk_entry_new( );
321        hig_workarea_add_row (t, &row, _( "Commen_t:" ), w, NULL );
322
323    hig_workarea_finish( t, &row );
324    gtk_box_pack_start_defaults( main_vbox, t );
325
326    w = gtk_frame_new( NULL );
327    gtk_frame_set_shadow_type( GTK_FRAME( w ), GTK_SHADOW_NONE );
328    gtk_container_set_border_width( GTK_CONTAINER( w ), GUI_PAD_BIG );
329   
330        ui->progressbar = gtk_progress_bar_new( );
331        gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar), _( "No files selected" ) );
332        gtk_container_add( GTK_CONTAINER( w ), ui->progressbar );
333
334    gtk_box_pack_start( main_vbox, w, FALSE, FALSE, GUI_PAD_BIG );
335
336    gtk_window_set_default_size( GTK_WINDOW(d), 400u, 0u );
337    gtk_widget_show_all( GTK_DIALOG(d)->vbox );
338    setIsBuilding( ui, FALSE );
339    return d;
340}
Note: See TracBrowser for help on using the repository browser.