source: trunk/gtk/tr-icon.c @ 7464

Last change on this file since 7464 was 7464, checked in by charles, 12 years ago

(trunk gtk) #1585: use g_timeout_add_seconds() where appropriate to group timers together for fewer scheduled wakeups

  • Property svn:keywords set to Date Rev Author Id
File size: 2.8 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.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: tr-icon.c 7464 2008-12-22 05:39:03Z charles $
11 */
12
13#include <glib/gi18n.h>
14#include <gtk/gtk.h>
15#include "actions.h"
16#include "tr-icon.h"
17#include "util.h"
18
19#ifndef STATUS_ICON_SUPPORTED
20
21gpointer
22tr_icon_new( TrCore * core )
23{
24    return NULL;
25}
26
27#else
28
29#define UPDATE_INTERVAL_SECONDS 2
30
31static void
32activated( GtkStatusIcon   * self      UNUSED,
33           gpointer          user_data UNUSED )
34{
35    action_activate ( "toggle-main-window" );
36}
37
38static void
39popup( GtkStatusIcon *       self,
40       guint                 button,
41       guint                 when,
42       gpointer         data UNUSED )
43{
44    GtkWidget * w = action_get_widget( "/icon-popup" );
45
46    gtk_menu_popup ( GTK_MENU( w ), NULL, NULL,
47                     gtk_status_icon_position_menu,
48                     self, button, when );
49}
50
51static gboolean
52refresh_tooltip_cb( gpointer data )
53{
54    GtkStatusIcon *   icon = GTK_STATUS_ICON( data );
55    TrCore *          core = g_object_get_data( G_OBJECT( icon ), "tr-core" );
56    struct core_stats stats;
57    char              downStr[32], upStr[32];
58    char              tip[256];
59
60    tr_core_get_stats( core, &stats );
61
62    tr_strlspeed( downStr, stats.clientDownloadSpeed, sizeof( downStr ) );
63    tr_strlspeed( upStr, stats.clientUploadSpeed, sizeof( upStr ) );
64    g_snprintf( tip, sizeof( tip ),
65                /* %1$'d is the number of torrents we're seeding,
66                   %2$'d is the number of torrents we're downloading,
67                   %3$s is our download speed,
68                   %4$s is our upload speed */
69                _( "%1$'d Seeding, %2$'d Downloading\nDown: %3$s, Up: %4$s" ),
70                stats.seedingCount,
71                stats.downloadCount,
72                downStr, upStr );
73    gtk_status_icon_set_tooltip( GTK_STATUS_ICON( icon ), tip );
74
75    return TRUE;
76}
77
78static void
79closeTag( gpointer tag )
80{
81    g_source_remove( GPOINTER_TO_UINT( tag ) );
82}
83
84gpointer
85tr_icon_new( TrCore * core )
86{
87    guint           id;
88    GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name(
89        "transmission" );
90
91    g_signal_connect( icon, "activate", G_CALLBACK( activated ), NULL );
92    g_signal_connect( icon, "popup-menu", G_CALLBACK( popup ), NULL );
93    id = gtr_timeout_add_seconds( UPDATE_INTERVAL_SECONDS, refresh_tooltip_cb, icon );
94    g_object_set_data( G_OBJECT( icon ), "tr-core", core );
95    g_object_set_data_full( G_OBJECT(
96                                icon ), "update-tag", GUINT_TO_POINTER(
97                                id ), closeTag );
98    return icon;
99}
100
101#endif
Note: See TracBrowser for help on using the repository browser.