]> git.sesse.net Git - vlc/blob - modules/misc/notify/telepathy.c
telepathy: 10 calls to the "item-change" callback may be necessary to have the metada...
[vlc] / modules / misc / notify / telepathy.c
1 /*****************************************************************************
2  * telepathy.c : changes Telepathy Presence information using MissionControl
3  *****************************************************************************
4  * Copyright © 2007 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 #include <vlc/vlc.h>
29 #include <vlc_interface.h>
30 #include <vlc_meta.h>
31 #include <vlc_playlist.h>
32 #include <vlc_strings.h>
33 #include <dbus/dbus.h>
34
35 /*****************************************************************************
36  * intf_sys_t: description and status of log interface
37  *****************************************************************************/
38 struct intf_sys_t
39 {
40     char            *psz_format;
41     DBusConnection  *p_conn;
42     int             i_id;
43     int             i_item_changes;
44 };
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  Open    ( vlc_object_t * );
50 static void Close   ( vlc_object_t * );
51
52 static int ItemChange( vlc_object_t *, const char *,
53                        vlc_value_t, vlc_value_t, void * );
54 static int SendToTelepathy( intf_thread_t *, const char * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 #define FORMAT_DEFAULT "$a - $t"
60 #define FORMAT_TEXT N_("Title format string")
61 #define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
62 "Defaults to \"Artist - Title\" ($a - $t). " \
63 "You can use the following substitutions: " \
64 "$a Artist, $b Album, $c Copyright, $d Description, $e Encoder, $g Genre, " \
65 "$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
66 "$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
67 "$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
68 "$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")
69
70 vlc_module_begin();
71     set_category( CAT_INTERFACE );
72     set_subcategory( SUBCAT_INTERFACE_CONTROL );
73     set_shortname( "Telepathy" );
74     set_description( _("Telepathy \"Now Playing\" using MissionControl") );
75
76     add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
77                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
78
79     set_capability( "interface", 0 );
80     set_callbacks( Open, Close );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * Open: initialize and create stuff
85  *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
87 {
88     intf_thread_t *p_intf = (intf_thread_t *)p_this;
89     playlist_t *p_playlist;
90     DBusConnection  *p_conn;
91     DBusError       error;
92
93     MALLOC_ERR( p_intf->p_sys, intf_sys_t );
94
95     p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
96     if( !p_intf->p_sys->psz_format )
97     {
98         msg_Dbg( p_intf, "no format provided" );
99         p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
100     }
101     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
102
103     p_intf->p_sys->i_id = -1;
104
105     p_playlist = pl_Yield( p_intf );
106     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
107     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
108     pl_Release( p_intf );
109
110     dbus_error_init( &error );
111
112     /* connect to the session bus */
113     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
114     if( !p_conn )
115     {
116         msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
117                 error.message );
118         dbus_error_free( &error );
119         free( p_intf->p_sys );
120         return VLC_EGENERIC;
121     }
122     p_intf->p_sys->p_conn = p_conn;
123     return VLC_SUCCESS;
124 }
125
126 /*****************************************************************************
127  * Close: destroy interface stuff
128  *****************************************************************************/
129 static void Close( vlc_object_t *p_this )
130 {
131     intf_thread_t *p_intf = (intf_thread_t *)p_this;
132     playlist_t *p_playlist = pl_Yield( p_this );
133
134     /* Clears the Presence message ... else it looks like we're still playing
135      * something although VLC (or the Telepathy plugin) is closed */
136
137     /* Do not check for VLC_ENOMEM as we're closing */
138     SendToTelepathy( p_intf, "" );
139
140     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
141     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
142     pl_Release( p_this );
143
144     /* we won't use the DBus connection anymore */
145     dbus_connection_unref( p_intf->p_sys->p_conn );
146
147     /* Destroy structure */
148     free( p_intf->p_sys->psz_format );
149     free( p_intf->p_sys );
150 }
151
152 /*****************************************************************************
153  * ItemChange: Playlist item change callback
154  *****************************************************************************/
155 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
156                        vlc_value_t oldval, vlc_value_t newval, void *param )
157 {
158     intf_thread_t *p_intf = (intf_thread_t *)param;
159     char *psz_buf = NULL;
160     input_thread_t *p_input;
161
162     /* Don't update Telepathy presence each time an item has been preparsed */
163     if( !strncmp( "playlist-current", psz_var, 16 ) )
164     { /* stores the current input item id */
165         p_intf->p_sys->i_id = newval.i_int;
166         p_intf->p_sys->i_item_changes = 0;
167     }
168     else
169     {
170         if( newval.i_int != p_intf->p_sys->i_id ) /* "item-change" */
171             return VLC_SUCCESS;
172         /* Some variable bitrate inputs call "item-change callbacks each time
173          * their length is updated, that is several times per second.
174          * We'll limit the number of changes to 10 per input. */
175         if( p_intf->p_sys->i_item_changes > 10 )
176             return VLC_SUCCESS;
177         p_intf->p_sys->i_item_changes++;
178     }
179
180
181     playlist_t *p_playlist = pl_Yield( p_this );
182
183     p_input = p_playlist->p_input;
184     pl_Release( p_this );
185
186     if( !p_input ) return VLC_SUCCESS;
187     vlc_object_yield( p_input );
188
189     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
190     {
191         vlc_object_release( p_input );
192         /* Not playing anything ... */
193         switch( SendToTelepathy( p_intf, "" ) )
194         {
195             case VLC_ENOMEM:
196                 Close( p_this );
197                 return VLC_ENOMEM;
198             default:
199                 return VLC_SUCCESS;
200         }
201     }
202
203     /* We format the string to be displayed */
204     psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
205     /* We don't need the input anymore */
206     vlc_object_release( p_input );
207
208     if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
209     {
210         free( psz_buf );
211         Close( p_this );
212         return VLC_ENOMEM;
213     }
214     free( psz_buf );
215     return VLC_SUCCESS;
216 }
217
218 /*****************************************************************************
219  * SendToTelepathy
220  *****************************************************************************/
221 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
222 {
223     DBusConnection *p_conn;
224     DBusMessage *p_msg;
225     DBusMessage *p_reply;
226     DBusMessageIter args;
227     DBusError error;
228     dbus_error_init( &error );
229     dbus_uint32_t i_status;
230
231     p_conn = p_intf->p_sys->p_conn;
232
233     /* first we need to get the actual status */
234     p_msg = dbus_message_new_method_call(
235             "org.freedesktop.Telepathy.MissionControl",
236            "/org/freedesktop/Telepathy/MissionControl",
237             "org.freedesktop.Telepathy.MissionControl",
238             "GetPresence" );
239     if( !p_msg )
240         return VLC_ENOMEM;
241
242     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
243         50, &error ); /* blocks 50ms maximum */
244
245     dbus_message_unref( p_msg );
246     if( p_reply == NULL )
247     {   /* MC is not active, or too slow. Better luck next time? */
248         return VLC_SUCCESS;
249     }
250
251     /* extract the status from the reply */
252     if( dbus_message_get_args( p_reply, &error,
253             DBUS_TYPE_UINT32, &i_status,
254             DBUS_TYPE_INVALID ) == FALSE )
255     {
256         return VLC_ENOMEM;
257     }
258
259     p_msg = dbus_message_new_method_call(
260             "org.freedesktop.Telepathy.MissionControl",
261            "/org/freedesktop/Telepathy/MissionControl",
262             "org.freedesktop.Telepathy.MissionControl",
263             "SetPresence" );
264     if( !p_msg )
265         return VLC_ENOMEM;
266
267     dbus_message_iter_init_append( p_msg, &args );
268
269     /* first argument is the status */
270     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
271     {
272         dbus_message_unref( p_msg );
273         return VLC_ENOMEM;
274     }
275     /* second argument is the message */
276     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
277     {
278         dbus_message_unref( p_msg );
279         return VLC_ENOMEM;
280     }
281
282
283     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
284         return VLC_ENOMEM;
285
286     dbus_connection_flush( p_conn );
287     dbus_message_unref( p_msg );
288
289     return VLC_SUCCESS;
290 }