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: stats.c 5313 2008-03-19 20:07:27Z 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 | struct stat_ui |
---|
20 | { |
---|
21 | GtkWidget * one_up_lb; |
---|
22 | GtkWidget * one_down_lb; |
---|
23 | GtkWidget * one_ratio_lb; |
---|
24 | GtkWidget * one_time_lb; |
---|
25 | GtkWidget * all_up_lb; |
---|
26 | GtkWidget * all_down_lb; |
---|
27 | GtkWidget * all_ratio_lb; |
---|
28 | GtkWidget * all_time_lb; |
---|
29 | GtkWidget * all_sessions_lb; |
---|
30 | TrCore * core; |
---|
31 | }; |
---|
32 | |
---|
33 | static void |
---|
34 | setLabel( GtkWidget * w, const char * str ) |
---|
35 | { |
---|
36 | gtk_label_set_text( GTK_LABEL(w), str ); |
---|
37 | } |
---|
38 | |
---|
39 | static void |
---|
40 | setLabelFromRatio( GtkWidget * w, double d ) |
---|
41 | { |
---|
42 | char buf[128]; |
---|
43 | tr_strlratio( buf, d, sizeof( buf ) ); |
---|
44 | setLabel( w, buf ); |
---|
45 | } |
---|
46 | |
---|
47 | static gboolean |
---|
48 | updateStats( gpointer gdata ) |
---|
49 | { |
---|
50 | const char * fmt; |
---|
51 | char buf[128]; |
---|
52 | |
---|
53 | struct stat_ui * ui = gdata; |
---|
54 | tr_session_stats one, all; |
---|
55 | tr_getSessionStats( tr_core_handle( ui->core ), &one ); |
---|
56 | tr_getCumulativeSessionStats( tr_core_handle( ui->core ), &all ); |
---|
57 | |
---|
58 | setLabel( ui->one_up_lb, tr_strlsize( buf, one.uploadedBytes, sizeof(buf) ) ); |
---|
59 | setLabel( ui->one_down_lb, tr_strlsize( buf, one.downloadedBytes, sizeof(buf) ) ); |
---|
60 | setLabel( ui->one_time_lb, tr_strltime( buf, one.secondsActive, sizeof(buf) ) ); |
---|
61 | setLabelFromRatio( ui->one_ratio_lb, one.ratio ); |
---|
62 | |
---|
63 | fmt = ngettext( "Started %'d time", "Started %'d times", (int)all.sessionCount ); |
---|
64 | g_snprintf( buf, sizeof(buf), fmt, (int)all.sessionCount ); |
---|
65 | setLabel( ui->all_sessions_lb, buf ); |
---|
66 | setLabel( ui->all_up_lb, tr_strlsize( buf, all.uploadedBytes, sizeof(buf) ) ); |
---|
67 | setLabel( ui->all_down_lb, tr_strlsize( buf, all.downloadedBytes, sizeof(buf) ) ); |
---|
68 | setLabel( ui->all_time_lb, tr_strltime( buf, all.secondsActive, sizeof(buf) ) ); |
---|
69 | setLabelFromRatio( ui->all_ratio_lb, all.ratio ); |
---|
70 | |
---|
71 | return TRUE; |
---|
72 | } |
---|
73 | |
---|
74 | static void |
---|
75 | dialogResponse( GtkDialog * dialog, gint response UNUSED, gpointer unused UNUSED ) |
---|
76 | { |
---|
77 | g_source_remove( GPOINTER_TO_UINT( g_object_get_data( G_OBJECT(dialog), "TrTimer" ) ) ); |
---|
78 | gtk_widget_destroy( GTK_WIDGET( dialog ) ); |
---|
79 | } |
---|
80 | |
---|
81 | GtkWidget* |
---|
82 | stats_dialog_create( GtkWindow * parent, TrCore * core ) |
---|
83 | { |
---|
84 | guint i; |
---|
85 | int row = 0; |
---|
86 | GtkWidget * d; |
---|
87 | GtkWidget * t; |
---|
88 | GtkWidget * l; |
---|
89 | struct stat_ui * ui = g_new0( struct stat_ui, 1 ); |
---|
90 | |
---|
91 | d = gtk_dialog_new_with_buttons( _("Statistics"), |
---|
92 | parent, |
---|
93 | GTK_DIALOG_DESTROY_WITH_PARENT, |
---|
94 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
---|
95 | NULL ); |
---|
96 | t = hig_workarea_create( ); |
---|
97 | gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(d)->vbox), t ); |
---|
98 | ui->core = core; |
---|
99 | |
---|
100 | hig_workarea_add_section_title( t, &row, _( "Current Session" ) ); |
---|
101 | l = ui->one_up_lb = gtk_label_new( NULL ); |
---|
102 | hig_workarea_add_row( t, &row, _("Uploaded:"), l, NULL ); |
---|
103 | l = ui->one_down_lb = gtk_label_new( NULL ); |
---|
104 | hig_workarea_add_row( t, &row, _("Downloaded:"), l, NULL ); |
---|
105 | l = ui->one_ratio_lb = gtk_label_new( NULL ); |
---|
106 | hig_workarea_add_row( t, &row, _("Ratio:"), l, NULL ); |
---|
107 | l = ui->one_time_lb = gtk_label_new( NULL ); |
---|
108 | hig_workarea_add_row( t, &row, _("Duration:"), l, NULL ); |
---|
109 | hig_workarea_add_section_divider( t, &row ); |
---|
110 | hig_workarea_add_section_title( t, &row, _("Total") ); |
---|
111 | l = ui->all_sessions_lb = gtk_label_new( _("Started %'d time") ); |
---|
112 | hig_workarea_add_label_w( t, row++, l ); |
---|
113 | l = ui->all_up_lb = gtk_label_new( NULL ); |
---|
114 | hig_workarea_add_row( t, &row, _("Uploaded:"), l, NULL ); |
---|
115 | l = ui->all_down_lb = gtk_label_new( NULL ); |
---|
116 | hig_workarea_add_row( t, &row, _("Downloaded:"), l, NULL ); |
---|
117 | l = ui->all_ratio_lb = gtk_label_new( NULL ); |
---|
118 | hig_workarea_add_row( t, &row, _("Ratio:"), l, NULL ); |
---|
119 | l = ui->all_time_lb = gtk_label_new( NULL ); |
---|
120 | hig_workarea_add_row( t, &row, _("Duration:"), l, NULL ); |
---|
121 | hig_workarea_finish( t, &row ); |
---|
122 | gtk_widget_show_all( t ); |
---|
123 | |
---|
124 | updateStats( ui ); |
---|
125 | g_object_set_data_full( G_OBJECT(d), "data", ui, g_free ); |
---|
126 | g_signal_connect( d, "response", G_CALLBACK(dialogResponse), NULL ); |
---|
127 | i = g_timeout_add( 1000, updateStats, ui ); |
---|
128 | g_object_set_data( G_OBJECT(d), "TrTimer", GUINT_TO_POINTER(i) ); |
---|
129 | return d; |
---|
130 | } |
---|