Changeset 5605 for trunk/gtk/util.c


Ignore:
Timestamp:
Apr 13, 2008, 2:56:26 AM (14 years ago)
Author:
charles
Message:

(gtk) #307: inhibit/prevent hibernation when downloading

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/util.c

    r5583 r5605  
    3333#include <gio/gio.h> /* g_file_trash() */
    3434#endif
     35#ifdef HAVE_DBUS_GLIB
     36#include <dbus/dbus-glib.h>
     37#endif
    3538
    3639#include <libevent/evhttp.h>
    3740
    3841#include <libtransmission/transmission.h> /* TR_RATIO_NA, TR_RATIO_INF */
     42#include <libtransmission/utils.h> /* tr_inf */
    3943
    4044#include "conf.h"
     
    462466    }
    463467}
     468
     469#ifdef HAVE_DBUS_GLIB
     470static DBusGProxy*
     471get_hibernation_inhibit_proxy( void )
     472{
     473    GError * error = NULL;
     474    DBusGConnection * conn;
     475
     476    conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error );
     477    if( error )
     478    {
     479        g_warning ("DBUS cannot connect : %s", error->message);
     480        g_error_free (error);
     481        return NULL;
     482    }
     483
     484    return dbus_g_proxy_new_for_name (conn,
     485               "org.freedesktop.PowerManagement",
     486               "/org/freedesktop/PowerManagement/Inhibit",
     487               "org.freedesktop.PowerManagement.Inhibit" );
     488}
     489#endif
     490
     491guint
     492gtr_inhibit_hibernation( void )
     493{
     494    guint inhibit_cookie = 0;
     495#ifdef HAVE_DBUS_GLIB
     496    DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
     497    if( proxy )
     498    {
     499        GError * error = NULL;
     500        const char * application = _( "Transmission Bittorrent Client" );
     501        const char * reason = _( "BitTorrent Activity" );
     502        gboolean success = dbus_g_proxy_call( proxy, "Inhibit", &error,
     503                                              G_TYPE_STRING, application,
     504                                              G_TYPE_STRING, reason,
     505                                              G_TYPE_INVALID,
     506                                              G_TYPE_UINT, &inhibit_cookie,
     507                                              G_TYPE_INVALID );
     508        if( success )
     509            tr_inf( _( "Desktop hibernation disabled while Transmission is running" ) );
     510        else {
     511            tr_err( _( "Couldn't disable desktop hibernation: %s." ), error->message );
     512            g_error_free( error );
     513        }
     514
     515        g_object_unref( G_OBJECT( proxy ) );
     516    }
     517#endif
     518    return inhibit_cookie;
     519}
     520
     521void
     522gtr_uninhibit_hibernation( guint inhibit_cookie )
     523{
     524#ifdef HAVE_DBUS_GLIB
     525    DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
     526    if( proxy )
     527    {
     528        GError * error = NULL;
     529        gboolean success = dbus_g_proxy_call( proxy, "UnInhibit", &error,
     530                                              G_TYPE_UINT, inhibit_cookie,
     531                                              G_TYPE_INVALID,
     532                                              G_TYPE_INVALID );
     533        if( !success ) {
     534            g_warning( "Couldn't uninhibit the system from suspending: %s.", error->message );
     535            g_error_free( error );
     536        }
     537
     538        g_object_unref( G_OBJECT( proxy ) );
     539    }
     540#endif
     541}
Note: See TracChangeset for help on using the changeset viewer.