Update documentation, remove obsolete GQview import options
[geeqie.git] / src / remote.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "remote.h"
24
25 #include "cache_maint.h"
26 #include "collect.h"
27 #include "collect-io.h"
28 #include "filedata.h"
29 #include "filefilter.h"
30 #include "image.h"
31 #include "img-view.h"
32 #include "layout.h"
33 #include "layout_image.h"
34 #include "layout_util.h"
35 #include "misc.h"
36 #include "pixbuf-renderer.h"
37 #include "slideshow.h"
38 #include "ui_fileops.h"
39 #include "rcfile.h"
40
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <signal.h>
44 #include <errno.h>
45
46 #include "glua.h"
47
48 #define SERVER_MAX_CLIENTS 8
49
50 #define REMOTE_SERVER_BACKLOG 4
51
52
53 #ifndef UNIX_PATH_MAX
54 #define UNIX_PATH_MAX 108
55 #endif
56
57
58 static RemoteConnection *remote_client_open(const gchar *path);
59 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
60 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
61
62 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
63
64 typedef struct _RemoteClient RemoteClient;
65 struct _RemoteClient {
66         gint fd;
67         guint channel_id; /* event source id */
68         RemoteConnection *rc;
69 };
70
71 typedef struct _RemoteData RemoteData;
72 struct _RemoteData {
73         CollectionData *command_collection;
74 };
75
76 /* Remote commands from main.c are prepended with the current dir the remote
77  * command was made from. Some remote commands require this. The
78  * value is stored here
79  */
80 static gchar *pwd = NULL;
81
82 /**
83  * @brief Ensures file path is absolute.
84  * @param[in] filename Filepath, absolute or relative to calling directory
85  * @returns absolute path
86  * 
87  * If first character of input filepath is not the directory
88  * separator, assume it as a relative path and prepend
89  * the directory the remote command was initiated from
90  * 
91  * Return value must be freed with g_free()
92  */
93 static gchar *set_pwd(gchar *filename)
94 {
95         gchar *temp;
96
97         if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
98                 {
99                 temp = g_build_filename(pwd, filename, NULL);
100                 }
101         else
102                 {
103                 temp = g_strdup(filename);
104                 }
105
106         return temp;
107 }
108
109 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
110 {
111         RemoteClient *client = data;
112         RemoteConnection *rc;
113         GIOStatus status = G_IO_STATUS_NORMAL;
114
115         lw_id = NULL;
116         rc = client->rc;
117
118         if (condition & G_IO_IN)
119                 {
120                 gchar *buffer = NULL;
121                 GError *error = NULL;
122                 gsize termpos;
123
124                 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
125                         {
126                         if (buffer)
127                                 {
128                                 buffer[termpos] = '\0';
129
130                                 if (strlen(buffer) > 0)
131                                         {
132                                         if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
133                                         g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
134                                         g_io_channel_flush(source, NULL);
135                                         }
136                                 g_free(buffer);
137
138                                 buffer = NULL;
139                                 }
140                         }
141
142                 if (error)
143                         {
144                         log_printf("error reading socket: %s\n", error->message);
145                         g_error_free(error);
146                         }
147                 }
148
149         if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
150                 {
151                 rc->clients = g_list_remove(rc->clients, client);
152
153                 DEBUG_1("HUP detected, closing client.");
154                 DEBUG_1("client count %d", g_list_length(rc->clients));
155
156                 g_source_remove(client->channel_id);
157                 close(client->fd);
158                 g_free(client);
159                 }
160
161         return TRUE;
162 }
163
164 static void remote_server_client_add(RemoteConnection *rc, gint fd)
165 {
166         RemoteClient *client;
167         GIOChannel *channel;
168
169         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
170                 {
171                 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
172                 close(fd);
173                 return;
174                 }
175
176         client = g_new0(RemoteClient, 1);
177         client->rc = rc;
178         client->fd = fd;
179
180         channel = g_io_channel_unix_new(fd);
181         client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
182                                                  remote_server_client_cb, client, NULL);
183         g_io_channel_unref(channel);
184
185         rc->clients = g_list_append(rc->clients, client);
186         DEBUG_1("client count %d", g_list_length(rc->clients));
187 }
188
189 static void remote_server_clients_close(RemoteConnection *rc)
190 {
191         while (rc->clients)
192                 {
193                 RemoteClient *client = rc->clients->data;
194
195                 rc->clients = g_list_remove(rc->clients, client);
196
197                 g_source_remove(client->channel_id);
198                 close(client->fd);
199                 g_free(client);
200                 }
201 }
202
203 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
204 {
205         RemoteConnection *rc = data;
206         gint fd;
207         guint alen;
208
209         fd = accept(rc->fd, NULL, &alen);
210         if (fd == -1)
211                 {
212                 log_printf("error accepting socket: %s\n", strerror(errno));
213                 return TRUE;
214                 }
215
216         remote_server_client_add(rc, fd);
217
218         return TRUE;
219 }
220
221 gboolean remote_server_exists(const gchar *path)
222 {
223         RemoteConnection *rc;
224
225         /* verify server up */
226         rc = remote_client_open(path);
227         remote_close(rc);
228
229         if (rc) return TRUE;
230
231         /* unable to connect, remove socket file to free up address */
232         unlink(path);
233         return FALSE;
234 }
235
236 static RemoteConnection *remote_server_open(const gchar *path)
237 {
238         RemoteConnection *rc;
239         struct sockaddr_un addr;
240         gint sun_path_len;
241         gint fd;
242         GIOChannel *channel;
243
244         if (remote_server_exists(path))
245                 {
246                 log_printf("Address already in use: %s\n", path);
247                 return NULL;
248                 }
249
250         fd = socket(PF_UNIX, SOCK_STREAM, 0);
251         if (fd == -1) return NULL;
252
253         addr.sun_family = AF_UNIX;
254         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
255         strncpy(addr.sun_path, path, sun_path_len);
256         if (bind(fd, &addr, sizeof(addr)) == -1 ||
257             listen(fd, REMOTE_SERVER_BACKLOG) == -1)
258                 {
259                 log_printf("error subscribing to socket: %s\n", strerror(errno));
260                 close(fd);
261                 return NULL;
262                 }
263
264         rc = g_new0(RemoteConnection, 1);
265
266         rc->server = TRUE;
267         rc->fd = fd;
268         rc->path = g_strdup(path);
269
270         channel = g_io_channel_unix_new(rc->fd);
271         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
272
273         rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
274                                              remote_server_read_cb, rc, NULL);
275         g_io_channel_unref(channel);
276
277         return rc;
278 }
279
280 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
281 {
282         if (!rc || !rc->server) return;
283
284         rc->read_func = func;
285         rc->read_data = data;
286 }
287
288
289 static RemoteConnection *remote_client_open(const gchar *path)
290 {
291         RemoteConnection *rc;
292         struct stat st;
293         struct sockaddr_un addr;
294         gint sun_path_len;
295         gint fd;
296
297         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
298
299         fd = socket(PF_UNIX, SOCK_STREAM, 0);
300         if (fd == -1) return NULL;
301
302         addr.sun_family = AF_UNIX;
303         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
304         strncpy(addr.sun_path, path, sun_path_len);
305         if (connect(fd, &addr, sizeof(addr)) == -1)
306                 {
307                 DEBUG_1("error connecting to socket: %s", strerror(errno));
308                 close(fd);
309                 return NULL;
310                 }
311
312         rc = g_new0(RemoteConnection, 1);
313         rc->server = FALSE;
314         rc->fd = fd;
315         rc->path = g_strdup(path);
316
317         return rc;
318 }
319
320 static sig_atomic_t sigpipe_occurred = FALSE;
321
322 static void sighandler_sigpipe(gint sig)
323 {
324         sigpipe_occurred = TRUE;
325 }
326
327 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
328 {
329         struct sigaction new_action, old_action;
330         gboolean ret = FALSE;
331         GError *error = NULL;
332         GIOChannel *channel;
333
334         if (!rc || rc->server) return FALSE;
335         if (!text) return TRUE;
336
337         sigpipe_occurred = FALSE;
338
339         new_action.sa_handler = sighandler_sigpipe;
340         sigemptyset(&new_action.sa_mask);
341         new_action.sa_flags = 0;
342
343         /* setup our signal handler */
344         sigaction(SIGPIPE, &new_action, &old_action);
345
346         channel = g_io_channel_unix_new(rc->fd);
347
348         g_io_channel_write_chars(channel, text, -1, NULL, &error);
349         g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
350         g_io_channel_flush(channel, &error);
351
352         if (error)
353                 {
354                 log_printf("error reading socket: %s\n", error->message);
355                 g_error_free(error);
356                 ret = FALSE;;
357                 }
358         else
359                 {
360                 ret = TRUE;
361                 }
362
363         if (ret)
364                 {
365                 gchar *buffer = NULL;
366                 gsize termpos;
367                 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
368                         {
369                         if (buffer)
370                                 {
371                                 if (buffer[0] == '\n') /* empty line finishes the command */
372                                         {
373                                         g_free(buffer);
374                                         fflush(stdout);
375                                         break;
376                                         }
377                                 buffer[termpos] = '\0';
378                                 printf("%s\n", buffer);
379                                 g_free(buffer);
380                                 buffer = NULL;
381                                 }
382                         }
383
384                 if (error)
385                         {
386                         log_printf("error reading socket: %s\n", error->message);
387                         g_error_free(error);
388                         ret = FALSE;
389                         }
390                 }
391
392
393         /* restore the original signal handler */
394         sigaction(SIGPIPE, &old_action, NULL);
395         g_io_channel_unref(channel);
396         return ret;
397 }
398
399 void remote_close(RemoteConnection *rc)
400 {
401         if (!rc) return;
402
403         if (rc->server)
404                 {
405                 remote_server_clients_close(rc);
406
407                 g_source_remove(rc->channel_id);
408                 unlink(rc->path);
409                 }
410
411         if (rc->read_data)
412                 g_free(rc->read_data);
413
414         close(rc->fd);
415
416         g_free(rc->path);
417         g_free(rc);
418 }
419
420 /*
421  *-----------------------------------------------------------------------------
422  * remote functions
423  *-----------------------------------------------------------------------------
424  */
425
426 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
427 {
428         layout_image_next(lw_id);
429 }
430
431 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
432 {
433         LayoutWindow *lw = NULL;
434
435         if (!layout_valid(&lw)) return;
436
437         lw_id = layout_menu_new_window(NULL, lw);
438 }
439
440 static gboolean gr_close_window_cb()
441 {
442         if (!layout_valid(&lw_id)) return FALSE;
443
444         layout_menu_close_cb(NULL, lw_id);
445
446         return FALSE;
447 }
448
449 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
450 {
451         g_idle_add(gr_close_window_cb, NULL);
452 }
453
454 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
455 {
456         layout_image_prev(lw_id);
457 }
458
459 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
460 {
461         layout_image_first(lw_id);
462 }
463
464 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
465 {
466         layout_image_last(lw_id);
467 }
468
469 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
470 {
471         layout_image_full_screen_toggle(lw_id);
472 }
473
474 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
475 {
476         layout_image_full_screen_start(lw_id);
477 }
478
479 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
480 {
481         layout_image_full_screen_stop(lw_id);
482 }
483
484 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
485 {
486         lw_id = layout_find_by_layout_id(text);
487         if (!lw_id)
488                 {
489                 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
490                 }
491         layout_valid(&lw_id);
492 }
493
494 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
495 {
496         GList *list;
497         FileData *dir_fd = file_data_new_dir(text);
498         list = filelist_recursive(dir_fd);
499         file_data_unref(dir_fd);
500         if (!list) return;
501 //printf("length: %d\n", g_list_length(list));
502         layout_image_slideshow_stop(lw_id);
503         layout_image_slideshow_start_from_list(lw_id, list);
504 }
505
506 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
507 {
508         if (!g_strcmp0(text, "clear"))
509                 cache_maintain_home_remote(FALSE, TRUE);
510         else if (!g_strcmp0(text, "clean"))
511                 cache_maintain_home_remote(FALSE, FALSE);
512 }
513
514 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
515 {
516         if (!g_strcmp0(text, "clear"))
517                 cache_manager_standard_process_remote(TRUE);
518         else if (!g_strcmp0(text, "clean"))
519                 cache_manager_standard_process_remote(FALSE);
520 }
521
522 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
523 {
524         cache_maintain_home_remote(TRUE, FALSE);
525 }
526
527 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
528 {
529         cache_manager_render_remote(text, FALSE, FALSE);
530 }
531
532 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
533 {
534         cache_manager_render_remote(text, TRUE, FALSE);
535 }
536
537 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
538 {
539         if(options->thumbnails.spec_standard)
540                 cache_manager_render_remote(text, FALSE, TRUE);
541 }
542
543 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
544 {
545         if(options->thumbnails.spec_standard)
546                 cache_manager_render_remote(text, TRUE, TRUE);
547 }
548
549 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
550 {
551         layout_image_slideshow_toggle(lw_id);
552 }
553
554 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
555 {
556         layout_image_slideshow_start(lw_id);
557 }
558
559 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
560 {
561         layout_image_slideshow_stop(lw_id);
562 }
563
564 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
565 {
566         gdouble t1, t2, t3, n;
567         gint res;
568
569         res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
570         if (res == 3)
571                 {
572                 n = (t1 * 3600) + (t2 * 60) + t3;
573                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
574                                 t1 >= 24 || t2 >= 60 || t3 >= 60)
575                         {
576                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
577                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
578                         return;
579                         }
580                 }
581         else if (res == 2)
582                 {
583                 n = t1 * 60 + t2;
584                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
585                                 t1 >= 60 || t2 >= 60)
586                         {
587                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
588                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
589                         return;
590                         }
591                 }
592         else if (res == 1)
593                 {
594                 n = t1;
595                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
596                         {
597                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
598                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
599                         return;
600                         }
601                 }
602         else
603                 {
604                 n = 0;
605                 }
606
607         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
608 }
609
610 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
611 {
612         gboolean popped;
613         gboolean hidden;
614
615         if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
616                 {
617                 layout_tools_float_set(lw_id, popped, FALSE);
618                 }
619 }
620
621 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
622 {
623         gboolean popped;
624         gboolean hidden;
625
626         if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
627                 {
628                 layout_tools_float_set(lw_id, popped, TRUE);
629                 }
630 }
631
632 static gboolean gr_quit_idle_cb(gpointer data)
633 {
634         exit_program();
635
636         return FALSE;
637 }
638
639 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
640 {
641         /* schedule exit when idle, if done from within a
642          * remote handler remote_close will crash
643          */
644         g_idle_add(gr_quit_idle_cb, NULL);
645 }
646
647 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
648 {
649         gchar *filename;
650         gchar *tilde_filename = expand_tilde(text);
651
652         filename = set_pwd(tilde_filename);
653
654         if (isfile(filename))
655                 {
656                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
657                         {
658                         collection_window_new(filename);
659                         }
660                 else
661                         {
662                         layout_set_path(lw_id, filename);
663                         }
664                 }
665         else if (isdir(filename))
666                 {
667                 layout_set_path(lw_id, filename);
668                 }
669         else
670                 {
671                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
672                 layout_set_path(lw_id, homedir());
673                 }
674
675         g_free(filename);
676         g_free(tilde_filename);
677 }
678
679 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
680 {
681         gr_file_load_no_raise(text, channel, data);
682
683         gr_raise(text, channel, data);
684 }
685
686 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
687 {
688         gchar *pixel_info;
689         gint x_pixel, y_pixel;
690         gint width, height;
691         gint r_mouse, g_mouse, b_mouse;
692         PixbufRenderer *pr;
693         LayoutWindow *lw = NULL;
694
695         if (!layout_valid(&lw_id)) return;
696
697         pr = (PixbufRenderer*)lw_id->image->pr;
698
699         if (pr)
700                 {
701                 pixbuf_renderer_get_image_size(pr, &width, &height);
702                 if (width < 1 || height < 1) return;
703
704                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
705
706                 if (x_pixel >= 0 && y_pixel >= 0)
707                         {
708                         pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
709                                                          &r_mouse, &g_mouse, &b_mouse);
710
711                         pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
712                                                  x_pixel, y_pixel,
713                                                  r_mouse, g_mouse, b_mouse);
714
715                         g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
716                         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
717
718                         g_free(pixel_info);
719                         }
720                 else
721                         {
722                         return;
723                         }
724                 }
725         else
726                 {
727                 return;
728                 }
729 }
730
731 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
732 {
733         gchar *rectangle_info;
734         PixbufRenderer *pr;
735         LayoutWindow *lw = NULL;
736         gint x1, y1, x2, y2;
737
738         if (!options->draw_rectangle) return;
739         if (!layout_valid(&lw_id)) return;
740
741         pr = (PixbufRenderer*)lw_id->image->pr;
742
743         if (pr)
744                 {
745                 image_get_rectangle(&x1, &y1, &x2, &y2);
746                 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
747                                         (x2 > x1) ? x2 - x1 : x1 - x2,
748                                         (y2 > y1) ? y2 - y1 : y1 - y2,
749                                         (x2 > x1) ? x1 : x2,
750                                         (y2 > y1) ? y1 : y2);
751
752                 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
753                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
754
755                 g_free(rectangle_info);
756                 }
757 }
758
759 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
760 {
761         gchar *render_intent;
762
763         switch (options->color_profile.render_intent)
764                 {
765                 case 0:
766                         render_intent = g_strdup("Perceptual");
767                         break;
768                 case 1:
769                         render_intent = g_strdup("Relative Colorimetric");
770                         break;
771                 case 2:
772                         render_intent = g_strdup("Saturation");
773                         break;
774                 case 3:
775                         render_intent = g_strdup("Absolute Colorimetric");
776                         break;
777                 default:
778                         render_intent = g_strdup("none");
779                         break;
780                 }
781
782         g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
783         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
784
785         g_free(render_intent);
786 }
787
788 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
789 {
790         GList *list = NULL;
791         FileFormatClass class;
792         FileData *dir_fd;
793         FileData *fd;
794         GString *out_string = g_string_new(NULL);
795         GList *work;
796
797         if (strcmp(text, "") == 0)
798                 {
799                 if (layout_valid(&lw_id))
800                         {
801                         dir_fd = file_data_new_dir(lw_id->dir_fd->path);
802                         }
803                 else
804                         {
805                         return;
806                         }
807                 }
808         else
809                 {
810                 if (isdir(text))
811                         {
812                         dir_fd = file_data_new_dir(text);
813                         }
814                 else
815                         {
816                         return;
817                         }
818                 }
819
820         if (recurse)
821                 {
822                 list = filelist_recursive(dir_fd);
823                 }
824         else
825                 {
826                 filelist_read(dir_fd, &list, NULL);
827                 }
828
829         work = list;
830         while (work)
831                 {
832                 fd = work->data;
833                 g_string_append_printf(out_string, "%s", fd->path);
834                 class = filter_file_get_class(fd->path);
835
836                 switch (class)
837                         {
838                         case FORMAT_CLASS_IMAGE:
839                                 out_string = g_string_append(out_string, "    Class: Image");
840                                 break;
841                         case FORMAT_CLASS_RAWIMAGE:
842                                 out_string = g_string_append(out_string, "    Class: RAW image");
843                                 break;
844                         case FORMAT_CLASS_META:
845                                 out_string = g_string_append(out_string, "    Class: Metadata");
846                                 break;
847                         case FORMAT_CLASS_VIDEO:
848                                 out_string = g_string_append(out_string, "    Class: Video");
849                                 break;
850                         case FORMAT_CLASS_COLLECTION:
851                                 out_string = g_string_append(out_string, "    Class: Collection");
852                                 break;
853                         case FORMAT_CLASS_PDF:
854                                 out_string = g_string_append(out_string, "    Class: PDF");
855                                 break;
856                         case FORMAT_CLASS_UNKNOWN:
857                                 out_string = g_string_append(out_string, "    Class: Unknown");
858                                 break;
859                         default:
860                                 out_string = g_string_append(out_string, "    Class: Unknown");
861                                 break;
862                         }
863                 out_string = g_string_append(out_string, "\n");
864                 work = work->next;
865                 }
866
867         g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
868         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
869
870         g_string_free(out_string, TRUE);
871         filelist_free(list);
872         file_data_unref(dir_fd);
873 }
874
875 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
876 {
877         GString *contents = g_string_new(NULL);
878
879         if (is_collection(text))
880                 {
881                 collection_contents(text, &contents);
882                 }
883         else
884                 {
885                 return;
886                 }
887
888         g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
889         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
890
891         g_string_free(contents, TRUE);
892 }
893
894 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
895 {
896
897         GList *collection_list = NULL;
898         GList *work;
899         GString *out_string = g_string_new(NULL);
900
901         collect_manager_list(&collection_list, NULL, NULL);
902
903         work = collection_list;
904         while (work)
905                 {
906                 const gchar *collection_name = work->data;
907                 out_string = g_string_append(out_string, g_strdup(collection_name));
908                 out_string = g_string_append(out_string, "\n");
909
910                 work = work->next;
911                 }
912
913         g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
914         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
915
916         string_list_free(collection_list);
917         g_string_free(out_string, TRUE);
918 }
919
920
921 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
922 {
923         get_filelist(text, channel, FALSE);
924 }
925
926 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
927 {
928         get_filelist(text, channel, TRUE);
929 }
930
931 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
932 {
933         gchar *out_string;
934         gchar *collection_name = NULL;
935
936         if (!layout_valid(&lw_id)) return;
937
938         if (image_get_path(lw_id->image))
939                 {
940                 if (lw_id->image->collection && lw_id->image->collection->name)
941                         {
942                         collection_name = remove_extension_from_path(lw_id->image->collection->name);
943                         out_string = g_strconcat(image_get_path(lw_id->image), "    Collection: ", collection_name, NULL);
944                         }
945                 else
946                         {
947                         out_string = g_strconcat(image_get_path(lw_id->image), NULL);
948                         }
949
950                 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
951                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
952
953                 g_free(collection_name);
954                 g_free(out_string);
955                 }
956 }
957
958 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
959 {
960         gchar *filename = expand_tilde(text);
961
962         if (isfile(filename))
963                 {
964                 load_config_from_file(filename, FALSE);
965                 }
966         else
967                 {
968                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
969                 layout_set_path(NULL, homedir());
970                 }
971
972         g_free(filename);
973 }
974
975 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
976 {
977         gchar *filename = expand_tilde(text);
978         FileData *fd = file_data_new_group(filename);
979
980         GList *work;
981         if (fd->parent) fd = fd->parent;
982
983         g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
984         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
985
986         work = fd->sidecar_files;
987
988         while (work)
989                 {
990                 fd = work->data;
991                 work = work->next;
992                 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
993                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
994                 }
995         g_free(filename);
996 }
997
998 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
999 {
1000         gchar *filename = expand_tilde(text);
1001         FileData *fd = file_data_new_group(filename);
1002
1003         if (fd->change && fd->change->dest)
1004                 {
1005                 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
1006                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1007                 }
1008         g_free(filename);
1009 }
1010
1011 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
1012 {
1013         gchar *filename;
1014         gchar *tilde_filename = expand_tilde(text);
1015
1016         filename = set_pwd(tilde_filename);
1017
1018         view_window_new(file_data_new_group(filename));
1019         g_free(filename);
1020         g_free(tilde_filename);
1021 }
1022
1023 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
1024 {
1025         RemoteData *remote_data = data;
1026
1027         if (remote_data->command_collection)
1028                 {
1029                 collection_unref(remote_data->command_collection);
1030                 remote_data->command_collection = NULL;
1031                 }
1032 }
1033
1034 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
1035 {
1036         RemoteData *remote_data = data;
1037         gboolean new = TRUE;
1038
1039         if (!remote_data->command_collection)
1040                 {
1041                 CollectionData *cd;
1042
1043                 cd = collection_new("");
1044
1045                 g_free(cd->path);
1046                 cd->path = NULL;
1047                 g_free(cd->name);
1048                 cd->name = g_strdup(_("Command line"));
1049
1050                 remote_data->command_collection = cd;
1051                 }
1052         else
1053                 {
1054                 new = (!collection_get_first(remote_data->command_collection));
1055                 }
1056
1057         if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1058                 {
1059                 layout_image_set_collection(NULL, remote_data->command_collection,
1060                                             collection_get_first(remote_data->command_collection));
1061                 }
1062 }
1063
1064 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1065 {
1066         LayoutWindow *lw = NULL;
1067
1068         if (layout_valid(&lw_id))
1069                 {
1070                 gtk_window_present(GTK_WINDOW(lw_id->window));
1071                 }
1072 }
1073
1074 static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
1075 {
1076         LayoutWindow *lw = NULL;
1077
1078         g_free(pwd);
1079         pwd = g_strdup(text);
1080 }
1081
1082 #ifdef HAVE_LUA
1083 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1084 {
1085         gchar *result = NULL;
1086         gchar **lua_command;
1087
1088         lua_command = g_strsplit(text, ",", 2);
1089
1090         if (lua_command[0] && lua_command[1])
1091                 {
1092                 FileData *fd = file_data_new_group(lua_command[0]);
1093                 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1094                 if (result)
1095                         {
1096                         g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1097                         }
1098                 else
1099                         {
1100                         g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1101                         }
1102                 }
1103         else
1104                 {
1105                 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1106                 }
1107
1108         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
1109
1110         g_strfreev(lua_command);
1111         g_free(result);
1112 }
1113 #endif
1114
1115 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1116 struct _RemoteCommandEntry {
1117         gchar *opt_s;
1118         gchar *opt_l;
1119         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1120         gboolean needs_extra;
1121         gboolean prefer_command_line;
1122         gchar *parameter;
1123         gchar *description;
1124 };
1125
1126 static RemoteCommandEntry remote_commands[] = {
1127         /* short, long                  callback,               extra, prefer, parameter, description */
1128         { "-n", "--next",               gr_image_next,          FALSE, FALSE, NULL, N_("next image") },
1129         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, NULL, N_("previous image") },
1130         { NULL, "--first",              gr_image_first,         FALSE, FALSE, NULL, N_("first image") },
1131         { NULL, "--last",               gr_image_last,          FALSE, FALSE, NULL, N_("last image") },
1132         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  NULL, N_("toggle full screen") },
1133         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, NULL, N_("start full screen") },
1134         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, NULL, N_("stop full screen") },
1135         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  NULL, N_("toggle slide show") },
1136         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, NULL, N_("start slide show") },
1137         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, NULL, N_("stop slide show") },
1138         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1139         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1140         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
1141         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
1142         { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
1143         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
1144         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1145         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
1146         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1147         { NULL, "--file:",              gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1148         { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1149         { NULL, "--File:",              gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1150         { NULL, "--tell",               gr_file_tell,           FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1151         { NULL, "--pixel-info",         gr_pixel_info,          FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1152         { NULL, "--get-rectangle",      gr_rectangle,           FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1153         { NULL, "--get-render-intent",  gr_render_intent,       FALSE, FALSE, NULL, N_("get render intent") },
1154         { NULL, "--get-filelist:",      gr_filelist,            TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1155         { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1156         { NULL, "--get-collection:",    gr_collection,          TRUE,  FALSE, N_("<COLLECTION>"), N_("get collection content") },
1157         { NULL, "--get-collection-list", gr_collection_list,    FALSE, FALSE, NULL, N_("get collection list") },
1158         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1159         { NULL, "--view:",              gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1160         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL, N_("clear command line collection list") },
1161         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1162         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1163         { NULL, "--raise",              gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1164         { NULL, "--id:",                gr_lw_id,               TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1165         { NULL, "--new-window",         gr_new_window,          FALSE, FALSE, NULL, N_("new window") },
1166         { NULL, "--close-window",       gr_close_window,        FALSE, FALSE, NULL, N_("close window") },
1167         { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1168         { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1169         { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, NULL, N_("    clean the metadata cache") },
1170         { "-cr:", "--cache-render:",    gr_cache_render,        TRUE, FALSE, N_("<folder>  "), N_(" render thumbnails") },
1171         { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1172         { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1173         { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1174 #ifdef HAVE_LUA
1175         { NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1176 #endif
1177         { NULL, "--PWD:",               gr_pwd,                 TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1178         { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1179 };
1180
1181 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1182 {
1183         gboolean match = FALSE;
1184         gint i;
1185
1186         i = 0;
1187         while (!match && remote_commands[i].func != NULL)
1188                 {
1189                 if (remote_commands[i].needs_extra)
1190                         {
1191                         if (remote_commands[i].opt_s &&
1192                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1193                                 {
1194                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1195                                 return &remote_commands[i];
1196                                 }
1197                         else if (remote_commands[i].opt_l &&
1198                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1199                                 {
1200                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1201                                 return &remote_commands[i];
1202                                 }
1203                         }
1204                 else
1205                         {
1206                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1207                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1208                                 {
1209                                 if (offset) *offset = text;
1210                                 return &remote_commands[i];
1211                                 }
1212                         }
1213
1214                 i++;
1215                 }
1216
1217         return NULL;
1218 }
1219
1220 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1221 {
1222         RemoteCommandEntry *entry;
1223         const gchar *offset;
1224
1225         entry = remote_command_find(text, &offset);
1226         if (entry && entry->func)
1227                 {
1228                 entry->func(offset, channel, data);
1229                 }
1230         else
1231                 {
1232                 log_printf("unknown remote command:%s\n", text);
1233                 }
1234 }
1235
1236 void remote_help(void)
1237 {
1238         gint i;
1239         gchar *s_opt_param;
1240         gchar *l_opt_param;
1241
1242         print_term(FALSE, _("Remote command list:\n"));
1243
1244         i = 0;
1245         while (remote_commands[i].func != NULL)
1246                 {
1247                 if (remote_commands[i].description)
1248                         {
1249                         s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1250                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1251                         printf_term(FALSE, "  %-11s%-1s %-30s%-s\n",
1252                                     (remote_commands[i].opt_s) ? s_opt_param : "",
1253                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1254                                     (remote_commands[i].opt_l) ? l_opt_param : "",
1255                                     _(remote_commands[i].description));
1256                         g_free(s_opt_param);
1257                         g_free(l_opt_param);
1258                         }
1259                 i++;
1260                 }
1261         printf_term(FALSE, N_("\n  All other command line parameters are used as plain files if they exists.\n"));
1262 }
1263
1264 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1265 {
1266         gint i;
1267
1268         i = 1;
1269         while (i < argc)
1270                 {
1271                 RemoteCommandEntry *entry;
1272
1273                 entry = remote_command_find(argv[i], NULL);
1274                 if (entry)
1275                         {
1276                         list = g_list_append(list, argv[i]);
1277                         }
1278                 else if (errors && !isfile(argv[i]))
1279                         {
1280                         *errors = g_list_append(*errors, argv[i]);
1281                         }
1282                 i++;
1283                 }
1284
1285         return list;
1286 }
1287
1288 /**
1289  * \param arg_exec Binary (argv0)
1290  * \param remote_list Evaluated and recognized remote commands
1291  * \param path The current path
1292  * \param cmd_list List of all non collections in Path
1293  * \param collection_list List of all collections in argv
1294  */
1295 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1296                     GList *cmd_list, GList *collection_list)
1297 {
1298         RemoteConnection *rc;
1299         gboolean started = FALSE;
1300         gchar *buf;
1301
1302         buf = g_build_filename(get_rc_dir(), ".command", NULL);
1303         rc = remote_client_open(buf);
1304         if (!rc)
1305                 {
1306                 GString *command;
1307                 GList *work;
1308                 gint retry_count = 12;
1309                 gboolean blank = FALSE;
1310
1311                 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1312
1313                 command = g_string_new(arg_exec);
1314
1315                 work = remote_list;
1316                 while (work)
1317                         {
1318                         gchar *text;
1319                         RemoteCommandEntry *entry;
1320
1321                         text = work->data;
1322                         work = work->next;
1323
1324                         entry = remote_command_find(text, NULL);
1325                         if (entry)
1326                                 {
1327                                 if (entry->prefer_command_line)
1328                                         {
1329                                         remote_list = g_list_remove(remote_list, text);
1330                                         g_string_append(command, " ");
1331                                         g_string_append(command, text);
1332                                         }
1333                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1334                                         {
1335                                         blank = TRUE;
1336                                         }
1337                                 }
1338                         }
1339
1340                 if (blank || cmd_list || path) g_string_append(command, " --blank");
1341                 if (get_debug_level()) g_string_append(command, " --debug");
1342
1343                 g_string_append(command, " &");
1344                 runcmd(command->str);
1345                 g_string_free(command, TRUE);
1346
1347                 while (!rc && retry_count > 0)
1348                         {
1349                         usleep((retry_count > 10) ? 500000 : 1000000);
1350                         rc = remote_client_open(buf);
1351                         if (!rc) print_term(FALSE, ".");
1352                         retry_count--;
1353                         }
1354
1355                 print_term(FALSE, "\n");
1356
1357                 started = TRUE;
1358                 }
1359         g_free(buf);
1360
1361         if (rc)
1362                 {
1363                 GList *work;
1364                 const gchar *prefix;
1365                 gboolean use_path = TRUE;
1366                 gboolean sent = FALSE;
1367
1368                 work = remote_list;
1369                 while (work)
1370                         {
1371                         gchar *text;
1372                         RemoteCommandEntry *entry;
1373
1374                         text = work->data;
1375                         work = work->next;
1376
1377                         entry = remote_command_find(text, NULL);
1378                         if (entry &&
1379                             entry->opt_l &&
1380                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1381
1382                         remote_client_send(rc, text);
1383
1384                         sent = TRUE;
1385                         }
1386
1387                 if (cmd_list && cmd_list->next)
1388                         {
1389                         prefix = "--list-add:";
1390                         remote_client_send(rc, "--list-clear");
1391                         }
1392                 else
1393                         {
1394                         prefix = "file:";
1395                         }
1396
1397                 work = cmd_list;
1398                 while (work)
1399                         {
1400                         FileData *fd;
1401                         gchar *text;
1402
1403                         fd = work->data;
1404                         work = work->next;
1405
1406                         text = g_strconcat(prefix, fd->path, NULL);
1407                         remote_client_send(rc, text);
1408                         g_free(text);
1409
1410                         sent = TRUE;
1411                         }
1412
1413                 if (path && !cmd_list && use_path)
1414                         {
1415                         gchar *text;
1416
1417                         text = g_strdup_printf("file:%s", path);
1418                         remote_client_send(rc, text);
1419                         g_free(text);
1420
1421                         sent = TRUE;
1422                         }
1423
1424                 work = collection_list;
1425                 while (work)
1426                         {
1427                         const gchar *name;
1428                         gchar *text;
1429
1430                         name = work->data;
1431                         work = work->next;
1432
1433                         text = g_strdup_printf("file:%s", name);
1434                         remote_client_send(rc, text);
1435                         g_free(text);
1436
1437                         sent = TRUE;
1438                         }
1439
1440                 if (!started && !sent)
1441                         {
1442                         remote_client_send(rc, "raise");
1443                         }
1444                 }
1445         else
1446                 {
1447                 print_term(TRUE, _("Remote not available\n"));
1448                 }
1449
1450         _exit(0);
1451 }
1452
1453 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1454 {
1455         RemoteConnection *remote_connection = remote_server_open(path);
1456         RemoteData *remote_data = g_new(RemoteData, 1);
1457
1458         remote_data->command_collection = command_collection;
1459
1460         remote_server_subscribe(remote_connection, remote_cb, remote_data);
1461         return remote_connection;
1462 }
1463 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */