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

Last change on this file since 3350 was 3350, checked in by charles, 15 years ago

Better GNOME Integration (#338): (1) follow HIG recommendation that dialog titles be the name of the command that opened the dialog (2) rename stop-torrent as pause-torrent in the underlying gtk code

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