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

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

more i18n strings work

  • Property svn:keywords set to Date Rev Author Id
File size: 10.1 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 5208 2008-03-06 21:59:00Z 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 = (MakeMetaUI *) 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->failed = FALSE;
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        if( ui->builder->failed )
94        {
95            const char * reason = ui->builder->abortFlag
96                ? _("Torrent creation cancelled.")
97                : _("Torrent creation failed.");
98
99            gtk_progress_bar_set_text( p, reason );
100            gtk_progress_bar_set_fraction( p, 0 );
101        }
102        else
103        {
104            gtk_progress_bar_set_text( p, _("Torrent Created") );
105        }
106
107        setIsBuilding( ui, FALSE );
108    }
109
110    return !ui->builder->isDone;
111}
112
113static void
114remove_tag (gpointer tag)
115{
116  g_source_remove (GPOINTER_TO_UINT(tag)); /* stop the periodic refresh */
117}
118
119static void
120response_cb( GtkDialog* d, int response, gpointer user_data )
121{
122    MakeMetaUI * ui = (MakeMetaUI*) user_data;
123    char *tmp;
124    char buf[1024];
125    guint tag;
126
127    if( response != GTK_RESPONSE_ACCEPT )
128    {
129        if( ui->builder == NULL )
130        {
131            gtk_widget_destroy( GTK_WIDGET( d ) );
132            return;
133        }
134
135        if( ui->builder->isDone || !ui->isBuilding )
136        {
137            gtk_widget_destroy( ui->dialog );
138        }
139        else
140        {
141            ui->builder->abortFlag = TRUE;
142        }
143       
144        return;
145    }
146
147    if( ui->builder == NULL || ui->isBuilding )
148        return;
149
150    setIsBuilding( ui, TRUE );
151
152    tmp = g_path_get_basename (ui->builder->top);
153    g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", ui->builder->top, 0 );
154
155    gtk_progress_bar_set_text( GTK_PROGRESS_BAR(ui->progressbar), buf );
156    g_free( tmp );
157
158    tr_makeMetaInfo( ui->builder,
159                     NULL, 
160                     gtk_entry_get_text( GTK_ENTRY( ui->announce_entry ) ),
161                     gtk_entry_get_text( GTK_ENTRY( ui->comment_entry ) ),
162                     gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ui->private_check ) ) );
163
164    tag = g_timeout_add (UPDATE_INTERVAL_MSEC, refresh_cb, ui);
165    g_object_set_data_full (G_OBJECT(d), "tag", GUINT_TO_POINTER(tag), remove_tag);
166}
167
168/***
169****
170***/
171
172static void
173file_selection_changed_cb( GtkFileChooser *chooser, gpointer user_data )
174{
175    MakeMetaUI * ui = (MakeMetaUI *) user_data;
176    char * filename;
177    char sizeStr[128];
178    char buf[MAX_PATH_LENGTH];
179    uint64_t totalSize=0;
180    int fileCount=0, pieceCount=0, pieceSize=0;
181
182    if( ui->builder ) {
183        tr_metaInfoBuilderFree( ui->builder );
184        ui->builder = NULL;
185    }
186
187    filename = gtk_file_chooser_get_filename( chooser );
188    if( !filename )
189        *buf = '\0';
190    else {
191        ui->builder = tr_metaInfoBuilderCreate( ui->handle, filename );
192        g_snprintf( buf, sizeof(buf), "%s.torrent (%d%%)", filename, 0 );
193        g_free( filename );
194        fileCount = ui->builder->fileCount;
195        totalSize = ui->builder->totalSize;
196        pieceCount = ui->builder->pieceCount;
197        pieceSize = ui->builder->pieceSize;
198    }
199    gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar ), buf );
200    refreshButtons( ui );
201
202    tr_strlsize( sizeStr, totalSize, sizeof(sizeStr) );
203    g_snprintf( buf, sizeof(buf), "<i>%s; %d %s</i>",
204                sizeStr, fileCount,
205                ngettext("File", "Files", fileCount) );
206    gtk_label_set_markup ( GTK_LABEL(ui->size_lb), buf );
207
208    tr_strlsize( sizeStr, pieceSize, sizeof(sizeStr) );
209    g_snprintf( buf, sizeof(buf), "<i>%d %s @ %s</i>",
210                pieceCount,
211                ngettext("Piece", "Pieces", pieceCount),
212                sizeStr );
213    gtk_label_set_markup ( GTK_LABEL(ui->pieces_lb), buf );
214}
215
216static void
217file_chooser_shown_cb( GtkWidget *w, gpointer folder_toggle )
218{
219    const gboolean isFolder = gtk_toggle_button_get_active( folder_toggle );
220    gtk_file_chooser_set_action (GTK_FILE_CHOOSER(w), isFolder
221        ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
222        : GTK_FILE_CHOOSER_ACTION_OPEN );
223}
224
225GtkWidget*
226make_meta_ui( GtkWindow * parent, tr_handle * handle )
227{
228    int row = 0;
229    GtkWidget *d, *t, *w, *h, *rb_file, *rb_dir;
230    GtkBox * main_vbox;
231    char name[256];
232    MakeMetaUI * ui = g_new0 ( MakeMetaUI, 1 );
233    ui->handle = handle;
234
235    d = gtk_dialog_new_with_buttons( _("New Torrent"),
236                                     parent,
237                                     GTK_DIALOG_DESTROY_WITH_PARENT,
238                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
239                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
240                                     GTK_STOCK_NEW, GTK_RESPONSE_ACCEPT,
241                                     NULL );
242    g_signal_connect( d, "response", G_CALLBACK(response_cb), ui );
243    g_object_set_data_full( G_OBJECT(d), "ui", ui, freeMetaUI );
244    ui->dialog = d;
245    main_vbox = GTK_BOX( GTK_DIALOG( d )->vbox );
246
247    t = hig_workarea_create ();
248
249    hig_workarea_add_section_title (t, &row, _("Files"));
250
251        g_snprintf( name, sizeof(name), "%s:", _("File _Type"));
252        h = gtk_hbox_new( FALSE, GUI_PAD_SMALL );
253        w = rb_dir = gtk_radio_button_new_with_mnemonic( NULL, _("Directory"));
254        gtk_box_pack_start ( GTK_BOX(h), w, FALSE, FALSE, 0 );
255        w = rb_file = gtk_radio_button_new_with_mnemonic_from_widget( GTK_RADIO_BUTTON(w), _("Single File") );
256        gtk_box_pack_start ( GTK_BOX(h), w, FALSE, FALSE, 0 );
257        hig_workarea_add_row (t, &row, name, h, NULL);
258
259        g_snprintf( name, sizeof(name), "%s:", _("_File"));
260
261        w = gtk_file_chooser_dialog_new ( NULL,
262                                          NULL,
263                                          GTK_FILE_CHOOSER_ACTION_OPEN,
264                                          GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
265                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
266                                          NULL );
267        g_signal_connect( w, "map", G_CALLBACK(file_chooser_shown_cb), rb_dir );
268        w = gtk_file_chooser_button_new_with_dialog( w );
269        g_signal_connect( w, "selection-changed", G_CALLBACK(file_selection_changed_cb), ui );
270        hig_workarea_add_row (t, &row, name, w, NULL);
271
272        g_snprintf( name, sizeof(name), "<i>%s</i>", _("No Files Selected"));
273        h = gtk_hbox_new( FALSE, GUI_PAD_SMALL );
274        w = ui->size_lb = gtk_label_new (NULL);
275        gtk_label_set_markup ( GTK_LABEL(w), name );
276        gtk_box_pack_start( GTK_BOX(h), w, FALSE, FALSE, 0 );
277        w = ui->pieces_lb = gtk_label_new (NULL);
278        gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, 0 );
279        w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
280        gtk_widget_set_size_request (w, 2 * GUI_PAD_BIG, 0);
281        gtk_box_pack_start_defaults ( GTK_BOX(h), w );
282        hig_workarea_add_row (t, &row, "", h, NULL);
283       
284
285    hig_workarea_add_section_divider( t, &row );
286    hig_workarea_add_section_title (t, &row, _("Torrent"));
287
288        g_snprintf( name, sizeof(name), _("Private to this Tracker") );
289        w = ui->private_check = hig_workarea_add_wide_checkbutton( t, &row, name, FALSE );
290
291        g_snprintf( name, sizeof(name), "%s:", _("Announce _URL"));
292        w = ui->announce_entry = gtk_entry_new( );
293        gtk_entry_set_text(GTK_ENTRY(w), "http://");
294        hig_workarea_add_row (t, &row, name, w, NULL );
295
296        g_snprintf( name, sizeof(name), "%s:", _("Commen_t"));
297        w = ui->comment_entry = gtk_entry_new( );
298        hig_workarea_add_row (t, &row, name, w, NULL );
299
300    hig_workarea_finish( t, &row );
301    gtk_box_pack_start_defaults( main_vbox, t );
302
303    w = gtk_frame_new( NULL );
304    gtk_frame_set_shadow_type( GTK_FRAME( w ), GTK_SHADOW_NONE );
305    gtk_container_set_border_width( GTK_CONTAINER( w ), GUI_PAD_BIG );
306   
307        ui->progressbar = gtk_progress_bar_new( );
308        gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar), _( "No Files Selected" ) );
309        gtk_container_add( GTK_CONTAINER( w ), ui->progressbar );
310
311    gtk_box_pack_start( main_vbox, w, FALSE, FALSE, GUI_PAD_BIG );
312
313    gtk_window_set_default_size( GTK_WINDOW(d), 400u, 0u );
314    gtk_widget_show_all( GTK_DIALOG(d)->vbox );
315    setIsBuilding( ui, FALSE );
316    return d;
317}
Note: See TracBrowser for help on using the repository browser.