--- xfmedia-0.9.1/src/remote.c	2005-09-05 06:56:37.000000000 +0200
+++ xfmedia-svn-beta/src/remote.c	2006-11-13 00:02:37.000000000 +0100
@@ -37,6 +37,14 @@
 #include <unistd.h>
 #endif
 
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
 #ifdef HAVE_DBUS
 
 #ifndef DBUS_API_SUBJECT_TO_CHANGE  /* exo 0.2 sets this, but 0.3 doesn't */
@@ -55,6 +63,10 @@
 #define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER DBUS_SERVICE_REPLY_PRIMARY_OWNER
 #endif
 
+#ifndef DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
+#define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT DBUS_NAME_FLAG_DO_NOT_QUEUE
+#endif
+
 #endif  /* HAVE_DBUS */
 
 #include <libxfce4util/libxfce4util.h>
@@ -133,11 +145,20 @@
                     DBUS_TYPE_INVALID)
                 && filename && *filename)
         {
-            if(xfmedia_playlists_load(mwin->plist, filename))
-                msg_ret = dbus_message_new_method_return(message);
-            g_free(filename);
+            gchar *lcfn = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
+            
+            if(lcfn) {
+                if(xfmedia_playlists_load(mwin->plist, lcfn))
+                    msg_ret = dbus_message_new_method_return(message);
+                g_free(lcfn);
+            }
         } else
             dbus_error_free(&derr);
+
+#ifdef DBUS_USE_OLD_API
+        if(filename)
+            dbus_free(filename);
+#endif
         
         if(!msg_ret)
             msg_ret = dbus_message_new_error(message, XFMEDIA_DBUS_ERROR,
@@ -158,11 +179,20 @@
                     DBUS_TYPE_INVALID)
                 && filename && *filename)
         {
-            if(xfmedia_playlists_save(mwin->plist, filename, XFMEDIA_PLAYLIST_FORMAT_UNKNOWN))
-                msg_ret = dbus_message_new_method_return(message);
-            g_free(filename);
+            gchar *lcfn = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
+            
+            if(lcfn) {
+                if(xfmedia_playlists_save(mwin->plist, lcfn, XFMEDIA_PLAYLIST_FORMAT_UNKNOWN))
+                    msg_ret = dbus_message_new_method_return(message);
+                g_free(lcfn);
+            }
         } else
             dbus_error_free(&derr);
+
+#ifdef DBUS_USE_OLD_API
+        if(filename)
+            dbus_free(filename);
+#endif
         
         if(!msg_ret)
             msg_ret = dbus_message_new_error(message, XFMEDIA_DBUS_ERROR,
@@ -186,20 +216,26 @@
             gint length = -1;
             
             if(xfmedia_playlist_get(mwin->plist, idx, &title, &length, &filename)) {
-                if(!title)
-                    title = g_strdup(_("(no title)"));
-                msg_ret = dbus_message_new_method_return(message);
-                dbus_message_append_args(msg_ret,
+                gchar *utf8_fn = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
+                
+                if(utf8_fn) {
+                    if(!title)
+                        title = g_strdup(_("(no title)"));
+                    msg_ret = dbus_message_new_method_return(message);
+                    dbus_message_append_args(msg_ret,
 #ifdef DBUS_USE_OLD_API
-                        DBUS_TYPE_STRING, title,
-                        DBUS_TYPE_INT32, length,
-                        DBUS_TYPE_STRING, filename,
+                            DBUS_TYPE_STRING, title,
+                            DBUS_TYPE_INT32, length,
+                            DBUS_TYPE_STRING, utf8_fn,
 #else
-                        DBUS_TYPE_STRING, &title,
-                        DBUS_TYPE_INT32, &length,
-                        DBUS_TYPE_STRING, &filename,
+                            DBUS_TYPE_STRING, &title,
+                            DBUS_TYPE_INT32, &length,
+                            DBUS_TYPE_STRING, &utf8_fn,
 #endif
-                        DBUS_TYPE_INVALID);
+                            DBUS_TYPE_INVALID);
+                    
+                    g_free(utf8_fn);
+                }
             }
             g_free(title);
             g_free(filename);
@@ -256,32 +292,39 @@
                     DBUS_TYPE_INVALID)
                 && filename && *filename)
         {
-            gchar *name_utf8 = xfmedia_filename_to_name(filename);
-            if(idx < 0)
-                idx = xfmedia_playlist_append_entry(mwin->plist, name_utf8, -1, filename, FALSE);
-            else
-                idx = xfmedia_playlist_insert_entry(mwin->plist, idx, name_utf8, -1, filename, FALSE);
-            g_free(name_utf8);
+            gchar *lcfn = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
             
-            msg_ret = dbus_message_new_method_return(message);
-            dbus_message_append_args(msg_ret,
+            if(lcfn) {
+                gchar *name_utf8 = xfmedia_filename_to_name(filename);
+                if(idx < 0)
+                    idx = xfmedia_playlist_append_entry(mwin->plist, name_utf8, -1, lcfn, FALSE);
+                else
+                    idx = xfmedia_playlist_insert_entry(mwin->plist, idx, name_utf8, -1, lcfn, FALSE);
+                g_free(name_utf8);
+            
+                msg_ret = dbus_message_new_method_return(message);
+                dbus_message_append_args(msg_ret,
 #ifdef DBUS_USE_OLD_API
-                    DBUS_TYPE_INT32, idx,
+                        DBUS_TYPE_INT32, idx,
 #else
-                    DBUS_TYPE_INT32, &idx,
+                        DBUS_TYPE_INT32, &idx,
 #endif
-                    DBUS_TYPE_INVALID);
-            dbus_connection_send(connection, msg_ret, NULL);
-            dbus_message_unref(msg_ret);
-        } else {
+                        DBUS_TYPE_INVALID);
+
+                g_free(lcfn);
+            }
+        } 
+        
+        if(!msg_ret)
             msg_ret = dbus_message_new_error(message, XFMEDIA_DBUS_ERROR,
                     XFMEDIA_REMOTE_ADD_FILE);
-            dbus_connection_send(connection, msg_ret, NULL);
-            dbus_message_unref(msg_ret);
-        }
         
+        dbus_connection_send(connection, msg_ret, NULL);
+        dbus_message_unref(msg_ret);
+#ifdef DBUS_USE_OLD_API
         if(filename)
             dbus_free(filename);
+#endif
         
         ret = DBUS_HANDLER_RESULT_HANDLED;
     } else if(dbus_message_is_method_call(message, XFMEDIA_DBUS_INTERFACE,
@@ -416,27 +459,35 @@
            && (now_playing = xfmedia_playlist_entry_ref_get_index(mwin->cur_playing)) != -1
            && xfmedia_playlist_get(mwin->plist, now_playing, &title, &length, &filename)) 
         {
-            if(!title)
-                title = g_strdup(_("(no title)"));
+            gchar *utf8_fn = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
             
-            msg_ret = dbus_message_new_method_return(message);
-            dbus_message_append_args(msg_ret,
+            if(utf8_fn) {
+                if(!title)
+                    title = g_strdup(_("(no title)"));
+
+                msg_ret = dbus_message_new_method_return(message);
+                dbus_message_append_args(msg_ret,
 #ifdef DBUS_USE_OLD_API
-                    DBUS_TYPE_INT32, now_playing,
-                    DBUS_TYPE_STRING, title,
-                    DBUS_TYPE_INT32, length,
-                    DBUS_TYPE_STRING, filename,
+                        DBUS_TYPE_INT32, now_playing,
+                        DBUS_TYPE_STRING, title,
+                        DBUS_TYPE_INT32, length,
+                        DBUS_TYPE_STRING, utf8_fn,
 #else
-                    DBUS_TYPE_INT32, &now_playing,
-                    DBUS_TYPE_STRING, &title,
-                    DBUS_TYPE_INT32, &length,
-                    DBUS_TYPE_STRING, &filename,
+                        DBUS_TYPE_INT32, &now_playing,
+                        DBUS_TYPE_STRING, &title,
+                        DBUS_TYPE_INT32, &length,
+                        DBUS_TYPE_STRING, &utf8_fn,
 #endif
-                    DBUS_TYPE_INVALID);
-            
-            g_free(title);
+                        DBUS_TYPE_INVALID);
+                
+                g_free(utf8_fn);
+            }
+
             g_free(filename);
-        } else {
+            g_free(title);
+        }
+        
+        if(!msg_ret) {
             msg_ret = dbus_message_new_error(message, XFMEDIA_DBUS_ERROR,
                     XFMEDIA_REMOTE_NOW_PLAYING);
         }
@@ -461,6 +512,50 @@
     return ret;
 }
 
+static void
+xfmedia_remote_write_location_file(gint session_id)
+{
+    const gchar *env_vars[] = {
+        "DBUS_SESSION_BUS_ADDRESS",
+        "DBUS_SESSION_BUS_PID",
+        NULL,
+    };
+    FILE *fp;
+    gchar *filename;
+    gint i;
+    const gchar *value;
+    
+    g_return_if_fail(session_id >= 0);
+    
+    filename = g_strdup_printf("%s/xfmedia-remote-env.%d.%d", g_get_tmp_dir(),
+                               (gint)getuid(), session_id);
+    if(g_file_test(filename, G_FILE_TEST_EXISTS)) {
+        if(unlink(filename)) {
+            g_warning("Unable to remove '%s'", filename);
+            g_free(filename);
+            return;
+        }
+    }
+    
+    fp = fopen(filename, "w");
+    if(!fp) {
+        g_warning("Unable to create '%s'", filename);
+        g_free(filename);
+        return;
+    }
+    
+    for(i = 0; env_vars[i]; ++i) {
+        value = g_getenv(env_vars[i]);
+        if(value)
+            fprintf(fp, "%s=%s\n", env_vars[i], value);
+    }
+    
+    fclose(fp);
+    
+    chmod(filename, 0600);
+    g_free(filename);
+}
+
 gint
 xfmedia_remote_init(XfmediaMainwin *mwin)
 {
@@ -515,6 +610,7 @@
             &xfmedia_dbus_vtable, mwin))
     {
         DBG("Successfully registered remote with session id %d.", i);
+        xfmedia_remote_write_location_file(i);
         return i;
     }
     
@@ -537,8 +633,13 @@
         return;
     
     if(G_LIKELY(mwin->session_id >= 0)) {
-        gchar path_name[64];
-        g_snprintf(path_name, 64, XFMEDIA_DBUS_PATH_FMT, mwin->session_id);
+        gchar path_name[PATH_MAX];
+        
+        g_snprintf(path_name, PATH_MAX, "%s/xfmedia-remote-env.%d.%d",
+                   g_get_tmp_dir(), (gint)getuid(), mwin->session_id);
+        unlink(path_name);
+        
+        g_snprintf(path_name, PATH_MAX, XFMEDIA_DBUS_PATH_FMT, mwin->session_id);
         mwin->session_id = -1;
         dbus_connection_unregister_object_path(dbus_conn, path_name);
     }
