1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.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 6795 2008-09-23 19:11:04Z 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 | |
---|
21 | gpointer |
---|
22 | tr_icon_new( TrCore * core ) |
---|
23 | { |
---|
24 | return NULL; |
---|
25 | } |
---|
26 | |
---|
27 | #else |
---|
28 | |
---|
29 | #define UPDATE_INTERVAL 2500 |
---|
30 | |
---|
31 | static void |
---|
32 | activated( GtkStatusIcon * self UNUSED, |
---|
33 | gpointer user_data UNUSED ) |
---|
34 | { |
---|
35 | action_activate ( "toggle-main-window" ); |
---|
36 | } |
---|
37 | |
---|
38 | static void |
---|
39 | popup( 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 | |
---|
51 | static gboolean |
---|
52 | refresh_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 | |
---|
78 | static void |
---|
79 | closeTag( gpointer tag ) |
---|
80 | { |
---|
81 | g_source_remove( GPOINTER_TO_UINT( tag ) ); |
---|
82 | } |
---|
83 | |
---|
84 | gpointer |
---|
85 | tr_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 = g_timeout_add( UPDATE_INTERVAL, 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 |
---|