source: trunk/gtk/details.c @ 5221

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

lots more i18n string work -- making strings more consistent, folding redundant strings together, etc.

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