source: trunk/gtk/stats.c @ 4481

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

(gtk) handle TR_RATIO_NA and TR_RATIO_INF everywhere

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