(gtk) #942: Option to inhibit / allow hibernation

(gtk) #943: Option to show / hide the tray icon
This commit is contained in:
Charles Kerr 2008-05-24 23:23:20 +00:00
parent 0ab37e45d5
commit d39b7dced7
8 changed files with 170 additions and 100 deletions

2
NEWS
View File

@ -6,6 +6,8 @@ http://trac.transmissionbt.com/query?group=component&milestone=1.30&order=severi
+ Quick Look integration in the main window and inspector's file tab
+ Transfers can be dragged to different groups
+ Status strings are toggled from the action button (they are no longer clickable)
- GTK+
+ Add preferences options to inhibit hibernation and to toggle the tray icon
1.21 (2008/05/21)
http://trac.transmissionbt.com/query?group=component&milestone=1.21&order=severity

View File

@ -407,9 +407,6 @@ main( int argc, char ** argv )
if( didlock && ( didinit || cf_init( configDir, &err ) ) )
{
gboolean do_inhibit = FALSE;
guint inhibit_cookie = 0;
tr_handle * h = tr_sessionInitFull(
configDir,
"gtk",
@ -442,13 +439,7 @@ main( int argc, char ** argv )
appsetup( win, argfiles, cbdata, startpaused, startminimized );
tr_sessionSetRPCCallback( h, onRPCChanged, cbdata );
if(( do_inhibit = pref_flag_get( PREF_KEY_INHIBIT_HIBERNATION )))
inhibit_cookie = gtr_inhibit_hibernation( );
gtk_main();
if( do_inhibit && inhibit_cookie )
gtr_uninhibit_hibernation( inhibit_cookie );
}
else if( err )
{
@ -479,6 +470,9 @@ appsetup( TrWindow * wind, GSList * torrentFiles,
cbdata->errqueue = NULL;
cbdata->minimized = minimized;
if( minimized )
pref_flag_set( PREF_KEY_TRAY_ICON_ENABLED, TRUE );
actions_set_core( cbdata->core );
/* set up core handlers */
@ -499,8 +493,8 @@ appsetup( TrWindow * wind, GSList * torrentFiles,
/* set up main window */
winsetup( cbdata, wind );
/* set up the system tray */
cbdata->icon = tr_icon_new( cbdata->core );
/* set up the icon */
prefschanged( cbdata->core, PREF_KEY_TRAY_ICON_ENABLED, cbdata );
/* start model update timer */
cbdata->timer = g_timeout_add( UPDATE_INTERVAL, updatemodel, cbdata );
@ -895,6 +889,17 @@ prefschanged( TrCore * core UNUSED, const char * key, gpointer data )
const int port = pref_int_get( key );
tr_sessionSetPeerPort( tr, port );
}
else if( !strcmp( key, PREF_KEY_TRAY_ICON_ENABLED ) )
{
const int show = pref_flag_get( key );
action_sensitize ( "close", show );
if( show && !cbdata->icon )
cbdata->icon = tr_icon_new( cbdata->core );
else if( !show && cbdata->icon ) {
g_object_unref( cbdata->icon );
cbdata->icon = NULL;
}
}
else if( !strcmp( key, PREF_KEY_DL_LIMIT_ENABLED ) )
{
const gboolean b = pref_flag_get( key );
@ -935,7 +940,7 @@ prefschanged( TrCore * core UNUSED, const char * key, gpointer data )
}
else if( !strcmp( key, PREF_KEY_RPC_ENABLED ) )
{
g_message( "preferences option not recognized: %s", key );
tr_sessionSetRPCEnabled( tr, pref_flag_get( key ) );
}
}

View File

@ -45,6 +45,8 @@
#include "tr-torrent.h"
#include "util.h"
static void tr_core_set_hibernation_allowed( TrCore * core, gboolean allowed );
struct TrCorePrivate
{
#ifdef HAVE_GIO
@ -54,6 +56,9 @@ struct TrCorePrivate
GSList * monitor_files;
guint monitor_idle_tag;
#endif
gboolean inhibit_allowed;
gboolean have_inhibit_cookie;
guint inhibit_cookie;
GtkTreeModel * model;
tr_handle * handle;
};
@ -473,6 +478,10 @@ prefsChanged( TrCore * core, const char * key, gpointer data UNUSED )
const uint16_t val = pref_int_get( key );
tr_sessionSetPeerLimit( tr_core_handle( core ), val );
}
else if( !strcmp( key, PREF_KEY_ALLOW_HIBERNATION ) )
{
tr_core_set_hibernation_allowed( core, pref_flag_get( key ) );
}
#ifdef HAVE_GIO
else if( !strcmp( key, PREF_KEY_DIR_WATCH ) ||
!strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) )
@ -564,6 +573,7 @@ tr_core_new( tr_handle * h )
prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL );
prefsChanged( core, PREF_KEY_DIR_WATCH_ENABLED, NULL );
prefsChanged( core, PREF_KEY_MAX_PEERS_GLOBAL, NULL );
prefsChanged( core, PREF_KEY_ALLOW_HIBERNATION, NULL );
g_signal_connect( core, "prefs-changed", G_CALLBACK(prefsChanged), NULL );
return core;
@ -889,6 +899,109 @@ tr_core_quit( TrCore * core )
g_signal_emit( core, TR_CORE_GET_CLASS(core)->quitsig, 0 );
}
/**
*** Hibernate
**/
#ifdef HAVE_DBUS_GLIB
static DBusGProxy*
get_hibernation_inhibit_proxy( void )
{
GError * error = NULL;
DBusGConnection * conn;
conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error );
if( error )
{
g_warning ("DBUS cannot connect : %s", error->message);
g_error_free (error);
return NULL;
}
return dbus_g_proxy_new_for_name (conn,
"org.freedesktop.PowerManagement",
"/org/freedesktop/PowerManagement/Inhibit",
"org.freedesktop.PowerManagement.Inhibit" );
}
static gboolean
gtr_inhibit_hibernation( guint * cookie )
{
gboolean success = FALSE;
DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
if( proxy )
{
GError * error = NULL;
const char * application = _( "Transmission Bittorrent Client" );
const char * reason = _( "BitTorrent Activity" );
success = dbus_g_proxy_call( proxy, "Inhibit", &error,
G_TYPE_STRING, application,
G_TYPE_STRING, reason,
G_TYPE_INVALID,
G_TYPE_UINT, cookie,
G_TYPE_INVALID );
if( success )
tr_inf( _( "Disallowing desktop hibernation" ) );
else {
tr_err( _( "Couldn't disable desktop hibernation: %s" ), error->message );
g_error_free( error );
}
g_object_unref( G_OBJECT( proxy ) );
}
return success != 0;
}
static void
gtr_uninhibit_hibernation( guint inhibit_cookie )
{
DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
if( proxy )
{
GError * error = NULL;
gboolean success = dbus_g_proxy_call( proxy, "UnInhibit", &error,
G_TYPE_UINT, inhibit_cookie,
G_TYPE_INVALID,
G_TYPE_INVALID );
if( success )
tr_inf( _( "Allowing desktop hibernation" ) );
else {
g_warning( "Couldn't uninhibit the system from suspending: %s.", error->message );
g_error_free( error );
}
g_object_unref( G_OBJECT( proxy ) );
}
}
#endif
void
tr_core_set_hibernation_allowed( TrCore * core, gboolean allowed )
{
#ifdef HAVE_DBUS_GLIB
g_return_if_fail( core );
g_return_if_fail( core->priv );
core->priv->inhibit_allowed = allowed != 0;
if( allowed && core->priv->have_inhibit_cookie )
{
gtr_uninhibit_hibernation( core->priv->inhibit_cookie );
core->priv->have_inhibit_cookie = FALSE;
}
if( !allowed && !core->priv->have_inhibit_cookie )
{
core->priv->have_inhibit_cookie = gtr_inhibit_hibernation( &core->priv->inhibit_cookie );
core->priv->have_inhibit_cookie = TRUE;
}
#endif
}
/**
*** Prefs
**/

View File

@ -47,12 +47,6 @@ popup ( GtkStatusIcon * self,
self, button, when );
}
static void
core_destroyed( gpointer data, GObject * core UNUSED )
{
g_source_remove( GPOINTER_TO_UINT( data ) );
}
static gboolean
refresh_tooltip_cb( gpointer data )
{
@ -80,6 +74,12 @@ refresh_tooltip_cb( gpointer data )
return TRUE;
}
static void
closeTag( gpointer tag )
{
g_source_remove( GPOINTER_TO_UINT( tag ) );
}
gpointer
tr_icon_new( TrCore * core )
{
@ -88,8 +88,8 @@ tr_icon_new( TrCore * core )
g_signal_connect( icon, "activate", G_CALLBACK( activated ), NULL );
g_signal_connect( icon, "popup-menu", G_CALLBACK( popup ), NULL );
id = g_timeout_add( UPDATE_INTERVAL, refresh_tooltip_cb, icon );
g_object_weak_ref( G_OBJECT( core ), core_destroyed, GUINT_TO_POINTER( id ) );
g_object_set_data( G_OBJECT( icon ), "tr-core", core );
g_object_set_data_full( G_OBJECT( icon ), "update-tag", GUINT_TO_POINTER( id ), closeTag );
return icon;
}

View File

@ -44,7 +44,7 @@ tr_prefs_init_global( void )
#endif
pref_int_set_default ( PREF_KEY_PEER_SOCKET_TOS, TR_DEFAULT_PEER_SOCKET_TOS );
pref_flag_set_default ( PREF_KEY_INHIBIT_HIBERNATION, TRUE );
pref_flag_set_default ( PREF_KEY_ALLOW_HIBERNATION, TRUE );
pref_flag_set_default ( PREF_KEY_BLOCKLIST_ENABLED, TR_DEFAULT_BLOCKLIST_ENABLED );
pref_string_set_default ( PREF_KEY_OPEN_DIALOG_FOLDER, g_get_home_dir( ) );
@ -55,6 +55,7 @@ tr_prefs_init_global( void )
pref_flag_set_default ( PREF_KEY_TOOLBAR, TRUE );
pref_flag_set_default ( PREF_KEY_FILTERBAR, TRUE );
pref_flag_set_default ( PREF_KEY_STATUSBAR, TRUE );
pref_flag_set_default ( PREF_KEY_TRAY_ICON_ENABLED, TRUE );
pref_string_set_default ( PREF_KEY_STATUSBAR_STATS, "total-ratio" );
pref_flag_set_default ( PREF_KEY_DL_LIMIT_ENABLED, FALSE );
@ -504,6 +505,30 @@ peerPage( GObject * core )
return t;
}
static GtkWidget*
desktopPage( GObject * core )
{
int row = 0;
const char * s;
GtkWidget * t;
GtkWidget * w;
t = hig_workarea_create( );
hig_workarea_add_section_title (t, &row, _("Options"));
s = _( "Allow desktop _hibernation" );
w = new_check_button( s, PREF_KEY_ALLOW_HIBERNATION, core );
hig_workarea_add_wide_control( t, &row, w );
s = _( "Show tray _icon" );
w = new_check_button( s, PREF_KEY_TRAY_ICON_ENABLED, core );
hig_workarea_add_wide_control( t, &row, w );
hig_workarea_finish( t, &row );
return t;
}
static GtkWidget*
networkPage( GObject * core, gpointer alive )
{
@ -597,6 +622,9 @@ tr_prefs_dialog_new( GObject * core, GtkWindow * parent )
gtk_notebook_append_page( GTK_NOTEBOOK( n ),
networkPage( core, alive ),
gtk_label_new (_("Network")) );
gtk_notebook_append_page( GTK_NOTEBOOK( n ),
desktopPage( core ),
gtk_label_new (_("Desktop")) );
g_signal_connect( d, "response", G_CALLBACK(response_cb), core );
gtk_box_pack_start_defaults( GTK_BOX(GTK_DIALOG(d)->vbox), n );

View File

@ -27,9 +27,10 @@ GtkWidget * tr_prefs_dialog_new( GObject * core, GtkWindow * parent );
#define PREF_KEY_OPTIONS_PROMPT "show-options-window"
#define PREF_KEY_DOWNLOAD_DIR "default-download-directory"
#define PREF_KEY_OPEN_DIALOG_FOLDER "open-dialog-folder"
#define PREF_KEY_INHIBIT_HIBERNATION "inhibit-hibernation"
#define PREF_KEY_ALLOW_HIBERNATION "allow-hibernation"
#define PREF_KEY_DIR_WATCH "watch-folder"
#define PREF_KEY_DIR_WATCH_ENABLED "watch-folder-enabled"
#define PREF_KEY_TRAY_ICON_ENABLED "tray-icon-enabled"
#define PREF_KEY_START "start-added-torrents"
#define PREF_KEY_TRASH_ORIGINAL "trash-original-torrent-files"
#define PREF_KEY_PEER_SOCKET_TOS "peer-socket-tos"

View File

@ -448,81 +448,6 @@ gtr_open_file( const char * path )
}
}
#ifdef HAVE_DBUS_GLIB
static DBusGProxy*
get_hibernation_inhibit_proxy( void )
{
GError * error = NULL;
DBusGConnection * conn;
conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error );
if( error )
{
g_warning ("DBUS cannot connect : %s", error->message);
g_error_free (error);
return NULL;
}
return dbus_g_proxy_new_for_name (conn,
"org.freedesktop.PowerManagement",
"/org/freedesktop/PowerManagement/Inhibit",
"org.freedesktop.PowerManagement.Inhibit" );
}
#endif
guint
gtr_inhibit_hibernation( void )
{
guint inhibit_cookie = 0;
#ifdef HAVE_DBUS_GLIB
DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
if( proxy )
{
GError * error = NULL;
const char * application = _( "Transmission Bittorrent Client" );
const char * reason = _( "BitTorrent Activity" );
gboolean success = dbus_g_proxy_call( proxy, "Inhibit", &error,
G_TYPE_STRING, application,
G_TYPE_STRING, reason,
G_TYPE_INVALID,
G_TYPE_UINT, &inhibit_cookie,
G_TYPE_INVALID );
if( success )
tr_inf( _( "Desktop hibernation disabled while Transmission is running" ) );
else {
tr_err( _( "Couldn't disable desktop hibernation: %s" ), error->message );
g_error_free( error );
}
g_object_unref( G_OBJECT( proxy ) );
}
#endif
return inhibit_cookie;
}
void
gtr_uninhibit_hibernation( guint inhibit_cookie )
{
#ifdef HAVE_DBUS_GLIB
DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
if( proxy )
{
GError * error = NULL;
gboolean success = dbus_g_proxy_call( proxy, "UnInhibit", &error,
G_TYPE_UINT, inhibit_cookie,
G_TYPE_INVALID,
G_TYPE_INVALID );
if( !success ) {
g_warning( "Couldn't uninhibit the system from suspending: %s.", error->message );
g_error_free( error );
}
g_object_unref( G_OBJECT( proxy ) );
}
#endif
}
#define VALUE_SERVICE_NAME "com.transmissionbt.Transmission"
#define VALUE_SERVICE_OBJECT_PATH "/com/transmissionbt/Transmission"
#define VALUE_SERVICE_INTERFACE "com.transmissionbt.Transmission"

View File

@ -82,10 +82,6 @@ checkfilenames( int argc, char ** argv );
void gtr_open_file( const char * path );
guint gtr_inhibit_hibernation( void );
void gtr_uninhibit_hibernation( guint );
gboolean gtr_dbus_add_torrent( const char * filename );
char* gtr_get_help_url( void );