]> git.sesse.net Git - vlc/blob - modules/misc/notify/notify.c
Fix
[vlc] / modules / misc / notify / notify.c
1 /*****************************************************************************
2  * notify.c : libnotify notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006 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 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>
29
30 #include <libnotify/notify.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34 #include <vlc_meta.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Open    ( vlc_object_t * );
40 static void Close   ( vlc_object_t * );
41 static void Run     ( intf_thread_t * );
42
43 static int ItemChange( vlc_object_t *, const char *,
44                        vlc_value_t, vlc_value_t, void * );
45 static int Notify( vlc_object_t *, const char * );
46 #define MAX_LENGTH 256
47
48 /*****************************************************************************
49  * Module descriptor
50  ****************************************************************************/
51
52
53 #define APPLICATION_NAME "VLC media player"
54
55 #define TIMEOUT_TEXT N_("Timeout (ms)")
56 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")
57
58 vlc_module_begin();
59     set_category( CAT_INTERFACE );
60     set_subcategory( SUBCAT_INTERFACE_CONTROL );
61     set_shortname( N_( "Notify" ) );
62     set_description( _("LibNotify Notification Plugin") );
63
64     add_integer( "notify-timeout", 4000,NULL,
65                 TIMEOUT_TEXT, TIMEOUT_LONGTEXT, VLC_TRUE );
66
67     set_capability( "interface", 0 );
68     set_callbacks( Open, Close );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Open: initialize and create stuff
73  *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
75 {
76     intf_thread_t *p_intf = (intf_thread_t *)p_this;
77     playlist_t *p_playlist;
78
79     if( !notify_init( APPLICATION_NAME ) )
80     {
81         msg_Err( p_intf, "can't find notification daemon" );
82         return VLC_EGENERIC;
83     }
84
85     p_playlist = pl_Yield( p_intf );
86     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
87     pl_Release( p_intf );
88
89     p_intf->pf_run = Run;
90     return VLC_SUCCESS;
91 }
92
93 /*****************************************************************************
94  * Close: destroy interface stuff
95  *****************************************************************************/
96 static void Close( vlc_object_t *p_this )
97 {
98     playlist_t *p_playlist = pl_Yield( p_this );
99     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
100     pl_Release( p_this );
101
102     notify_uninit();
103 }
104
105 /*****************************************************************************
106  * Run
107  *****************************************************************************/
108 static void Run( intf_thread_t *p_this )
109 {
110     msleep( 10*INTF_IDLE_SLEEP );
111 }
112
113 /*****************************************************************************
114  * ItemChange: Playlist item change callback
115  *****************************************************************************/
116 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
117                        vlc_value_t oldval, vlc_value_t newval, void *param )
118 {
119     char psz_tmp[MAX_LENGTH];
120     char *psz_title = NULL;
121     char *psz_artist = NULL;
122     char *psz_album = NULL;
123     input_thread_t *p_input=NULL;
124     playlist_t * p_playlist = pl_Yield( p_this );
125
126     p_input = p_playlist->p_input;
127     vlc_object_release( p_playlist );
128
129     if( !p_input ) return VLC_SUCCESS;
130     vlc_object_yield( p_input );
131
132     if( p_input->b_dead || !p_input->input.p_item->psz_name )
133     {
134         /* Not playing anything ... */
135         vlc_object_release( p_input );
136         return VLC_SUCCESS;
137     }
138
139     /* Playing something ... */
140     psz_artist = p_input->input.p_item->p_meta->psz_artist ?
141                   strdup( p_input->input.p_item->p_meta->psz_artist ) :
142                   strdup( _("no artist") );
143     psz_album = p_input->input.p_item->p_meta->psz_album ?
144                   strdup( p_input->input.p_item->p_meta->psz_album ) :
145                   strdup( _("no album") );
146     psz_title = strdup( p_input->input.p_item->psz_name );
147     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
148     snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s - %s",
149               psz_title, psz_artist, psz_album );
150     free( psz_title );
151     free( psz_artist );
152     free( psz_album );
153
154     Notify( p_this, psz_tmp );
155
156     vlc_object_release( p_input );
157     return VLC_SUCCESS;
158 }
159
160 static int Notify( vlc_object_t *p_this, const char *psz_temp )
161 {
162     NotifyNotification * notification;
163     notification = notify_notification_new( _("Now Playing"),
164                              psz_temp,
165                              DATA_PATH "/vlc128x128.png",NULL);
166     notify_notification_set_timeout( notification,
167                                      config_GetInt(p_this, "notify-timeout") );
168     notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
169     notify_notification_show( notification, NULL);
170     return VLC_SUCCESS;
171 }
172