source: trunk/gtk/dialogs.c @ 1506

Last change on this file since 1506 was 1506, checked in by joshe, 15 years ago

Add a window to view a torrent's files.

  • Property svn:keywords set to Date Rev Author Id
File size: 18.3 KB
Line 
1/******************************************************************************
2 * $Id: dialogs.c 1506 2007-02-23 06:18:45Z joshe $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <errno.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <gtk/gtk.h>
30#include <glib/gi18n.h>
31
32#include "conf.h"
33#include "dialogs.h"
34#include "tr_icon.h"
35#include "tr_prefs.h"
36#include "util.h"
37
38#include "transmission.h"
39
40#define PREFNAME                "transmission-dialog-pref-name"
41#define FILESWIND_EXTRA_INDENT  4
42
43#define STRIPROOT( path )                                                     \
44    ( g_path_is_absolute( (path) ) ? g_path_skip_root( (path) ) : (path) )
45
46struct addcb {
47  add_torrents_func_t addfunc;
48  void *data;
49  gboolean autostart;
50  gboolean usingaltdir;
51  GtkFileChooser *altdir;
52  GtkButtonBox *altbox;
53};
54
55struct dirdata
56{
57    add_torrents_func_t addfunc;
58    void              * cbdata;
59    GList             * files;
60    guint               flags;
61};
62
63struct quitdata
64{
65    callbackfunc_t func;
66    void         * cbdata;
67};
68
69struct fileswind
70{
71    TrTorrent    * tor;
72    GtkTreeModel * model;
73};
74
75static void
76autoclick(GtkWidget *widget, gpointer gdata);
77static void
78dirclick(GtkWidget *widget, gpointer gdata);
79static void
80addresp(GtkWidget *widget, gint resp, gpointer gdata);
81static void
82promptresp( GtkWidget * widget, gint resp, gpointer data );
83static void
84quitresp( GtkWidget * widget, gint resp, gpointer data );
85static void
86parsepath( GtkTreeStore * store, GtkTreeIter * ret,
87           const char * path, int index, uint64_t size );
88static void
89setscroll( void * arg );
90static void
91fileswindresp( GtkWidget * widget, gint resp SHUTUP, gpointer data );
92
93void
94makeaddwind(GtkWindow *parent, add_torrents_func_t addfunc, void *cbdata) {
95  GtkWidget *wind = gtk_file_chooser_dialog_new(_("Add a Torrent"), parent,
96    GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
97    GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
98  struct addcb *data = g_new(struct addcb, 1);
99  GtkWidget *vbox = gtk_vbox_new(FALSE, 3);
100  GtkWidget *bbox = gtk_hbutton_box_new();
101  GtkWidget *autocheck = gtk_check_button_new_with_mnemonic(
102    _("Automatically _start torrent"));
103  GtkWidget *dircheck = gtk_check_button_new_with_mnemonic(
104    _("Use alternate _download directory"));
105  GtkFileFilter *filter = gtk_file_filter_new();
106  GtkFileFilter *unfilter = gtk_file_filter_new();
107  GtkWidget *getdir = gtk_file_chooser_button_new(
108    _("Choose a download directory"), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
109
110  data->addfunc = addfunc;
111  data->data = cbdata;
112  data->autostart = TRUE;
113  data->usingaltdir = FALSE;
114  data->altdir = GTK_FILE_CHOOSER(getdir);
115  data->altbox = GTK_BUTTON_BOX(bbox);
116
117  gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_EDGE);
118  gtk_box_pack_start_defaults(GTK_BOX(bbox), dircheck);
119  gtk_box_pack_start_defaults(GTK_BOX(bbox), getdir);
120
121  gtk_box_pack_start_defaults(GTK_BOX(vbox), autocheck);
122  gtk_box_pack_start_defaults(GTK_BOX(vbox), bbox);
123
124  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(autocheck), TRUE);
125  gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( getdir ),
126                                       getdownloaddir() );
127
128  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dircheck), FALSE);
129  gtk_widget_set_sensitive(getdir, FALSE);
130
131  gtk_file_filter_set_name(filter, _("Torrent files"));
132  gtk_file_filter_add_pattern(filter, "*.torrent");
133  gtk_file_filter_set_name(unfilter, _("All files"));
134  gtk_file_filter_add_pattern(unfilter, "*");
135
136  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(wind), filter);
137  gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(wind), unfilter);
138  gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(wind), TRUE);
139  gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(wind), vbox);
140
141  g_signal_connect(G_OBJECT(autocheck), "clicked", G_CALLBACK(autoclick),data);
142  g_signal_connect(G_OBJECT(dircheck), "clicked", G_CALLBACK(dirclick), data);
143  g_signal_connect(G_OBJECT(wind), "response", G_CALLBACK(addresp), data);
144
145  gtk_widget_show_all(wind);
146}
147
148static void
149autoclick(GtkWidget *widget, gpointer gdata) {
150  struct addcb *data = gdata;
151
152  data->autostart = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
153}
154
155static void
156dirclick(GtkWidget *widget, gpointer gdata) {
157  struct addcb *data = gdata;
158
159  data->usingaltdir = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
160  gtk_widget_set_sensitive(GTK_WIDGET(data->altdir), data->usingaltdir);
161}
162
163static void
164addresp(GtkWidget *widget, gint resp, gpointer gdata) {
165  struct addcb *data = gdata;
166  GSList *files, *ii;
167  GList *stupidgtk;
168  int flags;
169  char *dir;
170
171  if(GTK_RESPONSE_ACCEPT == resp) {
172    dir = NULL;
173    if(data->usingaltdir)
174      dir = gtk_file_chooser_get_filename(data->altdir);
175    files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(widget));
176    stupidgtk = NULL;
177    for(ii = files; NULL != ii; ii = ii->next)
178      stupidgtk = g_list_append(stupidgtk, ii->data);
179    flags = ( data->autostart ? TR_TORNEW_RUNNING : TR_TORNEW_PAUSED );
180    flags |= addactionflag( tr_prefs_get( PREF_ID_ADDSTD ) );
181    data->addfunc( data->data, NULL, stupidgtk, dir, flags );
182    if(NULL != dir)
183      g_free(dir);
184    g_slist_free(files);
185    freestrlist(stupidgtk);
186  }
187
188  g_free( data );
189  gtk_widget_destroy(widget);
190}
191
192#define INFOLINE(tab, ii, nam, val) \
193  do { \
194    char *txt = g_markup_printf_escaped("<b>%s</b>", nam); \
195    GtkWidget *wid = gtk_label_new(NULL); \
196    gtk_misc_set_alignment(GTK_MISC(wid), 0, .5); \
197    gtk_label_set_markup(GTK_LABEL(wid), txt); \
198    gtk_table_attach_defaults(GTK_TABLE(tab), wid, 0, 1, ii, ii + 1); \
199    wid = gtk_label_new(val); \
200    gtk_label_set_selectable(GTK_LABEL(wid), TRUE); \
201    gtk_misc_set_alignment(GTK_MISC(wid), 0, .5); \
202    gtk_table_attach_defaults(GTK_TABLE(tab), wid, 1, 2, ii, ii + 1); \
203    ii++; \
204    g_free(txt); \
205  } while(0)
206
207#define INFOLINEF(tab, ii, fmt, nam, val) \
208  do { \
209    char *buf = g_strdup_printf(fmt, val); \
210    INFOLINE(tab, ii, nam, buf); \
211    g_free(buf); \
212  } while(0)
213
214#define INFOLINEA(tab, ii, nam, val) \
215  do { \
216    char *buf = val; \
217    INFOLINE(tab, ii, nam, buf); \
218    g_free(buf); \
219  } while(0)
220
221#define INFOSEP(tab, ii) \
222  do { \
223    GtkWidget *wid = gtk_hseparator_new(); \
224    gtk_table_attach_defaults(GTK_TABLE(tab), wid, 0, 2, ii, ii + 1); \
225    ii++; \
226  } while(0)
227
228void
229makeinfowind(GtkWindow *parent, TrTorrent *tor) {
230  tr_stat_t *sb;
231  tr_info_t *in;
232  GtkWidget *wind, *label;
233  int ii;
234  char *str;
235  const int rowcount = 15;
236  GtkWidget *table = gtk_table_new(rowcount, 2, FALSE);
237
238  /* XXX should use model and update this window regularly */
239
240  sb = tr_torrent_stat(tor);
241  in = tr_torrent_info(tor);
242  str = g_strdup_printf(_("%s Properties"), in->name);
243  wind = gtk_dialog_new_with_buttons(str, parent,
244    GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
245    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
246  g_free(str);
247
248  gtk_widget_set_name(wind, "TransmissionDialog");
249  gtk_table_set_col_spacings(GTK_TABLE(table), 12);
250  gtk_table_set_row_spacings(GTK_TABLE(table), 12);
251  gtk_dialog_set_default_response(GTK_DIALOG(wind), GTK_RESPONSE_ACCEPT);
252  gtk_container_set_border_width(GTK_CONTAINER(table), 6);
253  gtk_window_set_resizable(GTK_WINDOW(wind), FALSE);
254
255  label = gtk_label_new(NULL);
256  gtk_label_set_selectable(GTK_LABEL(label), TRUE);
257  str = g_markup_printf_escaped("<big>%s</big>", in->name);
258  gtk_label_set_markup(GTK_LABEL(label), str);
259  g_free(str);
260  gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 2, 0, 1);
261
262  ii = 1;
263
264  INFOSEP(table, ii);
265
266  if(80 == sb->trackerPort)
267    INFOLINEA(table, ii, _("Tracker:"), g_strdup_printf("http://%s",
268              sb->trackerAddress));
269  else
270    INFOLINEA(table, ii, _("Tracker:"), g_strdup_printf("http://%s:%i",
271              sb->trackerAddress, sb->trackerPort));
272  INFOLINE(table, ii, _("Announce:"), sb->trackerAnnounce);
273  INFOLINEA(table, ii, _("Piece Size:"), readablesize(in->pieceSize));
274  INFOLINEF(table, ii, "%i", _("Pieces:"), in->pieceCount);
275  INFOLINEA(table, ii, _("Total Size:"), readablesize(in->totalSize));
276  if(0 > sb->seeders)
277    INFOLINE(table, ii, _("Seeders:"), _("?"));
278  else
279    INFOLINEF(table, ii, "%i", _("Seeders:"), sb->seeders);
280  if(0 > sb->leechers)
281    INFOLINE(table, ii, _("Leechers:"), _("?"));
282  else
283    INFOLINEF(table, ii, "%i", _("Leechers:"), sb->leechers);
284  if(0 > sb->completedFromTracker)
285    INFOLINE(table, ii, _("Completed:"), _("?"));
286  else
287    INFOLINEF(table, ii, "%i", _("Completed:"), sb->completedFromTracker);
288
289  INFOSEP(table, ii);
290
291  INFOLINE(table, ii, _("Directory:"), tr_torrentGetFolder(tr_torrent_handle(tor)));
292  INFOLINEA(table, ii, _("Downloaded:"), readablesize(sb->downloaded));
293  INFOLINEA(table, ii, _("Uploaded:"), readablesize(sb->uploaded));
294
295  INFOSEP(table, ii);
296
297  g_assert(rowcount == ii);
298
299  gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(wind)->vbox), table);
300  g_signal_connect(G_OBJECT(wind), "response",
301                   G_CALLBACK(gtk_widget_destroy), NULL);
302  gtk_widget_show_all(wind);
303}
304
305void
306promptfordir( GtkWindow * parent, add_torrents_func_t addfunc, void *cbdata,
307              GList * files, guint flags )
308{
309    struct dirdata * stuff;
310    GtkWidget      * wind;
311
312    stuff = g_new( struct dirdata, 1 );
313    stuff->addfunc = addfunc;
314    stuff->cbdata  = cbdata;
315    stuff->files   = dupstrlist( files );
316    stuff->flags   = flags;
317
318    wind =  gtk_file_chooser_dialog_new( _("Choose a directory"), parent,
319                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
320                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
321                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
322                                         NULL );
323    gtk_file_chooser_set_local_only( GTK_FILE_CHOOSER( wind ), TRUE );
324    gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER( wind ), FALSE );
325    gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( wind ),
326                                   getdownloaddir() );
327
328    g_signal_connect( G_OBJECT( wind ), "response",
329                      G_CALLBACK( promptresp ), stuff );
330
331    gtk_widget_show_all(wind);
332}
333
334static void
335promptresp( GtkWidget * widget, gint resp, gpointer data )
336{
337    struct dirdata * stuff;
338    char           * dir;
339
340    stuff = data;
341
342    if( GTK_RESPONSE_ACCEPT == resp )
343    {
344        dir = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( widget ) );
345        /* it seems that we will always get a directory */
346        g_assert( NULL != dir );
347        stuff->addfunc( stuff->cbdata, NULL, stuff->files, dir, stuff->flags );
348        g_free( dir );
349    }
350
351    freestrlist( stuff->files );
352    g_free( stuff );
353    gtk_widget_destroy( widget );
354}
355
356void
357askquit( GtkWindow * parent, callbackfunc_t func, void * cbdata )
358{
359    struct quitdata * stuff;
360    GtkWidget * wind;
361
362    stuff          = g_new( struct quitdata, 1 );
363    stuff->func    = func;
364    stuff->cbdata  = cbdata;
365
366    wind = gtk_message_dialog_new( parent, GTK_DIALOG_DESTROY_WITH_PARENT,
367                                   GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
368                                   _("Are you sure you want to quit %s?"),
369                                   g_get_application_name() );
370    gtk_dialog_set_default_response( GTK_DIALOG( wind ), GTK_RESPONSE_YES );
371    g_signal_connect( G_OBJECT( wind ), "response",
372                      G_CALLBACK( quitresp ), stuff );
373
374    gtk_widget_show_all( wind );
375}
376
377static void
378quitresp( GtkWidget * widget, gint resp, gpointer data )
379{
380    struct quitdata * stuff;
381
382    stuff = data;
383
384    if( GTK_RESPONSE_YES == resp )
385    {
386        stuff->func( stuff->cbdata );
387    }
388
389    g_free( stuff );
390    gtk_widget_destroy( widget );
391}
392
393enum filescols { FC_INDEX = 0, FC_LABEL, FC_KEY, FC_STOCK, FC__COUNT };
394
395void
396makefileswind( GtkWindow * parent, TrTorrent * tor )
397{
398    GType cols[] = { G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING };
399    tr_info_t         * inf;
400    GtkTreeStore      * store;
401    int                 ii;
402    GtkWidget         * view, * scroll, * frame, * wind;
403    GtkCellRenderer   * rend;
404    GtkTreeViewColumn * col;
405    GtkTreeSelection  * sel;
406    char              * label;
407    struct fileswind  * fw;
408
409    g_assert( ALEN( cols ) == FC__COUNT );
410
411    /* set up the model */
412    inf   = tr_torrent_info( tor );
413    store = gtk_tree_store_newv( FC__COUNT, cols );
414    for( ii = 0; ii < inf->fileCount; ii++ )
415    {
416        parsepath( store, NULL, STRIPROOT( inf->files[ii].name ),
417                   ii, inf->files[ii].length );
418    }
419    gtk_tree_sortable_set_sort_column_id( GTK_TREE_SORTABLE( store ),
420                                          FC_KEY, GTK_SORT_ASCENDING );
421
422    /* create the view */
423    view = gtk_tree_view_new_with_model( GTK_TREE_MODEL( store ) );
424    col = gtk_tree_view_column_new();
425    gtk_tree_view_column_set_title( col, _("File") );
426    /* add icon renderer */
427    rend = gtk_cell_renderer_pixbuf_new();
428    gtk_tree_view_column_pack_start( col, rend, FALSE );
429    gtk_tree_view_column_add_attribute( col, rend, "stock-id", FC_STOCK );
430    /* add text renderer */
431    rend = gtk_cell_renderer_text_new();
432    gtk_tree_view_column_pack_start( col, rend, TRUE );
433    gtk_tree_view_column_add_attribute( col, rend, "markup", FC_LABEL );
434    gtk_tree_view_append_column( GTK_TREE_VIEW( view ), col );
435    /* set up view */
436    sel = gtk_tree_view_get_selection( GTK_TREE_VIEW( view ) );
437    gtk_tree_selection_set_mode( sel, GTK_SELECTION_NONE );
438    gtk_tree_view_expand_all( GTK_TREE_VIEW( view ) );
439    gtk_tree_view_set_search_column( GTK_TREE_VIEW( view ), FC_LABEL );
440    gtk_widget_show( view );
441
442    /* create the scrolled window and stick the view in it */
443    scroll = gtk_scrolled_window_new( NULL, NULL );
444    gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scroll ),
445                                    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
446    gtk_container_add( GTK_CONTAINER( scroll ), view );
447    gtk_widget_show( scroll );
448
449    /* create a frame around the scroll to make it look a little better */
450    frame = gtk_frame_new( NULL );
451    gtk_frame_set_shadow_type( GTK_FRAME( frame ), GTK_SHADOW_IN );
452    gtk_container_add( GTK_CONTAINER( frame ), scroll );
453    gtk_widget_show( frame );
454
455    /* create the window */
456    label = g_strdup_printf( _("Files for %s"), inf->name );
457    wind = gtk_dialog_new_with_buttons( label, parent,
458                                        GTK_DIALOG_DESTROY_WITH_PARENT |
459                                        GTK_DIALOG_NO_SEPARATOR, 
460                                        GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
461                                        NULL );
462    g_free( label );
463    gtk_dialog_set_default_response( GTK_DIALOG( wind ), GTK_RESPONSE_ACCEPT );
464    gtk_window_set_resizable( GTK_WINDOW( wind ), TRUE );
465    gtk_box_pack_start_defaults( GTK_BOX( GTK_DIALOG( wind )->vbox ), frame );
466
467    /* set up the callback data */
468    fw        = g_new0( struct fileswind, 1 );
469    fw->tor   = tor;
470    fw->model = GTK_TREE_MODEL( store );
471    g_object_add_weak_pointer( G_OBJECT( tor ), ( gpointer * )&fw->tor );
472    g_signal_connect( wind, "response", G_CALLBACK( fileswindresp ), fw );
473
474    /* show the window with a nice initial size */
475    windowsizehack( wind, scroll, view, setscroll, scroll );
476}
477
478static void
479parsepath( GtkTreeStore * store, GtkTreeIter * ret,
480           const char * path, int index, uint64_t size )
481{
482    GtkTreeModel * model;
483    GtkTreeIter  * parent, start, iter;
484    char         * file, * dir, * mykey, * modelkey, * label, * sizestr;
485    const char   * stock;
486
487    model  = GTK_TREE_MODEL( store );
488    parent = NULL;
489    file   = g_path_get_basename( path );
490    if( 0 != strcmp( file, path ) )
491    {
492        dir = g_path_get_dirname( path );
493        parsepath( store, &start, dir, index, size );
494        g_free( dir );
495        parent = &start;
496    }
497
498    mykey = g_utf8_collate_key( file, -1 );
499    if( gtk_tree_model_iter_children( model, &iter, parent ) )
500    {
501        do
502        {
503            gtk_tree_model_get( model, &iter, FC_KEY, &modelkey, -1 );
504            if( NULL != modelkey && 0 == strcmp( mykey, modelkey ) )
505            {
506                goto done;
507            }
508        }
509        while( gtk_tree_model_iter_next( model, &iter ) );
510    }
511
512    gtk_tree_store_append( store, &iter, parent );
513    if( NULL == ret )
514    {
515        sizestr = readablesize( size );
516        label = g_markup_printf_escaped( _("<small>%s (%s)</small>"),
517                                         file, sizestr );
518        g_free( sizestr );
519        stock = GTK_STOCK_FILE;
520    }
521    else
522    {
523        label = g_markup_printf_escaped( _("<small>%s</small>"), file );
524        stock = GTK_STOCK_DIRECTORY;
525    }
526    gtk_tree_store_set( store, &iter, FC_INDEX, index, FC_LABEL, label,
527                        FC_KEY, mykey, FC_STOCK, stock, -1 );
528    g_free( label );
529  done:
530    g_free( mykey );
531    g_free( file );
532    if( NULL != ret )
533    {
534        memcpy( ret, &iter, sizeof( iter ) );
535    }
536}
537
538static void
539setscroll( void * arg )
540{
541    gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( arg ),
542        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
543}
544
545static void
546fileswindresp( GtkWidget * widget, gint resp SHUTUP, gpointer data )
547{
548    struct fileswind * fw;
549
550    fw = data;
551
552    g_object_unref( fw->model );
553    g_object_remove_weak_pointer( G_OBJECT( fw->tor ),
554                                  ( gpointer * )&fw->tor );
555    g_free( fw );
556    gtk_widget_destroy( widget );
557}
Note: See TracBrowser for help on using the repository browser.