]> git.sesse.net Git - vlc/blob - modules/misc/notify/telepathy.c
Typos
[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_threads_init_default();
110
111     dbus_error_init( &error );
112
113     /* connect to the session bus */
114     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
115     if( !p_conn )
116     {
117         msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
118                 error.message );
119         dbus_error_free( &error );
120         free( p_intf->p_sys );
121         return VLC_EGENERIC;
122     }
123     p_intf->p_sys->p_conn = p_conn;
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * Close: destroy interface stuff
129  *****************************************************************************/
130 static void Close( vlc_object_t *p_this )
131 {
132     intf_thread_t *p_intf = (intf_thread_t *)p_this;
133     playlist_t *p_playlist = pl_Yield( p_this );
134
135     /* Clears the Presence message ... else it looks like we're still playing
136      * something although VLC (or the Telepathy plugin) is closed */
137
138     /* Do not check for VLC_ENOMEM as we're closing */
139     SendToTelepathy( p_intf, "" );
140
141     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
142     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
143     pl_Release( p_this );
144
145     /* we won't use the DBus connection anymore */
146     dbus_connection_unref( p_intf->p_sys->p_conn );
147
148     /* Destroy structure */
149     free( p_intf->p_sys->psz_format );
150     free( p_intf->p_sys );
151 }
152
153 /*****************************************************************************
154  * ItemChange: Playlist item change callback
155  *****************************************************************************/
156 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
157                        vlc_value_t oldval, vlc_value_t newval, void *param )
158 {
159     intf_thread_t *p_intf = (intf_thread_t *)param;
160     char *psz_buf = NULL;
161     input_thread_t *p_input;
162
163     /* Don't update Telepathy presence each time an item has been preparsed */
164     if( !strncmp( "playlist-current", psz_var, 16 ) )
165     { /* stores the current input item id */
166         p_intf->p_sys->i_id = newval.i_int;
167     }
168     else if( newval.i_int != p_intf->p_sys->i_id ) /* "item-change" */
169         return VLC_SUCCESS;
170
171
172     playlist_t *p_playlist = pl_Yield( p_this );
173
174     p_input = p_playlist->p_input;
175     pl_Release( p_this );
176
177     if( !p_input ) return VLC_SUCCESS;
178     vlc_object_yield( p_input );
179
180     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
181     {
182         vlc_object_release( p_input );
183         /* Not playing anything ... */
184         switch( SendToTelepathy( p_intf, "" ) )
185         {
186             case VLC_ENOMEM:
187                 Close( p_this );
188                 return VLC_ENOMEM;
189             default:
190                 return VLC_SUCCESS;
191         }
192     }
193
194     /* We format the string to be displayed */
195     psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
196     /* We don't need the input anymore */
197     vlc_object_release( p_input );
198
199     if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
200     {
201         free( psz_buf );
202         Close( p_this );
203         return VLC_ENOMEM;
204     }
205     free( psz_buf );
206     return VLC_SUCCESS;
207 }
208
209 /*****************************************************************************
210  * SendToTelepathy
211  *****************************************************************************/
212 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
213 {
214     DBusConnection *p_conn;
215     DBusMessage *p_msg;
216     DBusMessage *p_reply;
217     DBusMessageIter args;
218     DBusError error;
219     dbus_error_init( &error );
220     dbus_uint32_t i_status;
221
222     p_conn = p_intf->p_sys->p_conn;
223
224     /* first we need to get the actual status */
225     p_msg = dbus_message_new_method_call(
226             "org.freedesktop.Telepathy.MissionControl",
227            "/org/freedesktop/Telepathy/MissionControl",
228             "org.freedesktop.Telepathy.MissionControl",
229             "GetPresence" );
230     if( !p_msg )
231         return VLC_ENOMEM;
232
233     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
234         50, &error ); /* blocks 50ms maximum */
235
236     dbus_message_unref( p_msg );
237     if( p_reply == NULL )
238     {   /* MC is not active, or too slow. Better luck next time? */
239         return VLC_SUCCESS;
240     }
241
242     /* extract the status from the reply */
243     if( dbus_message_get_args( p_reply, &error,
244             DBUS_TYPE_UINT32, &i_status,
245             DBUS_TYPE_INVALID ) == FALSE )
246     {
247         return VLC_ENOMEM;
248     }
249
250     p_msg = dbus_message_new_method_call(
251             "org.freedesktop.Telepathy.MissionControl",
252            "/org/freedesktop/Telepathy/MissionControl",
253             "org.freedesktop.Telepathy.MissionControl",
254             "SetPresence" );
255     if( !p_msg )
256         return VLC_ENOMEM;
257
258     dbus_message_iter_init_append( p_msg, &args );
259
260     /* first argument is the status */
261     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
262     {
263         dbus_message_unref( p_msg );
264         return VLC_ENOMEM;
265     }
266     /* second argument is the message */
267     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
268     {
269         dbus_message_unref( p_msg );
270         return VLC_ENOMEM;
271     }
272
273
274     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
275         return VLC_ENOMEM;
276
277     dbus_connection_flush( p_conn );
278     dbus_message_unref( p_msg );
279
280     return VLC_SUCCESS;
281 }