2008-03-19 19:30:03 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
|
2007-02-19 22:09:05 +00:00
|
|
|
*
|
2008-03-19 19:30:03 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-03-19 19:30:03 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2008-09-23 19:11:04 +00:00
|
|
|
*
|
2008-03-19 19:30:03 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2007-02-19 22:09:05 +00:00
|
|
|
|
2008-01-16 16:03:18 +00:00
|
|
|
#include <glib/gi18n.h>
|
2007-02-19 22:09:05 +00:00
|
|
|
#include <gtk/gtk.h>
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "actions.h"
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-icon.h"
|
2007-02-19 22:09:05 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#ifndef STATUS_ICON_SUPPORTED
|
2007-02-19 22:09:05 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
gpointer
|
2008-01-18 00:11:51 +00:00
|
|
|
tr_icon_new( TrCore * core )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
return NULL;
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#else
|
2007-02-19 22:09:05 +00:00
|
|
|
|
2008-12-22 05:39:03 +00:00
|
|
|
#define UPDATE_INTERVAL_SECONDS 2
|
2008-04-01 02:36:41 +00:00
|
|
|
|
2007-02-19 22:09:05 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
activated( GtkStatusIcon * self UNUSED,
|
|
|
|
gpointer user_data UNUSED )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
action_activate ( "toggle-main-window" );
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
popup( GtkStatusIcon * self,
|
|
|
|
guint button,
|
|
|
|
guint when,
|
|
|
|
gpointer data UNUSED )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
GtkWidget * w = action_get_widget( "/icon-popup" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
gtk_menu_popup ( GTK_MENU( w ), NULL, NULL,
|
|
|
|
gtk_status_icon_position_menu,
|
|
|
|
self, button, when );
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 16:03:18 +00:00
|
|
|
static gboolean
|
|
|
|
refresh_tooltip_cb( gpointer data )
|
|
|
|
{
|
2009-06-30 18:08:50 +00:00
|
|
|
double d;
|
|
|
|
int limit;
|
|
|
|
char up[64];
|
|
|
|
char upLimit[64];
|
|
|
|
char down[64];
|
|
|
|
char downLimit[64];
|
|
|
|
char tip[1024];
|
|
|
|
const char * idle = _( "Idle" );
|
|
|
|
GtkStatusIcon * icon = GTK_STATUS_ICON( data );
|
|
|
|
tr_session * session = tr_core_session( g_object_get_data( G_OBJECT( icon ), "tr-core" ) );
|
|
|
|
|
|
|
|
/* up */
|
|
|
|
if(((d = tr_sessionGetRawSpeed( session, TR_UP ))) < 0.1 )
|
|
|
|
g_strlcpy( up, idle, sizeof( up ) );
|
|
|
|
else
|
|
|
|
tr_strlspeed( up, d, sizeof( up ) );
|
|
|
|
|
|
|
|
/* up limit */
|
|
|
|
if( !tr_sessionGetActiveSpeedLimit( session, TR_UP, &limit ) )
|
|
|
|
*upLimit = '\0';
|
|
|
|
else {
|
|
|
|
char buf[64];
|
|
|
|
tr_strlspeed( buf, limit, sizeof( buf ) );
|
|
|
|
g_snprintf( upLimit, sizeof( upLimit ), _( "(Limit: %s)" ), buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* down */
|
|
|
|
if(((d = tr_sessionGetRawSpeed( session, TR_DOWN ))) < 0.1 )
|
|
|
|
g_strlcpy( down, idle, sizeof( down ) );
|
|
|
|
else
|
|
|
|
tr_strlspeed( down, d, sizeof( down ) );
|
|
|
|
|
|
|
|
/* down limit */
|
|
|
|
if( !tr_sessionGetActiveSpeedLimit( session, TR_DOWN, &limit ) )
|
|
|
|
*downLimit = '\0';
|
|
|
|
else {
|
|
|
|
char buf[64];
|
|
|
|
tr_strlspeed( buf, limit, sizeof( buf ) );
|
|
|
|
g_snprintf( downLimit, sizeof( downLimit ), _( "(Limit: %s)" ), buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %1$s: current upload speed
|
|
|
|
* %2$s: current upload limit, if any
|
|
|
|
* %3$s: current download speed
|
|
|
|
* %4$s: current download limit, if any */
|
|
|
|
g_snprintf( tip, sizeof( tip ), _( "Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s" ), up, upLimit, down, downLimit );
|
|
|
|
|
2009-06-20 23:55:36 +00:00
|
|
|
#if GTK_CHECK_VERSION( 2,16,0 )
|
|
|
|
gtk_status_icon_set_tooltip_text( GTK_STATUS_ICON( icon ), tip );
|
|
|
|
#else
|
2008-01-16 16:03:18 +00:00
|
|
|
gtk_status_icon_set_tooltip( GTK_STATUS_ICON( icon ), tip );
|
2009-06-20 23:55:36 +00:00
|
|
|
#endif
|
2008-01-16 16:03:18 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-05-24 23:23:20 +00:00
|
|
|
static void
|
|
|
|
closeTag( gpointer tag )
|
|
|
|
{
|
|
|
|
g_source_remove( GPOINTER_TO_UINT( tag ) );
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
gpointer
|
2008-01-16 16:03:18 +00:00
|
|
|
tr_icon_new( TrCore * core )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
guint id;
|
|
|
|
GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name(
|
|
|
|
"transmission" );
|
|
|
|
|
2008-01-16 16:03:18 +00:00
|
|
|
g_signal_connect( icon, "activate", G_CALLBACK( activated ), NULL );
|
|
|
|
g_signal_connect( icon, "popup-menu", G_CALLBACK( popup ), NULL );
|
2008-12-22 05:39:03 +00:00
|
|
|
id = gtr_timeout_add_seconds( UPDATE_INTERVAL_SECONDS, refresh_tooltip_cb, icon );
|
2008-01-16 16:03:18 +00:00
|
|
|
g_object_set_data( G_OBJECT( icon ), "tr-core", core );
|
2008-09-23 19:11:04 +00:00
|
|
|
g_object_set_data_full( G_OBJECT(
|
|
|
|
icon ), "update-tag", GUINT_TO_POINTER(
|
|
|
|
id ), closeTag );
|
2008-01-16 16:03:18 +00:00
|
|
|
return icon;
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#endif
|