qt: playlist: use item title if available
[vlc.git] / modules / notify / notify.c
blob138ec8225eb7c806d51a7c96e17d393941128e35
1 /*****************************************************************************
2 * notify.c : libnotify notification plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2009 the VideoLAN team
6 * Authors: Christophe Mutricy <xtophe -at- videolan -dot- org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
34 #include <vlc_player.h>
35 #include <vlc_playlist.h>
36 #include <vlc_url.h>
38 #include <gdk-pixbuf/gdk-pixbuf.h>
39 #include <libnotify/notify.h>
41 typedef struct GtkIconTheme GtkIconTheme;
42 enum GtkIconLookupFlags { dummy = 0x7fffffff };
44 VLC_WEAK GtkIconTheme *gtk_icon_theme_get_default(void);
45 VLC_WEAK GdkPixbuf *gtk_icon_theme_load_icon(GtkIconTheme *,
46 const char *icon_name, int size, enum GtkIconLookupFlags, GError **);
48 #ifndef NOTIFY_CHECK_VERSION
49 # define NOTIFY_CHECK_VERSION(x,y,z) 0
50 #endif
52 /*****************************************************************************
53 * Module descriptor
54 ****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
58 #define APPLICATION_NAME "VLC media player"
60 #define TIMEOUT_TEXT N_("Timeout (ms)")
61 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed.")
63 vlc_module_begin ()
64 set_category( CAT_INTERFACE )
65 set_subcategory( SUBCAT_INTERFACE_CONTROL )
66 set_shortname( N_( "Notify" ) )
67 set_description( N_("LibNotify Notification Plugin") )
69 add_integer( "notify-timeout", 4000,
70 TIMEOUT_TEXT, TIMEOUT_LONGTEXT, true )
72 set_capability( "interface", 0 )
73 set_callbacks( Open, Close )
74 vlc_module_end ()
77 /*****************************************************************************
78 * Local prototypes
79 *****************************************************************************/
80 static void on_current_media_changed(vlc_player_t *player,
81 input_item_t *new_media, void *data);
82 static int Notify( vlc_object_t *, const char *, GdkPixbuf *, intf_thread_t * );
83 #define MAX_LENGTH 256
85 struct intf_sys_t
87 NotifyNotification *notification;
88 vlc_mutex_t lock;
89 bool b_has_actions;
90 vlc_playlist_t *playlist;
91 struct vlc_player_listener_id *player_listener;
94 static void foreach_g_free(void *data, void *userdata)
96 g_free(data);
97 VLC_UNUSED(userdata);
100 /*****************************************************************************
101 * Open: initialize and create stuff
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
105 intf_thread_t *p_intf = (intf_thread_t *)p_this;
106 intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
108 if( !p_sys )
109 return VLC_ENOMEM;
111 p_sys->playlist = vlc_intf_GetMainPlaylist(p_intf);
112 vlc_player_t *player = vlc_playlist_GetPlayer(p_sys->playlist);
113 static const struct vlc_player_cbs player_cbs =
115 .on_current_media_changed = on_current_media_changed
117 vlc_player_Lock(player);
118 p_sys->player_listener = vlc_player_AddListener(player, &player_cbs, p_intf);
119 vlc_player_Unlock(player);
120 if (!p_sys->player_listener)
122 free(p_sys);
123 return VLC_EGENERIC;
126 if( !notify_init( APPLICATION_NAME ) )
128 vlc_player_Lock(player);
129 vlc_player_RemoveListener(player, p_sys->player_listener);
130 vlc_player_Unlock(player);
131 free( p_sys );
132 msg_Err( p_intf, "can't find notification daemon" );
133 return VLC_EGENERIC;
135 p_intf->p_sys = p_sys;
137 vlc_mutex_init( &p_sys->lock );
138 p_sys->notification = NULL;
139 p_sys->b_has_actions = false;
141 GList *p_caps = notify_get_server_caps ();
142 if( p_caps )
144 for( GList *c = p_caps; c != NULL; c = c->next )
146 if( !strcmp( (char*)c->data, "actions" ) )
148 p_sys->b_has_actions = true;
149 break;
152 g_list_foreach( p_caps, foreach_g_free, NULL );
153 g_list_free( p_caps );
156 return VLC_SUCCESS;
159 /*****************************************************************************
160 * Close: destroy interface stuff
161 *****************************************************************************/
162 static void Close( vlc_object_t *p_this )
164 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
165 intf_sys_t *p_sys = p_intf->p_sys;
167 vlc_player_t *player = vlc_playlist_GetPlayer(p_sys->playlist);
168 vlc_player_Lock(player);
169 vlc_player_RemoveListener(player, p_sys->player_listener);
170 vlc_player_Unlock(player);
172 if( p_sys->notification )
174 GError *p_error = NULL;
175 notify_notification_close( p_sys->notification, &p_error );
176 g_object_unref( p_sys->notification );
179 free( p_sys );
180 notify_uninit();
183 static void on_current_media_changed(vlc_player_t *player,
184 input_item_t *p_input_item, void *data)
186 (void) player;
187 intf_thread_t *p_intf = data;
188 intf_sys_t *p_sys = p_intf->p_sys;
189 char psz_tmp[MAX_LENGTH];
190 char psz_notify[MAX_LENGTH];
191 char *psz_title;
192 char *psz_artist;
193 char *psz_album;
194 char *psz_arturl;
196 if (!p_input_item)
197 return;
199 /* Checking for click on directories */
200 if(p_input_item->i_type == ITEM_TYPE_DIRECTORY || p_input_item->i_type == ITEM_TYPE_PLAYLIST
201 || p_input_item->i_type == ITEM_TYPE_NODE || p_input_item->i_type== ITEM_TYPE_UNKNOWN
202 || p_input_item->i_type == ITEM_TYPE_CARD){
203 return;
206 psz_title = input_item_GetTitleFbName( p_input_item );
207 /* We need at least a title */
208 if( EMPTY_STR( psz_title ) )
210 free( psz_title );
211 return;
214 psz_artist = input_item_GetArtist( p_input_item );
215 psz_album = input_item_GetAlbum( p_input_item );
217 if( !EMPTY_STR( psz_artist ) )
219 if( !EMPTY_STR( psz_album ) )
220 snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s\n[%s]",
221 psz_title, psz_artist, psz_album );
222 else
223 snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s",
224 psz_title, psz_artist );
226 else
227 snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>", psz_title );
229 free( psz_title );
230 free( psz_artist );
231 free( psz_album );
233 GdkPixbuf *pix = NULL;
234 psz_arturl = input_item_GetArtURL( p_input_item );
236 if( psz_arturl )
238 char *psz = vlc_uri2path( psz_arturl );
239 free( psz_arturl );
240 psz_arturl = psz;
243 if( psz_arturl )
244 { /* scale the art to show it in notify popup */
245 GError *p_error = NULL;
246 pix = gdk_pixbuf_new_from_file_at_scale( psz_arturl,
247 72, 72, TRUE, &p_error );
248 free( psz_arturl );
250 else
251 /* else we show state-of-the art logo */
252 if( gtk_icon_theme_get_default != NULL
253 && gtk_icon_theme_load_icon != NULL )
255 /* First try to get an icon from the current theme. */
256 GtkIconTheme* p_theme = gtk_icon_theme_get_default();
257 pix = gtk_icon_theme_load_icon( p_theme, "vlc", 72, 0, NULL);
259 else
260 { /* Load icon from share/ */
261 GError *p_error = NULL;
262 char *psz_pixbuf = config_GetSysPath(VLC_SYSDATA_DIR,
263 "icons/hicolor/48x48/"PACKAGE_NAME".png");
264 if (psz_pixbuf != NULL)
266 pix = gdk_pixbuf_new_from_file( psz_pixbuf, &p_error );
267 free( psz_pixbuf );
271 /* we need to replace '&' with '&amp;' because '&' is a keyword of
272 * notification-daemon parser */
273 const int i_len = strlen( psz_tmp );
274 int i_notify = 0;
275 for( int i = 0; i < i_len && i_notify < ( MAX_LENGTH - 5 ); i++ )
276 { /* we use MAX_LENGTH - 5 because if the last char of psz_tmp is '&'
277 * we will need 5 more characters: 'amp;\0' .
278 * however that's unlikely to happen because the last char is '\0' */
279 if( psz_tmp[i] != '&' )
281 psz_notify[i_notify] = psz_tmp[i];
283 else
285 strcpy( &psz_notify[i_notify], "&amp;" );
286 i_notify += 4;
288 i_notify++;
290 psz_notify[i_notify] = '\0';
292 vlc_mutex_lock( &p_sys->lock );
294 Notify( VLC_OBJECT(p_intf), psz_notify, pix, p_intf );
296 vlc_mutex_unlock( &p_sys->lock );
299 /* libnotify callback, called when the "Next" button is pressed */
300 static void Next( NotifyNotification *notification, gchar *psz, gpointer p )
302 intf_thread_t *p_intf = (intf_thread_t *)p;
303 intf_sys_t *p_sys = p_intf->p_sys;
305 VLC_UNUSED(psz);
306 notify_notification_close( notification, NULL );
307 vlc_playlist_Lock( p_sys->playlist );
308 vlc_playlist_Next( p_sys->playlist );
309 vlc_playlist_Unlock( p_sys->playlist );
312 /* libnotify callback, called when the "Previous" button is pressed */
313 static void Prev( NotifyNotification *notification, gchar *psz, gpointer p )
315 intf_thread_t *p_intf = (intf_thread_t *)p;
316 intf_sys_t *p_sys = p_intf->p_sys;
318 VLC_UNUSED(psz);
319 notify_notification_close( notification, NULL );
320 vlc_playlist_Lock( p_sys->playlist );
321 vlc_playlist_Prev( p_sys->playlist );
322 vlc_playlist_Unlock( p_sys->playlist );
325 static int Notify( vlc_object_t *p_this, const char *psz_temp, GdkPixbuf *pix,
326 intf_thread_t *p_intf )
328 intf_sys_t *p_sys = p_intf->p_sys;
330 NotifyNotification * notification;
332 /* Close previous notification if still active */
333 if( p_sys->notification )
335 GError *p_error = NULL;
336 notify_notification_close( p_sys->notification, &p_error );
337 g_object_unref( p_sys->notification );
340 notification = notify_notification_new( _("Now Playing"),
341 psz_temp, NULL
342 #if NOTIFY_CHECK_VERSION (0, 7, 0)
344 #else
345 , NULL );
346 #endif
347 notify_notification_set_timeout( notification,
348 var_InheritInteger(p_this, "notify-timeout") );
349 notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
350 if( pix )
352 notify_notification_set_icon_from_pixbuf( notification, pix );
353 g_object_unref( pix );
356 /* Adds previous and next buttons in the notification if actions are supported. */
357 if( p_sys->b_has_actions )
359 notify_notification_add_action( notification, "previous", _("Previous"), Prev,
360 (gpointer*)p_intf, NULL );
361 notify_notification_add_action( notification, "next", _("Next"), Next,
362 (gpointer*)p_intf, NULL );
365 notify_notification_show( notification, NULL);
367 /* Stores the notification to be able to close it */
368 p_sys->notification = notification;
369 return VLC_SUCCESS;