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