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

Last change on this file since 8763 was 8763, checked in by charles, 13 years ago

(trunk, gtk/qt) use Monsoon's strings for encryption preferences and Deluge's system tray tooltip.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.6 KB
Line 
1/*
2 * This file Copyright (C) 2007-2009 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 8763 2009-06-30 18:08:50Z 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    double d;
55    int limit;
56    char up[64];
57    char upLimit[64];
58    char down[64];
59    char downLimit[64];
60    char tip[1024];
61    const char * idle = _( "Idle" );
62    GtkStatusIcon * icon = GTK_STATUS_ICON( data );
63    tr_session * session = tr_core_session( g_object_get_data( G_OBJECT( icon ), "tr-core" ) );
64
65    /* up */
66    if(((d = tr_sessionGetRawSpeed( session, TR_UP ))) < 0.1 )
67        g_strlcpy( up, idle, sizeof( up ) );
68    else
69        tr_strlspeed( up, d, sizeof( up ) );
70
71    /* up limit */
72    if( !tr_sessionGetActiveSpeedLimit( session, TR_UP, &limit ) )
73        *upLimit = '\0';
74    else {
75        char buf[64];
76        tr_strlspeed( buf, limit, sizeof( buf ) );
77        g_snprintf( upLimit, sizeof( upLimit ), _( "(Limit: %s)" ), buf );
78    }
79
80    /* down */
81    if(((d = tr_sessionGetRawSpeed( session, TR_DOWN ))) < 0.1 )
82        g_strlcpy( down, idle, sizeof( down ) );
83    else
84        tr_strlspeed( down, d, sizeof( down ) );
85
86    /* down limit */   
87    if( !tr_sessionGetActiveSpeedLimit( session, TR_DOWN, &limit ) )
88        *downLimit = '\0';
89    else {
90        char buf[64];
91        tr_strlspeed( buf, limit, sizeof( buf ) );
92        g_snprintf( downLimit, sizeof( downLimit ), _( "(Limit: %s)" ), buf );
93    }
94
95    /* %1$s: current upload speed
96     * %2$s: current upload limit, if any
97     * %3$s: current download speed
98     * %4$s: current download limit, if any */
99    g_snprintf( tip, sizeof( tip ), _( "Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s" ), up, upLimit, down, downLimit );
100
101#if GTK_CHECK_VERSION( 2,16,0 )
102    gtk_status_icon_set_tooltip_text( GTK_STATUS_ICON( icon ), tip );
103#else
104    gtk_status_icon_set_tooltip( GTK_STATUS_ICON( icon ), tip );
105#endif
106
107    return TRUE;
108}
109
110static void
111closeTag( gpointer tag )
112{
113    g_source_remove( GPOINTER_TO_UINT( tag ) );
114}
115
116gpointer
117tr_icon_new( TrCore * core )
118{
119    guint           id;
120    GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name(
121        "transmission" );
122
123    g_signal_connect( icon, "activate", G_CALLBACK( activated ), NULL );
124    g_signal_connect( icon, "popup-menu", G_CALLBACK( popup ), NULL );
125    id = gtr_timeout_add_seconds( UPDATE_INTERVAL_SECONDS, refresh_tooltip_cb, icon );
126    g_object_set_data( G_OBJECT( icon ), "tr-core", core );
127    g_object_set_data_full( G_OBJECT(
128                                icon ), "update-tag", GUINT_TO_POINTER(
129                                id ), closeTag );
130    return icon;
131}
132
133#endif
Note: See TracBrowser for help on using the repository browser.