source: trunk/gtk/stats.c

Last change on this file was 14241, checked in by jordan, 8 years ago

Copyedit the license's revised text: (1) remove unnecessary repitition use of the word 'license' from the top of the header and source files (2) add the standard 'we hope it's useful, but no warranty' clause to COPYING (3) make explicit that linking OpenSSL is allowed (see https://people.gnome.org/~markmc/openssl-and-the-gpl.html for background) (4) sync the Qt and GTK+ clients' license popups with COPYING's revised text

  • Property svn:keywords set to Date Rev Author Id
File size: 6.4 KB
Line 
1/*
2 * This file Copyright (C) 2007-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: stats.c 14241 2014-01-21 03:10:30Z mikedld $
8 */
9
10#include <glib/gi18n.h>
11#include <gtk/gtk.h>
12#include "hig.h"
13#include "stats.h"
14#include "tr-core.h"
15#include "tr-prefs.h"
16#include "util.h"
17
18enum
19{
20  TR_RESPONSE_RESET = 1
21};
22
23struct stat_ui
24{
25  GtkLabel * one_up_lb;
26  GtkLabel * one_down_lb;
27  GtkLabel * one_ratio_lb;
28  GtkLabel * one_time_lb;
29
30  GtkLabel * all_up_lb;
31  GtkLabel * all_down_lb;
32  GtkLabel * all_ratio_lb;
33  GtkLabel * all_time_lb;
34
35  GtkLabel * all_sessions_lb;
36
37  TrCore * core;
38};
39
40static void
41setLabel (GtkLabel * l, const char * str)
42{
43  gtr_label_set_text (l, str);
44}
45
46static void
47setLabelFromRatio (GtkLabel * l, double d)
48{
49  char buf[128];
50
51  tr_strlratio (buf, d, sizeof (buf));
52  setLabel (l, buf);
53}
54
55static gboolean
56updateStats (gpointer gdata)
57{
58  char buf[128];
59  const char * fmt;
60  tr_session_stats one, all;
61  const size_t buflen = sizeof (buf);
62  struct stat_ui * ui = gdata;
63
64  tr_sessionGetStats (gtr_core_session (ui->core), &one);
65  tr_sessionGetCumulativeStats (gtr_core_session (ui->core), &all);
66
67  setLabel (ui->one_up_lb, tr_strlsize (buf, one.uploadedBytes, buflen));
68  setLabel (ui->one_down_lb, tr_strlsize (buf, one.downloadedBytes, buflen));
69  setLabel (ui->one_time_lb, tr_strltime (buf, one.secondsActive, buflen));
70  setLabelFromRatio (ui->one_ratio_lb, one.ratio);
71
72  fmt = ngettext ("Started %'d time", "Started %'d times", (int)all.sessionCount);
73  g_snprintf (buf, buflen, fmt, (int)all.sessionCount);
74  setLabel (ui->all_sessions_lb, buf);
75  setLabel (ui->all_up_lb, tr_strlsize (buf, all.uploadedBytes, buflen));
76  setLabel (ui->all_down_lb, tr_strlsize (buf, all.downloadedBytes, buflen));
77  setLabel (ui->all_time_lb, tr_strltime (buf, all.secondsActive, buflen));
78  setLabelFromRatio (ui->all_ratio_lb, all.ratio);
79
80  return G_SOURCE_CONTINUE;
81}
82
83static void
84dialogDestroyed (gpointer p, GObject * dialog UNUSED)
85{
86  g_source_remove (GPOINTER_TO_UINT (p));
87}
88
89static void
90dialogResponse (GtkDialog * dialog, gint response, gpointer gdata)
91{
92  struct stat_ui * ui = gdata;
93
94  if (response == TR_RESPONSE_RESET)
95    {
96      const char * primary = _("Reset your statistics?");
97      const char * secondary = _("These statistics are for your information only. "
98                                 "Resetting them doesn't affect the statistics logged by your BitTorrent trackers.");
99      const int flags = GTK_DIALOG_DESTROY_WITH_PARENT
100                      | GTK_DIALOG_MODAL;
101      GtkWidget * w = gtk_message_dialog_new (GTK_WINDOW (dialog),
102                                              flags,
103                                              GTK_MESSAGE_QUESTION,
104                                              GTK_BUTTONS_NONE,
105                                              "%s", primary);
106      gtk_dialog_add_buttons (GTK_DIALOG (w),
107                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
108                              _("_Reset"), TR_RESPONSE_RESET,
109                              NULL);
110      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (w), "%s", secondary);
111      if (gtk_dialog_run (GTK_DIALOG (w)) == TR_RESPONSE_RESET)
112        {
113          tr_sessionClearStats (gtr_core_session (ui->core));
114          updateStats (ui);
115        }
116      gtk_widget_destroy (w);
117    }
118
119  if (response == GTK_RESPONSE_CLOSE)
120    gtk_widget_destroy (GTK_WIDGET (dialog));
121}
122
123GtkWidget*
124gtr_stats_dialog_new (GtkWindow * parent, TrCore * core)
125{
126  guint i;
127  GtkWidget * d;
128  GtkWidget * t;
129  GtkWidget * l;
130  guint row = 0;
131  struct stat_ui * ui = g_new0 (struct stat_ui, 1);
132
133  d = gtk_dialog_new_with_buttons (_("Statistics"),
134                                   parent,
135                                   GTK_DIALOG_DESTROY_WITH_PARENT,
136                                   _("_Reset"), TR_RESPONSE_RESET,
137                                   GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
138                                   NULL);
139  gtk_dialog_set_default_response (GTK_DIALOG (d),
140                                   GTK_RESPONSE_CLOSE);
141  gtk_dialog_set_alternative_button_order (GTK_DIALOG (d),
142                                           GTK_RESPONSE_CLOSE,
143                                           TR_RESPONSE_RESET,
144                                           -1);
145  t = hig_workarea_create ();
146  ui->core = core;
147
148  hig_workarea_add_section_title (t, &row, _("Current Session"));
149  l = gtk_label_new (NULL);
150  ui->one_up_lb = GTK_LABEL (l);
151  gtk_label_set_single_line_mode (ui->one_up_lb, TRUE);
152  hig_workarea_add_row (t, &row, _("Uploaded:"), l, NULL);
153  l = gtk_label_new (NULL);
154  ui->one_down_lb = GTK_LABEL (l);
155  gtk_label_set_single_line_mode (ui->one_down_lb, TRUE);
156  hig_workarea_add_row (t, &row, _("Downloaded:"), l, NULL);
157  l = gtk_label_new (NULL);
158  ui->one_ratio_lb = GTK_LABEL (l);
159  gtk_label_set_single_line_mode (ui->one_ratio_lb, TRUE);
160  hig_workarea_add_row (t, &row, _("Ratio:"), l, NULL);
161  l = gtk_label_new (NULL);
162  ui->one_time_lb = GTK_LABEL (l);
163  gtk_label_set_single_line_mode (ui->one_time_lb, TRUE);
164  hig_workarea_add_row (t, &row, _("Duration:"), l, NULL);
165  hig_workarea_add_section_divider (t, &row);
166  hig_workarea_add_section_title (t, &row, _("Total"));
167  l = gtk_label_new (_("Started %'d time"));
168  ui->all_sessions_lb = GTK_LABEL (l);
169  gtk_label_set_single_line_mode (ui->all_sessions_lb, TRUE);
170  hig_workarea_add_label_w (t, row++, l);
171  l = gtk_label_new (NULL);
172  ui->all_up_lb = GTK_LABEL (l);
173  gtk_label_set_single_line_mode (ui->all_up_lb, TRUE);
174  hig_workarea_add_row (t, &row, _("Uploaded:"), l, NULL);
175  l = gtk_label_new (NULL);
176  ui->all_down_lb = GTK_LABEL (l);
177  gtk_label_set_single_line_mode (ui->all_down_lb, TRUE);
178  hig_workarea_add_row (t, &row, _("Downloaded:"), l, NULL);
179  l = gtk_label_new (NULL);
180  ui->all_ratio_lb = GTK_LABEL (l);
181  gtk_label_set_single_line_mode (ui->all_ratio_lb, TRUE);
182  hig_workarea_add_row (t, &row, _("Ratio:"), l, NULL);
183  l = gtk_label_new (NULL);
184  ui->all_time_lb = GTK_LABEL (l);
185  gtk_label_set_single_line_mode (ui->all_time_lb, TRUE);
186  hig_workarea_add_row (t, &row, _("Duration:"), l, NULL);
187  gtr_dialog_set_content (GTK_DIALOG (d), t);
188
189  updateStats (ui);
190  g_object_set_data_full (G_OBJECT (d), "data", ui, g_free);
191  g_signal_connect (d, "response", G_CALLBACK (dialogResponse), ui);
192  i = gdk_threads_add_timeout_seconds (SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS, updateStats, ui);
193  g_object_weak_ref (G_OBJECT (d), dialogDestroyed, GUINT_TO_POINTER (i));
194  return d;
195}
Note: See TracBrowser for help on using the repository browser.