1 | /* |
---|
2 | * This file Copyright (C) 2007-2010 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 9868 2010-01-04 21:00:47Z charles $ |
---|
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 | |
---|
19 | enum |
---|
20 | { |
---|
21 | TR_RESPONSE_RESET = 1 |
---|
22 | }; |
---|
23 | |
---|
24 | struct stat_ui |
---|
25 | { |
---|
26 | GtkWidget * one_up_lb; |
---|
27 | GtkWidget * one_down_lb; |
---|
28 | GtkWidget * one_ratio_lb; |
---|
29 | GtkWidget * one_time_lb; |
---|
30 | GtkWidget * all_up_lb; |
---|
31 | GtkWidget * all_down_lb; |
---|
32 | GtkWidget * all_ratio_lb; |
---|
33 | GtkWidget * all_time_lb; |
---|
34 | GtkWidget * all_sessions_lb; |
---|
35 | TrCore * core; |
---|
36 | }; |
---|
37 | |
---|
38 | static void |
---|
39 | setLabel( GtkWidget * w, |
---|
40 | const char * str ) |
---|
41 | { |
---|
42 | gtk_label_set_text( GTK_LABEL( w ), str ); |
---|
43 | } |
---|
44 | |
---|
45 | static void |
---|
46 | setLabelFromRatio( GtkWidget * w, |
---|
47 | double d ) |
---|
48 | { |
---|
49 | char buf[128]; |
---|
50 | |
---|
51 | tr_strlratio( buf, d, sizeof( buf ) ); |
---|
52 | setLabel( w, buf ); |
---|
53 | } |
---|
54 | |
---|
55 | static gboolean |
---|
56 | updateStats( gpointer gdata ) |
---|
57 | { |
---|
58 | const char * fmt; |
---|
59 | char buf[128]; |
---|
60 | |
---|
61 | struct stat_ui * ui = gdata; |
---|
62 | tr_session_stats one, all; |
---|
63 | |
---|
64 | tr_sessionGetStats( tr_core_session( ui->core ), &one ); |
---|
65 | tr_sessionGetCumulativeStats( tr_core_session( ui->core ), &all ); |
---|
66 | |
---|
67 | setLabel( ui->one_up_lb, |
---|
68 | tr_strlsize( buf, one.uploadedBytes, sizeof( buf ) ) ); |
---|
69 | setLabel( ui->one_down_lb, |
---|
70 | tr_strlsize( buf, one.downloadedBytes, sizeof( buf ) ) ); |
---|
71 | setLabel( ui->one_time_lb, |
---|
72 | tr_strltime( buf, one.secondsActive, sizeof( buf ) ) ); |
---|
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, sizeof( buf ), fmt, (int)all.sessionCount ); |
---|
78 | setLabel( ui->all_sessions_lb, buf ); |
---|
79 | setLabel( ui->all_up_lb, |
---|
80 | tr_strlsize( buf, all.uploadedBytes, sizeof( buf ) ) ); |
---|
81 | setLabel( ui->all_down_lb, |
---|
82 | tr_strlsize( buf, all.downloadedBytes, sizeof( buf ) ) ); |
---|
83 | setLabel( ui->all_time_lb, |
---|
84 | tr_strltime( buf, all.secondsActive, sizeof( buf ) ) ); |
---|
85 | setLabelFromRatio( ui->all_ratio_lb, all.ratio ); |
---|
86 | |
---|
87 | return TRUE; |
---|
88 | } |
---|
89 | |
---|
90 | static void |
---|
91 | dialogDestroyed( gpointer p, |
---|
92 | GObject * dialog UNUSED ) |
---|
93 | { |
---|
94 | g_source_remove( GPOINTER_TO_UINT( p ) ); |
---|
95 | } |
---|
96 | |
---|
97 | static void |
---|
98 | dialogResponse( GtkDialog * dialog, |
---|
99 | gint response, |
---|
100 | gpointer gdata ) |
---|
101 | { |
---|
102 | struct stat_ui * ui = gdata; |
---|
103 | |
---|
104 | if( response == TR_RESPONSE_RESET ) |
---|
105 | { |
---|
106 | const char * primary = _( "Reset your statistics?" ); |
---|
107 | const char * secondary = _( "These statistics are for your information only. " |
---|
108 | "Resetting them doesn't affect the statistics logged by your BitTorrent trackers." ); |
---|
109 | const int flags = GTK_DIALOG_DESTROY_WITH_PARENT |
---|
110 | | GTK_DIALOG_MODAL; |
---|
111 | GtkWidget * w = gtk_message_dialog_new( GTK_WINDOW( dialog ), |
---|
112 | flags, |
---|
113 | GTK_MESSAGE_QUESTION, |
---|
114 | GTK_BUTTONS_NONE, |
---|
115 | "%s", primary ); |
---|
116 | gtk_dialog_add_buttons( GTK_DIALOG( w ), |
---|
117 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
---|
118 | _( "_Reset" ), TR_RESPONSE_RESET, |
---|
119 | NULL ); |
---|
120 | gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( w ), "%s", secondary ); |
---|
121 | if( gtk_dialog_run( GTK_DIALOG( w ) ) == TR_RESPONSE_RESET ) |
---|
122 | { |
---|
123 | tr_sessionClearStats( tr_core_session( ui->core ) ); |
---|
124 | updateStats( ui ); |
---|
125 | } |
---|
126 | gtk_widget_destroy( w ); |
---|
127 | } |
---|
128 | |
---|
129 | if( response == GTK_RESPONSE_CLOSE ) |
---|
130 | { |
---|
131 | gtk_widget_destroy( GTK_WIDGET( dialog ) ); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | GtkWidget* |
---|
136 | stats_dialog_create( GtkWindow * parent, |
---|
137 | TrCore * core ) |
---|
138 | { |
---|
139 | guint i; |
---|
140 | int row = 0; |
---|
141 | GtkWidget * d; |
---|
142 | GtkWidget * t; |
---|
143 | GtkWidget * l; |
---|
144 | struct stat_ui * ui = g_new0( struct stat_ui, 1 ); |
---|
145 | |
---|
146 | d = gtk_dialog_new_with_buttons( _( "Statistics" ), |
---|
147 | parent, |
---|
148 | GTK_DIALOG_DESTROY_WITH_PARENT | |
---|
149 | GTK_DIALOG_NO_SEPARATOR, |
---|
150 | _( "_Reset" ), TR_RESPONSE_RESET, |
---|
151 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
---|
152 | NULL ); |
---|
153 | gtk_dialog_set_default_response( GTK_DIALOG( d ), |
---|
154 | GTK_RESPONSE_CLOSE ); |
---|
155 | gtk_dialog_set_alternative_button_order( GTK_DIALOG( d ), |
---|
156 | GTK_RESPONSE_CLOSE, |
---|
157 | TR_RESPONSE_RESET, |
---|
158 | -1 ); |
---|
159 | t = hig_workarea_create( ); |
---|
160 | gtk_box_pack_start( GTK_BOX( GTK_DIALOG( d )->vbox ), t, TRUE, TRUE, 0 ); |
---|
161 | ui->core = core; |
---|
162 | |
---|
163 | hig_workarea_add_section_title( t, &row, _( "Current Session" ) ); |
---|
164 | l = ui->one_up_lb = gtk_label_new( NULL ); |
---|
165 | hig_workarea_add_row( t, &row, _( "Uploaded:" ), l, NULL ); |
---|
166 | l = ui->one_down_lb = gtk_label_new( NULL ); |
---|
167 | hig_workarea_add_row( t, &row, _( "Downloaded:" ), l, NULL ); |
---|
168 | l = ui->one_ratio_lb = gtk_label_new( NULL ); |
---|
169 | hig_workarea_add_row( t, &row, _( "Ratio:" ), l, NULL ); |
---|
170 | l = ui->one_time_lb = gtk_label_new( NULL ); |
---|
171 | hig_workarea_add_row( t, &row, _( "Duration:" ), l, NULL ); |
---|
172 | hig_workarea_add_section_divider( t, &row ); |
---|
173 | hig_workarea_add_section_title( t, &row, _( "Total" ) ); |
---|
174 | l = ui->all_sessions_lb = gtk_label_new( _( "Started %'d time" ) ); |
---|
175 | hig_workarea_add_label_w( t, row++, l ); |
---|
176 | l = ui->all_up_lb = gtk_label_new( NULL ); |
---|
177 | hig_workarea_add_row( t, &row, _( "Uploaded:" ), l, NULL ); |
---|
178 | l = ui->all_down_lb = gtk_label_new( NULL ); |
---|
179 | hig_workarea_add_row( t, &row, _( "Downloaded:" ), l, NULL ); |
---|
180 | l = ui->all_ratio_lb = gtk_label_new( NULL ); |
---|
181 | hig_workarea_add_row( t, &row, _( "Ratio:" ), l, NULL ); |
---|
182 | l = ui->all_time_lb = gtk_label_new( NULL ); |
---|
183 | hig_workarea_add_row( t, &row, _( "Duration:" ), l, NULL ); |
---|
184 | hig_workarea_finish( t, &row ); |
---|
185 | gtk_widget_show_all( t ); |
---|
186 | |
---|
187 | updateStats( ui ); |
---|
188 | g_object_set_data_full( G_OBJECT( d ), "data", ui, g_free ); |
---|
189 | g_signal_connect( d, "response", G_CALLBACK( dialogResponse ), ui ); |
---|
190 | i = gtr_timeout_add_seconds( 1, updateStats, ui ); |
---|
191 | g_object_weak_ref( G_OBJECT( d ), dialogDestroyed, GUINT_TO_POINTER( i ) ); |
---|
192 | return d; |
---|
193 | } |
---|
194 | |
---|