source: trunk/gtk/details.c @ 5320

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

(gtk) #804: Details window, Info tab, the torrent hash should be selectable (for copy/paste and to use it in search engines)

  • Property svn:keywords set to Date Rev Author Id
File size: 40.6 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: details.c 5320 2008-03-21 14:12:32Z charles $
11 */
12
13#include <stddef.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <glib/gi18n.h>
17#include <gtk/gtk.h>
18
19#include <libtransmission/transmission.h>
20
21#include "actions.h"
22#include "details.h"
23#include "file-list.h"
24#include "tr-torrent.h"
25#include "hig.h"
26#include "util.h"
27
28#define UPDATE_INTERVAL_MSEC 2000
29
30/****
31*****  PIECES VIEW
32****/
33
34/* define SHOW_PIECES */
35
36#ifdef SHOW_PIECES
37static int
38getGridSize (int pieceCount, int * n_rows, int * n_cols)
39{
40  const int MAX_ACROSS = 16;
41  if (pieceCount >= (MAX_ACROSS * MAX_ACROSS)) {
42    *n_rows = *n_cols = MAX_ACROSS;
43    return MAX_ACROSS * MAX_ACROSS;
44  }
45  else {
46    int i;
47    for (i=0; (i*i) < pieceCount; ++i);
48    *n_rows = *n_cols = i;
49    return pieceCount;
50  }
51}
52
53#define TO16(a) ((guint16)((a<<8)|(a)))
54#define RGB_2_GDK(R,G,B) { 0, TO16(R), TO16(G), TO16(B) }
55
56enum { DRAW_AVAIL, DRAW_PROG };
57
58static void
59release_gobject_array (gpointer data)
60{
61  int i;
62  GObject **objects = (GObject**) data;
63  for (i=0; objects[i]!=NULL; ++i)
64    g_object_unref (G_OBJECT(objects[i]));
65  g_free (objects);
66}
67
68static gboolean
69refresh_pieces( GtkWidget * da, GdkEventExpose * event UNUSED, gpointer gtor )
70{
71  tr_torrent * tor = tr_torrent_handle( TR_TORRENT(gtor) );
72  const tr_info * info = tr_torrent_info( TR_TORRENT(gtor) );
73  int mode = GPOINTER_TO_INT (g_object_get_data (G_OBJECT(da), "draw-mode"));
74
75  GdkColormap * colormap = gtk_widget_get_colormap (da);
76  const int widget_w = da->allocation.width;
77  const int widget_h = da->allocation.height;
78  int n_rows, n_cols;
79  const int n_cells = getGridSize (info->pieceCount,  &n_rows, &n_cols);
80  const GdkRectangle grid_bounds = { 0, 0, widget_w, widget_h };
81  const double piece_w = grid_bounds.width / (double)n_cols;
82  const double piece_h = grid_bounds.height / (double)n_rows;
83  const int piece_w_int = (int) (piece_w + 1); /* pad for roundoff */
84  const int piece_h_int = (int) (piece_h + 1); /* pad for roundoff */
85  const gboolean rtl = gtk_widget_get_direction( da ) == GTK_TEXT_DIR_RTL;
86
87  guint8 * prev_color = NULL;
88  gboolean first_time = FALSE;
89
90  int i, x, y;
91  int8_t * pieces = NULL;
92  float * completeness = NULL;
93
94  /**
95  ***  Get the Graphics Contexts...
96  **/
97
98  enum { ALL, LOTS, SOME, FEW, NONE,
99         BLACK, GRAY, BLINK,
100         N_COLORS };
101  GdkGC **gcs = (GdkGC**) g_object_get_data (G_OBJECT(da), "graphics-contexts");
102  if (gcs == NULL)
103  {
104    const GdkColor colors [N_COLORS] = {
105      RGB_2_GDK ( 114, 159, 207 ), /* all */
106      RGB_2_GDK (  52, 101, 164 ), /* lots */
107      RGB_2_GDK (  32,  74, 135 ), /* some */
108      RGB_2_GDK (  85,  87,  83 ), /* few */
109      RGB_2_GDK ( 238, 238, 236 ), /* none - tango aluminum highlight */
110      RGB_2_GDK (  46,  52,  54 ), /* black - tango slate shadow */
111      RGB_2_GDK ( 186, 189, 182 ), /* gray - tango aluminum shadow */
112      RGB_2_GDK ( 252, 233,  79 ), /* blink - tango butter highlight */
113    };
114
115    gcs = g_new (GdkGC*, N_COLORS+1);
116
117    for (i=0; i<N_COLORS; ++i) {
118      gcs[i] = gdk_gc_new (da->window);
119      gdk_gc_set_colormap (gcs[i], colormap);
120      gdk_gc_set_rgb_fg_color (gcs[i], &colors[i]);
121      gdk_gc_set_rgb_bg_color (gcs[i], &colors[i]);
122    };
123
124    gcs[N_COLORS] = NULL; /* a sentinel in the release function */
125    g_object_set_data_full (G_OBJECT(da), "graphics-contexts",
126                            gcs, release_gobject_array);
127  }
128
129  /**
130  ***  Get the cells' previous colors...
131  ***  (this is used for blinking when the color changes)
132  **/
133
134  prev_color = (guint8*) g_object_get_data (G_OBJECT(da), "prev-color");
135  if (prev_color == NULL)
136  {
137    first_time = TRUE;
138    prev_color = g_new0 (guint8, n_cells);
139    g_object_set_data_full (G_OBJECT(da), "prev-color", prev_color, g_free);
140  }
141
142  /**
143  ***  Get the piece data values...
144  **/
145
146  switch (mode) {
147    case DRAW_AVAIL:
148      pieces = g_new (int8_t, n_cells);
149      tr_torrentAvailability ( tor, pieces, n_cells );
150      break;
151    case DRAW_PROG:
152      completeness = g_new (float, n_cells);
153      tr_torrentAmountFinished ( tor, completeness, n_cells );
154      break;
155    default: g_error("no mode defined!");
156  }
157
158  /**
159  ***  Draw...
160  **/
161
162  i = 0; 
163  for (y=0; y<n_rows; ++y) {
164    for (x=0; x<n_cols; ++x) {
165      int draw_x = grid_bounds.x + (int)(x * piece_w);
166      int draw_y = grid_bounds.y + (int)(y * piece_h);
167      int color = BLACK;
168      int border = BLACK;
169
170      if( rtl )
171          draw_x = grid_bounds.x + grid_bounds.width - (int)((x+1) * piece_w);
172      else
173          draw_x = grid_bounds.x + (int)(x * piece_w);
174      draw_y = grid_bounds.y + (int)(y * piece_h);
175
176      if (i < n_cells)
177      {
178        border = GRAY;
179
180        if (mode == DRAW_AVAIL) {
181          const int8_t val = pieces[i];
182               if (val <  0) color = ALL;
183          else if (val == 0) color = NONE;
184          else if (val <= 4) color = FEW;
185          else if (val <= 8) color = SOME;
186          else               color = LOTS;
187        } else { /* completeness */
188          const float val = completeness[i];
189               if (val >= 1.00) color = ALL;
190          else if (val >= 0.66) color = LOTS;
191          else if (val >= 0.33) color = SOME;
192          else if (val >= 0.01) color = FEW;
193          else                  color = NONE;
194        }
195
196        /* draw a "blink" for one interval when a piece changes */
197        if (first_time)
198          prev_color[i] = color;
199        else if (color != prev_color[i]) {
200          prev_color[i] = color;
201          color = border = BLINK;
202        }
203      }
204
205      gdk_draw_rectangle (da->window, gcs[color], TRUE,
206                          draw_x, draw_y,
207                          piece_w_int, piece_h_int);
208
209      if (i < n_cells)
210        gdk_draw_rectangle (da->window, gcs[border], FALSE,
211                            draw_x, draw_y,
212                            piece_w_int, piece_h_int);
213
214      ++i;
215    }
216  }
217
218  gdk_draw_rectangle (da->window, gcs[GRAY], FALSE,
219                      grid_bounds.x, grid_bounds.y, 
220                      grid_bounds.width-1, grid_bounds.height-1);
221
222  g_free (pieces);
223  g_free (completeness);
224  return FALSE;
225}
226#endif
227
228/****
229*****  PEERS TAB
230****/
231
232enum
233{
234  PEER_COL_ADDRESS,
235  PEER_COL_CLIENT,
236  PEER_COL_PROGRESS,
237  PEER_COL_IS_ENCRYPTED,
238  PEER_COL_DOWNLOAD_RATE,
239  PEER_COL_UPLOAD_RATE,
240  PEER_COL_STATUS,
241  N_PEER_COLS
242};
243
244static const char* peer_column_names[N_PEER_COLS] =
245{
246  N_("Address"),
247  N_("Client"),
248  /* 'percent done' column header. terse to keep the column narrow. */
249  N_("%"),
250  " ",
251  /* 'download speed' column header. terse to keep the column narrow. */
252  N_("Down"),
253  /* 'upload speed' column header.  terse to keep the column narrow. */
254  N_("Up"),
255  N_("Status")
256};
257
258static int compare_peers (const void * a, const void * b)
259{
260  const tr_peer_stat * pa = a;
261  const tr_peer_stat * pb = b;
262  return strcmp (pa->addr, pb->addr);
263}
264static int compare_addr_to_peer (const void * a, const void * b)
265{
266  const char * addr = (const char *) a;
267  const tr_peer_stat * peer = b;
268  return strcmp (addr, peer->addr);
269}
270
271static void
272peer_row_set (GtkTreeStore        * store,
273              GtkTreeIter         * iter,
274              const tr_peer_stat  * peer)
275{
276  const char * client = peer->client;
277
278  if (!client || !strcmp(client,"Unknown Client"))
279    client = " ";
280
281  gtk_tree_store_set (store, iter,
282                      PEER_COL_ADDRESS, peer->addr,
283                      PEER_COL_CLIENT, client,
284                      PEER_COL_IS_ENCRYPTED, peer->isEncrypted,
285                      PEER_COL_PROGRESS, (int)(100.0*peer->progress),
286                      PEER_COL_DOWNLOAD_RATE, peer->downloadFromRate,
287                      PEER_COL_UPLOAD_RATE, peer->uploadToRate,
288                      PEER_COL_STATUS, peer->flagStr,
289                      -1);
290}
291
292static void
293append_peers_to_model (GtkTreeStore        * store,
294                       const tr_peer_stat  * peers,
295                       int                   n_peers)
296{
297  int i;
298  for (i=0; i<n_peers; ++i) {
299    GtkTreeIter iter;
300    gtk_tree_store_append (store, &iter, NULL);
301    peer_row_set (store, &iter, &peers[i]);
302  }
303}
304
305static GtkTreeModel*
306peer_model_new (tr_torrent * tor)
307{
308  GtkTreeStore * m = gtk_tree_store_new (N_PEER_COLS,
309                                         G_TYPE_STRING,   /* addr */
310                                         G_TYPE_STRING,   /* client */
311                                         G_TYPE_INT,      /* progress [0..100] */
312                                         G_TYPE_BOOLEAN,  /* isEncrypted */
313                                         G_TYPE_FLOAT,    /* downloadFromRate */
314                                         G_TYPE_FLOAT,    /* uploadToRate */
315                                         G_TYPE_STRING ); /* flagString */
316
317  int n_peers = 0;
318  tr_peer_stat * peers = tr_torrentPeers (tor, &n_peers);
319  qsort (peers, n_peers, sizeof(tr_peer_stat), compare_peers);
320  append_peers_to_model (m, peers, n_peers);
321  tr_torrentPeersFree( peers, 0 );
322  return GTK_TREE_MODEL (m);
323}
324
325static void
326render_encrypted (GtkTreeViewColumn  * column UNUSED,
327                  GtkCellRenderer    * renderer,
328                  GtkTreeModel       * tree_model,
329                  GtkTreeIter        * iter,
330                  gpointer             data UNUSED)
331{
332  gboolean is_encrypted = FALSE;
333  gtk_tree_model_get (tree_model, iter, PEER_COL_IS_ENCRYPTED, &is_encrypted,
334                                        -1);
335  g_object_set (renderer, "xalign", (gfloat)0.0,
336                          "yalign", (gfloat)0.5,
337                          "stock-id", (is_encrypted ? "transmission-lock" : NULL),
338                          NULL);
339}
340
341static void
342render_ul_rate (GtkTreeViewColumn  * column UNUSED,
343                GtkCellRenderer    * renderer,
344                GtkTreeModel       * tree_model,
345                GtkTreeIter        * iter,
346                gpointer             data UNUSED)
347{
348  float rate = 0.0;
349  gtk_tree_model_get (tree_model, iter, PEER_COL_UPLOAD_RATE, &rate, -1);
350  if( rate < 0.01 )
351    g_object_set (renderer, "text", "", NULL);
352  else {
353    char speedStr[64];
354    tr_strlspeed( speedStr, rate, sizeof(speedStr) );
355    g_object_set( renderer, "text", speedStr, NULL );
356  }
357}
358
359static void
360render_dl_rate (GtkTreeViewColumn  * column UNUSED,
361                GtkCellRenderer    * renderer,
362                GtkTreeModel       * tree_model,
363                GtkTreeIter        * iter,
364                gpointer             data UNUSED)
365{
366  float rate = 0.0;
367  gtk_tree_model_get (tree_model, iter, PEER_COL_DOWNLOAD_RATE, &rate, -1);
368  if( rate < 0.01 )
369    g_object_set (renderer, "text", "", NULL);
370  else {
371    char speedStr[64];
372    tr_strlspeed( speedStr, rate, sizeof(speedStr) );
373    g_object_set( renderer, "text", speedStr, NULL );
374  }
375}
376
377static void
378render_client (GtkTreeViewColumn   * column UNUSED,
379               GtkCellRenderer     * renderer,
380               GtkTreeModel        * tree_model,
381               GtkTreeIter         * iter,
382               gpointer              data UNUSED)
383{
384  char * client = NULL;
385  gtk_tree_model_get (tree_model, iter, PEER_COL_CLIENT, &client,
386                                        -1);
387  g_object_set (renderer, "text", (client ? client : ""), NULL);
388  g_free (client);
389}
390
391typedef struct
392{
393  TrTorrent * gtor;
394  GtkTreeModel * model; /* same object as store, but recast */
395  GtkTreeStore * store; /* same object as model, but recast */
396  GtkWidget * completeness;
397  GtkWidget * seeders_lb;
398  GtkWidget * leechers_lb;
399  GtkWidget * completed_lb;
400}
401PeerData;
402
403static void
404fmtpeercount (GtkWidget * l, int count)
405{
406  if( 0 > count ) {
407    gtk_label_set_text( GTK_LABEL(l), "?" );
408  } else {
409    char str[16];
410    g_snprintf( str, sizeof str, "%i", count );
411    gtk_label_set_text( GTK_LABEL(l), str );
412  }
413}
414
415static void
416refresh_peers (GtkWidget * top)
417{
418  int i;
419  int n_peers;
420  GtkTreeIter iter;
421  PeerData * p = (PeerData*) g_object_get_data (G_OBJECT(top), "peer-data");
422  tr_torrent * tor = tr_torrent_handle ( p->gtor );
423  GtkTreeModel * model = p->model;
424  GtkTreeStore * store = p->store;
425  tr_peer_stat * peers;
426  const tr_stat * stat = tr_torrent_stat( p->gtor );
427
428  /**
429  ***  merge the peer diffs into the tree model.
430  ***
431  ***  this is more complicated than creating a new model,
432  ***  but is also (a) more efficient and (b) doesn't undo
433  ***  the view's visible area and sorting on every refresh.
434  **/
435
436  n_peers = 0;
437  peers = tr_torrentPeers (tor, &n_peers);
438  qsort (peers, n_peers, sizeof(tr_peer_stat), compare_peers);
439
440  i = 0;
441  if (gtk_tree_model_get_iter_first (model, &iter)) do
442  {
443    char * addr = NULL;
444    tr_peer_stat * peer = NULL;
445    gtk_tree_model_get (model, &iter, PEER_COL_ADDRESS, &addr, -1);
446    peer = bsearch (addr, peers, n_peers, sizeof(tr_peer_stat),
447                    compare_addr_to_peer);
448    g_free (addr);
449
450    if (peer) /* update a pre-existing row */
451    {
452      const int pos = peer - peers;
453      const int n_rhs = n_peers - (pos+1);
454      g_assert (n_rhs >= 0);
455
456      peer_row_set (store, &iter, peer);
457
458      /* remove it from the tr_peer_stat list */
459      g_memmove (peer, peer+1, sizeof(tr_peer_stat)*n_rhs);
460      --n_peers;
461    }
462    else if (!gtk_tree_store_remove (store, &iter))
463      break; /* we removed the model's last item */
464  }
465  while (gtk_tree_model_iter_next (model, &iter));
466
467  append_peers_to_model (store, peers, n_peers);  /* all these are new */
468
469#ifdef SHOW_PIECES
470  if (GDK_IS_DRAWABLE (p->completeness->window))
471    refresh_pieces (p->completeness, NULL, p->gtor);
472#endif
473
474  fmtpeercount (p->seeders_lb, stat->seeders);
475  fmtpeercount (p->leechers_lb, stat->leechers);
476  fmtpeercount (p->completed_lb, stat->completedFromTracker );
477
478  free( peers );
479}
480
481static GtkWidget* peer_page_new ( TrTorrent * gtor )
482{
483  guint i;
484  GtkTreeModel *m;
485  GtkWidget *h, *v, *w, *ret, *sw, *l, *vbox, *hbox;
486  tr_torrent * tor = tr_torrent_handle (gtor);
487  PeerData * p = g_new (PeerData, 1);
488
489  /* TODO: make this configurable? */
490  int view_columns[] = { PEER_COL_IS_ENCRYPTED,
491                         PEER_COL_UPLOAD_RATE,
492                         PEER_COL_DOWNLOAD_RATE,
493                         PEER_COL_PROGRESS,
494                         PEER_COL_STATUS,
495                         PEER_COL_ADDRESS,
496                         PEER_COL_CLIENT };
497
498  m  = peer_model_new (tor);
499  v = gtk_tree_view_new_with_model (m);
500  gtk_widget_set_size_request( v, 550, 0 );
501  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(v), TRUE);
502  g_object_unref (G_OBJECT(m));
503
504  for (i=0; i<G_N_ELEMENTS(view_columns); ++i)
505  {
506    const int col = view_columns[i];
507    const char * t = _(peer_column_names[col]);
508    gboolean resizable = TRUE;
509    GtkTreeViewColumn * c;
510    GtkCellRenderer * r;
511
512    switch (col)
513    {
514      case PEER_COL_ADDRESS:
515        r = gtk_cell_renderer_text_new ();
516        c = gtk_tree_view_column_new_with_attributes (t, r, "text", col, NULL);
517        break;
518
519      case PEER_COL_CLIENT:
520        r = gtk_cell_renderer_text_new ();
521        c = gtk_tree_view_column_new_with_attributes (t, r, "text", col, NULL);
522        gtk_tree_view_column_set_cell_data_func (c, r, render_client,
523                                                 NULL, NULL);
524        break;
525
526      case PEER_COL_PROGRESS:
527        r = gtk_cell_renderer_progress_new ();
528        c = gtk_tree_view_column_new_with_attributes (t, r, "value", PEER_COL_PROGRESS, NULL);
529        break;
530
531      case PEER_COL_IS_ENCRYPTED:
532        resizable = FALSE;
533        r = gtk_cell_renderer_pixbuf_new ();
534        c = gtk_tree_view_column_new_with_attributes (t, r, NULL);
535        gtk_tree_view_column_set_sizing (c, GTK_TREE_VIEW_COLUMN_FIXED);
536        gtk_tree_view_column_set_fixed_width (c, 20);
537        gtk_tree_view_column_set_cell_data_func (c, r, render_encrypted,
538                                                 NULL, NULL);
539        break;
540
541      case PEER_COL_DOWNLOAD_RATE:
542        r = gtk_cell_renderer_text_new ();
543        c = gtk_tree_view_column_new_with_attributes (t, r, "text", col, NULL);
544        gtk_tree_view_column_set_cell_data_func (c, r, render_dl_rate,
545                                                 NULL, NULL);
546        break;
547
548      case PEER_COL_UPLOAD_RATE:
549        r = gtk_cell_renderer_text_new ();
550        c = gtk_tree_view_column_new_with_attributes (t, r, "text", col, NULL);
551        gtk_tree_view_column_set_cell_data_func (c, r, render_ul_rate,
552                                                 NULL, NULL);
553        break;
554
555      case PEER_COL_STATUS:
556        r = gtk_cell_renderer_text_new( );
557        c = gtk_tree_view_column_new_with_attributes (t, r, "text", col, NULL);
558        break;
559
560      default:
561        abort ();
562    }
563
564    gtk_tree_view_column_set_resizable (c, resizable);
565    gtk_tree_view_column_set_sort_column_id (c, col);
566    gtk_tree_view_append_column (GTK_TREE_VIEW(v), c);
567  }
568
569  /* the 'expander' column has a 10-pixel margin on the left
570     that doesn't look quite correct in any of these columns...
571     so create a non-visible column and assign it as the
572     'expander column. */
573  {
574    GtkTreeViewColumn *c = gtk_tree_view_column_new ();
575    gtk_tree_view_column_set_visible (c, FALSE);
576    gtk_tree_view_append_column (GTK_TREE_VIEW(v), c);
577    gtk_tree_view_set_expander_column (GTK_TREE_VIEW(v), c);
578  }
579
580  w = sw = gtk_scrolled_window_new (NULL, NULL);
581  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(w),
582                                  GTK_POLICY_NEVER,
583                                  GTK_POLICY_AUTOMATIC);
584  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(w),
585                                       GTK_SHADOW_IN);
586  gtk_container_add (GTK_CONTAINER(w), v);
587
588
589  vbox = gtk_vbox_new (FALSE, GUI_PAD);
590  gtk_container_set_border_width (GTK_CONTAINER(vbox), GUI_PAD_BIG);
591
592#ifdef SHOW_PIECES
593    l = gtk_label_new (NULL);
594    gtk_misc_set_alignment (GTK_MISC(l), 0.0f, 0.5f);
595    gtk_label_set_markup (GTK_LABEL(l), _( "<b>Availability</b>" ) );
596    gtk_box_pack_start (GTK_BOX(vbox), l, FALSE, FALSE, 0);
597
598    w = da = p->completeness = gtk_drawing_area_new ();
599    gtk_widget_set_size_request (w, 0u, 100u);
600    g_object_set_data (G_OBJECT(w), "draw-mode", GINT_TO_POINTER(DRAW_AVAIL));
601    g_signal_connect (w, "expose-event", G_CALLBACK(refresh_pieces), gtor);
602
603    h = gtk_hbox_new (FALSE, GUI_PAD);
604    w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
605    gtk_widget_set_size_request (w, GUI_PAD_BIG, 0);
606    gtk_box_pack_start (GTK_BOX(h), w, FALSE, FALSE, 0);
607    gtk_box_pack_start_defaults (GTK_BOX(h), da);
608    gtk_box_pack_start (GTK_BOX(vbox), h, FALSE, FALSE, 0);
609
610    /* a small vertical spacer */
611    w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
612    gtk_widget_set_size_request (w, 0u, GUI_PAD);
613    gtk_box_pack_start (GTK_BOX(vbox), w, FALSE, FALSE, 0);
614
615    l = gtk_label_new (NULL);
616    gtk_misc_set_alignment (GTK_MISC(l), 0.0f, 0.5f);
617    gtk_label_set_markup (GTK_LABEL(l), _( "<b>Connected Peers</b>" ) );
618    gtk_box_pack_start (GTK_BOX(vbox), l, FALSE, FALSE, 0);
619#endif
620
621    h = gtk_hbox_new (FALSE, GUI_PAD);
622    gtk_box_pack_start_defaults (GTK_BOX(h), sw);
623    gtk_box_pack_start_defaults (GTK_BOX(vbox), h);
624
625    hbox = gtk_hbox_new (FALSE, GUI_PAD);
626        l = gtk_label_new (NULL);
627        gtk_label_set_markup (GTK_LABEL(l), _( "<b>Seeders:</b>" ) );
628        gtk_box_pack_start (GTK_BOX(hbox), l, FALSE, FALSE, 0);
629        l = p->seeders_lb = gtk_label_new (NULL);
630        gtk_box_pack_start (GTK_BOX(hbox), l, FALSE, FALSE, 0);
631    gtk_box_pack_start_defaults (GTK_BOX(hbox),
632                                 gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f));
633        l = gtk_label_new (NULL);
634        gtk_label_set_markup (GTK_LABEL(l), _( "<b>Leechers:</b>" ) );
635        gtk_box_pack_start (GTK_BOX(hbox), l, FALSE, FALSE, 0);
636        l = p->leechers_lb = gtk_label_new (NULL);
637        gtk_box_pack_start (GTK_BOX(hbox), l, FALSE, FALSE, 0);
638    gtk_box_pack_start_defaults (GTK_BOX(hbox),
639                                 gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f));
640        l = gtk_label_new (NULL);
641        gtk_label_set_markup (GTK_LABEL(l), _( "<b>Completed:</b>" ) );
642        gtk_box_pack_start (GTK_BOX(hbox), l, FALSE, FALSE, 0);
643        l = p->completed_lb = gtk_label_new (NULL);
644        gtk_box_pack_start (GTK_BOX(hbox), l, FALSE, FALSE, 0);
645    gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
646
647  ret = vbox;
648  p->gtor = gtor;
649  p->model = m;
650  p->store = GTK_TREE_STORE(m);
651  g_object_set_data_full (G_OBJECT(ret), "peer-data", p, g_free);
652  return ret;
653}
654
655/****
656*****  INFO TAB
657****/
658
659static GtkWidget*
660info_page_new (tr_torrent * tor)
661{
662  int row = 0;
663  GtkWidget *t = hig_workarea_create ();
664  GtkWidget *l, *w, *fr;
665  char *pch;
666  char sizeStr[128];
667  char buf[256];
668  GtkTextBuffer * b;
669  const tr_info * info = tr_torrentInfo(tor);
670
671  hig_workarea_add_section_title (t, &row, _("Details"));
672
673    tr_strlsize( sizeStr, info->pieceSize, sizeof(sizeStr) );
674    g_snprintf( buf, sizeof( buf ),
675                /* %1$'d is number of pieces
676                   %2$s is how big each piece is */
677                ngettext( "%1$'d Piece @ %2$s",
678                          "%1$'d Pieces @ %2$s",
679                          info->pieceCount ),
680                info->pieceCount, sizeStr );
681    l = gtk_label_new (buf);
682    hig_workarea_add_row (t, &row, _("Pieces:"), l, NULL);
683
684    l = g_object_new( GTK_TYPE_LABEL, "label", info->hashString, "selectable", TRUE,
685                                      "ellipsize", PANGO_ELLIPSIZE_END, NULL );
686    hig_workarea_add_row (t, &row, _("Hash:"), l, NULL);
687
688    pch = (info->isPrivate )
689      ? _("Private to this tracker -- PEX disabled")
690      : _("Public torrent");
691    l = gtk_label_new (pch);
692    hig_workarea_add_row (t, &row, _("Privacy:"), l, NULL);
693
694    b = gtk_text_buffer_new (NULL);
695    if( info->comment )
696        gtk_text_buffer_set_text (b, info->comment, -1);
697    w = gtk_text_view_new_with_buffer (b);
698    gtk_widget_set_size_request (w, 0u, 100u);
699    gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW(w), GTK_WRAP_WORD); 
700    gtk_text_view_set_editable (GTK_TEXT_VIEW(w), FALSE);
701    fr = gtk_frame_new (NULL);
702    gtk_frame_set_shadow_type (GTK_FRAME(fr), GTK_SHADOW_IN);
703    gtk_container_add (GTK_CONTAINER(fr), w);
704    w = hig_workarea_add_row (t, &row, _("Comment:"), fr, NULL);
705    gtk_misc_set_alignment (GTK_MISC(w), 0.0f, 0.0f);
706
707  hig_workarea_add_section_divider (t, &row);
708  hig_workarea_add_section_title (t, &row, _("Origins"));
709 
710    l = gtk_label_new (*info->creator ? info->creator : _("Unknown"));
711    hig_workarea_add_row (t, &row, _("Creator:"), l, NULL);
712
713    pch = rfc822date ((guint64)info->dateCreated * 1000u);
714    l = gtk_label_new (pch);
715    hig_workarea_add_row (t, &row, _("Date:"), l, NULL); 
716    g_free (pch);
717
718  hig_workarea_add_section_divider (t, &row);
719  hig_workarea_add_section_title (t, &row, _("Location"));
720
721    l = g_object_new( GTK_TYPE_LABEL, "label", tr_torrentGetFolder( tor ), "selectable", TRUE,
722                                      "ellipsize", PANGO_ELLIPSIZE_END, NULL );
723    hig_workarea_add_row (t, &row, _( "Destination folder:" ), l, NULL); 
724
725    l = g_object_new( GTK_TYPE_LABEL, "label", info->torrent, "selectable", TRUE,
726                                      "ellipsize", PANGO_ELLIPSIZE_END, NULL );
727    hig_workarea_add_row (t, &row, _( "Torrent file:" ), l, NULL); 
728
729  hig_workarea_finish (t, &row);
730  return t;
731}
732
733/****
734*****  ACTIVITY TAB
735****/
736
737typedef struct
738{
739  GtkWidget * state_lb;
740  GtkWidget * progress_lb;
741  GtkWidget * have_lb;
742  GtkWidget * dl_lb;
743  GtkWidget * ul_lb;
744  GtkWidget * failed_lb;
745  GtkWidget * ratio_lb;
746  GtkWidget * err_lb;
747  GtkWidget * swarm_lb;
748  GtkWidget * date_added_lb;
749  GtkWidget * last_activity_lb;
750  GtkWidget * availability_da;
751  TrTorrent * gtor;
752}
753Activity;
754
755static void
756refresh_activity (GtkWidget * top)
757{
758  Activity * a = (Activity*) g_object_get_data (G_OBJECT(top), "activity-data");
759  const tr_stat * stat = tr_torrent_stat( a->gtor );
760  char *pch;
761  char sizeStr[64];
762  char sizeStr2[64];
763  char buf[128];
764
765  pch = tr_torrent_status_str( a->gtor );
766  gtk_label_set_text (GTK_LABEL(a->state_lb), pch);
767  g_free (pch);
768
769  /* %1$.1f is percent of how much of what we want's been downloaded,
770     %2$.1f is percent of how much of the whole torrent we've downloaded */
771  pch = g_strdup_printf( _( "%1$.1f%% (%2$.1f%% selected)" ), stat->percentComplete*100.0, stat->percentDone*100.0 );
772  gtk_label_set_text (GTK_LABEL(a->progress_lb), pch);
773  g_free (pch);
774
775  tr_strlsize( sizeStr,  stat->haveValid + stat->haveUnchecked, sizeof(sizeStr) );
776  tr_strlsize( sizeStr2, stat->haveValid,                       sizeof(sizeStr2) );
777  /* %1$s is total size of what we've saved to disk
778     %2$s is how much of it's passed the checksum test */
779  g_snprintf( buf, sizeof(buf), _("%1$s (%2$s verified)"), sizeStr, sizeStr2 );
780  gtk_label_set_text( GTK_LABEL( a->have_lb ), buf );
781
782  tr_strlsize( sizeStr, stat->downloadedEver, sizeof(sizeStr) );
783  gtk_label_set_text( GTK_LABEL(a->dl_lb), sizeStr );
784
785  tr_strlsize( sizeStr, stat->uploadedEver, sizeof(sizeStr) );
786  gtk_label_set_text( GTK_LABEL(a->ul_lb), sizeStr );
787
788  tr_strlsize( sizeStr, stat->corruptEver, sizeof( sizeStr ) );
789  gtk_label_set_text( GTK_LABEL( a->failed_lb ), sizeStr );
790
791  tr_strlratio( buf, stat->ratio, sizeof( buf ) );
792  gtk_label_set_text( GTK_LABEL( a->ratio_lb ), buf );
793
794  tr_strlspeed( buf, stat->swarmspeed, sizeof(buf) );
795  gtk_label_set_text (GTK_LABEL(a->swarm_lb), buf );
796
797  gtk_label_set_text (GTK_LABEL(a->err_lb),
798                      *stat->errorString ? stat->errorString : _("None"));
799
800  pch = stat->startDate ? rfc822date( stat->startDate )
801                        : g_strdup_printf( _( "Unknown" ) );
802  gtk_label_set_text (GTK_LABEL(a->date_added_lb), pch);
803  g_free (pch);
804
805  pch = stat->activityDate ? rfc822date( stat->activityDate )
806                           : g_strdup_printf( _( "Unknown" ) );
807  gtk_label_set_text (GTK_LABEL(a->last_activity_lb), pch);
808  g_free (pch);
809
810#ifdef SHOW_PIECES
811  if (GDK_IS_DRAWABLE (a->availability_da->window))
812    refresh_pieces (a->availability_da, NULL, a->gtor);
813#endif
814}
815 
816
817static GtkWidget*
818activity_page_new (TrTorrent * gtor)
819{
820  Activity * a = g_new (Activity, 1);
821  int row = 0;
822  GtkWidget *t = hig_workarea_create ();
823  GtkWidget *l;
824
825  a->gtor = gtor;
826
827  hig_workarea_add_section_title (t, &row, _("Transfer"));
828
829    l = a->state_lb = gtk_label_new (NULL);
830    hig_workarea_add_row (t, &row, _("State:"), l, NULL);
831
832    l = a->progress_lb = gtk_label_new (NULL);
833    hig_workarea_add_row (t, &row, _("Progress:"), l, NULL);
834
835    l = a->have_lb = gtk_label_new (NULL);
836    /* "Have" refers to how much of the torrent we have */
837    hig_workarea_add_row (t, &row, _("Have:"), l, NULL);
838
839    l = a->dl_lb = gtk_label_new (NULL);
840    hig_workarea_add_row (t, &row, _("Downloaded:"), l, NULL);
841
842    l = a->ul_lb = gtk_label_new (NULL);
843    hig_workarea_add_row (t, &row, _("Uploaded:"), l, NULL);
844
845    /* how much downloaded data was corrupt */
846    l = a->failed_lb = gtk_label_new (NULL);
847    hig_workarea_add_row (t, &row, _("Failed DL:"), l, NULL);
848
849    l = a->ratio_lb = gtk_label_new (NULL);
850    hig_workarea_add_row (t, &row, _("Ratio:"), l, NULL);
851
852    l = a->swarm_lb = gtk_label_new (NULL);
853    hig_workarea_add_row (t, &row, _("Swarm rate:"), l, NULL);
854
855    l = a->err_lb = gtk_label_new (NULL);
856    hig_workarea_add_row (t, &row, _("Error:"), l, NULL);
857
858#ifdef SHOW_PIECES
859  hig_workarea_add_section_divider (t, &row);
860  hig_workarea_add_section_title (t, &row, _("Completion"));
861
862    w = a->availability_da = gtk_drawing_area_new ();
863    gtk_widget_set_size_request (w, 0u, 100u);
864    g_object_set_data (G_OBJECT(w), "draw-mode", GINT_TO_POINTER(DRAW_PROG));
865    g_signal_connect (w, "expose-event", G_CALLBACK(refresh_pieces), gtor);
866    hig_workarea_add_wide_control( t, &row, w );
867#endif
868
869  hig_workarea_add_section_divider (t, &row);
870  hig_workarea_add_section_title (t, &row, _("Dates"));
871
872    l = a->date_added_lb = gtk_label_new (NULL);
873    hig_workarea_add_row (t, &row, _("Started at:"), l, NULL);
874
875    l = a->last_activity_lb = gtk_label_new (NULL);
876    hig_workarea_add_row (t, &row, _("Last activity at:"), l, NULL);
877
878  hig_workarea_add_section_divider (t, &row);
879  hig_workarea_finish (t, &row);
880  g_object_set_data_full (G_OBJECT(t), "activity-data", a, g_free);
881  return t;
882}
883
884
885/****
886*****  OPTIONS
887****/
888
889static void
890speed_toggled_cb( GtkToggleButton * tb, gpointer gtor, int up_or_down )
891{
892  tr_torrent * tor = tr_torrent_handle (gtor);
893  gboolean b = gtk_toggle_button_get_active(tb);
894  tr_torrentSetSpeedMode( tor, up_or_down, b ? TR_SPEEDLIMIT_SINGLE
895                                             : TR_SPEEDLIMIT_GLOBAL );
896}
897static void
898ul_speed_toggled_cb (GtkToggleButton *tb, gpointer gtor)
899{
900  speed_toggled_cb( tb, gtor, TR_UP );
901}
902static void
903dl_speed_toggled_cb (GtkToggleButton *tb, gpointer gtor)
904{
905  speed_toggled_cb( tb, gtor, TR_DOWN );
906}
907
908
909#if 0
910static void
911seeding_cap_toggled_cb (GtkToggleButton *tb, gpointer gtor)
912{
913  tr_torrent_set_seeding_cap_enabled (TR_TORRENT(gtor),
914                                      gtk_toggle_button_get_active(tb));
915}
916#endif
917
918static void
919sensitize_from_check_cb (GtkToggleButton *toggle, gpointer w)
920{
921  gtk_widget_set_sensitive (GTK_WIDGET(w),
922                            gtk_toggle_button_get_active(toggle));
923}
924
925static void
926setSpeedLimit( GtkSpinButton* spin, gpointer gtor, int up_or_down )
927{
928  tr_torrent * tor = tr_torrent_handle (gtor);
929  int kb_sec = gtk_spin_button_get_value_as_int (spin);
930  tr_torrentSetSpeedLimit( tor, up_or_down, kb_sec );
931}
932static void
933ul_speed_spun_cb (GtkSpinButton *spin, gpointer gtor)
934{
935  setSpeedLimit( spin, gtor, TR_UP );
936}
937static void
938dl_speed_spun_cb (GtkSpinButton *spin, gpointer gtor)
939{
940  setSpeedLimit( spin, gtor, TR_DOWN );
941}
942
943#if 0
944static void
945seeding_ratio_spun_cb (GtkSpinButton *spin, gpointer gtor)
946{
947  tr_torrent_set_seeding_cap_ratio (TR_TORRENT(gtor),
948                                    gtk_spin_button_get_value(spin));
949}
950#endif
951
952static void
953max_peers_spun_cb( GtkSpinButton * spin, gpointer gtor )
954{
955  const uint16_t n = gtk_spin_button_get_value( spin );
956  tr_torrentSetMaxConnectedPeers( tr_torrent_handle( gtor ), n );
957}
958
959static GtkWidget*
960options_page_new ( TrTorrent * gtor )
961{
962  uint16_t maxConnectedPeers;
963  int i, row;
964  gboolean b;
965  GtkAdjustment *a;
966  GtkWidget *t, *w, *tb;
967  tr_torrent * tor = tr_torrent_handle (gtor);
968
969  row = 0;
970  t = hig_workarea_create ();
971  hig_workarea_add_section_title (t, &row, _("Bandwidth") );
972
973    tb = gtk_check_button_new_with_mnemonic (_("Limit _download speed (KB/s):"));
974    b = tr_torrentGetSpeedMode(tor,TR_DOWN) == TR_SPEEDLIMIT_SINGLE;
975    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(tb), b );
976    g_signal_connect (tb, "toggled", G_CALLBACK(dl_speed_toggled_cb), gtor);
977
978    i = tr_torrentGetSpeedLimit( tor, TR_DOWN );
979    a = (GtkAdjustment*) gtk_adjustment_new (i, 0.0, G_MAXDOUBLE, 1, 1, 1);
980    w = gtk_spin_button_new (a, 1, 0);
981    g_signal_connect (w, "value-changed", G_CALLBACK(dl_speed_spun_cb), gtor);
982    g_signal_connect (tb, "toggled", G_CALLBACK(sensitize_from_check_cb), w);
983    sensitize_from_check_cb (GTK_TOGGLE_BUTTON(tb), w);
984    hig_workarea_add_row_w (t, &row, tb, w, NULL);
985
986    tb = gtk_check_button_new_with_mnemonic (_("Limit _upload speed (KB/s):"));
987    b = tr_torrentGetSpeedMode(tor,TR_UP) == TR_SPEEDLIMIT_SINGLE;
988    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(tb), b );
989    g_signal_connect (tb, "toggled", G_CALLBACK(ul_speed_toggled_cb), gtor);
990
991    i = tr_torrentGetSpeedLimit( tor, TR_UP );
992    a = (GtkAdjustment*) gtk_adjustment_new (i, 0.0, G_MAXDOUBLE, 1, 1, 1);
993    w = gtk_spin_button_new (a, 1, 0);
994    g_signal_connect (w, "value-changed", G_CALLBACK(ul_speed_spun_cb), gtor);
995    g_signal_connect (tb, "toggled", G_CALLBACK(sensitize_from_check_cb), w);
996    sensitize_from_check_cb (GTK_TOGGLE_BUTTON(tb), w);
997    hig_workarea_add_row_w (t, &row, tb, w, NULL);
998
999  hig_workarea_add_section_divider (t, &row);
1000  hig_workarea_add_section_title (t, &row, _("Peer Connections"));
1001
1002    maxConnectedPeers = tr_torrentGetMaxConnectedPeers( tor );
1003    w = gtk_spin_button_new_with_range( 1, 3000, 5 );
1004    gtk_spin_button_set_value( GTK_SPIN_BUTTON( w ), maxConnectedPeers );
1005    hig_workarea_add_row( t, &row, _( "_Maximum peers:" ), w, w );
1006    g_signal_connect( w, "value-changed", G_CALLBACK( max_peers_spun_cb ), gtor );
1007
1008#if 0
1009    tb = gtk_check_button_new_with_mnemonic (_("_Stop seeding at ratio:"));
1010    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(tb), gtor->seeding_cap_enabled);
1011    g_signal_connect (tb, "toggled", G_CALLBACK(seeding_cap_toggled_cb), gtor);
1012    a = (GtkAdjustment*) gtk_adjustment_new (gtor->seeding_cap, 0.0, G_MAXDOUBLE, 1, 1, 1);
1013    w = gtk_spin_button_new (a, 1, 1);
1014    g_signal_connect (w, "value-changed", G_CALLBACK(seeding_ratio_spun_cb), gtor);
1015    g_signal_connect (tb, "toggled", G_CALLBACK(sensitize_from_check_cb), w);
1016    sensitize_from_check_cb (GTK_TOGGLE_BUTTON(tb), w);
1017    hig_workarea_add_row_w (t, &row, tb, w, NULL);
1018#endif
1019
1020  hig_workarea_finish (t, &row);
1021  return t;
1022}
1023
1024static void
1025refresh_options (GtkWidget * top UNUSED)
1026{
1027}
1028
1029/****
1030*****  TRACKER
1031****/
1032
1033#define TRACKER_PAGE "tracker-page"
1034
1035struct tracker_page
1036{
1037    TrTorrent * gtor;
1038
1039    GtkWidget * last_scrape_time_lb;
1040    GtkWidget * last_scrape_response_lb;
1041    GtkWidget * next_scrape_countdown_lb;
1042
1043    GtkWidget * last_announce_time_lb;
1044    GtkWidget * last_announce_response_lb;
1045    GtkWidget * next_announce_countdown_lb;
1046    GtkWidget * manual_announce_countdown_lb;
1047};
1048
1049GtkWidget*
1050tracker_page_new( TrTorrent * gtor )
1051{
1052    GtkWidget * t;
1053    GtkWidget * l;
1054    int row = 0;
1055    const char * s;
1056    char * tmp;
1057    struct tracker_page * page = g_new0( struct tracker_page, 1 );
1058    const tr_tracker_info * track;
1059    const tr_info * info = tr_torrent_info (gtor);
1060
1061    page->gtor = gtor;
1062
1063    t = hig_workarea_create( );
1064    hig_workarea_add_section_title( t, &row, _( "Scrape" ) );
1065
1066        s = _( "Last scrape at:" );
1067        l = gtk_label_new( NULL );
1068        page->last_scrape_time_lb = l;
1069        hig_workarea_add_row( t, &row, s, l, NULL );
1070
1071        s = _( "Tracker responded:");
1072        l = gtk_label_new( NULL );
1073        page->last_scrape_response_lb = l;
1074        hig_workarea_add_row( t, &row, s, l, NULL );
1075
1076        s = _( "Next scrape in:" );
1077        l = gtk_label_new( NULL );
1078        page->next_scrape_countdown_lb = l;
1079        hig_workarea_add_row( t, &row, s, l, NULL );
1080
1081    hig_workarea_add_section_divider( t, &row );
1082    hig_workarea_add_section_title( t, &row, _( "Announce" ) );
1083
1084        track = info->trackerList->list;
1085        tmp = track->port==80
1086          ? g_strdup_printf( "http://%s%s", track->address, track->announce )
1087          : g_strdup_printf( "http://%s:%d%s", track->address, track->port, track->announce );
1088        l = gtk_label_new( tmp );
1089        gtk_label_set_ellipsize( GTK_LABEL( l ), PANGO_ELLIPSIZE_END );
1090        hig_workarea_add_row (t, &row, _( "Tracker:" ), l, NULL);
1091        g_free( tmp );
1092
1093        s = _( "Last announce at:" );
1094        l = gtk_label_new( NULL );
1095        page->last_announce_time_lb = l;
1096        hig_workarea_add_row( t, &row, s, l, NULL );
1097
1098        s = _( "Tracker responded:");
1099        l = gtk_label_new( NULL );
1100        page->last_announce_response_lb = l;
1101        hig_workarea_add_row( t, &row, s, l, NULL );
1102
1103        s = _( "Next announce in:" );
1104        l = gtk_label_new( NULL );
1105        page->next_announce_countdown_lb = l;
1106        hig_workarea_add_row( t, &row, s, l, NULL );
1107
1108        /* how long until the tracker will honor user
1109         * pressing the "ask for more peers" button */
1110        s = _( "Manual announce allowed in:" );
1111        l = gtk_label_new( NULL );
1112        page->manual_announce_countdown_lb = l;
1113        hig_workarea_add_row( t, &row, s, l, NULL );
1114
1115    g_object_set_data_full( G_OBJECT( t ), TRACKER_PAGE, page, g_free );
1116    hig_workarea_finish( t, &row );
1117    return t;
1118}
1119
1120static void
1121refresh_countdown_lb( GtkWidget * l, time_t t )
1122{
1123    const time_t now = time( NULL );
1124
1125    if( !t || ( t < now ) )
1126        gtk_label_set_text( GTK_LABEL( l ), _( "Never" ) );
1127    else {
1128        char buf[1024];
1129        const int seconds = t - now;
1130        tr_strltime( buf, seconds, sizeof( buf ) );
1131        gtk_label_set_text( GTK_LABEL( l ), buf );
1132    }
1133}
1134
1135static void
1136refresh_time_lb( GtkWidget * l, time_t t )
1137{
1138    const char * never = _( "Never" );
1139    if( !t )
1140        gtk_label_set_text( GTK_LABEL( l ), never );
1141    else {
1142        char * str = rfc822date( t * 1000 );
1143        gtk_label_set_text( GTK_LABEL( l ), str );
1144        g_free( str );
1145    }
1146}
1147
1148static void
1149refresh_tracker( GtkWidget * w )
1150{
1151    GtkWidget * l;
1152    time_t t;
1153    struct tracker_page * page = g_object_get_data( G_OBJECT( w ), TRACKER_PAGE );
1154    const tr_stat * torStat = tr_torrent_stat( page->gtor );
1155
1156    l = page->last_scrape_time_lb;
1157    t = torStat->tracker_stat.lastScrapeTime;
1158    refresh_time_lb( l, t );
1159
1160    l = page->last_scrape_response_lb;
1161    gtk_label_set_text( GTK_LABEL( l ), torStat->tracker_stat.scrapeResponse );
1162
1163    l = page->next_scrape_countdown_lb;
1164    t = torStat->tracker_stat.nextScrapeTime;
1165    refresh_countdown_lb( l, t );
1166
1167    l = page->last_announce_time_lb;
1168    t = torStat->tracker_stat.lastAnnounceTime;
1169    refresh_time_lb( l, t );
1170
1171    l = page->last_announce_response_lb;
1172    gtk_label_set_text( GTK_LABEL( l ), torStat->tracker_stat.announceResponse );
1173
1174    l = page->next_announce_countdown_lb;
1175    t = torStat->tracker_stat.nextAnnounceTime;
1176    refresh_countdown_lb( l, t );
1177
1178    l = page->manual_announce_countdown_lb;
1179    t = torStat->tracker_stat.nextManualAnnounceTime;
1180    refresh_countdown_lb( l, t );
1181}
1182
1183/****
1184*****  DIALOG
1185****/
1186
1187static void
1188torrent_destroyed (gpointer dialog, GObject * dead_torrent UNUSED)
1189{
1190  gtk_widget_destroy (GTK_WIDGET(dialog));
1191}
1192
1193static void
1194remove_tag (gpointer tag)
1195{
1196  g_source_remove (GPOINTER_TO_UINT(tag)); /* stop the periodic refresh */
1197}
1198
1199static void
1200response_cb (GtkDialog *dialog, int response UNUSED, gpointer gtor)
1201{
1202  g_object_weak_unref (G_OBJECT(gtor), torrent_destroyed, dialog);
1203  gtk_widget_destroy (GTK_WIDGET(dialog));
1204}
1205
1206static gboolean
1207periodic_refresh (gpointer data)
1208{
1209  refresh_tracker   (g_object_get_data (G_OBJECT(data), "tracker-top"));
1210  refresh_peers     (g_object_get_data (G_OBJECT(data), "peers-top"));
1211  refresh_activity  (g_object_get_data (G_OBJECT(data), "activity-top"));
1212  refresh_options   (g_object_get_data (G_OBJECT(data), "options-top"));
1213  return TRUE;
1214}
1215
1216GtkWidget*
1217torrent_inspector_new ( GtkWindow * parent, TrTorrent * gtor )
1218{
1219  guint tag;
1220  GtkWidget *d, *n, *w;
1221  tr_torrent * tor = tr_torrent_handle (gtor);
1222  char sizeStr[64];
1223  char title[512];
1224  const tr_info * info = tr_torrent_info (gtor);
1225
1226  /* create the dialog */
1227  tr_strlsize( sizeStr, info->totalSize, sizeof(sizeStr) );
1228  /* %1$s is torrent name
1229     %2$s its file size */
1230  g_snprintf( title, sizeof(title), _( "Details for %1$s (%2$s)" ), info->name, sizeStr );
1231  d = gtk_dialog_new_with_buttons (title, parent, 0,
1232                                   GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
1233                                   NULL);
1234  gtk_window_set_role (GTK_WINDOW(d), "tr-info" );
1235  g_signal_connect (d, "response", G_CALLBACK (response_cb), gtor);
1236  gtk_dialog_set_has_separator( GTK_DIALOG( d ), FALSE );
1237  gtk_container_set_border_width (GTK_CONTAINER (d), 5);
1238  gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (d)->vbox), 2);
1239  g_object_weak_ref (G_OBJECT(gtor), torrent_destroyed, d);
1240 
1241
1242  /* add the notebook */
1243  n = gtk_notebook_new ();
1244  gtk_container_set_border_width ( GTK_CONTAINER ( n ), 5 );
1245
1246  w = activity_page_new (gtor);
1247  g_object_set_data (G_OBJECT(d), "activity-top", w);
1248  gtk_notebook_append_page (GTK_NOTEBOOK(n), w, 
1249                            gtk_label_new (_("Activity")));
1250
1251  w = peer_page_new (gtor);
1252  g_object_set_data (G_OBJECT(d), "peers-top", w);
1253  gtk_notebook_append_page (GTK_NOTEBOOK(n),  w,
1254                            gtk_label_new (_("Peers")));
1255
1256  gtk_notebook_append_page (GTK_NOTEBOOK(n),
1257                            info_page_new (tor),
1258                            gtk_label_new (_("Information")));
1259
1260  w = file_list_new( gtor );
1261  gtk_container_set_border_width( GTK_CONTAINER( w ), GUI_PAD_BIG );
1262  g_object_set_data (G_OBJECT(d), "files-top", w);
1263  gtk_notebook_append_page (GTK_NOTEBOOK(n), w,
1264                            gtk_label_new (_("Files")));
1265
1266  w = tracker_page_new( gtor );
1267  g_object_set_data( G_OBJECT( d ), "tracker-top", w );
1268  gtk_notebook_append_page( GTK_NOTEBOOK( n ), w,
1269                            gtk_label_new( _( "Tracker" ) ) );
1270
1271  w = options_page_new (gtor);
1272  g_object_set_data (G_OBJECT(d), "options-top", w);
1273  gtk_notebook_append_page (GTK_NOTEBOOK(n), w,
1274                            gtk_label_new (_("Options")));
1275
1276  gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(d)->vbox), n);
1277
1278  tag = g_timeout_add (UPDATE_INTERVAL_MSEC, periodic_refresh, d);
1279  g_object_set_data_full (G_OBJECT(d), "tag",
1280                          GUINT_TO_POINTER(tag), remove_tag);
1281
1282  /* return the results */
1283  periodic_refresh (d);
1284  gtk_widget_show_all (GTK_DIALOG(d)->vbox);
1285  return d;
1286}
Note: See TracBrowser for help on using the repository browser.