source: trunk/gtk/stats.c @ 6060

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

(gtk) #998: gtk client crashes if you quit while the stats window is still up

  • Property svn:keywords set to Date Rev Author Id
File size: 4.9 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: stats.c 6060 2008-06-05 20:41:32Z 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
19enum
20{
21    TR_RESPONSE_CLEAR = 1
22};
23
24struct 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
38static void
39setLabel( GtkWidget * w, const char * str )
40{
41    gtk_label_set_text( GTK_LABEL(w), str );
42}
43
44static void
45setLabelFromRatio( GtkWidget * w, double d )
46{
47    char buf[128];
48    tr_strlratio( buf, d, sizeof( buf ) );
49    setLabel( w, buf );
50}
51
52static gboolean
53updateStats( gpointer gdata )
54{
55    const char * fmt;
56    char buf[128];
57
58    struct stat_ui * ui = gdata;
59    tr_session_stats one, all;
60    tr_sessionGetStats( tr_core_handle( ui->core ), &one );
61    tr_sessionGetCumulativeStats( tr_core_handle( ui->core ), &all );
62
63    setLabel( ui->one_up_lb, tr_strlsize( buf, one.uploadedBytes, sizeof(buf) ) );
64    setLabel( ui->one_down_lb, tr_strlsize( buf, one.downloadedBytes, sizeof(buf) ) );
65    setLabel( ui->one_time_lb, tr_strltime( buf, one.secondsActive, sizeof(buf) ) );
66    setLabelFromRatio( ui->one_ratio_lb, one.ratio );
67
68    fmt = ngettext( "Started %'d time", "Started %'d times", (int)all.sessionCount );
69    g_snprintf( buf, sizeof(buf), fmt, (int)all.sessionCount );
70    setLabel( ui->all_sessions_lb, buf );
71    setLabel( ui->all_up_lb, tr_strlsize( buf, all.uploadedBytes, sizeof(buf) ) );
72    setLabel( ui->all_down_lb, tr_strlsize( buf, all.downloadedBytes, sizeof(buf) ) );
73    setLabel( ui->all_time_lb, tr_strltime( buf, all.secondsActive, sizeof(buf) ) );
74    setLabelFromRatio( ui->all_ratio_lb, all.ratio );
75
76    return TRUE;
77}
78
79static void
80dialogDestroyed( gpointer p, GObject * dialog UNUSED )
81{
82    g_source_remove( GPOINTER_TO_UINT( p ) );
83}
84
85static void
86dialogResponse( GtkDialog * dialog, gint response, gpointer gdata )
87{
88    struct stat_ui * ui = gdata;
89
90    if( response == TR_RESPONSE_CLEAR )
91    {
92        tr_handle * handle = tr_core_handle( ui->core );
93        tr_sessionClearStats( handle );
94        updateStats( ui );
95    }
96
97    if( response == GTK_RESPONSE_CLOSE )
98    {
99        gtk_widget_destroy( GTK_WIDGET( dialog ) );
100    }
101}
102
103GtkWidget*
104stats_dialog_create( GtkWindow * parent, TrCore * core )
105{
106    guint i;
107    int row = 0;
108    GtkWidget * d;
109    GtkWidget * t;
110    GtkWidget * l;
111    struct stat_ui * ui = g_new0( struct stat_ui, 1 );
112
113    d = gtk_dialog_new_with_buttons( _("Statistics"),
114                                     parent,
115                                     GTK_DIALOG_DESTROY_WITH_PARENT,
116                                     GTK_STOCK_CLEAR, TR_RESPONSE_CLEAR,
117                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
118                                     NULL );
119    t = hig_workarea_create( );
120    gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(d)->vbox), t );
121    ui->core = core;
122
123    hig_workarea_add_section_title( t, &row, _( "Current Session" ) );
124        l = ui->one_up_lb = gtk_label_new( NULL );
125        hig_workarea_add_row( t, &row, _("Uploaded:"), l, NULL );
126        l = ui->one_down_lb = gtk_label_new( NULL );
127        hig_workarea_add_row( t, &row, _("Downloaded:"), l, NULL );
128        l = ui->one_ratio_lb = gtk_label_new( NULL );
129        hig_workarea_add_row( t, &row, _("Ratio:"), l, NULL );
130        l = ui->one_time_lb = gtk_label_new( NULL );
131        hig_workarea_add_row( t, &row, _("Duration:"), l, NULL );
132    hig_workarea_add_section_divider( t, &row );
133    hig_workarea_add_section_title( t, &row, _("Total") );
134        l = ui->all_sessions_lb = gtk_label_new( _("Started %'d time") );
135        hig_workarea_add_label_w( t, row++, l );
136        l = ui->all_up_lb = gtk_label_new( NULL );
137        hig_workarea_add_row( t, &row, _("Uploaded:"), l, NULL );
138        l = ui->all_down_lb = gtk_label_new( NULL );
139        hig_workarea_add_row( t, &row, _("Downloaded:"), l, NULL );
140        l = ui->all_ratio_lb = gtk_label_new( NULL );
141        hig_workarea_add_row( t, &row, _("Ratio:"), l, NULL );
142        l = ui->all_time_lb = gtk_label_new( NULL );
143        hig_workarea_add_row( t, &row, _("Duration:"), l, NULL );
144    hig_workarea_finish( t, &row );
145    gtk_widget_show_all( t );
146
147    updateStats( ui );
148    g_object_set_data_full( G_OBJECT(d), "data", ui, g_free );
149    g_signal_connect( d, "response", G_CALLBACK(dialogResponse), ui );
150    i = g_timeout_add( 1000, updateStats, ui );
151    g_object_weak_ref( G_OBJECT( d ), dialogDestroyed, GUINT_TO_POINTER( i ) );
152    return d;
153}
Note: See TracBrowser for help on using the repository browser.