source: branches/file_selection/gtk/makemeta-ui.c @ 2138

Last change on this file since 2138 was 2138, checked in by charles, 15 years ago
  • when re-queueing an aborted builder, zero out its 'done' and 'aborted' flags
  • gtk+: simplify the "create torrent" cleanup code
File size: 10.3 KB
Line 
1/*
2 * This file Copyright (C) 2007 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
11#include <string.h>
12
13#include <glib/gi18n.h>
14#include <gtk/gtk.h>
15
16#include "transmission.h"
17#include "makemeta.h"
18
19#include "hig.h"
20#include "makemeta-ui.h"
21#include "util.h"
22
23#define UPDATE_INTERVAL_MSEC 200
24
25typedef struct
26{
27    char torrent_name[2048];
28    GtkWidget * size_lb;
29    GtkWidget * pieces_lb;
30    GtkWidget * announce_entry;
31    GtkWidget * comment_entry;
32    GtkWidget * progressbar;
33    GtkWidget * private_check;
34    GtkWidget * dialog;
35    GtkWidget * progress_dialog;
36    tr_metainfo_builder_t * builder;
37    tr_handle_t * handle;
38}
39MakeMetaUI;
40
41static void
42freeMetaUI( gpointer p )
43{
44    MakeMetaUI * ui = (MakeMetaUI *) p;
45    tr_metaInfoBuilderFree( ui->builder );
46    memset( ui, ~0, sizeof(MakeMetaUI) );
47    g_free( ui );
48}
49
50static void
51progress_response_cb ( GtkDialog *d UNUSED, int response, gpointer user_data )
52{
53    MakeMetaUI * ui = (MakeMetaUI *) user_data;
54
55    if( response == GTK_RESPONSE_CANCEL )
56    {
57        ui->builder->abortFlag = TRUE;
58    }
59    else
60    {
61        gtk_widget_destroy( ui->dialog );
62    }
63}
64
65static gboolean
66refresh_cb ( gpointer user_data )
67{
68    char buf[1024];
69    double fraction;
70    MakeMetaUI * ui = (MakeMetaUI *) user_data;
71    GtkProgressBar * p = GTK_PROGRESS_BAR( ui->progressbar );
72
73    fraction = (double)ui->builder->pieceIndex / ui->builder->pieceCount;
74    gtk_progress_bar_set_fraction( p, fraction );
75    g_snprintf( buf, sizeof(buf), "%s (%d%%)", ui->torrent_name, (int)(fraction*100 + 0.5));
76    gtk_progress_bar_set_text( p, buf );
77
78    if( ui->builder->isDone )
79    {
80        GtkWidget * w;
81
82        if( ui->builder->failed )
83        {
84            const char * reason = ui->builder->abortFlag
85                ? _("Torrent creation aborted.")
86                : _("Torrent creation failed.");
87            w = gtk_message_dialog_new (GTK_WINDOW(ui->progress_dialog),
88                                        GTK_DIALOG_DESTROY_WITH_PARENT,
89                                        GTK_MESSAGE_ERROR,
90                                        GTK_BUTTONS_CLOSE, reason );
91            gtk_dialog_run( GTK_DIALOG( w ) );
92            gtk_widget_destroy( ui->progress_dialog );
93        }
94        else
95        {
96            GtkWidget * w = ui->progress_dialog;
97            gtk_window_set_title (GTK_WINDOW(ui->progress_dialog), _("Torrent Created"));
98            gtk_dialog_set_response_sensitive (GTK_DIALOG(w), GTK_RESPONSE_CANCEL, FALSE);
99            gtk_dialog_set_response_sensitive (GTK_DIALOG(w), GTK_RESPONSE_CLOSE, TRUE);
100            gtk_progress_bar_set_text( p, buf );
101        }
102    }
103
104    return !ui->builder->isDone;
105}
106
107static void
108remove_tag (gpointer tag)
109{
110  g_source_remove (GPOINTER_TO_UINT(tag)); /* stop the periodic refresh */
111}
112
113static void
114response_cb( GtkDialog* d, int response, gpointer user_data )
115{
116    MakeMetaUI * ui = (MakeMetaUI*) user_data;
117    GtkWidget *w, *p, *fr;
118    char *tmp;
119    char buf[1024];
120    guint tag;
121
122    if( response != GTK_RESPONSE_ACCEPT )
123    {
124        gtk_widget_destroy( GTK_WIDGET( d ) );
125        return;
126    }
127
128    w = gtk_dialog_new_with_buttons( _("Making Torrent..."), 
129                                     GTK_WINDOW(d),
130                                     GTK_DIALOG_DESTROY_WITH_PARENT,
131                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
132                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
133                                     NULL );
134    g_signal_connect( w, "response", G_CALLBACK(progress_response_cb), ui );
135    ui->progress_dialog = w;
136    gtk_dialog_set_response_sensitive (GTK_DIALOG(w), GTK_RESPONSE_CLOSE, FALSE);
137
138    tmp = g_path_get_basename (ui->builder->top);
139    g_snprintf( ui->torrent_name, sizeof(ui->torrent_name), "%s.torrent", tmp );
140    g_snprintf( buf, sizeof(buf), "%s (%d%%)", ui->torrent_name, 0);
141    p = ui->progressbar = gtk_progress_bar_new ();
142    gtk_progress_bar_set_text( GTK_PROGRESS_BAR(p), buf );
143    fr = gtk_frame_new (NULL);
144    gtk_frame_set_shadow_type (GTK_FRAME(fr), GTK_SHADOW_NONE);
145    gtk_container_set_border_width( GTK_CONTAINER(fr), 20 );
146    gtk_container_add (GTK_CONTAINER(fr), p);
147    gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(w)->vbox), fr );
148    gtk_widget_show_all ( w );
149    g_free( tmp );
150
151    tr_makeMetaInfo( ui->builder,
152                     NULL, 
153                     gtk_entry_get_text( GTK_ENTRY( ui->announce_entry ) ),
154                     gtk_entry_get_text( GTK_ENTRY( ui->comment_entry ) ),
155                     gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ui->private_check ) ) );
156
157    tag = g_timeout_add (UPDATE_INTERVAL_MSEC, refresh_cb, ui);
158    g_object_set_data_full (G_OBJECT(w), "tag", GUINT_TO_POINTER(tag), remove_tag);
159}
160
161static void
162file_mode_toggled_cb (GtkToggleButton *togglebutton, gpointer user_data)
163{
164    if( gtk_toggle_button_get_active( togglebutton ) )
165    {
166        GtkFileChooserButton * w = GTK_FILE_CHOOSER_BUTTON(user_data);
167        gtk_file_chooser_set_action( GTK_FILE_CHOOSER( w ), GTK_FILE_CHOOSER_ACTION_OPEN );
168    }
169}
170
171static void
172dir_mode_toggled_cb (GtkToggleButton *togglebutton, gpointer user_data)
173{
174    if( gtk_toggle_button_get_active( togglebutton ) )
175    {
176        GtkFileChooserButton * w = GTK_FILE_CHOOSER_BUTTON(user_data);
177        gtk_file_chooser_set_action( GTK_FILE_CHOOSER( w ), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
178    }
179}
180
181/***
182****
183***/
184
185static void
186file_selection_changed_cb( GtkFileChooser *chooser, gpointer user_data )
187{
188    MakeMetaUI * ui = (MakeMetaUI *) user_data;
189    char * pch;
190    char * filename;
191    char buf[512];
192    size_t fileCount=0, totalSize=0;
193    size_t pieceCount=0, pieceSize=0;
194
195    if( ui->builder ) {
196        tr_metaInfoBuilderFree( ui->builder );
197        ui->builder = NULL;
198    }
199
200    filename = gtk_file_chooser_get_filename( chooser );
201    if( filename ) {
202        ui->builder = tr_metaInfoBuilderCreate( ui->handle, filename );
203        g_free( filename );
204        fileCount = ui->builder->fileCount;
205        totalSize = ui->builder->totalSize;
206        pieceCount = ui->builder->pieceCount;
207        pieceSize = ui->builder->pieceSize;
208    }
209
210    pch = readablesize( totalSize );
211    g_snprintf( buf, sizeof(buf), "<i>%s; %lu %s</i>",
212                pch, fileCount,
213                ngettext("file", "files", fileCount) );
214    gtk_label_set_markup ( GTK_LABEL(ui->size_lb), buf );
215    g_free( pch );
216
217    pch = readablesize( pieceSize );
218    g_snprintf( buf, sizeof(buf), "<i>%lu %s @ %s</i>",
219                pieceCount,
220                ngettext("piece", "pieces", fileCount),
221                pch );
222    gtk_label_set_markup ( GTK_LABEL(ui->pieces_lb), buf );
223    g_free( pch );
224}
225
226GtkWidget*
227make_meta_ui( GtkWindow * parent, tr_handle_t * handle )
228{
229    int row = 0;
230    GtkWidget *d, *t, *w, *h, *rb_file, *rb_dir;
231    char name[256];
232    MakeMetaUI * ui = g_new0 ( MakeMetaUI, 1 );
233    ui->handle = handle;
234
235    d = gtk_dialog_new_with_buttons( _("Make a New Torrent"),
236                                     parent,
237                                     GTK_DIALOG_DESTROY_WITH_PARENT,
238                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
239                                     GTK_STOCK_NEW, GTK_RESPONSE_ACCEPT,
240                                     NULL );
241    g_signal_connect( d, "response", G_CALLBACK(response_cb), ui );
242    g_object_set_data_full( G_OBJECT(d), "ui", ui, freeMetaUI );
243    ui->dialog = d;
244
245    t = hig_workarea_create ();
246
247    hig_workarea_add_section_title (t, &row, _("Files"));
248    hig_workarea_add_section_spacer (t, row, 3);
249
250        g_snprintf( name, sizeof(name), "%s:", _("File _Type"));
251        h = gtk_hbox_new( FALSE, GUI_PAD_SMALL );
252        w = rb_dir = gtk_radio_button_new_with_mnemonic( NULL, _("Directory"));
253        gtk_box_pack_start ( GTK_BOX(h), w, FALSE, FALSE, 0 );
254        w = rb_file = gtk_radio_button_new_with_mnemonic_from_widget( GTK_RADIO_BUTTON(w), _("Single File") );
255        gtk_box_pack_start ( GTK_BOX(h), w, FALSE, FALSE, 0 );
256        hig_workarea_add_row (t, &row, name, h, NULL);
257
258        g_snprintf( name, sizeof(name), "%s:", _("_File"));
259        w = gtk_file_chooser_button_new( _("File or Directory to Add to the New Torrent"),
260                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
261        g_signal_connect( w, "selection-changed", G_CALLBACK(file_selection_changed_cb), ui );
262        g_signal_connect( rb_file, "toggled", G_CALLBACK(file_mode_toggled_cb), w );
263        g_signal_connect( rb_dir, "toggled", G_CALLBACK(dir_mode_toggled_cb), w );
264        hig_workarea_add_row (t, &row, name, w, NULL);
265
266        g_snprintf( name, sizeof(name), "<i>%s</i>", _("No Files Selected"));
267        h = gtk_hbox_new( FALSE, GUI_PAD_SMALL );
268        w = ui->size_lb = gtk_label_new (NULL);
269        gtk_label_set_markup ( GTK_LABEL(w), name );
270        gtk_box_pack_start( GTK_BOX(h), w, FALSE, FALSE, 0 );
271        w = ui->pieces_lb = gtk_label_new (NULL);
272        gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, 0 );
273        w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
274        gtk_widget_set_usize (w, 2 * GUI_PAD_BIG, 0);
275        gtk_box_pack_start_defaults ( GTK_BOX(h), w );
276        hig_workarea_add_row (t, &row, "", h, NULL);
277       
278
279    hig_workarea_add_section_divider( t, &row );
280    hig_workarea_add_section_title (t, &row, _("Torrent"));
281    hig_workarea_add_section_spacer (t, row, 3);
282
283        g_snprintf( name, sizeof(name), _("Private to this Tracker") );
284        w = ui->private_check = hig_workarea_add_wide_checkbutton( t, &row, name, FALSE );
285
286        g_snprintf( name, sizeof(name), "%s:", _("Announce _URL"));
287        w = ui->announce_entry = gtk_entry_new( );
288        gtk_entry_set_text(GTK_ENTRY(w), "http://");
289        hig_workarea_add_row (t, &row, name, w, NULL );
290
291        g_snprintf( name, sizeof(name), "%s:", _("Commen_t"));
292        w = ui->comment_entry = gtk_entry_new( );
293        hig_workarea_add_row (t, &row, name, w, NULL );
294
295
296    gtk_window_set_default_size( GTK_WINDOW(d), 400u, 0u );
297    gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(d)->vbox), t );
298    gtk_widget_show_all( GTK_DIALOG(d)->vbox );
299    return d;
300}
Note: See TracBrowser for help on using the repository browser.