]> git.sesse.net Git - vlc/blob - modules/misc/notify/notify.c
notify: less includes.
[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  * Local prototypes
41  *****************************************************************************/
42 static int  Open    ( vlc_object_t * );
43 static void Close   ( vlc_object_t * );
44
45 static int ItemChange( vlc_object_t *, const char *,
46                        vlc_value_t, vlc_value_t, void * );
47 static int Notify( vlc_object_t *, const char *, GdkPixbuf *, intf_thread_t * );
48 #define MAX_LENGTH 256
49
50 struct intf_sys_t
51 {
52     NotifyNotification *notification;
53     vlc_mutex_t     lock;
54 };
55
56 /*****************************************************************************
57  * Module descriptor
58  ****************************************************************************/
59
60 #define APPLICATION_NAME "VLC media player"
61
62 #define TIMEOUT_TEXT N_("Timeout (ms)")
63 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")
64
65 vlc_module_begin ()
66     set_category( CAT_INTERFACE )
67     set_subcategory( SUBCAT_INTERFACE_CONTROL )
68     set_shortname( N_( "Notify" ) )
69     set_description( N_("LibNotify Notification Plugin") )
70
71     add_integer( "notify-timeout", 4000, NULL,
72                 TIMEOUT_TEXT, TIMEOUT_LONGTEXT, true )
73
74     set_capability( "interface", 0 )
75     set_callbacks( Open, Close )
76 vlc_module_end ()
77
78 /*****************************************************************************
79  * Open: initialize and create stuff
80  *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
82 {
83     intf_thread_t   *p_intf = (intf_thread_t *)p_this;
84     playlist_t      *p_playlist;
85     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
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
97     vlc_mutex_init( &p_sys->lock );
98
99     p_intf->p_sys = p_sys;
100
101     p_intf->p_sys->notification = NULL;
102
103     p_playlist = pl_Hold( p_intf );
104     var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
105     pl_Release( p_intf );
106
107     return VLC_SUCCESS;
108 }
109
110 /*****************************************************************************
111  * Close: destroy interface stuff
112  *****************************************************************************/
113 static void Close( vlc_object_t *p_this )
114 {
115     intf_thread_t   *p_intf = ( intf_thread_t* ) p_this;
116     intf_sys_t      *p_sys  = p_intf->p_sys;
117
118     playlist_t *p_playlist = pl_Hold( p_this );
119     var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
120     pl_Release( p_this );
121
122     if( p_intf->p_sys->notification )
123         g_object_unref( p_intf->p_sys->notification );
124
125     vlc_mutex_destroy( &p_sys->lock );
126     free( p_sys );
127     notify_uninit();
128 }
129
130 /*****************************************************************************
131  * ItemChange: Playlist item change callback
132  *****************************************************************************/
133 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
134                        vlc_value_t oldval, vlc_value_t newval, void *param )
135 {
136     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
137     char                psz_tmp[MAX_LENGTH];
138     char                psz_notify[MAX_LENGTH];
139     char                *psz_title      = NULL;
140     char                *psz_artist     = NULL;
141     char                *psz_album      = NULL;
142     char                *psz_arturl     = NULL;
143     input_thread_t      *p_input        =  playlist_CurrentInput(
144                                                     (playlist_t*) p_this );
145     intf_thread_t       *p_intf         = ( intf_thread_t* ) param;
146     intf_sys_t          *p_sys          = p_intf->p_sys;
147
148     if( !p_input ) return VLC_SUCCESS;
149
150     if( p_input->b_dead )
151     {
152         /* Not playing anything ... */
153         vlc_object_release( p_input );
154         return VLC_SUCCESS;
155     }
156     /*Wait a tad so the meta has been fetched*/
157     msleep( 1000*4 );
158
159     /* Playing something ... */
160     psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
161     psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
162     psz_title = input_item_GetTitle( input_GetItem( p_input ) );
163     if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
164     {
165         free( psz_title );
166         psz_title = input_item_GetName( input_GetItem( p_input ) );
167     }
168     if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
169     {  /* Not enough metadata ... */
170         free( psz_title );
171         free( psz_artist );
172         free( psz_album );
173         vlc_object_release( p_input );
174         return VLC_SUCCESS;
175     }
176     if( EMPTY_STR( psz_artist ) )
177     {
178         free( psz_artist );
179         psz_artist = NULL;
180     }
181     if( EMPTY_STR( psz_album ) )
182     {
183         free( psz_album );
184         psz_album = NULL;
185     }
186
187     vlc_object_release( p_input );
188
189     if( psz_artist && psz_album )
190         snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s\n[%s]",
191                   psz_title, psz_artist, psz_album );
192     else if( psz_artist )
193         snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s",
194                   psz_title, psz_artist );
195     else
196         snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>", psz_title );
197
198     free( psz_title );
199     free( psz_artist );
200     free( psz_album );
201
202     GdkPixbuf *pix = NULL;
203     GError *p_error = NULL;
204
205     psz_arturl = input_item_GetArtURL( input_GetItem( p_input ) );
206     if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
207                 strlen( psz_arturl ) > 7 )
208     { /* scale the art to show it in notify popup */
209         gboolean b = TRUE;
210         pix = gdk_pixbuf_new_from_file_at_scale(
211                 (psz_arturl + 7), 72, 72, b, &p_error );
212         free( psz_arturl );
213     }
214     else /* else we show state-of-the art logo */
215     {
216         char *psz_pixbuf;
217         if( asprintf( &psz_pixbuf, "%s/vlc48x48.png", config_GetDataDir() ) >= 0 )
218         {
219             pix = gdk_pixbuf_new_from_file( psz_pixbuf, &p_error );
220             free( psz_pixbuf );
221         }
222     }
223
224     /* we need to replace '&' with '&amp;' because '&' is a keyword of
225      * notification-daemon parser */
226     int i_notify, i_len, i;
227     i_len = strlen( psz_tmp );
228     i_notify = 0;
229     for( i = 0; ( ( i < i_len ) && ( i_notify < ( MAX_LENGTH - 5 ) ) ); i++ )
230     { /* we use MAX_LENGTH - 5 because if the last char of psz_tmp is '&'
231        * we will need 5 more characters: 'amp;\0' .
232        * however that's unlikely to happen because the last char is '\0' */
233         if( psz_tmp[i] != '&' )
234             psz_notify[i_notify] = psz_tmp[i];
235         else
236         {
237             snprintf( psz_notify + i_notify, 6, "&amp;" );
238             i_notify += 4;
239         }
240         i_notify++;
241     }
242     psz_notify[i_notify] = '\0';
243
244     vlc_mutex_lock( &p_sys->lock );
245
246     Notify( p_this, psz_notify, pix, p_intf );
247
248     vlc_mutex_unlock( &p_sys->lock );
249
250     return VLC_SUCCESS;
251 }
252
253 static void Next( NotifyNotification *notification, gchar *psz, gpointer p )
254 { /* libnotify callback, called when the "Next" button is pressed */
255     VLC_UNUSED(psz);
256     notify_notification_close (notification, NULL);
257     playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p) );
258     playlist_Next( p_playlist );
259     pl_Release( ((vlc_object_t*) p) );
260 }
261
262 static void Prev( NotifyNotification *notification, gchar *psz, gpointer p )
263 { /* libnotify callback, called when the "Previous" button is pressed */
264     VLC_UNUSED(psz);
265     notify_notification_close (notification, NULL);
266     playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p) );
267     playlist_Prev( p_playlist );
268     pl_Release( ((vlc_object_t*) p) );
269 }
270
271 static gboolean
272 can_support_actions ()
273 {
274     static gboolean supports_actions = FALSE;
275     static gboolean have_checked = FALSE;
276
277     if( !have_checked ) {
278         GList *caps = NULL;
279         GList *c;
280
281         have_checked = TRUE;
282
283         caps = notify_get_server_caps ();
284         if( caps != NULL ) {
285             for( c = caps; c != NULL; c = c->next ) {
286                 if( strcmp( (char*)c->data, "actions" ) == 0 ) {
287                     supports_actions = TRUE;
288                     break;
289                 }
290             }
291         }
292
293         g_list_foreach( caps, (GFunc)g_free, NULL );
294         g_list_free( caps );
295     }
296
297     return supports_actions;
298 }
299
300 static int Notify( vlc_object_t *p_this, const char *psz_temp, GdkPixbuf *pix,
301                    intf_thread_t *p_intf )
302 {
303     NotifyNotification * notification;
304     GError *p_error = NULL;
305
306     /* Close previous notification if still active */
307     if( p_intf->p_sys->notification )
308     {
309         notify_notification_close( p_intf->p_sys->notification, &p_error );
310         g_object_unref( p_intf->p_sys->notification );
311     }
312
313     notification = notify_notification_new( _("Now Playing"),
314             psz_temp, NULL, NULL);
315     notify_notification_set_timeout( notification,
316                                      config_GetInt(p_this, "notify-timeout") );
317     notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
318     if( pix )
319     {
320         notify_notification_set_icon_from_pixbuf( notification, pix );
321         gdk_pixbuf_unref( pix );
322     }
323
324     /* Adds previous and next buttons in the notification if actions are supported. */
325     if( can_support_actions() ) {
326       notify_notification_add_action( notification, "previous", _("Previous"), Prev,
327                                       (gpointer*) p_intf, NULL );
328       notify_notification_add_action( notification, "next", _("Next"), Next,
329                                       (gpointer*) p_intf, NULL );
330     }
331
332     notify_notification_show( notification, NULL);
333
334     /* Stores the notification to be able to close it */
335     p_intf->p_sys->notification = notification;
336     return VLC_SUCCESS;
337 }
338