]> git.sesse.net Git - vlc/blob - modules/notify/telepathy.c
fix crash with directory access
[vlc] / modules / notify / telepathy.c
1 /*****************************************************************************
2  * telepathy.c : changes Telepathy Presence information using MissionControl
3  *****************************************************************************
4  * Copyright © 2007-2009 the VideoLAN team
5  * $Id$
6  *
7  * Author: Rafaël Carré <funman@videoanorg>
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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_strings.h>                                /* str_format_meta */
37
38 #include <dbus/dbus.h>
39
40 #define FORMAT_DEFAULT "$a - $t"
41 /*****************************************************************************
42  * intf_sys_t: description and status of log interface
43  *****************************************************************************/
44 struct intf_sys_t
45 {
46     char            *psz_format;
47     DBusConnection  *p_conn;
48     int             i_id;
49     int             i_item_changes;
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57
58 static int ItemChange( vlc_object_t *, const char *,
59                        vlc_value_t, vlc_value_t, void * );
60 static int StateChange( vlc_object_t *, const char *,
61                         vlc_value_t, vlc_value_t, void * );
62 static int SendToTelepathy( intf_thread_t *, const char * );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin ()
68     set_category( CAT_INTERFACE )
69     set_subcategory( SUBCAT_INTERFACE_CONTROL )
70     set_shortname( "Telepathy" )
71     set_description( N_("Telepathy \"Now Playing\" (MissionControl)") )
72
73     add_obsolete_string( "telepathy-format")
74
75     set_capability( "interface", 0 )
76     set_callbacks( Open, Close )
77 vlc_module_end ()
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     playlist_t *p_playlist;
86     DBusError       error;
87
88     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
89     if( !p_intf->p_sys )
90         return VLC_ENOMEM;
91
92     dbus_error_init( &error );
93
94     /* connect privately to the session bus
95      * the connection will not be shared with other vlc modules which use dbus,
96      * thus avoiding a whole class of concurrency issues */
97     p_intf->p_sys->p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
98     if( !p_intf->p_sys->p_conn )
99     {
100         msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
101                 error.message );
102         dbus_error_free( &error );
103         free( p_intf->p_sys );
104         return VLC_EGENERIC;
105     }
106
107     p_intf->p_sys->psz_format = var_InheritString( p_intf, "input-title-format" );
108     if( !p_intf->p_sys->psz_format )
109     {
110         p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
111     }
112     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
113
114     p_intf->p_sys->i_id = -1;
115
116     p_playlist = pl_Get( p_intf );
117     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
118     var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Close: destroy interface stuff
125  *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
127 {
128     intf_thread_t *p_intf = (intf_thread_t *)p_this;
129     playlist_t *p_playlist = pl_Get( p_this );
130     input_thread_t *p_input = NULL;
131
132     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
133     var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
134     if( (p_input = playlist_CurrentInput( p_playlist )) )
135     {
136         var_DelCallback( p_input, "state", StateChange, p_intf );
137         vlc_object_release( p_input );
138     }
139
140     /* Clears the Presence message ... else it looks like we're still playing
141      * something although VLC (or the Telepathy plugin) is closed */
142
143     /* Do not check for VLC_ENOMEM as we're closing */
144     SendToTelepathy( p_intf, "" );
145
146     /* The dbus connection is private,
147      * so we are responsible for closing it */
148     dbus_connection_close( p_intf->p_sys->p_conn );
149     dbus_connection_unref( p_intf->p_sys->p_conn );
150
151     /* Destroy structure */
152     free( p_intf->p_sys->psz_format );
153     free( p_intf->p_sys );
154 }
155
156 /*****************************************************************************
157  * ItemChange: Playlist item change callback
158  *****************************************************************************/
159 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
160                        vlc_value_t oldval, vlc_value_t newval, void *param )
161 {
162     VLC_UNUSED(oldval);
163     intf_thread_t *p_intf = (intf_thread_t *)param;
164     playlist_t* p_playlist = (playlist_t*) p_this;
165     char *psz_buf = NULL;
166     input_thread_t *p_input;
167     input_item_t *p_item = newval.p_address;
168     bool b_is_item_current = !strcmp( "item-current", psz_var );
169
170     /* Don't update Telepathy presence each time an item has been preparsed */
171     if( b_is_item_current )
172     { /* stores the current input item id */
173         p_intf->p_sys->i_id = p_item->i_id;
174         p_intf->p_sys->i_item_changes = 0;
175     }
176     else
177     {
178
179         if( p_item->i_id != p_intf->p_sys->i_id ) /* "item-change" */
180             return VLC_SUCCESS;
181         /* Some variable bitrate inputs call "item-change callbacks each time
182          * their length is updated, that is several times per second.
183          * We'll limit the number of changes to 10 per input. */
184         if( p_intf->p_sys->i_item_changes > 10 )
185             return VLC_SUCCESS;
186         p_intf->p_sys->i_item_changes++;
187     }
188
189     p_input = playlist_CurrentInput( p_playlist );
190
191     if( !p_input ) return VLC_SUCCESS;
192
193     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
194     {
195         vlc_object_release( p_input );
196         /* Not playing anything ... */
197         switch( SendToTelepathy( p_intf, "" ) )
198         {
199             case VLC_ENOMEM:
200                 return VLC_ENOMEM;
201             default:
202                 return VLC_SUCCESS;
203         }
204     }
205
206     if( b_is_item_current )
207         var_AddCallback( p_input, "state", StateChange, p_intf );
208
209     /* We format the string to be displayed */
210     psz_buf = str_format_meta( (vlc_object_t*) p_intf,
211             p_intf->p_sys->psz_format );
212
213     /* We don't need the input anymore */
214     vlc_object_release( p_input );
215
216     if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
217     {
218         free( psz_buf );
219         return VLC_ENOMEM;
220     }
221     free( psz_buf );
222     return VLC_SUCCESS;
223 }
224
225 /*****************************************************************************
226  * StateChange: State change callback
227  *****************************************************************************/
228 static int StateChange( vlc_object_t *p_this, const char *psz_var,
229                        vlc_value_t oldval, vlc_value_t newval, void *param )
230 {
231     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
232     intf_thread_t *p_intf = (intf_thread_t *)param;
233     if( newval.i_int >= END_S )
234         return SendToTelepathy( p_intf, "" );
235     return VLC_SUCCESS;
236 }
237
238 /*****************************************************************************
239  * SendToTelepathy
240  *****************************************************************************/
241 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
242 {
243     DBusConnection *p_conn;
244     DBusMessage *p_msg;
245     DBusMessage *p_reply;
246     DBusMessageIter args;
247     DBusError error;
248     dbus_error_init( &error );
249     dbus_uint32_t i_status;
250
251     p_conn = p_intf->p_sys->p_conn;
252
253     /* first we need to get the actual status */
254     p_msg = dbus_message_new_method_call(
255             "org.freedesktop.Telepathy.MissionControl",
256            "/org/freedesktop/Telepathy/MissionControl",
257             "org.freedesktop.Telepathy.MissionControl",
258             "GetPresence" );
259     if( !p_msg )
260         return VLC_ENOMEM;
261
262     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
263         50, &error ); /* blocks 50ms maximum */
264
265     if( dbus_error_is_set( &error ) )
266         dbus_error_free( &error );
267
268     dbus_message_unref( p_msg );
269     if( p_reply == NULL )
270     {   /* MC is not active, or too slow. Better luck next time? */
271         return VLC_SUCCESS;
272     }
273
274     /* extract the status from the reply */
275     if( dbus_message_get_args( p_reply, &error,
276             DBUS_TYPE_UINT32, &i_status,
277             DBUS_TYPE_INVALID ) == FALSE )
278     {
279         return VLC_ENOMEM;
280     }
281
282     p_msg = dbus_message_new_method_call(
283             "org.freedesktop.Telepathy.MissionControl",
284            "/org/freedesktop/Telepathy/MissionControl",
285             "org.freedesktop.Telepathy.MissionControl",
286             "SetPresence" );
287     if( !p_msg )
288         return VLC_ENOMEM;
289
290     dbus_message_iter_init_append( p_msg, &args );
291
292     /* first argument is the status */
293     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
294     {
295         dbus_message_unref( p_msg );
296         return VLC_ENOMEM;
297     }
298     /* second argument is the message */
299     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
300     {
301         dbus_message_unref( p_msg );
302         return VLC_ENOMEM;
303     }
304
305
306     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
307         return VLC_ENOMEM;
308
309     dbus_connection_flush( p_conn );
310     dbus_message_unref( p_msg );
311
312     return VLC_SUCCESS;
313 }