2008-03-19 19:30:03 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2007-02-19 22:09:05 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2008-03-19 19:30:03 +00:00
|
|
|
* 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>
|
2010-02-07 19:32:35 +00:00
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
|
|
|
#include <libappindicator/app-indicator.h>
|
|
|
|
#endif
|
2010-07-03 00:25:22 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.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"
|
|
|
|
|
2010-08-05 13:24:46 +00:00
|
|
|
#define ICON_NAME "transmission"
|
2010-02-14 14:26:45 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#ifndef STATUS_ICON_SUPPORTED
|
2010-12-21 19:20:58 +00:00
|
|
|
gpointer gtr_icon_new( TrCore * core UNUSED ) { return NULL; }
|
|
|
|
void gtr_icon_refresh( gpointer vicon UNUSED ) { }
|
2009-11-18 06:21:01 +00:00
|
|
|
#else
|
2008-04-01 02:36:41 +00:00
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
|
|
|
void
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_icon_refresh( gpointer vindicator UNUSED )
|
2010-02-07 19:32:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#else
|
2007-02-19 22:09:05 +00:00
|
|
|
static void
|
2010-12-21 19:20:58 +00:00
|
|
|
activated( GtkStatusIcon * self UNUSED, gpointer user_data UNUSED )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_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
|
|
|
{
|
2010-12-21 19:20:58 +00:00
|
|
|
GtkWidget * w = gtr_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
|
|
|
}
|
|
|
|
|
2009-11-18 06:21:01 +00:00
|
|
|
void
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_icon_refresh( gpointer vicon )
|
2008-01-16 16:03:18 +00:00
|
|
|
{
|
2010-07-04 06:07:21 +00:00
|
|
|
double KBps;
|
|
|
|
double limit;
|
2009-06-30 18:08:50 +00:00
|
|
|
char up[64];
|
|
|
|
char upLimit[64];
|
|
|
|
char down[64];
|
|
|
|
char downLimit[64];
|
|
|
|
char tip[1024];
|
|
|
|
const char * idle = _( "Idle" );
|
2009-11-18 06:21:01 +00:00
|
|
|
GtkStatusIcon * icon = GTK_STATUS_ICON( vicon );
|
2009-06-30 18:08:50 +00:00
|
|
|
tr_session * session = tr_core_session( g_object_get_data( G_OBJECT( icon ), "tr-core" ) );
|
|
|
|
|
|
|
|
/* up */
|
2010-07-04 06:07:21 +00:00
|
|
|
KBps = tr_sessionGetRawSpeed_KBps( session, TR_UP );
|
|
|
|
if( KBps < 0.001 )
|
2009-06-30 18:08:50 +00:00
|
|
|
g_strlcpy( up, idle, sizeof( up ) );
|
|
|
|
else
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_speed_KBps( up, KBps, sizeof( up ) );
|
2009-06-30 18:08:50 +00:00
|
|
|
|
|
|
|
/* up limit */
|
2010-07-04 06:07:21 +00:00
|
|
|
if( !tr_sessionGetActiveSpeedLimit_KBps( session, TR_UP, &limit ) )
|
2009-06-30 18:08:50 +00:00
|
|
|
*upLimit = '\0';
|
|
|
|
else {
|
|
|
|
char buf[64];
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_speed_KBps( buf, limit, sizeof( buf ) );
|
2009-06-30 18:08:50 +00:00
|
|
|
g_snprintf( upLimit, sizeof( upLimit ), _( "(Limit: %s)" ), buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* down */
|
2010-07-04 06:07:21 +00:00
|
|
|
KBps = tr_sessionGetRawSpeed_KBps( session, TR_DOWN );
|
|
|
|
if( KBps < 0.001 )
|
2009-06-30 18:08:50 +00:00
|
|
|
g_strlcpy( down, idle, sizeof( down ) );
|
|
|
|
else
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_speed_KBps( down, KBps, sizeof( down ) );
|
2009-06-30 18:08:50 +00:00
|
|
|
|
2009-08-10 20:04:08 +00:00
|
|
|
/* down limit */
|
2010-07-04 06:07:21 +00:00
|
|
|
if( !tr_sessionGetActiveSpeedLimit_KBps( session, TR_DOWN, &limit ) )
|
2009-06-30 18:08:50 +00:00
|
|
|
*downLimit = '\0';
|
|
|
|
else {
|
|
|
|
char buf[64];
|
2010-07-04 06:07:21 +00:00
|
|
|
tr_formatter_speed_KBps( buf, limit, sizeof( buf ) );
|
2009-06-30 18:08:50 +00:00
|
|
|
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-05-24 23:23:20 +00:00
|
|
|
}
|
2010-02-07 19:32:35 +00:00
|
|
|
#endif
|
|
|
|
|
2010-07-04 18:27:03 +00:00
|
|
|
static const char *
|
|
|
|
getIconName( void )
|
2010-02-07 19:32:35 +00:00
|
|
|
{
|
2010-02-14 14:26:45 +00:00
|
|
|
const char * icon_name;
|
2010-07-04 18:27:03 +00:00
|
|
|
|
2010-02-14 14:26:45 +00:00
|
|
|
GtkIconTheme * theme = gtk_icon_theme_get_default( );
|
|
|
|
|
|
|
|
/* if the tray's icon is a 48x48 file, use it;
|
|
|
|
* otherwise, use the fallback builtin icon */
|
|
|
|
if( !gtk_icon_theme_has_icon( theme, TRAY_ICON ) )
|
2010-08-05 13:24:46 +00:00
|
|
|
icon_name = ICON_NAME;
|
2010-02-14 14:26:45 +00:00
|
|
|
else {
|
|
|
|
GtkIconInfo * icon_info = gtk_icon_theme_lookup_icon( theme, TRAY_ICON, 48, GTK_ICON_LOOKUP_USE_BUILTIN );
|
|
|
|
const gboolean icon_is_builtin = gtk_icon_info_get_filename ( icon_info ) == NULL;
|
|
|
|
gtk_icon_info_free ( icon_info );
|
2010-08-05 13:24:46 +00:00
|
|
|
icon_name = icon_is_builtin ? ICON_NAME : TRAY_ICON;
|
2010-02-14 14:26:45 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2010-07-04 18:27:03 +00:00
|
|
|
return icon_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
|
|
|
gpointer
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_icon_new( TrCore * core)
|
2010-07-04 18:27:03 +00:00
|
|
|
{
|
|
|
|
GtkWidget * w;
|
|
|
|
const char * icon_name = getIconName( );
|
2010-08-05 13:24:46 +00:00
|
|
|
AppIndicator * indicator = app_indicator_new( ICON_NAME, icon_name, APP_INDICATOR_CATEGORY_SYSTEM_SERVICES );
|
2010-02-14 14:26:45 +00:00
|
|
|
app_indicator_set_status( indicator, APP_INDICATOR_STATUS_ACTIVE );
|
2010-12-21 19:20:58 +00:00
|
|
|
w = gtr_action_get_widget( "/icon-popup" );
|
2010-02-14 14:26:45 +00:00
|
|
|
app_indicator_set_menu( indicator, GTK_MENU ( w ) );
|
|
|
|
g_object_set_data( G_OBJECT( indicator ), "tr-core", core );
|
|
|
|
return indicator;
|
2010-03-17 17:07:40 +00:00
|
|
|
}
|
2010-02-07 19:32:35 +00:00
|
|
|
#else
|
2007-06-18 03:40:41 +00:00
|
|
|
gpointer
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_icon_new( TrCore * core )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2010-07-04 18:27:03 +00:00
|
|
|
const char * icon_name = getIconName( );
|
2010-01-21 20:51:48 +00:00
|
|
|
GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name( icon_name );
|
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 );
|
|
|
|
g_object_set_data( G_OBJECT( icon ), "tr-core", core );
|
|
|
|
return icon;
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#endif
|
2010-02-07 19:32:35 +00:00
|
|
|
|
|
|
|
#endif
|