r4252: Bugfix: On 64-bit systems communication between ROX-Filer processes didn't
[rox-filer.git] / ROX-Filer / src / remote.c
blob357681102a6ea6fc7d7c4cb1bb9db3743dbf2773
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* This code is used to communicate between two copies of the filer:
23 * If the filer is run and the same version of the filer is already
24 * running on the same machine then the new copy simply asks the old
25 * one deal with it and quits.
28 #include "config.h"
30 #include <string.h>
32 #include <gdk/gdkx.h>
33 #include <X11/X.h>
34 #include <X11/Xatom.h>
35 #include <gtk/gtk.h>
36 #include <gtk/gtkinvisible.h>
37 #include <libxml/parser.h>
39 #include "global.h"
41 #include "main.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "run.h"
45 #include "remote.h"
46 #include "filer.h"
47 #include "pinboard.h"
48 #include "panel.h"
49 #include "action.h"
50 #include "type.h"
51 #include "display.h"
52 #include "xml.h"
53 #include "diritem.h"
55 static GdkAtom filer_atom; /* _ROX_FILER_EUID_VERSION_HOST */
56 static GdkAtom filer_atom_any; /* _ROX_FILER_EUID_HOST */
57 static GdkAtom xsoap; /* _XSOAP */
59 typedef struct _SOAP_call SOAP_call;
60 typedef xmlNodePtr (*SOAP_func)(GList *args);
62 struct _SOAP_call {
63 SOAP_func func;
64 gchar **required_args;
65 gchar **optional_args;
68 static GHashTable *rpc_calls = NULL; /* MethodName -> Function */
70 /* Static prototypes */
71 static GdkWindow *get_existing_ipc_window(void);
72 static gboolean get_ipc_property(GdkWindow *window, Window *r_xid);
73 static void soap_send(GtkWidget *from, GdkAtom prop, GdkWindow *dest);
74 static gboolean client_event(GtkWidget *window,
75 GdkEventClient *event,
76 gpointer data);
77 static void soap_done(GtkWidget *widget,
78 GdkEventProperty *event,
79 gpointer data);
80 static void soap_register(char *name, SOAP_func func, char *req, char *opt);
81 static xmlNodePtr soap_invoke(xmlNode *method);
83 static xmlNodePtr rpc_Version(GList *args);
84 static xmlNodePtr rpc_OpenDir(GList *args);
85 static xmlNodePtr rpc_CloseDir(GList *args);
86 static xmlNodePtr rpc_Examine(GList *args);
87 static xmlNodePtr rpc_Show(GList *args);
88 static xmlNodePtr rpc_Pinboard(GList *args);
89 static xmlNodePtr rpc_Panel(GList *args);
90 static xmlNodePtr rpc_Run(GList *args);
91 static xmlNodePtr rpc_Copy(GList *args);
92 static xmlNodePtr rpc_Move(GList *args);
93 static xmlNodePtr rpc_Link(GList *args);
94 static xmlNodePtr rpc_FileType(GList *args);
95 static xmlNodePtr rpc_Mount(GList *args);
96 static xmlNodePtr rpc_Unmount(GList *args);
98 static xmlNodePtr rpc_PanelAdd(GList *args);
99 static xmlNodePtr rpc_PanelRemove(GList *args);
100 static xmlNodePtr rpc_PinboardAdd(GList *args);
101 static xmlNodePtr rpc_PinboardRemove(GList *args);
102 static xmlNodePtr rpc_SetBackdrop(GList *args);
103 static xmlNodePtr rpc_SetBackdropApp(GList *args);
105 /****************************************************************
106 * EXTERNAL INTERFACE *
107 ****************************************************************/
110 /* Try to get an already-running filer to handle things (only if
111 * new_copy is FALSE); TRUE if we succeed.
112 * Create an IPC widget so that future filers can contact us.
114 gboolean remote_init(xmlDocPtr rpc, gboolean new_copy)
116 guchar *unique_id;
117 GdkWindow *existing_ipc_window;
118 GtkWidget *ipc_window;
119 Window xwindow;
121 /* xmlDocDump(stdout, rpc); */
123 rpc_calls = g_hash_table_new(g_str_hash, g_str_equal);
125 soap_register("Version", rpc_Version, NULL, NULL);
127 soap_register("Run", rpc_Run, "Filename", NULL);
128 soap_register("OpenDir", rpc_OpenDir, "Filename",
129 "Style,Details,Sort,Class,ID,Hidden,Filter");
130 soap_register("CloseDir", rpc_CloseDir, "Filename", NULL);
131 soap_register("Examine", rpc_Examine, "Filename", NULL);
132 soap_register("Show", rpc_Show, "Directory,Leafname", NULL);
134 soap_register("Pinboard", rpc_Pinboard, NULL, "Name");
135 soap_register("Panel", rpc_Panel, NULL, "Side,Name");
137 soap_register("FileType", rpc_FileType, "Filename", NULL);
139 soap_register("Copy", rpc_Copy, "From,To", "Leafname,Quiet");
140 soap_register("Move", rpc_Move, "From,To", "Leafname,Quiet");
141 soap_register("Link", rpc_Link, "From,To", "Leafname");
142 soap_register("Mount", rpc_Mount, "MountPoints", "OpenDir,Quiet");
143 soap_register("Unmount", rpc_Unmount, "MountPoints", "Quiet");
145 soap_register("SetBackdrop", rpc_SetBackdrop, "Filename,Style", NULL);
146 soap_register("SetBackdropApp", rpc_SetBackdropApp, "App", NULL);
147 soap_register("PinboardAdd", rpc_PinboardAdd, "Path,X,Y", "Label,Shortcut,Args");
148 soap_register("PinboardRemove", rpc_PinboardRemove, "Path", "Label");
149 soap_register("PanelAdd", rpc_PanelAdd, "Side,Path", "Label,After,Shortcut,Args");
150 soap_register("PanelRemove", rpc_PanelRemove, "Side,Path", "Label");
152 /* Look for a property on the root window giving the IPC window
153 * of an already-running copy of this version of the filer, running
154 * on the same machine and with the same euid.
156 unique_id = g_strdup_printf("_ROX_FILER_%d_%s_%s",
157 (int) euid, VERSION, our_host_name());
158 filer_atom = gdk_atom_intern(unique_id, FALSE);
159 g_free(unique_id);
161 xsoap = gdk_atom_intern("_XSOAP", FALSE);
163 /* If we find a running copy, we'll need a window to put the
164 * SOAP message in before sending.
165 * If there's no running copy, we'll need a window to receive
166 * future SOAP message events.
167 * Either way, we'll need a window for it...
169 ipc_window = gtk_invisible_new();
170 gtk_widget_realize(ipc_window);
172 XGrabServer(GDK_DISPLAY());
174 existing_ipc_window = new_copy ? NULL : get_existing_ipc_window();
175 if (existing_ipc_window)
177 xmlChar *mem;
178 int size;
180 XUngrabServer(GDK_DISPLAY());
182 xmlDocDumpMemory(rpc, &mem, &size);
183 g_return_val_if_fail(size > 0, FALSE);
185 /* Since Gtk might have selected this already, we'd
186 * better do it BEFORE changing the property.
188 gtk_widget_add_events(ipc_window, GDK_PROPERTY_CHANGE_MASK);
189 g_signal_connect(ipc_window, "property-notify-event",
190 G_CALLBACK(soap_done), GINT_TO_POINTER(xsoap));
192 gdk_property_change(ipc_window->window, xsoap,
193 gdk_x11_xatom_to_atom(XA_STRING), 8,
194 GDK_PROP_MODE_REPLACE, mem, size);
195 g_free(mem);
197 soap_send(ipc_window, xsoap, existing_ipc_window);
199 return TRUE;
202 xwindow = GDK_WINDOW_XWINDOW(ipc_window->window);
204 /* Make the IPC window contain a property pointing to
205 * itself - this can then be used to check that it really
206 * is an IPC window.
208 gdk_property_change(ipc_window->window, filer_atom,
209 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
210 GDK_PROP_MODE_REPLACE,
211 (void *) &xwindow, 1);
213 /* Get notified when we get a message */
214 g_signal_connect(ipc_window, "client-event",
215 G_CALLBACK(client_event), NULL);
217 /* Make the root window contain a pointer to the IPC window */
218 gdk_property_change(gdk_get_default_root_window(), filer_atom,
219 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
220 GDK_PROP_MODE_REPLACE,
221 (void *) &xwindow, 1);
223 XUngrabServer(GDK_DISPLAY());
225 /* Also have a property without the version number, for programs
226 * that are happy to talk to any version of the filer.
228 unique_id = g_strdup_printf("_ROX_FILER_%d_%s",
229 (int) euid, our_host_name());
230 filer_atom_any = gdk_atom_intern(unique_id, FALSE);
231 g_free(unique_id);
232 /* On the IPC window... */
233 gdk_property_change(ipc_window->window, filer_atom_any,
234 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
235 GDK_PROP_MODE_REPLACE,
236 (void *) &xwindow, 1);
237 /* ... and on the root */
238 gdk_property_change(gdk_get_default_root_window(), filer_atom_any,
239 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
240 GDK_PROP_MODE_REPLACE,
241 (void *) &xwindow, 1);
243 return FALSE;
246 /* Executes the RPC call(s) in the given SOAP message and returns
247 * the reply.
249 xmlDocPtr run_soap(xmlDocPtr soap)
251 xmlNodePtr body, node, rep_body, reply;
252 xmlDocPtr rep_doc = NULL;
254 g_return_val_if_fail(soap != NULL, NULL);
256 /* Make sure we don't quit before doing the whole list
257 * (there's a command that closes windows)
259 number_of_windows++;
261 node = xmlDocGetRootElement(soap);
262 if (!node->ns)
263 goto bad_soap;
265 if (strcmp(node->ns->href, SOAP_ENV_NS) != 0 &&
266 strcmp(node->ns->href, SOAP_ENV_NS_OLD) != 0)
267 goto bad_soap;
269 body = get_subnode(node, SOAP_ENV_NS, "Body");
270 if (!body)
271 body = get_subnode(node, SOAP_ENV_NS_OLD, "Body");
272 if (!body)
273 goto bad_soap;
275 for (node = body->xmlChildrenNode; node; node = node->next)
277 if (node->type != XML_ELEMENT_NODE)
278 continue;
280 if (node->ns == NULL || strcmp(node->ns->href, ROX_NS) != 0)
282 g_warning("Unknown namespace %s",
283 node->ns ? node->ns->href
284 : (xmlChar *) "(none)");
285 continue;
288 reply = soap_invoke(node);
290 if (reply)
292 if (!rep_doc)
293 rep_doc = soap_new(&rep_body);
294 xmlAddChild(rep_body, reply);
298 goto out;
300 bad_soap:
301 g_warning("Bad SOAP message received!");
303 out:
304 number_of_windows--;
306 return rep_doc;
310 /****************************************************************
311 * INTERNAL FUNCTIONS *
312 ****************************************************************/
314 /* Register this function to handle SOAP calls to method 'name'.
315 * 'req' and 'opt' are comma separated lists of argument names, in the order
316 * that they are to be delivered to the function.
317 * NULL will be passed for an opt argument if not given.
318 * Otherwise, the parameter is the xmlNode from the call.
320 static void soap_register(char *name, SOAP_func func, char *req, char *opt)
322 SOAP_call *call;
324 call = g_new(SOAP_call, 1);
325 call->func = func;
326 call->required_args = req ? g_strsplit(req, ",", 0) : NULL;
327 call->optional_args = opt ? g_strsplit(opt, ",", 0) : NULL;
329 g_hash_table_insert(rpc_calls, g_strdup(name), call);
332 /* Get the remote IPC window of the already-running filer if there
333 * is one.
335 static GdkWindow *get_existing_ipc_window(void)
337 Window xid, xid_confirm;
338 GdkWindow *window;
340 if (!get_ipc_property(gdk_get_default_root_window(), &xid))
341 return NULL;
343 if (gdk_window_lookup(xid))
344 return NULL; /* Stale handle which we now own */
346 window = gdk_window_foreign_new(xid);
347 if (!window)
348 return NULL;
350 if (!get_ipc_property(window, &xid_confirm) || xid_confirm != xid)
351 return NULL;
353 return window;
356 /* Returns the 'rox_atom' property of 'window' */
357 static gboolean get_ipc_property(GdkWindow *window, Window *r_xid)
359 guchar *data;
360 gint format, length;
361 gboolean retval = FALSE;
363 if (gdk_property_get(window, filer_atom,
364 gdk_x11_xatom_to_atom(XA_WINDOW), 0, 4,
365 FALSE, NULL, &format, &length, &data) && data)
367 /* Note: values with format=32 are stored as longs client-side,
368 * which may be more than 32 bits on some systems.
370 if (format == 32 && length >= 4)
372 retval = TRUE;
373 *r_xid = *((Window *) data);
375 g_free(data);
378 return retval;
381 static char *read_property(GdkWindow *window, GdkAtom prop, gint *out_length)
383 gint grab_len = 4096;
384 gint length;
385 guchar *data;
386 guchar *retval = NULL;
388 gdk_error_trap_push();
390 while (!retval)
392 if (!(gdk_property_get(window, prop,
393 gdk_x11_xatom_to_atom(XA_STRING), 0, grab_len,
394 FALSE, NULL, NULL,
395 &length, &data) && data))
396 goto out; /* Error? */
398 if (length >= grab_len)
400 /* Didn't get all of it - try again */
401 grab_len <<= 1;
402 g_free(data);
403 continue;
406 data = g_realloc(data, length + 1);
407 data[length] = '\0'; /* libxml seems to need this */
408 *out_length = length;
410 retval = data;
412 out:
414 if (gdk_error_trap_pop() == Success)
415 return retval;
417 g_warning("Error reading %s property!", gdk_atom_name(prop));
419 g_free(retval);
420 return NULL;
423 static gboolean client_event(GtkWidget *window,
424 GdkEventClient *event,
425 gpointer user_data)
427 GdkWindow *src_window;
428 GdkAtom prop;
429 xmlDocPtr reply;
430 guchar *data;
431 gint length;
432 xmlDocPtr doc;
434 if (event->message_type != xsoap)
435 return FALSE;
437 src_window = gdk_window_foreign_new(event->data.l[0]);
438 if (!src_window)
440 g_warning("SOAP message sender window was destroyed before I \n"
441 "could read it.");
442 return FALSE;
444 prop = gdk_x11_xatom_to_atom(event->data.l[1]);
446 data = read_property(src_window, prop, &length);
447 if (!data)
448 return TRUE;
450 doc = xmlParseMemory(g_strndup(data, length), length);
451 g_free(data);
453 reply = run_soap(doc);
454 if (number_of_windows == 0)
455 gtk_main_quit();
457 xmlFreeDoc(doc);
459 if (reply)
461 /* Send reply back... */
462 xmlChar *mem;
463 int size;
465 xmlDocDumpMemory(reply, &mem, &size);
466 g_return_val_if_fail(size > 0, TRUE);
468 gdk_error_trap_push();
469 gdk_property_change(src_window, prop,
470 gdk_x11_xatom_to_atom(XA_STRING), 8,
471 GDK_PROP_MODE_REPLACE, mem, size);
472 g_free(mem);
473 gdk_flush();
474 if (gdk_error_trap_pop() != Success)
475 g_warning("Failed to send SOAP reply!");
477 else
479 gdk_error_trap_push();
480 gdk_property_delete(src_window, prop);
481 gdk_flush();
482 if (gdk_error_trap_pop() != Success)
483 g_warning("Failed to send SOAP reply!");
486 return TRUE;
489 /* Some handy functions for processing SOAP RPC arguments... */
491 /* Returns TRUE, FALSE, or -1 (if argument is unspecified) */
492 static int bool_value(xmlNode *arg)
494 int answer;
495 char *optval;
497 if (!arg)
498 return -1;
500 optval = xmlNodeGetContent(arg);
501 answer = text_to_boolean(optval, -1); /* XXX: Report error? */
502 g_free(optval);
504 return answer;
507 /* Returns the text of this arg as a string, or NULL if the (optional)
508 * argument wasn't supplied. Returns "" if the arg is empty.
509 * g_free() the result.
511 static char *string_value(xmlNode *arg)
513 char *retval;
515 if (!arg)
516 return NULL;
518 retval = xmlNodeGetContent(arg);
520 return retval ? retval : g_strdup("");
523 /* Returns the text of this arg as an int, or the default value if not
524 * supplied or not an int.
526 static int int_value(xmlNode *arg, int def)
528 char *str, *end;
529 int i;
531 if (!arg)
532 return def;
534 str = xmlNodeGetContent(arg);
535 if (!str || !str[0])
536 return def;
538 i = (int) strtol(str, &end, 0);
540 return (end > str) ? i : def;
543 /* Return a list of strings, one for each child node of arg.
544 * g_list_free the list, and g_free each string.
546 static GList *list_value(xmlNode *arg)
548 GList *list = NULL;
549 xmlNode *node;
551 for (node = arg->xmlChildrenNode; node; node = node->next)
553 if (node->type != XML_ELEMENT_NODE)
554 continue;
556 list = g_list_append(list, string_value(node));
559 return list;
562 #define ARG(n) ((xmlNode *) g_list_nth(args, n)->data)
564 /* The RPC handlers all work in the same way -- they get passed a list of
565 * xmlNode arguments from the RPC call and they return the result node, or
566 * NULL if there isn't a result.
569 static xmlNodePtr rpc_Version(GList *args)
571 xmlNodePtr reply;
573 reply = xmlNewNode(NULL, "rox:VersionResponse");
574 xmlNewNs(reply, SOAP_RPC_NS, "soap");
575 xmlNewTextChild(reply, NULL, "soap:result", VERSION);
577 return reply;
580 /* Args: Path, [Style, Details, Sort, Class, Window] */
581 static xmlNodePtr rpc_OpenDir(GList *args)
583 char *path;
584 char *style, *details, *sort, *class, *window;
585 int hidden=FALSE;
586 char *filter_string=NULL;
587 FilerWindow *fwin = NULL;
589 path = string_value(ARG(0));
590 class = string_value(ARG(4));
591 window = string_value(ARG(5));
593 if (window)
594 fwin = filer_get_by_id(window);
596 if (!fwin)
598 fwin = filer_opendir(path, NULL, class);
599 if (window)
600 filer_set_id(fwin, window);
602 else
603 filer_change_to(fwin, path, NULL);
605 g_free(path);
606 g_free(class);
607 g_free(window);
608 if (!fwin)
609 return NULL;
611 style = string_value(ARG(1));
612 details = string_value(ARG(2));
613 sort = string_value(ARG(3));
614 hidden = bool_value(ARG(6));
615 filter_string = string_value(ARG(7));
617 if (style)
619 DisplayStyle ds;
621 ds = !g_strcasecmp(style, "Large") ? LARGE_ICONS :
622 !g_strcasecmp(style, "Small") ? SMALL_ICONS :
623 !g_strcasecmp(style, "Huge") ? HUGE_ICONS :
624 !g_strcasecmp(style, "Automatic") ? AUTO_SIZE_ICONS :
625 UNKNOWN_STYLE;
626 if (ds == UNKNOWN_STYLE)
627 delayed_error(_("Unknown style '%s'"), style);
628 else
629 display_set_layout(fwin, ds, fwin->details_type, TRUE);
631 g_free(style);
634 if (details)
636 DetailsType dt;
637 ViewType view_type;
639 dt = !g_strcasecmp(details, "None") ? DETAILS_NONE :
640 !g_strcasecmp(details, "ListView") ? DETAILS_NONE :
641 !g_strcasecmp(details, "Size") ? DETAILS_SIZE :
642 !g_strcasecmp(details, "Type") ? DETAILS_TYPE :
643 !g_strcasecmp(details, "Times") ? DETAILS_TIMES :
644 !g_strcasecmp(details, "Permissions")
645 ? DETAILS_PERMISSIONS :
646 DETAILS_UNKNOWN;
648 if (dt == DETAILS_UNKNOWN)
649 delayed_error(_("Unknown details type '%s'"), details);
650 else
651 display_set_layout(fwin, fwin->display_style_wanted,
652 dt, TRUE);
654 if (g_strcasecmp(details, "ListView") == 0)
655 view_type = VIEW_TYPE_DETAILS;
656 else
657 view_type = VIEW_TYPE_COLLECTION;
659 if (view_type != fwin->view_type)
660 filer_set_view_type(fwin, view_type);
662 g_free(details);
665 if (sort)
667 SortType type;
669 type = !g_strcasecmp(sort, "Name") ? SORT_NAME :
670 !g_strcasecmp(sort, "Type") ? SORT_TYPE :
671 !g_strcasecmp(sort, "Date") ? SORT_DATE :
672 !g_strcasecmp(sort, "Size") ? SORT_SIZE :
673 !g_strcasecmp(sort, "Owner") ? SORT_OWNER :
674 !g_strcasecmp(sort, "Group") ? SORT_GROUP :
676 if (type == -1)
677 delayed_error(_("Unknown sorting type '%s'"), sort);
678 else
679 display_set_sort_type(fwin, type, GTK_SORT_ASCENDING);
681 g_free(sort);
683 if (hidden!=-1)
685 display_set_hidden(fwin, hidden);
688 if (filter_string)
689 display_set_filter(fwin, FILER_SHOW_GLOB, filter_string);
690 else
691 display_set_filter(fwin, FILER_SHOW_ALL, NULL);
693 return NULL;
696 static xmlNodePtr rpc_Run(GList *args)
698 char *path;
700 path = string_value(ARG(0));
701 run_by_path(path);
702 g_free(path);
704 return NULL;
707 static xmlNodePtr rpc_CloseDir(GList *args)
709 char *path;
711 path = string_value(ARG(0));
712 filer_close_recursive(path);
713 g_free(path);
715 return NULL;
718 static xmlNodePtr rpc_Examine(GList *args)
720 char *path;
722 path = string_value(ARG(0));
723 examine(path);
724 g_free(path);
726 return NULL;
729 static xmlNodePtr rpc_Show(GList *args)
731 char *dir, *leaf;
733 dir = string_value(ARG(0));
734 leaf = string_value(ARG(1));
736 /* XXX: Seems silly to join them only to split again later... */
737 open_to_show(make_path(dir, leaf));
739 g_free(dir);
740 g_free(leaf);
742 return NULL;
745 static xmlNodePtr rpc_Pinboard(GList *args)
747 char *name = NULL;
749 name = string_value(ARG(0));
750 pinboard_activate(name);
751 g_free(name);
753 return NULL;
756 /* args = Filename, Style */
757 static xmlNodePtr rpc_SetBackdrop(GList *args)
759 char *file;
760 char *style;
761 BackdropStyle s;
763 file = string_value(ARG(0));
764 style = string_value(ARG(1));
766 s = !g_strcasecmp(style, "Tile") ? BACKDROP_TILE :
767 !g_strcasecmp(style, "Scale") ? BACKDROP_SCALE :
768 !g_strcasecmp(style, "Stretch") ? BACKDROP_STRETCH :
769 !g_strcasecmp(style, "Centre") ? BACKDROP_CENTRE :
770 BACKDROP_NONE;
772 if (s == BACKDROP_NONE)
773 g_warning("Invalid style '%s' for backdrop", style);
774 else
775 pinboard_set_backdrop(file, s);
777 g_free(file);
778 g_free(style);
780 return NULL;
783 /* args = App */
784 static xmlNodePtr rpc_SetBackdropApp(GList *args)
786 char *app;
788 app = string_value(ARG(0));
790 pinboard_set_backdrop_app(app);
792 g_free(app);
794 return NULL;
797 /* args = Path, X, Y, [Label] */
798 static xmlNodePtr rpc_PinboardAdd(GList *args)
800 char *path = NULL;
801 gchar *name, *shortcut, *xargs;
802 int x, y;
804 path = string_value(ARG(0));
805 x = int_value(ARG(1), 0);
806 y = int_value(ARG(2), 0);
807 name = string_value(ARG(3));
808 shortcut = string_value(ARG(4));
809 xargs = string_value(ARG(5));
811 pinboard_pin_with_args(path, name, x, y, shortcut, xargs);
813 g_free(path);
814 g_free(name);
815 g_free(shortcut);
816 g_free(xargs);
818 return NULL;
821 /* args = Path, [Label] */
822 static xmlNodePtr rpc_PinboardRemove(GList *args)
824 char *path = NULL;
825 gchar *name;
827 path = string_value(ARG(0));
828 name = string_value(ARG(1));
830 pinboard_remove(path, name);
832 g_free(path);
833 if(name)
834 g_free(name);
836 return NULL;
839 /* args = Side, [Name] */
840 static xmlNodePtr rpc_Panel(GList *args)
842 PanelSide side;
843 char *name, *side_name;
845 side_name = string_value(ARG(0));
846 if (side_name)
848 side = panel_name_to_side(side_name);
849 g_free(side_name);
850 g_return_val_if_fail(side != PANEL_NUMBER_OF_SIDES, NULL);
852 else
853 side = PANEL_DEFAULT_SIDE;
855 name = string_value(ARG(1));
857 panel_new(name, side);
859 g_free(name);
861 return NULL;
864 /* args = Side, Path, [Label, After] */
865 static xmlNodePtr rpc_PanelAdd(GList *args)
867 PanelSide side;
868 char *path, *side_name, *label, *shortcut, *arg;
869 gboolean after = FALSE;
870 int tmp;
872 side_name = string_value(ARG(0));
873 side = panel_name_to_side(side_name);
874 g_free(side_name);
875 g_return_val_if_fail(side != PANEL_NUMBER_OF_SIDES, NULL);
877 path = string_value(ARG(1));
878 label = string_value(ARG(2));
880 tmp = bool_value(ARG(3));
881 after = (tmp == -1) ? FALSE : tmp;
883 shortcut = string_value(ARG(4));
884 arg = string_value(ARG(5));
886 panel_add(side, path, label, after, shortcut, arg);
888 g_free(path);
889 g_free(label);
890 g_free(shortcut);
891 g_free(arg);
893 return NULL;
896 static xmlNodePtr rpc_PanelRemove(GList *args)
898 PanelSide side;
899 char *path, *side_name, *label;
901 side_name = string_value(ARG(0));
902 side = panel_name_to_side(side_name);
903 g_free(side_name);
904 g_return_val_if_fail(side != PANEL_NUMBER_OF_SIDES, NULL);
906 path = string_value(ARG(1));
907 label = string_value(ARG(2));
909 panel_remove_item(side, path, label);
911 g_free(path);
912 g_free(label);
914 return NULL;
917 static xmlNodePtr rpc_Copy(GList *args)
919 GList *from;
920 char *to;
921 char *leaf;
922 int quiet;
924 from = list_value(ARG(0));
925 to = string_value(ARG(1));
926 leaf = string_value(ARG(2));
927 quiet = bool_value(ARG(3));
929 if (from)
930 action_copy(from, to, leaf, quiet);
931 else
932 g_warning("No files in SOAP request list");
934 destroy_glist(&from);
935 g_free(to);
936 g_free(leaf);
938 return NULL;
941 static xmlNodePtr rpc_Move(GList *args)
943 GList *from;
944 char *to;
945 char *leaf;
946 int quiet;
948 from = list_value(ARG(0));
949 to = string_value(ARG(1));
950 leaf = string_value(ARG(2));
951 quiet = bool_value(ARG(3));
953 if (from)
954 action_move(from, to, leaf, quiet);
955 else
956 g_warning("No files in SOAP request list");
958 destroy_glist(&from);
959 g_free(to);
960 g_free(leaf);
962 return NULL;
965 static xmlNodePtr rpc_Link(GList *args)
967 GList *from;
968 char *to;
969 char *leaf;
971 from = list_value(ARG(0));
972 to = string_value(ARG(1));
973 leaf = string_value(ARG(2));
975 if (from)
976 action_link(from, to, leaf, TRUE);
977 else
978 g_warning("No files in SOAP request list");
980 destroy_glist(&from);
981 g_free(to);
982 g_free(leaf);
984 return NULL;
987 static xmlNodePtr rpc_FileType(GList *args)
989 MIME_type *type;
990 char *path, *tname;
991 xmlNodePtr reply;
993 path = string_value(ARG(0));
994 type = type_get_type(path);
995 g_free(path);
997 reply = xmlNewNode(NULL, "rox:FileTypeResponse");
998 tname = g_strconcat(type->media_type, "/", type->subtype, NULL);
1000 xmlNewNs(reply, SOAP_RPC_NS, "soap");
1001 xmlNewTextChild(reply, NULL, "soap:result", tname);
1002 g_free(tname);
1004 return reply;
1007 static xmlNodePtr rpc_Mount(GList *args)
1009 GList *paths;
1010 int open_dir, quiet;
1012 paths = list_value(ARG(0));
1013 open_dir = bool_value(ARG(1));
1014 quiet = bool_value(ARG(2));
1016 if (open_dir == -1)
1017 open_dir = TRUE;
1019 if (paths)
1020 action_mount(paths, open_dir, TRUE, quiet);
1022 destroy_glist(&paths);
1024 return NULL;
1027 static xmlNodePtr rpc_Unmount(GList *args)
1029 GList *paths;
1030 int quiet;
1032 paths = list_value(ARG(0));
1033 quiet = bool_value(ARG(1));
1035 if (paths)
1036 action_mount(paths, FALSE, FALSE, quiet);
1038 destroy_glist(&paths);
1040 return NULL;
1043 /* The first time the property changes, do nothing (it's us setting the
1044 * property). The second time, get the result.
1045 * Don't call this function three times!
1047 static void soap_done(GtkWidget *widget, GdkEventProperty *event, gpointer data)
1049 static int times_called = 0;
1050 GdkAtom prop = (GdkAtom) data;
1052 if (prop != event->atom)
1053 return;
1055 times_called++;
1056 g_return_if_fail(times_called < 3);
1058 if (times_called == 1)
1059 return;
1061 /* If we got a reply, display it here */
1062 if (event->state == GDK_PROPERTY_NEW_VALUE)
1064 gint length;
1066 data = read_property(event->window, event->atom, &length);
1068 if (data)
1069 puts(data);
1072 gtk_main_quit();
1075 static gboolean too_slow(gpointer data)
1077 g_warning("Existing ROX-Filer process is not responding! Try with -n");
1078 gtk_main_quit();
1080 return 0;
1083 /* Send the SOAP message in property 'prop' on 'from' to 'dest' */
1084 static void soap_send(GtkWidget *from, GdkAtom prop, GdkWindow *dest)
1086 GdkEventClient event;
1088 event.data.l[0] = GDK_WINDOW_XWINDOW(from->window);
1089 event.data.l[1] = gdk_x11_atom_to_xatom(prop);
1090 event.data_format = 32;
1091 event.message_type = xsoap;
1093 gdk_event_send_client_message((GdkEvent *) &event,
1094 GDK_WINDOW_XWINDOW(dest));
1096 g_timeout_add(10000, too_slow, NULL);
1098 gtk_main();
1101 /* Lookup this method in rpc_calls and invoke it.
1102 * Returns the SOAP reply or fault, or NULL if this method
1103 * doesn't return anything.
1105 static xmlNodePtr soap_invoke(xmlNode *method)
1107 GList *args = NULL;
1108 SOAP_call *call;
1109 gchar **arg;
1110 xmlNodePtr retval = NULL;
1111 GHashTable *name_to_node;
1112 xmlNode *node;
1114 call = g_hash_table_lookup(rpc_calls, method->name);
1115 if (!call)
1117 xmlNodePtr reply;
1118 gchar *err;
1120 err = g_strdup_printf(_("Attempt to invoke unknown SOAP "
1121 "method '%s'"), method->name);
1122 reply = xmlNewNode(NULL, "env:Fault");
1123 xmlNewNs(reply, SOAP_RPC_NS, "rpc");
1124 xmlNewNs(reply, SOAP_ENV_NS, "env");
1125 xmlNewTextChild(reply, NULL, "faultcode",
1126 "rpc:ProcedureNotPresent");
1127 xmlNewTextChild(reply, NULL, "faultstring", err);
1128 g_free(err);
1129 return reply;
1132 name_to_node = g_hash_table_new(g_str_hash, g_str_equal);
1133 for (node = method->xmlChildrenNode; node; node = node->next)
1135 if (node->type != XML_ELEMENT_NODE)
1136 continue;
1138 if (node->ns == NULL || strcmp(node->ns->href, ROX_NS) != 0)
1139 continue;
1141 g_hash_table_insert(name_to_node, (gchar *) node->name, node);
1144 if (call->required_args)
1146 for (arg = call->required_args; *arg; arg++)
1148 node = g_hash_table_lookup(name_to_node, *arg);
1149 if (!node)
1151 g_warning("Missing required argument '%s' "
1152 "in call to method '%s'", *arg,
1153 method->name);
1154 goto out;
1157 args = g_list_append(args, node);
1161 if (call->optional_args)
1163 for (arg = call->optional_args; *arg; arg++)
1164 args = g_list_append(args,
1165 g_hash_table_lookup(name_to_node, *arg));
1168 retval = call->func(args);
1170 out:
1171 g_hash_table_destroy(name_to_node);
1172 g_list_free(args);
1174 return retval;