source: branches/2.1x/gtk/notify.c @ 11552

Last change on this file since 11552 was 11552, checked in by charles, 12 years ago

backport #3837, #3838, #3834, #3719, #3826 to the 2.1x branch

  • Property svn:keywords set to Date Rev Author Id
File size: 4.6 KB
Line 
1/*
2 * This file Copyright (C) 2008-2010 Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: notify.c 11552 2010-12-20 12:26:18Z charles $
11 */
12
13#include <string.h>
14
15#ifdef HAVE_LIBCANBERRA
16 #include <canberra-gtk.h>
17#endif
18
19#ifdef HAVE_GIO
20 #include <gio/gio.h>
21#endif
22
23#include <glib/gi18n.h>
24#include "actions.h" /* NOTIFICATION_ICON */
25#include "conf.h"
26#include "notify.h"
27#include "tr-prefs.h"
28#include "util.h"
29
30#ifndef HAVE_LIBNOTIFY
31
32void tr_notify_init( void ) { }
33void tr_notify_send( TrTorrent * tor UNUSED ) { }
34void tr_notify_added( const char * name UNUSED ) { }
35
36#else
37 #include <libnotify/notify.h>
38
39void
40tr_notify_init( void )
41{
42    notify_init( "Transmission" );
43}
44
45static void
46notifyCallback( NotifyNotification * n UNUSED,
47                const char *           action,
48                gpointer               gdata )
49{
50    TrTorrent * gtor = TR_TORRENT( gdata );
51
52    if( !strcmp( action, "folder" ) )
53    {
54        tr_torrent_open_folder( gtor );
55    }
56    else if( !strcmp( action, "file" ) )
57    {
58        tr_torrent * tor = tr_torrent_handle( gtor );
59        const tr_info * info = tr_torrent_info( gtor );
60        if( tor && info )
61        {
62            const char * dir = tr_torrentGetDownloadDir( tor );
63            char * path = g_build_filename( dir, info->files[0].name, NULL );
64            gtr_open_file( path );
65            g_free( path );
66        }
67    }
68}
69
70static gboolean
71can_support_actions( void )
72{
73    static gboolean supported;
74    static gboolean have_checked = FALSE;
75
76    if( !have_checked )
77    {
78        GList * c;
79        GList * caps = notify_get_server_caps( );
80
81        have_checked = TRUE;
82
83        for( c=caps; c && !supported; c=c->next )
84            supported = !strcmp( "actions", (char*)c->data );
85
86        g_list_foreach( caps, (GFunc)g_free, NULL );
87        g_list_free( caps );
88    }
89
90    return supported;
91}
92
93static void
94addIcon( NotifyNotification * notify )
95{
96    int size = 32;
97    GtkIconTheme * theme;
98    GdkPixbuf * icon;
99
100    gtk_icon_size_lookup( GTK_ICON_SIZE_DIALOG, &size, &size );
101    theme = gtk_icon_theme_get_default( );
102    icon = gtk_icon_theme_load_icon( theme, NOTIFICATION_ICON, size, 0, NULL );
103
104    if( icon != NULL )
105    {
106        notify_notification_set_icon_from_pixbuf( notify, icon );
107        g_object_unref( icon );
108    }
109}
110
111void
112tr_notify_send( TrTorrent *tor )
113{
114#ifdef HAVE_LIBCANBERRA
115    if( pref_flag_get( PREF_KEY_PLAY_DOWNLOAD_COMPLETE_SOUND ) )
116    {
117        /* play the sound, using sounds from the naming spec */
118        ca_context_play( ca_gtk_context_get (), 0,
119                         CA_PROP_EVENT_ID, "complete-download",
120                         CA_PROP_APPLICATION_NAME, g_get_application_name,
121                         CA_PROP_EVENT_DESCRIPTION, _("Download complete"),
122                         NULL);
123    }
124#endif
125
126    if( pref_flag_get( PREF_KEY_SHOW_DESKTOP_NOTIFICATION ) )
127    {
128        const tr_info * info = tr_torrent_info( tor );
129        NotifyNotification * n;
130
131        n = notify_notification_new( _( "Torrent Complete" ),
132                                     info->name, NULL
133/* the fourth argument was removed in libnotify 0.7.0 */
134#if !defined(NOTIFY_VERSION_MINOR) || (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7)
135                                                     , NULL
136#endif
137                                                            );
138        addIcon( n );
139
140        if( can_support_actions( ) )
141        {
142            if( info->fileCount == 1 )
143                notify_notification_add_action(
144                    n, "file", _( "Open File" ),
145                    NOTIFY_ACTION_CALLBACK( notifyCallback ), tor,
146                    NULL );
147
148            notify_notification_add_action(
149                n, "folder", _( "Open Folder" ),
150                NOTIFY_ACTION_CALLBACK( notifyCallback ), tor, NULL );
151        }
152
153        notify_notification_show( n, NULL );
154    }
155}
156
157void
158tr_notify_added( const char * name )
159{
160    if( pref_flag_get( PREF_KEY_SHOW_DESKTOP_NOTIFICATION ) )
161    {
162        NotifyNotification * n = notify_notification_new(
163            _( "Torrent Added" ), name, NULL
164/* the fourth argument was removed in libnotify 0.7.0 */
165#if !defined(NOTIFY_VERSION_MINOR) || (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7)
166            , NULL
167#endif
168            );
169        addIcon( n );
170        notify_notification_set_timeout( n, NOTIFY_EXPIRES_DEFAULT );
171        notify_notification_show( n, NULL );
172    }
173}
174
175#endif
Note: See TracBrowser for help on using the repository browser.