Transmission: update to 2.82
[tomato.git] / release / src / router / transmission / gtk / tr-icon.c
blobd2375b9c61810c88ab79da16f77f601a588cd25e
1 /*
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>
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"
24 static TR_DEFINE_QUARK (tr_core, core)
26 #define ICON_NAME "transmission"
28 #ifdef HAVE_LIBAPPINDICATOR
29 void
30 gtr_icon_refresh (gpointer vindicator UNUSED)
33 #else
34 static void
35 activated (GtkStatusIcon * self UNUSED, gpointer user_data UNUSED)
37 gtr_action_activate ("toggle-main-window");
40 static void
41 popup (GtkStatusIcon * self,
42 guint button,
43 guint when,
44 gpointer data UNUSED)
46 GtkWidget * w = gtr_action_get_widget ("/icon-popup");
48 gtk_menu_popup (GTK_MENU (w), NULL, NULL,
49 gtk_status_icon_position_menu,
50 self, button, when);
53 void
54 gtr_icon_refresh (gpointer vicon)
56 double KBps;
57 double limit;
58 char up[64];
59 char upLimit[64];
60 char down[64];
61 char downLimit[64];
62 char tip[1024];
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 ()));
67 /* up */
68 KBps = tr_sessionGetRawSpeed_KBps (session, TR_UP);
69 if (KBps < 0.001)
70 g_strlcpy (up, idle, sizeof (up));
71 else
72 tr_formatter_speed_KBps (up, KBps, sizeof (up));
74 /* up limit */
75 *upLimit = '\0';
76 if (tr_sessionGetActiveSpeedLimit_KBps (session, TR_UP, &limit))
78 char buf[64];
79 tr_formatter_speed_KBps (buf, limit, sizeof (buf));
80 g_snprintf (upLimit, sizeof (upLimit), _(" (Limit: %s)"), buf);
83 /* down */
84 KBps = tr_sessionGetRawSpeed_KBps (session, TR_DOWN);
85 if (KBps < 0.001)
86 g_strlcpy (down, idle, sizeof (down));
87 else
88 tr_formatter_speed_KBps (down, KBps, sizeof (down));
90 /* down limit */
91 *downLimit = '\0';
92 if (tr_sessionGetActiveSpeedLimit_KBps (session, TR_DOWN, &limit))
94 char buf[64];
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);
107 #endif
109 static const char *
110 getIconName (void)
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;
122 else
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);
128 #else
129 gtk_icon_info_free (icon_info);
130 #endif
131 icon_name = icon_is_builtin ? ICON_NAME : TRAY_ICON;
134 return icon_name;
137 gpointer
138 gtr_icon_new (TrCore * core)
140 #ifdef HAVE_LIBAPPINDICATOR
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 app_indicator_set_title (indicator, g_get_application_name ());
148 g_object_set_qdata (G_OBJECT (indicator), core_quark (), core);
149 return indicator;
150 #else
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);
156 return icon;
157 #endif