1 | /****************************************************************************** |
---|
2 | * $Id: tr_icon.c 4998 2008-02-09 17:29:05Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <glib/gi18n.h> |
---|
26 | #include <gtk/gtk.h> |
---|
27 | #include "actions.h" |
---|
28 | #include "tr_icon.h" |
---|
29 | #include "util.h" |
---|
30 | |
---|
31 | #ifndef STATUS_ICON_SUPPORTED |
---|
32 | |
---|
33 | gpointer |
---|
34 | tr_icon_new( TrCore * core ) |
---|
35 | { |
---|
36 | return NULL; |
---|
37 | } |
---|
38 | |
---|
39 | #else |
---|
40 | |
---|
41 | static void |
---|
42 | activated ( GtkStatusIcon * self UNUSED, |
---|
43 | gpointer user_data UNUSED ) |
---|
44 | { |
---|
45 | action_activate ("toggle-main-window"); |
---|
46 | } |
---|
47 | |
---|
48 | static void |
---|
49 | popup ( GtkStatusIcon * self, |
---|
50 | guint button, |
---|
51 | guint when, |
---|
52 | gpointer data UNUSED ) |
---|
53 | { |
---|
54 | GtkWidget * w = action_get_widget( "/icon-popup" ); |
---|
55 | gtk_menu_popup (GTK_MENU(w), NULL, NULL, |
---|
56 | gtk_status_icon_position_menu, |
---|
57 | self, button, when ); |
---|
58 | } |
---|
59 | |
---|
60 | static void |
---|
61 | core_destroyed( gpointer data, GObject * core UNUSED ) |
---|
62 | { |
---|
63 | g_source_remove( GPOINTER_TO_UINT( data ) ); |
---|
64 | } |
---|
65 | |
---|
66 | static gboolean |
---|
67 | refresh_tooltip_cb( gpointer data ) |
---|
68 | { |
---|
69 | GtkStatusIcon * icon = GTK_STATUS_ICON( data ); |
---|
70 | TrCore * core = g_object_get_data( G_OBJECT( icon ), "tr-core" ); |
---|
71 | const struct core_stats * stats = tr_core_get_stats( core ); |
---|
72 | char downStr[32], upStr[32]; |
---|
73 | char tip[256]; |
---|
74 | |
---|
75 | tr_strlspeed( downStr, stats->clientDownloadSpeed, sizeof( downStr ) ); |
---|
76 | tr_strlspeed( upStr, stats->clientUploadSpeed, sizeof( upStr ) ); |
---|
77 | g_snprintf( tip, sizeof( tip ), |
---|
78 | _( "%d Seeding, %d Downloading\nDown: %s, Up: %s" ), |
---|
79 | stats->seedingCount, |
---|
80 | stats->downloadCount, |
---|
81 | downStr, upStr ); |
---|
82 | gtk_status_icon_set_tooltip( GTK_STATUS_ICON( icon ), tip ); |
---|
83 | |
---|
84 | return TRUE; |
---|
85 | } |
---|
86 | |
---|
87 | gpointer |
---|
88 | tr_icon_new( TrCore * core ) |
---|
89 | { |
---|
90 | guint id; |
---|
91 | GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name( "transmission" ); |
---|
92 | g_signal_connect( icon, "activate", G_CALLBACK( activated ), NULL ); |
---|
93 | g_signal_connect( icon, "popup-menu", G_CALLBACK( popup ), NULL ); |
---|
94 | id = g_timeout_add( 1000, refresh_tooltip_cb, icon ); |
---|
95 | g_object_weak_ref( G_OBJECT( core ), core_destroyed, GUINT_TO_POINTER( id ) ); |
---|
96 | g_object_set_data( G_OBJECT( icon ), "tr-core", core ); |
---|
97 | return icon; |
---|
98 | } |
---|
99 | |
---|
100 | #endif |
---|