New Telepathy plugin, to customize user Presence with MissionControl
authorRafaël Carré <funman@videolan.org>
Mon, 17 Sep 2007 18:36:37 +0000 (18:36 +0000)
committerRafaël Carré <funman@videolan.org>
Mon, 17 Sep 2007 18:36:37 +0000 (18:36 +0000)
NEWS
configure.ac
modules/misc/notify/Modules.am
modules/misc/notify/telepathy.c [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 636091b..190b0b9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -123,6 +123,7 @@ Interfaces:
    * Motion module use disk accelerometers to keep video horizontal
    * Ncurses interface now uses ncursesw to correctly display wide characters
      when using an UTF-8 locale.
+   * Plugin to set Telepathy presence message using MissionControl
 
 Linux Port:
  * VLC now complies with the XDG Base Directory Specification version 0.6
index e42e610..700dd7d 100644 (file)
@@ -890,6 +890,14 @@ then
           VLC_ADD_PLUGINS([dbus])
           VLC_ADD_LDFLAGS([dbus],[$DBUS_LIBS])
           VLC_ADD_CFLAGS([dbus],[$DBUS_CFLAGS])
+        fi
+      dnl Check for Telepathy
+        AC_ARG_ENABLE(telepathy,
+          [  --enable-telepathy          Telepathy Presence plugin through DBus(default enabled)])
+        if test "${enable_telepathy}" != "no"; then
+          VLC_ADD_PLUGINS([telepathy])
+          VLC_ADD_LDFLAGS([telepathy],[$DBUS_LIBS])
+          VLC_ADD_CFLAGS([telepathy],[$DBUS_CFLAGS])
         fi],
 
     if ${PKG_CONFIG} --exists dbus-1
index 9931df5..be1730d 100644 (file)
@@ -2,3 +2,4 @@ SOURCES_msn = msn.c
 SOURCES_growl = growl.c
 SOURCES_notify = notify.c
 SOURCES_xosd = xosd.c
+SOURCES_telepathy = telepathy.c
diff --git a/modules/misc/notify/telepathy.c b/modules/misc/notify/telepathy.c
new file mode 100644 (file)
index 0000000..b964bc9
--- /dev/null
@@ -0,0 +1,268 @@
+/*****************************************************************************
+ * telepathy.c : changes Telepathy Presence information using MissionControl
+ *****************************************************************************
+ * Copyright © 2007 the VideoLAN team
+ * $Id$
+ *
+ * Author: Rafaël Carré <funman@videoanorg>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+
+#include <vlc/vlc.h>
+#include <vlc_interface.h>
+#include <vlc_meta.h>
+#include <vlc_playlist.h>
+#include <vlc_strings.h>
+#include <dbus/dbus.h>
+
+/*****************************************************************************
+ * intf_sys_t: description and status of log interface
+ *****************************************************************************/
+struct intf_sys_t
+{
+    char *psz_format;
+    DBusConnection  *p_conn;
+};
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int  Open    ( vlc_object_t * );
+static void Close   ( vlc_object_t * );
+
+static int ItemChange( vlc_object_t *, const char *,
+                       vlc_value_t, vlc_value_t, void * );
+static int SendToTelepathy( intf_thread_t *, const char * );
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+#define FORMAT_DEFAULT "$a - $t"
+#define FORMAT_TEXT N_("Title format string")
+#define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
+"Defaults to \"Artist - Title\" ($a - $t)." \
+"You can use the following substitutions: " \
+"$a Artist, $b Album, $c, Copyright, $d Description, $e Encoder, $g Genre, " \
+"$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
+"$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
+"$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
+"$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")
+
+vlc_module_begin();
+    set_category( CAT_INTERFACE );
+    set_subcategory( SUBCAT_INTERFACE_CONTROL );
+    set_shortname( "Telepathy" );
+    set_description( _("Telepathy « Now Playing » using MissionControl") );
+
+    add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
+                FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
+
+    set_capability( "interface", 0 );
+    set_callbacks( Open, Close );
+vlc_module_end();
+
+/*****************************************************************************
+ * Open: initialize and create stuff
+ *****************************************************************************/
+static int Open( vlc_object_t *p_this )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)p_this;
+    playlist_t *p_playlist;
+    DBusConnection  *p_conn;
+    DBusError       error;
+
+    MALLOC_ERR( p_intf->p_sys, intf_sys_t );
+
+    p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
+    if( !p_intf->p_sys->psz_format )
+    {
+        msg_Dbg( p_intf, "no format provided" );
+        p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
+    }
+    msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
+
+    p_playlist = pl_Yield( p_intf );
+    var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
+    var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
+    pl_Release( p_intf );
+
+    dbus_threads_init_default();
+
+    dbus_error_init( &error );
+
+    /* connect to the session bus */
+    p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
+    if( !p_conn )
+    {
+        msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
+                error.message );
+        dbus_error_free( &error );
+        free( p_intf->p_sys );
+        return VLC_EGENERIC;
+    }
+    p_intf->p_sys->p_conn = p_conn;
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Close: destroy interface stuff
+ *****************************************************************************/
+static void Close( vlc_object_t *p_this )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)p_this;
+    playlist_t *p_playlist = pl_Yield( p_this );
+
+    /* Clears the Presence message ... else it looks like we're still playing
+     * something although VLC (or the Telepathy plugin) is closed */
+
+    /* Do not check for VLC_ENOMEM as we're closing */
+    SendToTelepathy( p_intf, "" );
+
+    var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
+    var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
+    pl_Release( p_this );
+
+    /* we won't use the DBus connection anymore */
+    dbus_connection_unref( p_intf->p_sys->p_conn );
+
+    /* Destroy structure */
+    free( p_intf->p_sys->psz_format );
+    free( p_intf->p_sys );
+}
+
+/*****************************************************************************
+ * ItemChange: Playlist item change callback
+ *****************************************************************************/
+static int ItemChange( vlc_object_t *p_this, const char *psz_var,
+                       vlc_value_t oldval, vlc_value_t newval, void *param )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)param;
+    char *psz_buf = NULL;
+    input_thread_t *p_input;
+    playlist_t *p_playlist = pl_Yield( p_this );
+
+    p_input = p_playlist->p_input;
+    pl_Release( p_this );
+
+    if( !p_input ) return VLC_SUCCESS;
+    vlc_object_yield( p_input );
+
+    if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
+    {
+        vlc_object_release( p_input );
+        /* Not playing anything ... */
+        switch( SendToTelepathy( p_intf, "" ) )
+        {
+            case VLC_ENOMEM:
+                Close( p_this );
+                return VLC_ENOMEM;
+            default:
+                return VLC_SUCCESS;
+        }
+    }
+
+    /* We format the string to be displayed */
+    psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
+    /* We don't need the input anymore */
+    vlc_object_release( p_input );
+
+    if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
+    {
+        free( psz_buf );
+        Close( p_this );
+        return VLC_ENOMEM;
+    }
+    free( psz_buf );
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * SendToTelepathy
+ *****************************************************************************/
+static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
+{
+    DBusConnection *p_conn;
+    DBusMessage *p_msg;
+    DBusMessage *p_reply;
+    DBusMessageIter args;
+    DBusError error;
+    dbus_error_init( &error );
+    dbus_uint32_t i_status;
+
+    p_conn = p_intf->p_sys->p_conn;
+
+    /* first we need to get the actual status */
+    p_msg = dbus_message_new_method_call(
+            "org.freedesktop.Telepathy.MissionControl",
+           "/org/freedesktop/Telepathy/MissionControl",
+            "org.freedesktop.Telepathy.MissionControl",
+            "GetPresence" );
+    if( !p_msg )
+        return VLC_ENOMEM;
+
+    p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
+        1000, &error );
+
+    dbus_message_unref( p_msg );
+    if( p_reply == NULL )
+    {   /* MC is not active, or too slow. Better luck next time? */
+        return VLC_SUCCESS;
+    }
+
+    /* extract the status from the reply */
+    if( dbus_message_get_args( p_reply, &error,
+            DBUS_TYPE_UINT32, &i_status,
+            DBUS_TYPE_INVALID ) == FALSE )
+    {
+        return VLC_ENOMEM;
+    }
+
+    p_msg = dbus_message_new_method_call(
+            "org.freedesktop.Telepathy.MissionControl",
+           "/org/freedesktop/Telepathy/MissionControl",
+            "org.freedesktop.Telepathy.MissionControl",
+            "SetPresence" );
+    if( !p_msg )
+        return VLC_ENOMEM;
+
+    dbus_message_iter_init_append( p_msg, &args );
+
+    /* first argument is the status */
+    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
+    {
+        dbus_message_unref( p_msg );
+        return VLC_ENOMEM;
+    }
+    /* second argument is the message */
+    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
+    {
+        dbus_message_unref( p_msg );
+        return VLC_ENOMEM;
+    }
+
+
+    if( !dbus_connection_send( p_conn, p_msg, NULL ) )
+        return VLC_ENOMEM;
+
+    dbus_connection_flush( p_conn );
+    dbus_message_unref( p_msg );
+
+    return VLC_SUCCESS;
+}