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

Last change on this file since 11599 was 11599, checked in by charles, 11 years ago

(trunk) Join the 21st century and use only 1 space at the end sentences. This commit is nearly as important as the semi-annual ones that remove trailing spaces from the ends of lines of code... :)

  • Property svn:keywords set to Date Rev Author Id
File size: 4.7 KB
Line 
1/*
2 * This file Copyright (C) 2007-2010 Mnemosyne LLC
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 11599 2010-12-27 19:18:17Z charles $
11 */
12
13#include <glib/gi18n.h>
14#include <gtk/gtk.h>
15#ifdef HAVE_LIBAPPINDICATOR
16 #include <libappindicator/app-indicator.h>
17#endif
18#include <libtransmission/transmission.h>
19#include <libtransmission/utils.h>
20#include "actions.h"
21#include "tr-icon.h"
22#include "util.h"
23
24#define ICON_NAME "transmission"
25
26#ifndef STATUS_ICON_SUPPORTED
27gpointer gtr_icon_new( TrCore * core UNUSED ) { return NULL; }
28void gtr_icon_refresh( gpointer vicon UNUSED ) { } 
29#else
30
31#ifdef HAVE_LIBAPPINDICATOR
32void
33gtr_icon_refresh( gpointer vindicator UNUSED )
34{
35}
36#else
37static void
38activated( GtkStatusIcon * self UNUSED, gpointer user_data UNUSED )
39{
40    gtr_action_activate( "toggle-main-window" );
41}
42
43static void
44popup( GtkStatusIcon *       self,
45       guint                 button,
46       guint                 when,
47       gpointer         data UNUSED )
48{
49    GtkWidget * w = gtr_action_get_widget( "/icon-popup" );
50
51    gtk_menu_popup ( GTK_MENU( w ), NULL, NULL,
52                     gtk_status_icon_position_menu,
53                     self, button, when );
54}
55
56void
57gtr_icon_refresh( gpointer vicon )
58{
59    double KBps;
60    double limit;
61    char up[64];
62    char upLimit[64];
63    char down[64];
64    char downLimit[64];
65    char tip[1024];
66    const char * idle = _( "Idle" );
67    GtkStatusIcon * icon = GTK_STATUS_ICON( vicon );
68    tr_session * session = tr_core_session( g_object_get_data( G_OBJECT( icon ), "tr-core" ) );
69
70    /* up */
71    KBps = tr_sessionGetRawSpeed_KBps( session, TR_UP );
72    if( KBps < 0.001 )
73        g_strlcpy( up, idle, sizeof( up ) );
74    else
75        tr_formatter_speed_KBps( up, KBps, sizeof( up ) );
76
77    /* up limit */
78    if( !tr_sessionGetActiveSpeedLimit_KBps( session, TR_UP, &limit ) )
79        *upLimit = '\0';
80    else {
81        char buf[64];
82        tr_formatter_speed_KBps( buf, limit, sizeof( buf ) );
83        g_snprintf( upLimit, sizeof( upLimit ), _( "(Limit: %s)" ), buf );
84    }
85
86    /* down */
87    KBps = tr_sessionGetRawSpeed_KBps( session, TR_DOWN );
88    if( KBps < 0.001 )
89        g_strlcpy( down, idle, sizeof( down ) );
90    else
91        tr_formatter_speed_KBps( down, KBps, sizeof( down ) );
92
93    /* down limit */
94    if( !tr_sessionGetActiveSpeedLimit_KBps( session, TR_DOWN, &limit ) )
95        *downLimit = '\0';
96    else {
97        char buf[64];
98        tr_formatter_speed_KBps( buf, limit, sizeof( buf ) );
99        g_snprintf( downLimit, sizeof( downLimit ), _( "(Limit: %s)" ), buf );
100    }
101
102    /* %1$s: current upload speed
103     * %2$s: current upload limit, if any
104     * %3$s: current download speed
105     * %4$s: current download limit, if any */
106    g_snprintf( tip, sizeof( tip ), _( "Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s" ), up, upLimit, down, downLimit );
107
108#if GTK_CHECK_VERSION( 2,16,0 )
109    gtk_status_icon_set_tooltip_text( GTK_STATUS_ICON( icon ), tip );
110#else
111    gtk_status_icon_set_tooltip( GTK_STATUS_ICON( icon ), tip );
112#endif
113}
114#endif
115
116static const char *
117getIconName( void )
118{
119    const char * icon_name;
120
121    GtkIconTheme * theme = gtk_icon_theme_get_default( );
122
123    /* if the tray's icon is a 48x48 file, use it;
124     * otherwise, use the fallback builtin icon */
125    if( !gtk_icon_theme_has_icon( theme, TRAY_ICON ) )
126        icon_name = ICON_NAME;
127    else {
128        GtkIconInfo * icon_info = gtk_icon_theme_lookup_icon( theme, TRAY_ICON, 48, GTK_ICON_LOOKUP_USE_BUILTIN );
129        const gboolean icon_is_builtin = gtk_icon_info_get_filename ( icon_info ) == NULL;
130        gtk_icon_info_free ( icon_info );
131        icon_name = icon_is_builtin ? ICON_NAME : TRAY_ICON;
132    }
133
134    return icon_name;
135}
136
137#ifdef HAVE_LIBAPPINDICATOR
138gpointer
139gtr_icon_new( TrCore * core)
140{
141    GtkWidget * w;
142    const char * icon_name = getIconName( );
143    AppIndicator * indicator = app_indicator_new( ICON_NAME, icon_name, APP_INDICATOR_CATEGORY_SYSTEM_SERVICES );
144    app_indicator_set_status( indicator, APP_INDICATOR_STATUS_ACTIVE );
145    w = gtr_action_get_widget( "/icon-popup" );
146    app_indicator_set_menu( indicator, GTK_MENU ( w ) );
147    g_object_set_data( G_OBJECT( indicator ), "tr-core", core );
148    return indicator;
149}
150#else
151gpointer
152gtr_icon_new( TrCore * core )
153{
154    const char * icon_name = getIconName( );
155    GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name( icon_name );
156    g_signal_connect( icon, "activate", G_CALLBACK( activated ), NULL );
157    g_signal_connect( icon, "popup-menu", G_CALLBACK( popup ), NULL );
158    g_object_set_data( G_OBJECT( icon ), "tr-core", core );
159    return icon;
160}
161
162#endif
163
164#endif
Note: See TracBrowser for help on using the repository browser.