2008-02-24 15:42:31 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2008-02-24 15:42:31 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2008-02-24 15:42:31 +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-02-24 15:42:31 +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-02 16:14:46 +00:00
|
|
|
* $Id$
|
2008-02-24 15:42:31 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <string.h> /* strcmp() */
|
2009-11-22 18:55:24 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBCANBERRA
|
|
|
|
#include <canberra-gtk.h>
|
|
|
|
#endif
|
|
|
|
|
2008-03-09 19:02:16 +00:00
|
|
|
#ifdef HAVE_GIO
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <gio/gio.h>
|
2008-03-09 19:02:16 +00:00
|
|
|
#endif
|
2009-11-22 18:55:24 +00:00
|
|
|
|
2008-03-03 04:44:27 +00:00
|
|
|
#include <glib/gi18n.h>
|
2010-01-21 20:51:48 +00:00
|
|
|
#include "actions.h" /* NOTIFICATION_ICON */
|
2009-01-13 16:39:19 +00:00
|
|
|
#include "conf.h"
|
2008-02-24 15:42:31 +00:00
|
|
|
#include "notify.h"
|
2009-01-12 21:15:14 +00:00
|
|
|
#include "tr-prefs.h"
|
2008-04-11 02:21:33 +00:00
|
|
|
#include "util.h"
|
2008-02-24 15:42:31 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_LIBNOTIFY
|
|
|
|
|
2010-12-21 19:20:58 +00:00
|
|
|
void gtr_notify_init( void ) { }
|
2011-03-03 01:59:25 +00:00
|
|
|
void gtr_notify_torrent_completed( TrCore * core UNUSED, int torrent_id UNUSED ) { }
|
|
|
|
void gtr_notify_torrent_added( const char * name UNUSED ) { }
|
2008-02-24 15:42:31 +00:00
|
|
|
|
|
|
|
#else
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <libnotify/notify.h>
|
2008-02-24 15:42:31 +00:00
|
|
|
|
|
|
|
void
|
2010-12-21 19:20:58 +00:00
|
|
|
gtr_notify_init( void )
|
2008-02-24 15:42:31 +00:00
|
|
|
{
|
|
|
|
notify_init( "Transmission" );
|
|
|
|
}
|
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
struct notify_callback_data
|
|
|
|
{
|
|
|
|
TrCore * core;
|
|
|
|
int torrent_id;
|
|
|
|
};
|
|
|
|
|
2008-02-24 15:42:31 +00:00
|
|
|
static void
|
2011-03-03 01:59:25 +00:00
|
|
|
notify_callback_data_free( gpointer gdata )
|
2008-02-24 15:42:31 +00:00
|
|
|
{
|
2011-03-03 01:59:25 +00:00
|
|
|
struct notify_callback_data * data = gdata;
|
|
|
|
g_object_unref( G_OBJECT( data->core ) );
|
|
|
|
g_free( data );
|
|
|
|
}
|
2008-02-24 15:42:31 +00:00
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
static struct notify_callback_data *
|
|
|
|
notify_callback_data_new( TrCore * core, int torrent_id )
|
|
|
|
{
|
|
|
|
struct notify_callback_data * data = g_new( struct notify_callback_data, 1 );
|
|
|
|
data->core = core;
|
|
|
|
data->torrent_id = torrent_id;
|
|
|
|
g_object_ref( G_OBJECT( data->core ) );
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notifyCallback( NotifyNotification * n UNUSED,
|
|
|
|
const char * action,
|
|
|
|
gpointer gdata )
|
|
|
|
{
|
|
|
|
struct notify_callback_data * data = gdata;
|
|
|
|
tr_torrent * tor = gtr_core_find_torrent( data->core, data->torrent_id );
|
|
|
|
|
|
|
|
if( tor != NULL )
|
2008-03-07 19:10:08 +00:00
|
|
|
{
|
2011-03-03 01:59:25 +00:00
|
|
|
if( !strcmp( action, "folder" ) )
|
2009-06-11 14:59:50 +00:00
|
|
|
{
|
2011-03-03 01:59:25 +00:00
|
|
|
gtr_core_open_folder( data->core, data->torrent_id );
|
|
|
|
}
|
|
|
|
else if( !strcmp( action, "file" ) )
|
|
|
|
{
|
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2009-06-11 14:59:50 +00:00
|
|
|
const char * dir = tr_torrentGetDownloadDir( tor );
|
2011-03-03 01:59:25 +00:00
|
|
|
char * path = g_build_filename( dir, inf->files[0].name, NULL );
|
2009-06-11 14:59:50 +00:00
|
|
|
gtr_open_file( path );
|
|
|
|
g_free( path );
|
|
|
|
}
|
2008-03-07 19:10:08 +00:00
|
|
|
}
|
2008-02-24 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2009-02-25 17:48:45 +00:00
|
|
|
static gboolean
|
|
|
|
can_support_actions( void )
|
|
|
|
{
|
|
|
|
static gboolean supported;
|
|
|
|
static gboolean have_checked = FALSE;
|
|
|
|
|
|
|
|
if( !have_checked )
|
|
|
|
{
|
|
|
|
GList * c;
|
|
|
|
GList * caps = notify_get_server_caps( );
|
|
|
|
|
|
|
|
have_checked = TRUE;
|
|
|
|
|
|
|
|
for( c=caps; c && !supported; c=c->next )
|
|
|
|
supported = !strcmp( "actions", (char*)c->data );
|
|
|
|
|
|
|
|
g_list_foreach( caps, (GFunc)g_free, NULL );
|
|
|
|
g_list_free( caps );
|
|
|
|
}
|
|
|
|
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
2010-01-21 20:51:48 +00:00
|
|
|
static void
|
|
|
|
addIcon( NotifyNotification * notify )
|
|
|
|
{
|
|
|
|
int size = 32;
|
|
|
|
GtkIconTheme * theme;
|
|
|
|
GdkPixbuf * icon;
|
|
|
|
|
|
|
|
gtk_icon_size_lookup( GTK_ICON_SIZE_DIALOG, &size, &size );
|
|
|
|
theme = gtk_icon_theme_get_default( );
|
|
|
|
icon = gtk_icon_theme_load_icon( theme, NOTIFICATION_ICON, size, 0, NULL );
|
|
|
|
|
|
|
|
if( icon != NULL )
|
|
|
|
{
|
|
|
|
notify_notification_set_icon_from_pixbuf( notify, icon );
|
|
|
|
g_object_unref( icon );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
static NotifyNotification *
|
|
|
|
tr_notify_notification_new( const char * summary,
|
|
|
|
const char * body,
|
|
|
|
const char * icon )
|
|
|
|
{
|
|
|
|
NotifyNotification * n = notify_notification_new( summary, body, icon
|
|
|
|
/* the fourth argument was removed in libnotify 0.7.0 */
|
|
|
|
#if !defined(NOTIFY_VERSION_MINOR) || (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7)
|
|
|
|
, NULL
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-02-24 15:42:31 +00:00
|
|
|
void
|
2011-03-03 01:59:25 +00:00
|
|
|
gtr_notify_torrent_completed( TrCore * core, int torrent_id )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-11-22 18:55:24 +00:00
|
|
|
#ifdef HAVE_LIBCANBERRA
|
2010-12-22 06:25:30 +00:00
|
|
|
if( gtr_pref_flag_get( PREF_KEY_PLAY_DOWNLOAD_COMPLETE_SOUND ) )
|
2009-11-22 18:55:24 +00:00
|
|
|
{
|
|
|
|
/* play the sound, using sounds from the naming spec */
|
|
|
|
ca_context_play( ca_gtk_context_get (), 0,
|
|
|
|
CA_PROP_EVENT_ID, "complete-download",
|
|
|
|
CA_PROP_APPLICATION_NAME, g_get_application_name,
|
|
|
|
CA_PROP_EVENT_DESCRIPTION, _("Download complete"),
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-12-22 06:25:30 +00:00
|
|
|
if( gtr_pref_flag_get( PREF_KEY_SHOW_DESKTOP_NOTIFICATION ) )
|
2009-01-12 18:48:20 +00:00
|
|
|
{
|
|
|
|
NotifyNotification * n;
|
2011-03-03 01:59:25 +00:00
|
|
|
tr_torrent * tor = gtr_core_find_torrent( core, torrent_id );
|
2009-01-12 18:48:20 +00:00
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
n = tr_notify_notification_new( _( "Torrent Complete" ), tr_torrentName( tor ), NULL );
|
2010-01-21 20:51:48 +00:00
|
|
|
addIcon( n );
|
2009-01-12 18:48:20 +00:00
|
|
|
|
2009-02-25 17:48:45 +00:00
|
|
|
if( can_support_actions( ) )
|
|
|
|
{
|
2011-03-03 01:59:25 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
|
|
|
if( inf->fileCount == 1 )
|
2009-02-25 17:48:45 +00:00
|
|
|
notify_notification_add_action(
|
|
|
|
n, "file", _( "Open File" ),
|
2011-03-03 01:59:25 +00:00
|
|
|
NOTIFY_ACTION_CALLBACK( notifyCallback ),
|
|
|
|
notify_callback_data_new( core, torrent_id ),
|
|
|
|
notify_callback_data_free );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-02-25 17:48:45 +00:00
|
|
|
notify_notification_add_action(
|
|
|
|
n, "folder", _( "Open Folder" ),
|
2011-03-03 01:59:25 +00:00
|
|
|
NOTIFY_ACTION_CALLBACK( notifyCallback ),
|
|
|
|
notify_callback_data_new( core, torrent_id ),
|
|
|
|
notify_callback_data_free );
|
2009-02-25 17:48:45 +00:00
|
|
|
}
|
2009-01-12 18:48:20 +00:00
|
|
|
|
|
|
|
notify_notification_show( n, NULL );
|
|
|
|
}
|
2008-02-24 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2009-06-11 16:17:48 +00:00
|
|
|
void
|
2011-03-03 01:59:25 +00:00
|
|
|
gtr_notify_torrent_added( const char * name )
|
2009-06-11 16:17:48 +00:00
|
|
|
{
|
2010-12-22 06:25:30 +00:00
|
|
|
if( gtr_pref_flag_get( PREF_KEY_SHOW_DESKTOP_NOTIFICATION ) )
|
2009-06-11 16:17:48 +00:00
|
|
|
{
|
2011-03-03 01:59:25 +00:00
|
|
|
NotifyNotification * n = tr_notify_notification_new( _( "Torrent Added" ), name, NULL );
|
2010-01-21 20:51:48 +00:00
|
|
|
addIcon( n );
|
2009-06-11 16:17:48 +00:00
|
|
|
notify_notification_set_timeout( n, NOTIFY_EXPIRES_DEFAULT );
|
|
|
|
notify_notification_show( n, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-24 15:42:31 +00:00
|
|
|
#endif
|