source: trunk/gtk/stats.c @ 13656

Last change on this file since 13656 was 13656, checked in by jordan, 9 years ago

fewer redundant gobject dynamic casts

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