2 * This file Copyright (C) Mnemosyne LLC
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.
10 * $Id: tr-icon.c 14142 2013-07-24 00:13:31Z jordan $
13 #include <glib/gi18n.h>
15 #ifdef HAVE_LIBAPPINDICATOR
16 #include <libappindicator/app-indicator.h>
18 #include <libtransmission/transmission.h>
19 #include <libtransmission/utils.h>
24 static TR_DEFINE_QUARK (tr_core
, core
)
26 #define ICON_NAME "transmission"
28 #ifdef HAVE_LIBAPPINDICATOR
30 gtr_icon_refresh (gpointer vindicator UNUSED
)
35 activated (GtkStatusIcon
* self UNUSED
, gpointer user_data UNUSED
)
37 gtr_action_activate ("toggle-main-window");
41 popup (GtkStatusIcon
* self
,
46 GtkWidget
* w
= gtr_action_get_widget ("/icon-popup");
48 gtk_menu_popup (GTK_MENU (w
), NULL
, NULL
,
49 gtk_status_icon_position_menu
,
54 gtr_icon_refresh (gpointer vicon
)
63 const char * idle
= _("Idle");
64 GtkStatusIcon
* icon
= GTK_STATUS_ICON (vicon
);
65 tr_session
* session
= gtr_core_session (g_object_get_qdata (G_OBJECT (icon
), core_quark ()));
68 KBps
= tr_sessionGetRawSpeed_KBps (session
, TR_UP
);
70 g_strlcpy (up
, idle
, sizeof (up
));
72 tr_formatter_speed_KBps (up
, KBps
, sizeof (up
));
76 if (tr_sessionGetActiveSpeedLimit_KBps (session
, TR_UP
, &limit
))
79 tr_formatter_speed_KBps (buf
, limit
, sizeof (buf
));
80 g_snprintf (upLimit
, sizeof (upLimit
), _(" (Limit: %s)"), buf
);
84 KBps
= tr_sessionGetRawSpeed_KBps (session
, TR_DOWN
);
86 g_strlcpy (down
, idle
, sizeof (down
));
88 tr_formatter_speed_KBps (down
, KBps
, sizeof (down
));
92 if (tr_sessionGetActiveSpeedLimit_KBps (session
, TR_DOWN
, &limit
))
95 tr_formatter_speed_KBps (buf
, limit
, sizeof (buf
));
96 g_snprintf (downLimit
, sizeof (downLimit
), _(" (Limit: %s)"), buf
);
99 /* %1$s: current upload speed
100 * %2$s: current upload limit, if any
101 * %3$s: current download speed
102 * %4$s: current download limit, if any */
103 g_snprintf (tip
, sizeof (tip
), _("Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s"), up
, upLimit
, down
, downLimit
);
105 gtk_status_icon_set_tooltip_text (GTK_STATUS_ICON (icon
), tip
);
112 const char * icon_name
;
114 GtkIconTheme
* theme
= gtk_icon_theme_get_default ();
116 /* if the tray's icon is a 48x48 file, use it;
117 * otherwise, use the fallback builtin icon */
118 if (!gtk_icon_theme_has_icon (theme
, TRAY_ICON
))
120 icon_name
= ICON_NAME
;
124 GtkIconInfo
* icon_info
= gtk_icon_theme_lookup_icon (theme
, TRAY_ICON
, 48, GTK_ICON_LOOKUP_USE_BUILTIN
);
125 const gboolean icon_is_builtin
= gtk_icon_info_get_filename (icon_info
) == NULL
;
126 #if GTK_CHECK_VERSION(3,8,0)
127 g_object_unref (icon_info
);
129 gtk_icon_info_free (icon_info
);
131 icon_name
= icon_is_builtin
? ICON_NAME
: TRAY_ICON
;
138 gtr_icon_new (TrCore
* core
)
140 #ifdef HAVE_LIBAPPINDICATOR
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 app_indicator_set_title (indicator
, g_get_application_name ());
148 g_object_set_qdata (G_OBJECT (indicator
), core_quark (), core
);
151 const char * icon_name
= getIconName ();
152 GtkStatusIcon
* icon
= gtk_status_icon_new_from_icon_name (icon_name
);
153 g_signal_connect (icon
, "activate", G_CALLBACK (activated
), NULL
);
154 g_signal_connect (icon
, "popup-menu", G_CALLBACK (popup
), NULL
);
155 g_object_set_qdata (G_OBJECT (icon
), core_quark (), core
);