r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / remote.c
blob6426f22e1224b691f911537f3e580eaa9f9ce6ef
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);
97 static xmlNodePtr rpc_PanelAdd(GList *args);
98 static xmlNodePtr rpc_PanelRemove(GList *args);
99 static xmlNodePtr rpc_PinboardAdd(GList *args);
100 static xmlNodePtr rpc_PinboardRemove(GList *args);
101 static xmlNodePtr rpc_SetBackdrop(GList *args);
102 static xmlNodePtr rpc_SetBackdropApp(GList *args);
104 /****************************************************************
105 * EXTERNAL INTERFACE *
106 ****************************************************************/
109 /* Try to get an already-running filer to handle things (only if
110 * new_copy is FALSE); TRUE if we succeed.
111 * Create an IPC widget so that future filers can contact us.
113 gboolean remote_init(xmlDocPtr rpc, gboolean new_copy)
115 guchar *unique_id;
116 GdkWindow *existing_ipc_window;
117 GtkWidget *ipc_window;
118 Window xwindow;
120 /* xmlDocDump(stdout, rpc); */
122 rpc_calls = g_hash_table_new(g_str_hash, g_str_equal);
124 soap_register("Version", rpc_Version, NULL, NULL);
126 soap_register("Run", rpc_Run, "Filename", NULL);
127 soap_register("OpenDir", rpc_OpenDir, "Filename",
128 "Style,Details,Sort,Class,ID,Hidden,Filter");
129 soap_register("CloseDir", rpc_CloseDir, "Filename", NULL);
130 soap_register("Examine", rpc_Examine, "Filename", NULL);
131 soap_register("Show", rpc_Show, "Directory,Leafname", NULL);
133 soap_register("Pinboard", rpc_Pinboard, NULL, "Name");
134 soap_register("Panel", rpc_Panel, "Side", "Name");
136 soap_register("FileType", rpc_FileType, "Filename", NULL);
138 soap_register("Copy", rpc_Copy, "From,To", "Leafname,Quiet");
139 soap_register("Move", rpc_Move, "From,To", "Leafname,Quiet");
140 soap_register("Link", rpc_Link, "From,To", "Leafname");
141 soap_register("Mount", rpc_Mount, "MountPoints", "OpenDir,Quiet");
143 soap_register("SetBackdrop", rpc_SetBackdrop, "Filename,Style", NULL);
144 soap_register("SetBackdropApp", rpc_SetBackdropApp, "App", NULL);
145 soap_register("PinboardAdd", rpc_PinboardAdd, "Path,X,Y", "Label");
146 soap_register("PinboardRemove", rpc_PinboardRemove, "Path", "Label");
147 soap_register("PanelAdd", rpc_PanelAdd, "Side,Path", "Label,After");
148 soap_register("PanelRemove", rpc_PanelRemove, "Side,Path", "Label");
150 /* Look for a property on the root window giving the IPC window
151 * of an already-running copy of this version of the filer, running
152 * on the same machine and with the same euid.
154 unique_id = g_strdup_printf("_ROX_FILER_%d_%s_%s",
155 (int) euid, VERSION, our_host_name());
156 filer_atom = gdk_atom_intern(unique_id, FALSE);
157 g_free(unique_id);
159 xsoap = gdk_atom_intern("_XSOAP", FALSE);
161 /* If we find a running copy, we'll need a window to put the
162 * SOAP message in before sending.
163 * If there's no running copy, we'll need a window to receive
164 * future SOAP message events.
165 * Either way, we'll need a window for it...
167 ipc_window = gtk_invisible_new();
168 gtk_widget_realize(ipc_window);
170 XGrabServer(GDK_DISPLAY());
172 existing_ipc_window = new_copy ? NULL : get_existing_ipc_window();
173 if (existing_ipc_window)
175 xmlChar *mem;
176 int size;
178 XUngrabServer(GDK_DISPLAY());
180 xmlDocDumpMemory(rpc, &mem, &size);
181 g_return_val_if_fail(size > 0, FALSE);
183 /* Since Gtk might have selected this already, we'd
184 * better do it BEFORE changing the property.
186 gtk_widget_add_events(ipc_window, GDK_PROPERTY_CHANGE_MASK);
187 g_signal_connect(ipc_window, "property-notify-event",
188 G_CALLBACK(soap_done), GINT_TO_POINTER(xsoap));
190 gdk_property_change(ipc_window->window, xsoap,
191 gdk_x11_xatom_to_atom(XA_STRING), 8,
192 GDK_PROP_MODE_REPLACE, mem, size);
193 g_free(mem);
195 soap_send(ipc_window, xsoap, existing_ipc_window);
197 return TRUE;
200 xwindow = GDK_WINDOW_XWINDOW(ipc_window->window);
202 /* Make the IPC window contain a property pointing to
203 * itself - this can then be used to check that it really
204 * is an IPC window.
206 gdk_property_change(ipc_window->window, filer_atom,
207 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
208 GDK_PROP_MODE_REPLACE,
209 (void *) &xwindow, 1);
211 /* Get notified when we get a message */
212 g_signal_connect(ipc_window, "client-event",
213 G_CALLBACK(client_event), NULL);
215 /* Make the root window contain a pointer to the IPC window */
216 gdk_property_change(gdk_get_default_root_window(), filer_atom,
217 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
218 GDK_PROP_MODE_REPLACE,
219 (void *) &xwindow, 1);
221 XUngrabServer(GDK_DISPLAY());
223 /* Also have a property without the version number, for programs
224 * that are happy to talk to any version of the filer.
226 unique_id = g_strdup_printf("_ROX_FILER_%d_%s",
227 (int) euid, our_host_name());
228 filer_atom_any = gdk_atom_intern(unique_id, FALSE);
229 g_free(unique_id);
230 /* On the IPC window... */
231 gdk_property_change(ipc_window->window, filer_atom_any,
232 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
233 GDK_PROP_MODE_REPLACE,
234 (void *) &xwindow, 1);
235 /* ... and on the root */
236 gdk_property_change(gdk_get_default_root_window(), filer_atom_any,
237 gdk_x11_xatom_to_atom(XA_WINDOW), 32,
238 GDK_PROP_MODE_REPLACE,
239 (void *) &xwindow, 1);
241 return FALSE;
244 /* Executes the RPC call(s) in the given SOAP message and returns
245 * the reply.
247 xmlDocPtr run_soap(xmlDocPtr soap)
249 xmlNodePtr body, node, rep_body, reply;
250 xmlDocPtr rep_doc = NULL;
252 g_return_val_if_fail(soap != NULL, NULL);
254 /* Make sure we don't quit before doing the whole list
255 * (there's a command that closes windows)
257 number_of_windows++;
259 node = xmlDocGetRootElement(soap);
260 if (!node->ns)
261 goto bad_soap;
263 if (strcmp(node->ns->href, SOAP_ENV_NS) != 0 &&
264 strcmp(node->ns->href, SOAP_ENV_NS_OLD) != 0)
265 goto bad_soap;
267 body = get_subnode(node, SOAP_ENV_NS, "Body");
268 if (!body)
269 body = get_subnode(node, SOAP_ENV_NS_OLD, "Body");
270 if (!body)
271 goto bad_soap;
273 for (node = body->xmlChildrenNode; node; node = node->next)
275 if (node->type != XML_ELEMENT_NODE)
276 continue;
278 if (node->ns == NULL || strcmp(node->ns->href, ROX_NS) != 0)
280 g_warning("Unknown namespace %s",
281 node->ns ? node->ns->href
282 : (xmlChar *) "(none)");
283 continue;
286 reply = soap_invoke(node);
288 if (reply)
290 if (!rep_doc)
291 rep_doc = soap_new(&rep_body);
292 xmlAddChild(rep_body, reply);
296 goto out;
298 bad_soap:
299 g_warning("Bad SOAP message received!");
301 out:
302 number_of_windows--;
304 return rep_doc;
308 /****************************************************************
309 * INTERNAL FUNCTIONS *
310 ****************************************************************/
312 /* Register this function to handle SOAP calls to method 'name'.
313 * 'req' and 'opt' are comma separated lists of argument names, in the order
314 * that they are to be delivered to the function.
315 * NULL will be passed for an opt argument if not given.
316 * Otherwise, the parameter is the xmlNode from the call.
318 static void soap_register(char *name, SOAP_func func, char *req, char *opt)
320 SOAP_call *call;
322 call = g_new(SOAP_call, 1);
323 call->func = func;
324 call->required_args = req ? g_strsplit(req, ",", 0) : NULL;
325 call->optional_args = opt ? g_strsplit(opt, ",", 0) : NULL;
327 g_hash_table_insert(rpc_calls, g_strdup(name), call);
330 /* Get the remote IPC window of the already-running filer if there
331 * is one.
333 static GdkWindow *get_existing_ipc_window(void)
335 Window xid, xid_confirm;
336 GdkWindow *window;
338 if (!get_ipc_property(gdk_get_default_root_window(), &xid))
339 return NULL;
341 if (gdk_window_lookup(xid))
342 return NULL; /* Stale handle which we now own */
344 window = gdk_window_foreign_new(xid);
345 if (!window)
346 return NULL;
348 if (!get_ipc_property(window, &xid_confirm) || xid_confirm != xid)
349 return NULL;
351 return window;
354 /* Returns the 'rox_atom' property of 'window' */
355 static gboolean get_ipc_property(GdkWindow *window, Window *r_xid)
357 guchar *data;
358 gint format, length;
359 gboolean retval = FALSE;
361 if (gdk_property_get(window, filer_atom,
362 gdk_x11_xatom_to_atom(XA_WINDOW), 0, 4,
363 FALSE, NULL, &format, &length, &data) && data)
365 if (format == 32 && length == 4)
367 retval = TRUE;
368 *r_xid = *((Window *) data);
370 g_free(data);
373 return retval;
376 static char *read_property(GdkWindow *window, GdkAtom prop, gint *out_length)
378 gint grab_len = 4096;
379 gint length;
380 guchar *data;
381 guchar *retval = NULL;
383 gdk_error_trap_push();
385 while (!retval)
387 if (!(gdk_property_get(window, prop,
388 gdk_x11_xatom_to_atom(XA_STRING), 0, grab_len,
389 FALSE, NULL, NULL,
390 &length, &data) && data))
391 goto out; /* Error? */
393 if (length >= grab_len)
395 /* Didn't get all of it - try again */
396 grab_len <<= 1;
397 g_free(data);
398 continue;
401 data = g_realloc(data, length + 1);
402 data[length] = '\0'; /* libxml seems to need this */
403 *out_length = length;
405 retval = data;
407 out:
409 if (gdk_error_trap_pop() == Success)
410 return retval;
412 g_warning("Error reading %s property!", gdk_atom_name(prop));
414 g_free(retval);
415 return NULL;
418 static gboolean client_event(GtkWidget *window,
419 GdkEventClient *event,
420 gpointer user_data)
422 GdkWindow *src_window;
423 GdkAtom prop;
424 xmlDocPtr reply;
425 guchar *data;
426 gint length;
427 xmlDocPtr doc;
429 if (event->message_type != xsoap)
430 return FALSE;
432 src_window = gdk_window_foreign_new(event->data.l[0]);
433 if (!src_window)
435 g_warning("SOAP message sender window was destroyed before I \n"
436 "could read it.");
437 return FALSE;
439 prop = gdk_x11_xatom_to_atom(event->data.l[1]);
441 data = read_property(src_window, prop, &length);
442 if (!data)
443 return TRUE;
445 doc = xmlParseMemory(g_strndup(data, length), length);
446 g_free(data);
448 reply = run_soap(doc);
449 if (number_of_windows == 0)
450 gtk_main_quit();
452 xmlFreeDoc(doc);
454 if (reply)
456 /* Send reply back... */
457 xmlChar *mem;
458 int size;
460 xmlDocDumpMemory(reply, &mem, &size);
461 g_return_val_if_fail(size > 0, TRUE);
463 gdk_error_trap_push();
464 gdk_property_change(src_window, prop,
465 gdk_x11_xatom_to_atom(XA_STRING), 8,
466 GDK_PROP_MODE_REPLACE, mem, size);
467 g_free(mem);
468 gdk_flush();
469 if (gdk_error_trap_pop() != Success)
470 g_warning("Failed to send SOAP reply!");
472 else
474 gdk_error_trap_push();
475 gdk_property_delete(src_window, prop);
476 gdk_flush();
477 if (gdk_error_trap_pop() != Success)
478 g_warning("Failed to send SOAP reply!");
481 return TRUE;
484 /* Some handy functions for processing SOAP RPC arguments... */
486 /* Returns TRUE, FALSE, or -1 (if argument is unspecified) */
487 static int bool_value(xmlNode *arg)
489 int answer;
490 char *optval;
492 if (!arg)
493 return -1;
495 optval = xmlNodeGetContent(arg);
496 answer = text_to_boolean(optval, -1); /* XXX: Report error? */
497 g_free(optval);
499 return answer;
502 /* Returns the text of this arg as a string, or NULL if the (optional)
503 * argument wasn't supplied. Returns "" if the arg is empty.
504 * g_free() the result.
506 static char *string_value(xmlNode *arg)
508 char *retval;
510 if (!arg)
511 return NULL;
513 retval = xmlNodeGetContent(arg);
515 return retval ? retval : g_strdup("");
518 /* Returns the text of this arg as an int, or the default value if not
519 * supplied or not an int.
521 static int int_value(xmlNode *arg, int def)
523 char *str, *end;
524 int i;
526 if (!arg)
527 return def;
529 str = xmlNodeGetContent(arg);
530 if (!str || !str[0])
531 return def;
533 i = (int) strtol(str, &end, 0);
535 return (end > str) ? i : def;
538 /* Return a list of strings, one for each child node of arg.
539 * g_list_free the list, and g_free each string.
541 static GList *list_value(xmlNode *arg)
543 GList *list = NULL;
544 xmlNode *node;
546 for (node = arg->xmlChildrenNode; node; node = node->next)
548 if (node->type != XML_ELEMENT_NODE)
549 continue;
551 list = g_list_append(list, string_value(node));
554 return list;
557 #define ARG(n) ((xmlNode *) g_list_nth(args, n)->data)
559 /* The RPC handlers all work in the same way -- they get passed a list of
560 * xmlNode arguments from the RPC call and they return the result node, or
561 * NULL if there isn't a result.
564 static xmlNodePtr rpc_Version(GList *args)
566 xmlNodePtr reply;
568 reply = xmlNewNode(NULL, "rox:VersionResponse");
569 xmlNewNs(reply, SOAP_RPC_NS, "soap");
570 xmlNewTextChild(reply, NULL, "soap:result", VERSION);
572 return reply;
575 /* Args: Path, [Style, Details, Sort, Class, Window] */
576 static xmlNodePtr rpc_OpenDir(GList *args)
578 char *path;
579 char *style, *details, *sort, *class, *window;
580 int hidden=FALSE;
581 char *filter_string=NULL;
582 FilerWindow *fwin = NULL;
584 path = string_value(ARG(0));
585 class = string_value(ARG(4));
586 window = string_value(ARG(5));
588 if (window)
589 fwin = filer_get_by_id(window);
591 if (!fwin)
593 fwin = filer_opendir(path, NULL, class);
594 if (window)
595 filer_set_id(fwin, window);
597 else
598 filer_change_to(fwin, path, NULL);
600 g_free(path);
601 g_free(class);
602 g_free(window);
603 if (!fwin)
604 return NULL;
606 style = string_value(ARG(1));
607 details = string_value(ARG(2));
608 sort = string_value(ARG(3));
609 hidden = bool_value(ARG(6));
610 filter_string = string_value(ARG(7));
612 if (style)
614 DisplayStyle ds;
616 ds = !g_strcasecmp(style, "Large") ? LARGE_ICONS :
617 !g_strcasecmp(style, "Small") ? SMALL_ICONS :
618 !g_strcasecmp(style, "Huge") ? HUGE_ICONS :
619 !g_strcasecmp(style, "Automatic") ? AUTO_SIZE_ICONS :
620 UNKNOWN_STYLE;
621 if (ds == UNKNOWN_STYLE)
622 delayed_error(_("Unknown style '%s'"), style);
623 else
624 display_set_layout(fwin, ds, fwin->details_type, TRUE);
626 g_free(style);
629 if (details)
631 DetailsType dt;
632 ViewType view_type;
634 dt = !g_strcasecmp(details, "None") ? DETAILS_NONE :
635 !g_strcasecmp(details, "ListView") ? DETAILS_NONE :
636 !g_strcasecmp(details, "Size") ? DETAILS_SIZE :
637 !g_strcasecmp(details, "Type") ? DETAILS_TYPE :
638 !g_strcasecmp(details, "Times") ? DETAILS_TIMES :
639 !g_strcasecmp(details, "Permissions")
640 ? DETAILS_PERMISSIONS :
641 DETAILS_UNKNOWN;
643 if (dt == DETAILS_UNKNOWN)
644 delayed_error(_("Unknown details type '%s'"), details);
645 else
646 display_set_layout(fwin, fwin->display_style_wanted,
647 dt, TRUE);
649 if (g_strcasecmp(details, "ListView") == 0)
650 view_type = VIEW_TYPE_DETAILS;
651 else
652 view_type = VIEW_TYPE_COLLECTION;
654 if (view_type != fwin->view_type)
655 filer_set_view_type(fwin, view_type);
657 g_free(details);
660 if (sort)
662 SortType type;
664 type = !g_strcasecmp(sort, "Name") ? SORT_NAME :
665 !g_strcasecmp(sort, "Type") ? SORT_TYPE :
666 !g_strcasecmp(sort, "Date") ? SORT_DATE :
667 !g_strcasecmp(sort, "Size") ? SORT_SIZE :
668 !g_strcasecmp(sort, "Owner") ? SORT_OWNER :
669 !g_strcasecmp(sort, "Group") ? SORT_GROUP :
671 if (type == -1)
672 delayed_error(_("Unknown sorting type '%s'"), sort);
673 else
674 display_set_sort_type(fwin, type, GTK_SORT_ASCENDING);
676 g_free(sort);
678 if (hidden!=-1)
680 display_set_hidden(fwin, hidden);
683 if (filter_string)
684 display_set_filter(fwin, FILER_SHOW_GLOB, filter_string);
685 else
686 display_set_filter(fwin, FILER_SHOW_ALL, NULL);
688 return NULL;
691 static xmlNodePtr rpc_Run(GList *args)
693 char *path;
695 path = string_value(ARG(0));
696 run_by_path(path);
697 g_free(path);
699 return NULL;
702 static xmlNodePtr rpc_CloseDir(GList *args)
704 char *path;
706 path = string_value(ARG(0));
707 filer_close_recursive(path);
708 g_free(path);
710 return NULL;
713 static xmlNodePtr rpc_Examine(GList *args)
715 char *path;
717 path = string_value(ARG(0));
718 examine(path);
719 g_free(path);
721 return NULL;
724 static xmlNodePtr rpc_Show(GList *args)
726 char *dir, *leaf;
728 dir = string_value(ARG(0));
729 leaf = string_value(ARG(1));
731 /* XXX: Seems silly to join them only to split again later... */
732 open_to_show(make_path(dir, leaf));
734 g_free(dir);
735 g_free(leaf);
737 return NULL;
740 static xmlNodePtr rpc_Pinboard(GList *args)
742 char *name = NULL;
744 name = string_value(ARG(0));
745 pinboard_activate(name);
746 g_free(name);
748 return NULL;
751 /* args = Filename, Style */
752 static xmlNodePtr rpc_SetBackdrop(GList *args)
754 char *file;
755 char *style;
756 BackdropStyle s;
758 file = string_value(ARG(0));
759 style = string_value(ARG(1));
761 s = !g_strcasecmp(style, "Tile") ? BACKDROP_TILE :
762 !g_strcasecmp(style, "Scale") ? BACKDROP_SCALE :
763 !g_strcasecmp(style, "Stretch") ? BACKDROP_STRETCH :
764 !g_strcasecmp(style, "Centre") ? BACKDROP_CENTRE :
765 BACKDROP_NONE;
767 if (s == BACKDROP_NONE)
768 g_warning("Invalid style '%s' for backdrop", style);
769 else
770 pinboard_set_backdrop(file, s);
772 g_free(file);
773 g_free(style);
775 return NULL;
778 /* args = App */
779 static xmlNodePtr rpc_SetBackdropApp(GList *args)
781 char *app;
783 app = string_value(ARG(0));
785 pinboard_set_backdrop_app(app);
787 g_free(app);
789 return NULL;
792 /* args = Path, X, Y, [Label] */
793 static xmlNodePtr rpc_PinboardAdd(GList *args)
795 char *path = NULL;
796 gchar *name;
797 int x, y;
799 path = string_value(ARG(0));
800 x = int_value(ARG(1), 0);
801 y = int_value(ARG(2), 0);
802 name = string_value(ARG(3));
804 pinboard_pin(path, name, x, y, NULL);
806 g_free(path);
807 if(name)
808 g_free(name);
810 return NULL;
813 /* args = Path, [Label] */
814 static xmlNodePtr rpc_PinboardRemove(GList *args)
816 char *path = NULL;
817 gchar *name;
819 path = string_value(ARG(0));
820 name = string_value(ARG(1));
822 pinboard_remove(path, name);
824 g_free(path);
825 if(name)
826 g_free(name);
828 return NULL;
831 /* Returns PANEL_NUMBER_OF_SIDES if name is invalid */
832 static PanelSide panel_name_to_side(gchar *side)
834 if (strcmp(side, "Top") == 0)
835 return PANEL_TOP;
836 else if (strcmp(side, "Bottom") == 0)
837 return PANEL_BOTTOM;
838 else if (strcmp(side, "Left") == 0)
839 return PANEL_LEFT;
840 else if (strcmp(side, "Right") == 0)
841 return PANEL_RIGHT;
842 else
843 g_warning("Unknown panel side '%s'", side);
844 return PANEL_NUMBER_OF_SIDES;
847 /* args = Side, [Name] */
848 static xmlNodePtr rpc_Panel(GList *args)
850 PanelSide side;
851 char *name, *side_name;
853 side_name = string_value(ARG(0));
854 side = panel_name_to_side(side_name);
855 g_free(side_name);
856 g_return_val_if_fail(side != PANEL_NUMBER_OF_SIDES, NULL);
858 name = string_value(ARG(1));
860 panel_new(name, side);
862 g_free(name);
864 return NULL;
867 /* args = Side, Path, [Label, After] */
868 static xmlNodePtr rpc_PanelAdd(GList *args)
870 PanelSide side;
871 char *path, *side_name, *label;
872 gboolean after = FALSE;
873 int tmp;
875 side_name = string_value(ARG(0));
876 side = panel_name_to_side(side_name);
877 g_free(side_name);
878 g_return_val_if_fail(side != PANEL_NUMBER_OF_SIDES, NULL);
880 path = string_value(ARG(1));
881 label = string_value(ARG(2));
883 tmp = bool_value(ARG(3));
884 after = (tmp == -1) ? FALSE : tmp;
886 panel_add(side, path, label, after);
888 g_free(path);
890 return NULL;
893 static xmlNodePtr rpc_PanelRemove(GList *args)
895 PanelSide side;
896 char *path, *side_name, *label;
898 side_name = string_value(ARG(0));
899 side = panel_name_to_side(side_name);
900 g_free(side_name);
901 g_return_val_if_fail(side != PANEL_NUMBER_OF_SIDES, NULL);
903 path = string_value(ARG(1));
904 label = string_value(ARG(2));
906 panel_remove_item(side, path, label);
908 g_free(path);
909 g_free(label);
911 return NULL;
914 static xmlNodePtr rpc_Copy(GList *args)
916 GList *from;
917 char *to;
918 char *leaf;
919 int quiet;
921 from = list_value(ARG(0));
922 to = string_value(ARG(1));
923 leaf = string_value(ARG(2));
924 quiet = bool_value(ARG(3));
926 if (from)
927 action_copy(from, to, leaf, quiet);
928 else
929 g_warning("No files in SOAP request list");
931 destroy_glist(&from);
932 g_free(to);
933 g_free(leaf);
935 return NULL;
938 static xmlNodePtr rpc_Move(GList *args)
940 GList *from;
941 char *to;
942 char *leaf;
943 int quiet;
945 from = list_value(ARG(0));
946 to = string_value(ARG(1));
947 leaf = string_value(ARG(2));
948 quiet = bool_value(ARG(3));
950 if (from)
951 action_move(from, to, leaf, quiet);
952 else
953 g_warning("No files in SOAP request list");
955 destroy_glist(&from);
956 g_free(to);
957 g_free(leaf);
959 return NULL;
962 static xmlNodePtr rpc_Link(GList *args)
964 GList *from;
965 char *to;
966 char *leaf;
968 from = list_value(ARG(0));
969 to = string_value(ARG(1));
970 leaf = string_value(ARG(2));
972 if (from)
973 action_link(from, to, leaf);
974 else
975 g_warning("No files in SOAP request list");
977 destroy_glist(&from);
978 g_free(to);
979 g_free(leaf);
981 return NULL;
984 static xmlNodePtr rpc_FileType(GList *args)
986 MIME_type *type;
987 char *path, *tname;
988 xmlNodePtr reply;
990 path = string_value(ARG(0));
991 type = type_get_type(path);
992 g_free(path);
994 reply = xmlNewNode(NULL, "rox:FileTypeResponse");
995 tname = g_strconcat(type->media_type, "/", type->subtype, NULL);
997 xmlNewNs(reply, SOAP_RPC_NS, "soap");
998 xmlNewTextChild(reply, NULL, "soap:result", tname);
999 g_free(tname);
1001 return reply;
1004 static xmlNodePtr rpc_Mount(GList *args)
1006 GList *paths;
1007 int open_dir, quiet;
1009 paths = list_value(ARG(0));
1010 open_dir = bool_value(ARG(1));
1011 quiet = bool_value(ARG(2));
1013 if (open_dir == -1)
1014 open_dir = TRUE;
1016 if (paths)
1017 action_mount(paths, open_dir, quiet);
1019 destroy_glist(&paths);
1021 return NULL;
1024 /* The first time the property changes, do nothing (it's us setting the
1025 * property). The second time, get the result.
1026 * Don't call this function three times!
1028 static void soap_done(GtkWidget *widget, GdkEventProperty *event, gpointer data)
1030 static int times_called = 0;
1031 GdkAtom prop = (GdkAtom) data;
1033 if (prop != event->atom)
1034 return;
1036 times_called++;
1037 g_return_if_fail(times_called < 3);
1039 if (times_called == 1)
1040 return;
1042 /* If we got a reply, display it here */
1043 if (event->state == GDK_PROPERTY_NEW_VALUE)
1045 gint length;
1047 data = read_property(event->window, event->atom, &length);
1049 if (data)
1050 puts(data);
1053 gtk_main_quit();
1056 static gboolean too_slow(gpointer data)
1058 g_warning("Existing ROX-Filer process is not responding! Try with -n");
1059 gtk_main_quit();
1061 return 0;
1064 /* Send the SOAP message in property 'prop' on 'from' to 'dest' */
1065 static void soap_send(GtkWidget *from, GdkAtom prop, GdkWindow *dest)
1067 GdkEventClient event;
1069 event.data.l[0] = GDK_WINDOW_XWINDOW(from->window);
1070 event.data.l[1] = gdk_x11_atom_to_xatom(prop);
1071 event.data_format = 32;
1072 event.message_type = xsoap;
1074 gdk_event_send_client_message((GdkEvent *) &event,
1075 GDK_WINDOW_XWINDOW(dest));
1077 g_timeout_add(10000, too_slow, NULL);
1079 gtk_main();
1082 /* Lookup this method in rpc_calls and invoke it.
1083 * Returns the SOAP reply or fault, or NULL if this method
1084 * doesn't return anything.
1086 static xmlNodePtr soap_invoke(xmlNode *method)
1088 GList *args = NULL;
1089 SOAP_call *call;
1090 gchar **arg;
1091 xmlNodePtr retval = NULL;
1092 GHashTable *name_to_node;
1093 xmlNode *node;
1095 call = g_hash_table_lookup(rpc_calls, method->name);
1096 if (!call)
1098 xmlNodePtr reply;
1099 gchar *err;
1101 err = g_strdup_printf(_("Attempt to invoke unknown SOAP "
1102 "method '%s'"), method->name);
1103 reply = xmlNewNode(NULL, "env:Fault");
1104 xmlNewNs(reply, SOAP_RPC_NS, "rpc");
1105 xmlNewNs(reply, SOAP_ENV_NS, "env");
1106 xmlNewTextChild(reply, NULL, "faultcode",
1107 "rpc:ProcedureNotPresent");
1108 xmlNewTextChild(reply, NULL, "faultstring", err);
1109 g_free(err);
1110 return reply;
1113 name_to_node = g_hash_table_new(g_str_hash, g_str_equal);
1114 for (node = method->xmlChildrenNode; node; node = node->next)
1116 if (node->type != XML_ELEMENT_NODE)
1117 continue;
1119 if (node->ns == NULL || strcmp(node->ns->href, ROX_NS) != 0)
1120 continue;
1122 g_hash_table_insert(name_to_node, (gchar *) node->name, node);
1125 if (call->required_args)
1127 for (arg = call->required_args; *arg; arg++)
1129 node = g_hash_table_lookup(name_to_node, *arg);
1130 if (!node)
1132 g_warning("Missing required argument '%s' "
1133 "in call to method '%s'", *arg,
1134 method->name);
1135 goto out;
1138 args = g_list_append(args, node);
1142 if (call->optional_args)
1144 for (arg = call->optional_args; *arg; arg++)
1145 args = g_list_append(args,
1146 g_hash_table_lookup(name_to_node, *arg));
1149 retval = call->func(args);
1151 out:
1152 g_hash_table_destroy(name_to_node);
1153 g_list_free(args);
1155 return retval;