source: trunk/gtk/details.c @ 5222

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

more i18n improvements. thanks to Gnome translator andre for feedback.

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