]> git.sesse.net Git - vlc/blob - modules/misc/notify/notify.c
Fixed potential segfault in notify.
[vlc] / modules / misc / notify / notify.c
1 /*****************************************************************************
2  * notify.c : libnotify notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Mutricy <xtophe -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35
36 #include <gdk-pixbuf/gdk-pixbuf.h>
37 #include <libnotify/notify.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  ****************************************************************************/
42 static int  Open    ( vlc_object_t * );
43 static void Close   ( vlc_object_t * );
44
45 #define APPLICATION_NAME "VLC media player"
46
47 #define TIMEOUT_TEXT N_("Timeout (ms)")
48 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")
49
50 vlc_module_begin ()
51     set_category( CAT_INTERFACE )
52     set_subcategory( SUBCAT_INTERFACE_CONTROL )
53     set_shortname( N_( "Notify" ) )
54     set_description( N_("LibNotify Notification Plugin") )
55
56     add_integer( "notify-timeout", 4000, NULL,
57                 TIMEOUT_TEXT, TIMEOUT_LONGTEXT, true )
58
59     set_capability( "interface", 0 )
60     set_callbacks( Open, Close )
61 vlc_module_end ()
62
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int ItemChange( vlc_object_t *, const char *,
68                        vlc_value_t, vlc_value_t, void * );
69 static int Notify( vlc_object_t *, const char *, GdkPixbuf *, intf_thread_t * );
70 #define MAX_LENGTH 256
71
72 struct intf_sys_t
73 {
74     NotifyNotification *notification;
75     vlc_mutex_t     lock;
76     bool            b_has_actions;
77 };
78
79 /*****************************************************************************
80  * Open: initialize and create stuff
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     intf_thread_t   *p_intf = (intf_thread_t *)p_this;
85     intf_sys_t      *p_sys  = malloc( sizeof( *p_sys ) );
86
87     if( !p_sys )
88         return VLC_ENOMEM;
89
90     if( !notify_init( APPLICATION_NAME ) )
91     {
92         free( p_sys );
93         msg_Err( p_intf, "can't find notification daemon" );
94         return VLC_EGENERIC;
95     }
96     p_intf->p_sys = p_sys;
97
98     vlc_mutex_init( &p_sys->lock );
99     p_sys->notification = NULL;
100     p_sys->b_has_actions = false;
101
102     GList *p_caps = notify_get_server_caps ();
103     if( p_caps )
104     {
105         for( GList *c = p_caps; c != NULL; c = c->next )
106         {
107             if( !strcmp( (char*)c->data, "actions" ) )
108             {
109                 p_sys->b_has_actions = true;
110                 break;
111             }
112         }
113         g_list_foreach( p_caps, (GFunc)g_free, NULL );
114         g_list_free( p_caps );
115     }
116
117     /* */
118     playlist_t *p_playlist = pl_Hold( p_intf );
119     var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
120     pl_Release( p_intf );
121
122     return VLC_SUCCESS;
123 }
124
125 /*****************************************************************************
126  * Close: destroy interface stuff
127  *****************************************************************************/
128 static void Close( vlc_object_t *p_this )
129 {
130     intf_thread_t   *p_intf = ( intf_thread_t* ) p_this;
131     intf_sys_t      *p_sys  = p_intf->p_sys;
132
133     playlist_t *p_playlist = pl_Hold( p_this );
134     var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
135     pl_Release( p_this );
136
137     if( p_sys->notification )
138         g_object_unref( p_sys->notification );
139
140     vlc_mutex_destroy( &p_sys->lock );
141     free( p_sys );
142     notify_uninit();
143 }
144
145 /*****************************************************************************
146  * ItemChange: Playlist item change callback
147  *****************************************************************************/
148 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
149                        vlc_value_t oldval, vlc_value_t newval, void *param )
150 {
151     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
152     char                psz_tmp[MAX_LENGTH];
153     char                psz_notify[MAX_LENGTH];
154     char                *psz_title      = NULL;
155     char                *psz_artist     = NULL;
156     char                *psz_album      = NULL;
157     char                *psz_arturl     = NULL;
158     input_thread_t      *p_input        =  playlist_CurrentInput(
159                                                     (playlist_t*) p_this );
160     intf_thread_t       *p_intf         = param;
161     intf_sys_t          *p_sys          = p_intf->p_sys;
162
163     if( !p_input )
164         return VLC_SUCCESS;
165
166     if( p_input->b_dead )
167     {
168         /* Not playing anything ... */
169         vlc_object_release( p_input );
170         return VLC_SUCCESS;
171     }
172     /*Wait a tad so the meta has been fetched*/
173     msleep( 1000*4 );
174
175     /* Playing something ... */
176     input_item_t *p_input_item = input_GetItem( p_input );
177     psz_artist = input_item_GetArtist( p_input_item );
178     psz_album = input_item_GetAlbum( p_input_item );
179     psz_title = input_item_GetTitle( p_input_item );
180     if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
181     {
182         free( psz_title );
183         psz_title = input_item_GetName( p_input_item );
184     }
185     if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
186     {  /* Not enough metadata ... */
187         free( psz_title );
188         free( psz_artist );
189         free( psz_album );
190         vlc_object_release( p_input );
191         return VLC_SUCCESS;
192     }
193     if( EMPTY_STR( psz_artist ) )
194     {
195         free( psz_artist );
196         psz_artist = NULL;
197     }
198     if( EMPTY_STR( psz_album ) )
199     {
200         free( psz_album );
201         psz_album = NULL;
202     }
203
204     if( psz_artist && psz_album )
205         snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s\n[%s]",
206                   psz_title, psz_artist, psz_album );
207     else if( psz_artist )
208         snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s",
209                   psz_title, psz_artist );
210     else
211         snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>", psz_title );
212
213     free( psz_title );
214     free( psz_artist );
215     free( psz_album );
216
217     GdkPixbuf *pix = NULL;
218     GError *p_error = NULL;
219
220     psz_arturl = input_item_GetArtURL( p_input_item );
221     vlc_object_release( p_input );
222
223     if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
224                 strlen( psz_arturl ) > 7 )
225     { /* scale the art to show it in notify popup */
226         gboolean b = TRUE;
227         pix = gdk_pixbuf_new_from_file_at_scale(
228                 (psz_arturl + 7), 72, 72, b, &p_error );
229         free( psz_arturl );
230     }
231     else /* else we show state-of-the art logo */
232     {
233         char *psz_pixbuf;
234         if( asprintf( &psz_pixbuf, "%s/vlc48x48.png", config_GetDataDir() ) >= 0 )
235         {
236             pix = gdk_pixbuf_new_from_file( psz_pixbuf, &p_error );
237             free( psz_pixbuf );
238         }
239     }
240
241     /* we need to replace '&' with '&amp;' because '&' is a keyword of
242      * notification-daemon parser */
243     int i_notify, i_len, i;
244     i_len = strlen( psz_tmp );
245     i_notify = 0;
246     for( i = 0; ( ( i < i_len ) && ( i_notify < ( MAX_LENGTH - 5 ) ) ); i++ )
247     { /* we use MAX_LENGTH - 5 because if the last char of psz_tmp is '&'
248        * we will need 5 more characters: 'amp;\0' .
249        * however that's unlikely to happen because the last char is '\0' */
250         if( psz_tmp[i] != '&' )
251             psz_notify[i_notify] = psz_tmp[i];
252         else
253         {
254             snprintf( psz_notify + i_notify, 6, "&amp;" );
255             i_notify += 4;
256         }
257         i_notify++;
258     }
259     psz_notify[i_notify] = '\0';
260
261     vlc_mutex_lock( &p_sys->lock );
262
263     Notify( p_this, psz_notify, pix, p_intf );
264
265     vlc_mutex_unlock( &p_sys->lock );
266
267     return VLC_SUCCESS;
268 }
269
270 static void Next( NotifyNotification *notification, gchar *psz, gpointer p )
271 { /* libnotify callback, called when the "Next" button is pressed */
272     VLC_UNUSED(psz);
273     notify_notification_close (notification, NULL);
274     playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p) );
275     playlist_Next( p_playlist );
276     pl_Release( ((vlc_object_t*) p) );
277 }
278
279 static void Prev( NotifyNotification *notification, gchar *psz, gpointer p )
280 { /* libnotify callback, called when the "Previous" button is pressed */
281     VLC_UNUSED(psz);
282     notify_notification_close (notification, NULL);
283     playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p) );
284     playlist_Prev( p_playlist );
285     pl_Release( ((vlc_object_t*) p) );
286 }
287
288 static int Notify( vlc_object_t *p_this, const char *psz_temp, GdkPixbuf *pix,
289                    intf_thread_t *p_intf )
290 {
291     intf_sys_t *p_sys = p_intf->p_sys;
292
293     NotifyNotification * notification;
294
295     /* Close previous notification if still active */
296     if( p_sys->notification )
297     {
298         GError *p_error = NULL;
299         notify_notification_close( p_sys->notification, &p_error );
300         g_object_unref( p_sys->notification );
301     }
302
303     notification = notify_notification_new( _("Now Playing"),
304             psz_temp, NULL, NULL);
305     notify_notification_set_timeout( notification,
306                                      config_GetInt(p_this, "notify-timeout") );
307     notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
308     if( pix )
309     {
310         notify_notification_set_icon_from_pixbuf( notification, pix );
311         gdk_pixbuf_unref( pix );
312     }
313
314     /* Adds previous and next buttons in the notification if actions are supported. */
315     if( p_sys->b_has_actions )
316     {
317       notify_notification_add_action( notification, "previous", _("Previous"), Prev,
318                                                       (gpointer*)p_intf, NULL );
319       notify_notification_add_action( notification, "next", _("Next"), Next,
320                                                       (gpointer*)p_intf, NULL );
321     }
322
323     notify_notification_show( notification, NULL);
324
325     /* Stores the notification to be able to close it */
326     p_sys->notification = notification;
327     return VLC_SUCCESS;
328 }
329