source: trunk/gtk/stats.c @ 5908

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

(libt) more janitorial work on cleaning up tr_session*() and tr_torrent*() functions: session stats, torrent count, and manual update.

  • 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 5908 2008-05-22 20:44:41Z 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
80dialogResponse( GtkDialog * dialog, gint response, gpointer gdata )
81{
82    struct stat_ui * ui = gdata;
83
84    if( response == TR_RESPONSE_CLEAR )
85    {
86        tr_handle * handle = tr_core_handle( ui->core );
87        tr_sessionClearStats( handle );
88        updateStats( ui );
89    }
90
91    if( response == GTK_RESPONSE_CLOSE )
92    {
93        g_source_remove( GPOINTER_TO_UINT( g_object_get_data( G_OBJECT(dialog), "TrTimer" ) ) );
94        gtk_widget_destroy( GTK_WIDGET( dialog ) );
95    }
96}
97
98GtkWidget*
99stats_dialog_create( GtkWindow * parent, TrCore * core )
100{
101    guint i;
102    int row = 0;
103    GtkWidget * d;
104    GtkWidget * t;
105    GtkWidget * l;
106    struct stat_ui * ui = g_new0( struct stat_ui, 1 );
107
108    d = gtk_dialog_new_with_buttons( _("Statistics"),
109                                     parent,
110                                     GTK_DIALOG_DESTROY_WITH_PARENT,
111                                     GTK_STOCK_CLEAR, TR_RESPONSE_CLEAR,
112                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
113                                     NULL );
114    t = hig_workarea_create( );
115    gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(d)->vbox), t );
116    ui->core = core;
117
118    hig_workarea_add_section_title( t, &row, _( "Current Session" ) );
119        l = ui->one_up_lb = gtk_label_new( NULL );
120        hig_workarea_add_row( t, &row, _("Uploaded:"), l, NULL );
121        l = ui->one_down_lb = gtk_label_new( NULL );
122        hig_workarea_add_row( t, &row, _("Downloaded:"), l, NULL );
123        l = ui->one_ratio_lb = gtk_label_new( NULL );
124        hig_workarea_add_row( t, &row, _("Ratio:"), l, NULL );
125        l = ui->one_time_lb = gtk_label_new( NULL );
126        hig_workarea_add_row( t, &row, _("Duration:"), l, NULL );
127    hig_workarea_add_section_divider( t, &row );
128    hig_workarea_add_section_title( t, &row, _("Total") );
129        l = ui->all_sessions_lb = gtk_label_new( _("Started %'d time") );
130        hig_workarea_add_label_w( t, row++, l );
131        l = ui->all_up_lb = gtk_label_new( NULL );
132        hig_workarea_add_row( t, &row, _("Uploaded:"), l, NULL );
133        l = ui->all_down_lb = gtk_label_new( NULL );
134        hig_workarea_add_row( t, &row, _("Downloaded:"), l, NULL );
135        l = ui->all_ratio_lb = gtk_label_new( NULL );
136        hig_workarea_add_row( t, &row, _("Ratio:"), l, NULL );
137        l = ui->all_time_lb = gtk_label_new( NULL );
138        hig_workarea_add_row( t, &row, _("Duration:"), l, NULL );
139    hig_workarea_finish( t, &row );
140    gtk_widget_show_all( t );
141
142    updateStats( ui );
143    g_object_set_data_full( G_OBJECT(d), "data", ui, g_free );
144    g_signal_connect( d, "response", G_CALLBACK(dialogResponse), ui );
145    i = g_timeout_add( 1000, updateStats, ui );
146    g_object_set_data( G_OBJECT(d), "TrTimer", GUINT_TO_POINTER(i) );
147    return d;
148}
Note: See TracBrowser for help on using the repository browser.