Implement remote keyboard actions
[geeqie.git] / src / remote.cc
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 "exif.h"
29 #include "filedata.h"
30 #include "filefilter.h"
31 #include "image.h"
32 #include "img-view.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 "utilops.h"
40 #include "rcfile.h"
41 #include "view-file.h"
42
43 #include <csignal>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46
47 #include "glua.h"
48
49 #define SERVER_MAX_CLIENTS 8
50
51 #define REMOTE_SERVER_BACKLOG 4
52
53
54 #ifndef UNIX_PATH_MAX
55 #define UNIX_PATH_MAX 108
56 #endif
57
58
59 static RemoteConnection *remote_client_open(const gchar *path);
60 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
61 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
62
63 static LayoutWindow *lw_id = nullptr; /* points to the window set by the --id option */
64
65 struct RemoteClient {
66         gint fd;
67         guint channel_id; /* event source id */
68         RemoteConnection *rc;
69 };
70
71 struct RemoteData {
72         CollectionData *command_collection;
73         GList *file_list;
74         gboolean single_dir;
75 };
76
77 /* To enable file names containing newlines to be processed correctly,
78  * the --print0 remote option sets returned data to be terminated with a null
79  * character rather a newline
80  */
81 static gboolean print0 = FALSE;
82
83 /* Remote commands from main.cc are prepended with the current dir the remote
84  * command was made from. Some remote commands require this. The
85  * value is stored here
86  */
87 static gchar *pwd = nullptr;
88
89 /**
90  * @brief Ensures file path is absolute.
91  * @param[in] filename Filepath, absolute or relative to calling directory
92  * @returns absolute path
93  *
94  * If first character of input filepath is not the directory
95  * separator, assume it as a relative path and prepend
96  * the directory the remote command was initiated from
97  *
98  * Return value must be freed with g_free()
99  */
100 static gchar *set_pwd(gchar *filename)
101 {
102         gchar *temp;
103
104         if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
105                 {
106                 temp = g_build_filename(pwd, filename, NULL);
107                 }
108         else
109                 {
110                 temp = g_strdup(filename);
111                 }
112
113         return temp;
114 }
115
116 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
117 {
118         auto client = static_cast<RemoteClient *>(data);
119         RemoteConnection *rc;
120         GIOStatus status = G_IO_STATUS_NORMAL;
121
122         lw_id = nullptr;
123         rc = client->rc;
124
125         if (condition & G_IO_IN)
126                 {
127                 gchar *buffer = nullptr;
128                 GError *error = nullptr;
129                 gsize termpos;
130                 /** @FIXME it should be possible to terminate the command with a null character */
131                 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
132                 while ((status = g_io_channel_read_line(source, &buffer, nullptr, &termpos, &error)) == G_IO_STATUS_NORMAL)
133                         {
134                         if (buffer)
135                                 {
136                                 buffer[termpos] = '\0';
137
138                                 if (strlen(buffer) > 0)
139                                         {
140                                         if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
141                                         g_io_channel_write_chars(source, "<gq_end_of_command>", -1, nullptr, nullptr); /* empty line finishes the command */
142                                         g_io_channel_flush(source, nullptr);
143                                         }
144                                 g_free(buffer);
145
146                                 buffer = nullptr;
147                                 }
148                         }
149
150                 if (error)
151                         {
152                         log_printf("error reading socket: %s\n", error->message);
153                         g_error_free(error);
154                         }
155                 }
156
157         if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
158                 {
159                 rc->clients = g_list_remove(rc->clients, client);
160
161                 DEBUG_1("HUP detected, closing client.");
162                 DEBUG_1("client count %d", g_list_length(rc->clients));
163
164                 g_source_remove(client->channel_id);
165                 close(client->fd);
166                 g_free(client);
167                 }
168
169         return TRUE;
170 }
171
172 static void remote_server_client_add(RemoteConnection *rc, gint fd)
173 {
174         RemoteClient *client;
175         GIOChannel *channel;
176
177         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
178                 {
179                 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
180                 close(fd);
181                 return;
182                 }
183
184         client = g_new0(RemoteClient, 1);
185         client->rc = rc;
186         client->fd = fd;
187
188         channel = g_io_channel_unix_new(fd);
189         client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, static_cast<GIOCondition>(G_IO_IN | G_IO_HUP),
190                                                  remote_server_client_cb, client, nullptr);
191         g_io_channel_unref(channel);
192
193         rc->clients = g_list_append(rc->clients, client);
194         DEBUG_1("client count %d", g_list_length(rc->clients));
195 }
196
197 static void remote_server_clients_close(RemoteConnection *rc)
198 {
199         while (rc->clients)
200                 {
201                 auto client = static_cast<RemoteClient *>(rc->clients->data);
202
203                 rc->clients = g_list_remove(rc->clients, client);
204
205                 g_source_remove(client->channel_id);
206                 close(client->fd);
207                 g_free(client);
208                 }
209 }
210
211 static gboolean remote_server_read_cb(GIOChannel *, GIOCondition, gpointer data)
212 {
213         auto rc = static_cast<RemoteConnection *>(data);
214         gint fd;
215         guint alen;
216
217         fd = accept(rc->fd, nullptr, &alen);
218         if (fd == -1)
219                 {
220                 log_printf("error accepting socket: %s\n", strerror(errno));
221                 return TRUE;
222                 }
223
224         remote_server_client_add(rc, fd);
225
226         return TRUE;
227 }
228
229 gboolean remote_server_exists(const gchar *path)
230 {
231         RemoteConnection *rc;
232
233         /* verify server up */
234         rc = remote_client_open(path);
235         remote_close(rc);
236
237         if (rc) return TRUE;
238
239         /* unable to connect, remove socket file to free up address */
240         unlink(path);
241         return FALSE;
242 }
243
244 static RemoteConnection *remote_server_open(const gchar *path)
245 {
246         RemoteConnection *rc;
247         struct sockaddr_un addr;
248         gint sun_path_len;
249         gint fd;
250         GIOChannel *channel;
251
252         if (remote_server_exists(path))
253                 {
254                 log_printf("Address already in use: %s\n", path);
255                 return nullptr;
256                 }
257
258         fd = socket(PF_UNIX, SOCK_STREAM, 0);
259         if (fd == -1) return nullptr;
260
261         addr.sun_family = AF_UNIX;
262         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
263         strncpy(addr.sun_path, path, sun_path_len);
264         if (bind(fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == -1 ||
265             listen(fd, REMOTE_SERVER_BACKLOG) == -1)
266                 {
267                 log_printf("error subscribing to socket: %s\n", strerror(errno));
268                 close(fd);
269                 return nullptr;
270                 }
271
272         rc = g_new0(RemoteConnection, 1);
273
274         rc->server = TRUE;
275         rc->fd = fd;
276         rc->path = g_strdup(path);
277
278         channel = g_io_channel_unix_new(rc->fd);
279         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, nullptr);
280
281         rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
282                                              remote_server_read_cb, rc, nullptr);
283         g_io_channel_unref(channel);
284
285         return rc;
286 }
287
288 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
289 {
290         if (!rc || !rc->server) return;
291
292         rc->read_func = func;
293         rc->read_data = data;
294 }
295
296
297 static RemoteConnection *remote_client_open(const gchar *path)
298 {
299         RemoteConnection *rc;
300         struct stat st;
301         struct sockaddr_un addr;
302         gint sun_path_len;
303         gint fd;
304
305         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return nullptr;
306
307         fd = socket(PF_UNIX, SOCK_STREAM, 0);
308         if (fd == -1) return nullptr;
309
310         addr.sun_family = AF_UNIX;
311         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
312         strncpy(addr.sun_path, path, sun_path_len);
313         if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
314                 {
315                 DEBUG_1("error connecting to socket: %s", strerror(errno));
316                 close(fd);
317                 return nullptr;
318                 }
319
320         rc = g_new0(RemoteConnection, 1);
321         rc->server = FALSE;
322         rc->fd = fd;
323         rc->path = g_strdup(path);
324
325         return rc;
326 }
327
328 static sig_atomic_t sigpipe_occurred = FALSE;
329
330 static void sighandler_sigpipe(gint)
331 {
332         sigpipe_occurred = TRUE;
333 }
334
335 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
336 {
337         struct sigaction new_action, old_action;
338         gboolean ret = FALSE;
339         GError *error = nullptr;
340         GIOChannel *channel;
341
342         if (!rc || rc->server) return FALSE;
343         if (!text) return TRUE;
344
345         sigpipe_occurred = FALSE;
346
347         new_action.sa_handler = sighandler_sigpipe;
348         sigemptyset(&new_action.sa_mask);
349         new_action.sa_flags = 0;
350
351         /* setup our signal handler */
352         sigaction(SIGPIPE, &new_action, &old_action);
353
354         channel = g_io_channel_unix_new(rc->fd);
355
356         g_io_channel_write_chars(channel, text, -1, nullptr, &error);
357         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, &error);
358         g_io_channel_flush(channel, &error);
359
360         if (error)
361                 {
362                 log_printf("error reading socket: %s\n", error->message);
363                 g_error_free(error);
364                 ret = FALSE;;
365                 }
366         else
367                 {
368                 ret = TRUE;
369                 }
370
371         if (ret)
372                 {
373                 gchar *buffer = nullptr;
374                 gsize termpos;
375                 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
376                 while (g_io_channel_read_line(channel, &buffer, nullptr, &termpos, &error) == G_IO_STATUS_NORMAL)
377                         {
378                         if (buffer)
379                                 {
380                                 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
381                                         {
382                                         g_free(buffer);
383                                         fflush(stdout);
384                                         break;
385                                         }
386                                 buffer[termpos] = '\0';
387                                 if (g_strstr_len(buffer, -1, "print0") != nullptr)
388                                         {
389                                         print0 = TRUE;
390                                         }
391                                 else
392                                         {
393                                         if (print0)
394                                                 {
395                                                 printf("%s%c", buffer, 0);
396                                                 }
397                                         else
398                                                 {
399                                                 printf("%s\n", buffer);
400                                                 }
401                                         }
402                                 g_free(buffer);
403                                 buffer = nullptr;
404                                 }
405                         }
406
407                 if (error)
408                         {
409                         log_printf("error reading socket: %s\n", error->message);
410                         g_error_free(error);
411                         ret = FALSE;
412                         }
413                 }
414
415
416         /* restore the original signal handler */
417         sigaction(SIGPIPE, &old_action, nullptr);
418         g_io_channel_unref(channel);
419         return ret;
420 }
421
422 void remote_close(RemoteConnection *rc)
423 {
424         if (!rc) return;
425
426         if (rc->server)
427                 {
428                 remote_server_clients_close(rc);
429
430                 g_source_remove(rc->channel_id);
431                 unlink(rc->path);
432                 }
433
434         if (rc->read_data)
435                 g_free(rc->read_data);
436
437         close(rc->fd);
438
439         g_free(rc->path);
440         g_free(rc);
441 }
442
443 /*
444  *-----------------------------------------------------------------------------
445  * remote functions
446  *-----------------------------------------------------------------------------
447  */
448
449 static void gr_image_next(const gchar *, GIOChannel *, gpointer)
450 {
451         layout_image_next(lw_id);
452 }
453
454 static void gr_new_window(const gchar *, GIOChannel *, gpointer)
455 {
456         LayoutWindow *lw = nullptr;
457
458         if (!layout_valid(&lw)) return;
459
460         lw_id = layout_new_from_default();
461
462         layout_set_path(lw_id, pwd);
463 }
464
465 static gboolean gr_close_window_cb(gpointer)
466 {
467         if (!layout_valid(&lw_id)) return FALSE;
468
469         layout_menu_close_cb(nullptr, lw_id);
470
471         return G_SOURCE_REMOVE;
472 }
473
474 static void gr_close_window(const gchar *, GIOChannel *, gpointer)
475 {
476         g_idle_add((gr_close_window_cb), nullptr);
477 }
478
479 static void gr_image_prev(const gchar *, GIOChannel *, gpointer)
480 {
481         layout_image_prev(lw_id);
482 }
483
484 static void gr_image_first(const gchar *, GIOChannel *, gpointer)
485 {
486         layout_image_first(lw_id);
487 }
488
489 static void gr_image_last(const gchar *, GIOChannel *, gpointer)
490 {
491         layout_image_last(lw_id);
492 }
493
494 static void gr_fullscreen_toggle(const gchar *, GIOChannel *, gpointer)
495 {
496         layout_image_full_screen_toggle(lw_id);
497 }
498
499 static void gr_fullscreen_start(const gchar *, GIOChannel *, gpointer)
500 {
501         layout_image_full_screen_start(lw_id);
502 }
503
504 static void gr_fullscreen_stop(const gchar *, GIOChannel *, gpointer)
505 {
506         layout_image_full_screen_stop(lw_id);
507 }
508
509 static void gr_lw_id(const gchar *text, GIOChannel *, gpointer)
510 {
511         lw_id = layout_find_by_layout_id(text);
512         if (!lw_id)
513                 {
514                 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
515                 }
516         layout_valid(&lw_id);
517 }
518
519 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *, gpointer)
520 {
521         GList *list;
522         gchar *tilde_filename;
523
524         tilde_filename = expand_tilde(text);
525
526         FileData *dir_fd = file_data_new_dir(tilde_filename);
527         g_free(tilde_filename);
528
529         layout_valid(&lw_id);
530         list = filelist_recursive_full(dir_fd, lw_id->options.file_view_list_sort.method, lw_id->options.file_view_list_sort.ascend, lw_id->options.file_view_list_sort.case_sensitive);
531         file_data_unref(dir_fd);
532         if (!list) return;
533
534         layout_image_slideshow_stop(lw_id);
535         layout_image_slideshow_start_from_list(lw_id, list);
536 }
537
538 static void gr_cache_thumb(const gchar *text, GIOChannel *, gpointer)
539 {
540         if (!g_strcmp0(text, "clear"))
541                 {
542                 cache_maintain_home_remote(FALSE, TRUE, nullptr);
543                 }
544         else if (!g_strcmp0(text, "clean"))
545                 {
546                 cache_maintain_home_remote(FALSE, FALSE, nullptr);
547                 }
548 }
549
550 static void gr_cache_shared(const gchar *text, GIOChannel *, gpointer)
551 {
552         if (!g_strcmp0(text, "clear"))
553                 cache_manager_standard_process_remote(TRUE);
554         else if (!g_strcmp0(text, "clean"))
555                 cache_manager_standard_process_remote(FALSE);
556 }
557
558 static void gr_cache_metadata(const gchar *, GIOChannel *, gpointer)
559 {
560         cache_maintain_home_remote(TRUE, FALSE, nullptr);
561 }
562
563 static void gr_cache_render(const gchar *text, GIOChannel *, gpointer)
564 {
565         cache_manager_render_remote(text, FALSE, FALSE, nullptr);
566 }
567
568 static void gr_cache_render_recurse(const gchar *text, GIOChannel *, gpointer)
569 {
570         cache_manager_render_remote(text, TRUE, FALSE, nullptr);
571 }
572
573 static void gr_cache_render_standard(const gchar *text, GIOChannel *, gpointer)
574 {
575         if(options->thumbnails.spec_standard)
576                 {
577                 cache_manager_render_remote(text, FALSE, TRUE, nullptr);
578                 }
579 }
580
581 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *, gpointer)
582 {
583         if(options->thumbnails.spec_standard)
584                 {
585                 cache_manager_render_remote(text, TRUE, TRUE, nullptr);
586                 }
587 }
588
589 static void gr_slideshow_toggle(const gchar *, GIOChannel *, gpointer)
590 {
591         layout_image_slideshow_toggle(lw_id);
592 }
593
594 static void gr_slideshow_start(const gchar *, GIOChannel *, gpointer)
595 {
596         layout_image_slideshow_start(lw_id);
597 }
598
599 static void gr_slideshow_stop(const gchar *, GIOChannel *, gpointer)
600 {
601         layout_image_slideshow_stop(lw_id);
602 }
603
604 static void gr_slideshow_delay(const gchar *text, GIOChannel *, gpointer)
605 {
606         gdouble t1, t2, t3, n;
607         gint res;
608
609         res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
610         if (res == 3)
611                 {
612                 n = (t1 * 3600) + (t2 * 60) + t3;
613                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
614                                 t1 >= 24 || t2 >= 60 || t3 >= 60)
615                         {
616                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
617                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
618                         return;
619                         }
620                 }
621         else if (res == 2)
622                 {
623                 n = t1 * 60 + t2;
624                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
625                                 t1 >= 60 || t2 >= 60)
626                         {
627                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
628                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
629                         return;
630                         }
631                 }
632         else if (res == 1)
633                 {
634                 n = t1;
635                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
636                         {
637                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
638                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
639                         return;
640                         }
641                 }
642         else
643                 {
644                 n = 0;
645                 }
646
647         options->slideshow.delay = static_cast<gint>(n * 10.0 + 0.01);
648 }
649
650 static void gr_tools_show(const gchar *, GIOChannel *, gpointer)
651 {
652         gboolean popped;
653         gboolean hidden;
654
655         if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
656                 {
657                 layout_tools_float_set(lw_id, popped, FALSE);
658                 }
659 }
660
661 static void gr_tools_hide(const gchar *, GIOChannel *, gpointer)
662 {
663         gboolean popped;
664         gboolean hidden;
665
666         if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
667                 {
668                 layout_tools_float_set(lw_id, popped, TRUE);
669                 }
670 }
671
672 static gboolean gr_quit_idle_cb(gpointer)
673 {
674         exit_program();
675
676         return G_SOURCE_REMOVE;
677 }
678
679 static void gr_quit(const gchar *, GIOChannel *, gpointer)
680 {
681         /* schedule exit when idle, if done from within a
682          * remote handler remote_close will crash
683          */
684         g_idle_add(gr_quit_idle_cb, nullptr);
685 }
686
687 static void gr_file_load_no_raise(const gchar *text, GIOChannel *, gpointer)
688 {
689         gchar *filename;
690         gchar *tilde_filename;
691
692         if (!download_web_file(text, TRUE, nullptr))
693                 {
694                 tilde_filename = expand_tilde(text);
695                 filename = set_pwd(tilde_filename);
696
697                 if (isfile(filename))
698                         {
699                         if (file_extension_match(filename, GQ_COLLECTION_EXT))
700                                 {
701                                 collection_window_new(filename);
702                                 }
703                         else
704                                 {
705                                 layout_set_path(lw_id, filename);
706                                 }
707                         }
708                 else if (isdir(filename))
709                         {
710                         layout_set_path(lw_id, filename);
711                         }
712                 else
713                         {
714                         log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
715                         layout_set_path(lw_id, homedir());
716                         }
717
718                 g_free(filename);
719                 g_free(tilde_filename);
720                 }
721 }
722
723 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
724 {
725         gr_file_load_no_raise(text, channel, data);
726
727         gr_raise(text, channel, data);
728 }
729
730 static void gr_pixel_info(const gchar *, GIOChannel *channel, gpointer)
731 {
732         gchar *pixel_info;
733         gint x_pixel, y_pixel;
734         gint width, height;
735         gint r_mouse, g_mouse, b_mouse;
736         PixbufRenderer *pr;
737
738         if (!layout_valid(&lw_id)) return;
739
740         pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
741
742         if (pr)
743                 {
744                 pixbuf_renderer_get_image_size(pr, &width, &height);
745                 if (width < 1 || height < 1) return;
746
747                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
748
749                 if (x_pixel >= 0 && y_pixel >= 0)
750                         {
751                         pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
752                                                          &r_mouse, &g_mouse, &b_mouse);
753
754                         pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
755                                                  x_pixel, y_pixel,
756                                                  r_mouse, g_mouse, b_mouse);
757
758                         g_io_channel_write_chars(channel, pixel_info, -1, nullptr, nullptr);
759                         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
760
761                         g_free(pixel_info);
762                         }
763                 else
764                         {
765                         return;
766                         }
767                 }
768         else
769                 {
770                 return;
771                 }
772 }
773
774 static void gr_rectangle(const gchar *, GIOChannel *channel, gpointer)
775 {
776         gchar *rectangle_info;
777         PixbufRenderer *pr;
778         gint x1, y1, x2, y2;
779
780         if (!options->draw_rectangle) return;
781         if (!layout_valid(&lw_id)) return;
782
783         pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
784
785         if (pr)
786                 {
787                 image_get_rectangle(&x1, &y1, &x2, &y2);
788                 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
789                                         (x2 > x1) ? x2 - x1 : x1 - x2,
790                                         (y2 > y1) ? y2 - y1 : y1 - y2,
791                                         (x2 > x1) ? x1 : x2,
792                                         (y2 > y1) ? y1 : y2);
793
794                 g_io_channel_write_chars(channel, rectangle_info, -1, nullptr, nullptr);
795                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
796
797                 g_free(rectangle_info);
798                 }
799 }
800
801 static void gr_render_intent(const gchar *, GIOChannel *channel, gpointer)
802 {
803         gchar *render_intent;
804
805         switch (options->color_profile.render_intent)
806                 {
807                 case 0:
808                         render_intent = g_strdup("Perceptual");
809                         break;
810                 case 1:
811                         render_intent = g_strdup("Relative Colorimetric");
812                         break;
813                 case 2:
814                         render_intent = g_strdup("Saturation");
815                         break;
816                 case 3:
817                         render_intent = g_strdup("Absolute Colorimetric");
818                         break;
819                 default:
820                         render_intent = g_strdup("none");
821                         break;
822                 }
823
824         g_io_channel_write_chars(channel, render_intent, -1, nullptr, nullptr);
825         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
826
827         g_free(render_intent);
828 }
829
830 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
831 {
832         GList *list = nullptr;
833         FileFormatClass format_class;
834         FileData *dir_fd;
835         FileData *fd;
836         GList *work;
837         gchar *tilde_filename;
838
839         if (strcmp(text, "") == 0)
840                 {
841                 if (layout_valid(&lw_id))
842                         {
843                         dir_fd = file_data_new_dir(lw_id->dir_fd->path);
844                         }
845                 else
846                         {
847                         return;
848                         }
849                 }
850         else
851                 {
852                 tilde_filename = expand_tilde(text);
853                 if (isdir(tilde_filename))
854                         {
855                         dir_fd = file_data_new_dir(tilde_filename);
856                         }
857                 else
858                         {
859                         g_free(tilde_filename);
860                         return;
861                         }
862                 g_free(tilde_filename);
863                 }
864
865         if (recurse)
866                 {
867                 list = filelist_recursive(dir_fd);
868                 }
869         else
870                 {
871                 filelist_read(dir_fd, &list, nullptr);
872                 }
873
874         GString *out_string = g_string_new(nullptr);
875         work = list;
876         while (work)
877                 {
878                 fd = static_cast<FileData *>(work->data);
879                 g_string_append_printf(out_string, "%s", fd->path);
880                 format_class = filter_file_get_class(fd->path);
881
882                 switch (format_class)
883                         {
884                         case FORMAT_CLASS_IMAGE:
885                                 out_string = g_string_append(out_string, "    Class: Image");
886                                 break;
887                         case FORMAT_CLASS_RAWIMAGE:
888                                 out_string = g_string_append(out_string, "    Class: RAW image");
889                                 break;
890                         case FORMAT_CLASS_META:
891                                 out_string = g_string_append(out_string, "    Class: Metadata");
892                                 break;
893                         case FORMAT_CLASS_VIDEO:
894                                 out_string = g_string_append(out_string, "    Class: Video");
895                                 break;
896                         case FORMAT_CLASS_COLLECTION:
897                                 out_string = g_string_append(out_string, "    Class: Collection");
898                                 break;
899                         case FORMAT_CLASS_DOCUMENT:
900                                 out_string = g_string_append(out_string, "    Class: Document");
901                                 break;
902                         case FORMAT_CLASS_ARCHIVE:
903                                 out_string = g_string_append(out_string, "    Class: Archive");
904                                 break;
905                         case FORMAT_CLASS_UNKNOWN:
906                                 out_string = g_string_append(out_string, "    Class: Unknown");
907                                 break;
908                         default:
909                                 out_string = g_string_append(out_string, "    Class: Unknown");
910                                 break;
911                         }
912                 out_string = g_string_append(out_string, "\n");
913                 work = work->next;
914                 }
915
916         g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
917         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
918
919         g_string_free(out_string, TRUE);
920         filelist_free(list);
921         file_data_unref(dir_fd);
922 }
923
924 static void gr_get_selection(const gchar *, GIOChannel *channel, gpointer)
925 {
926         if (!layout_valid(&lw_id)) return;
927
928         GList *selected = layout_selection_list(lw_id);  // Keep copy to free.
929         GString *out_string = g_string_new(nullptr);
930
931         GList *work = selected;
932         while (work)
933                 {
934                 auto fd = static_cast<FileData *>(work->data);
935                 g_assert(fd->magick == FD_MAGICK);
936
937                 g_string_append_printf(out_string, "%s    %s\n",
938                                        fd->path,
939                                        format_class_list[filter_file_get_class(fd->path)]);
940
941                 work = work->next;
942                 }
943
944         g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
945         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
946
947         filelist_free(selected);
948         g_string_free(out_string, TRUE);
949 }
950
951 static void gr_selection_add(const gchar *text, GIOChannel *, gpointer)
952 {
953         if (!layout_valid(&lw_id)) return;
954
955         FileData *fd_to_select = nullptr;
956         if (strcmp(text, "") == 0)
957                 {
958                 // No file specified, use current fd.
959                 fd_to_select = layout_image_get_fd(lw_id);
960                 }
961         else
962                 {
963                 // Search through the current file list for a file matching the specified path.
964                 // "Match" is either a basename match or a file path match.
965                 gchar *path = expand_tilde(text);
966                 gchar *filename = g_path_get_basename(path);
967                 gchar *slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
968
969                 GList *file_list = layout_list(lw_id);
970                 for (GList *work = file_list; work && !fd_to_select; work = work->next)
971                         {
972                         auto fd = static_cast<FileData *>(work->data);
973                         if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
974                                 {
975                                 fd_to_select = file_data_ref(fd);
976                                 continue;  // will exit loop.
977                                 }
978
979                         for (GList *sidecar = fd->sidecar_files; sidecar && !fd_to_select; sidecar = sidecar->next)
980                                 {
981                                 auto side_fd = static_cast<FileData *>(sidecar->data);
982                                 if (!strcmp(path, side_fd->path)
983                                     || g_str_has_suffix(side_fd->path, slash_plus_filename))
984                                         {
985                                         fd_to_select = file_data_ref(side_fd);
986                                         continue;  // will exit both nested loops.
987                                         }
988                                 }
989                         }
990
991                 if (!fd_to_select)
992                         {
993                         log_printf("remote sent --selection-add filename that could not be found: \"%s\"\n",
994                                    filename);
995                         }
996
997                 filelist_free(file_list);
998                 g_free(slash_plus_filename);
999                 g_free(filename);
1000                 g_free(path);
1001                 }
1002
1003         if (fd_to_select)
1004                 {
1005                 GList *to_select = g_list_append(nullptr, fd_to_select);
1006                 // Using the "_list" variant doesn't clear the existing selection.
1007                 layout_select_list(lw_id, to_select);
1008                 filelist_free(to_select);
1009                 }
1010 }
1011
1012 static void gr_selection_clear(const gchar *, GIOChannel *, gpointer)
1013 {
1014         layout_select_none(lw_id);  // Checks lw_id validity internally.
1015 }
1016
1017 static void gr_selection_remove(const gchar *text, GIOChannel *, gpointer)
1018 {
1019         if (!layout_valid(&lw_id)) return;
1020
1021         GList *selected = layout_selection_list(lw_id);  // Keep copy to free.
1022         if (!selected)
1023                 {
1024                 log_printf("remote sent --selection-remove with empty selection.");
1025                 return;
1026                 }
1027
1028         FileData *fd_to_deselect = nullptr;
1029         gchar *path = nullptr;
1030         gchar *filename = nullptr;
1031         gchar *slash_plus_filename = nullptr;
1032         if (strcmp(text, "") == 0)
1033                 {
1034                 // No file specified, use current fd.
1035                 fd_to_deselect = layout_image_get_fd(lw_id);
1036                 if (!fd_to_deselect)
1037                         {
1038                         log_printf("remote sent \"--selection-remove:\" with no current image");
1039                         filelist_free(selected);
1040                         return;
1041                         }
1042                 }
1043         else
1044                 {
1045                 // Search through the selection list for a file matching the specified path.
1046                 // "Match" is either a basename match or a file path match.
1047                 path = expand_tilde(text);
1048                 filename = g_path_get_basename(path);
1049                 slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
1050                 }
1051
1052         GList *prior_link = nullptr;  // Stash base for link removal to avoid a second traversal.
1053         GList *link_to_remove = nullptr;
1054         for (GList *work = selected; work; prior_link = work, work = work->next)
1055                 {
1056                 auto fd = static_cast<FileData *>(work->data);
1057                 if (fd_to_deselect)
1058                         {
1059                         if (fd == fd_to_deselect)
1060                                 {
1061                                 link_to_remove = work;
1062                                 break;
1063                                 }
1064                         }
1065                 else
1066                         {
1067                         // path, filename, and slash_plus_filename should be defined.
1068
1069                         if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
1070                                 {
1071                                 link_to_remove = work;
1072                                 break;
1073                                 }
1074                         }
1075                 }
1076
1077         if (!link_to_remove)
1078                 {
1079                 if (fd_to_deselect)
1080                         {
1081                         log_printf("remote sent \"--selection-remove:\" but current image is not selected");
1082                         }
1083                 else
1084                         {
1085                         log_printf("remote sent \"--selection-remove:%s\" but that filename is not selected",
1086                                    filename);
1087                         }
1088                 }
1089         else
1090                 {
1091                 if (link_to_remove == selected)
1092                         {
1093                         // Remove first link.
1094                         selected = g_list_remove_link(selected, link_to_remove);
1095                         filelist_free(link_to_remove);
1096                         link_to_remove = nullptr;
1097                         }
1098                 else
1099                         {
1100                         // Remove a subsequent link.
1101                         prior_link = g_list_remove_link(prior_link, link_to_remove);
1102                         filelist_free(link_to_remove);
1103                         link_to_remove = nullptr;
1104                         }
1105
1106                 // Re-select all but the deselected item.
1107                 layout_select_none(lw_id);
1108                 layout_select_list(lw_id, selected);
1109                 }
1110
1111         filelist_free(selected);
1112         file_data_unref(fd_to_deselect);
1113         g_free(slash_plus_filename);
1114         g_free(filename);
1115         g_free(path);
1116 }
1117
1118 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer)
1119 {
1120         GString *contents = g_string_new(nullptr);
1121
1122         if (is_collection(text))
1123                 {
1124                 collection_contents(text, &contents);
1125                 }
1126         else
1127                 {
1128                 return;
1129                 }
1130
1131         g_io_channel_write_chars(channel, contents->str, -1, nullptr, nullptr);
1132         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1133
1134         g_string_free(contents, TRUE);
1135 }
1136
1137 static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
1138 {
1139
1140         GList *collection_list = nullptr;
1141         GList *work;
1142         GString *out_string = g_string_new(nullptr);
1143
1144         collect_manager_list(&collection_list, nullptr, nullptr);
1145
1146         work = collection_list;
1147         while (work)
1148                 {
1149                 auto collection_name = static_cast<const gchar *>(work->data);
1150                 out_string = g_string_append(out_string, g_strdup(collection_name));
1151                 out_string = g_string_append(out_string, "\n");
1152
1153                 work = work->next;
1154                 }
1155
1156         g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1157         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1158
1159         g_list_free_full(collection_list, g_free);
1160         g_string_free(out_string, TRUE);
1161 }
1162
1163 static gboolean wait_cb(gpointer data)
1164 {
1165         gint position = GPOINTER_TO_INT(data);
1166         gint x = position >> 16;
1167         gint y = position - (x << 16);
1168
1169         gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
1170
1171         return G_SOURCE_REMOVE;
1172 }
1173
1174 static void gr_geometry(const gchar *text, GIOChannel *, gpointer)
1175 {
1176         gchar **geometry;
1177
1178         if (!layout_valid(&lw_id) || !text)
1179                 {
1180                 return;
1181                 }
1182
1183         if (text[0] == '+')
1184                 {
1185                 geometry = g_strsplit_set(text, "+", 3);
1186                 if (geometry[1] != nullptr && geometry[2] != nullptr )
1187                         {
1188                         gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1189                         }
1190                 }
1191         else
1192                 {
1193                 geometry = g_strsplit_set(text, "+x", 4);
1194                 if (geometry[0] != nullptr && geometry[1] != nullptr)
1195                         {
1196                         gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1197                         }
1198                 if (geometry[2] != nullptr && geometry[3] != nullptr)
1199                         {
1200                         /* There is an occasional problem with a window_move immediately after a window_resize */
1201                         g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1202                         }
1203                 }
1204         g_strfreev(geometry);
1205 }
1206
1207 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer)
1208 {
1209         get_filelist(text, channel, FALSE);
1210 }
1211
1212 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer)
1213 {
1214         get_filelist(text, channel, TRUE);
1215 }
1216
1217 static void gr_file_tell(const gchar *, GIOChannel *channel, gpointer)
1218 {
1219         gchar *out_string;
1220         gchar *collection_name = nullptr;
1221
1222         if (!layout_valid(&lw_id)) return;
1223
1224         if (image_get_path(lw_id->image))
1225                 {
1226                 if (lw_id->image->collection && lw_id->image->collection->name)
1227                         {
1228                         collection_name = remove_extension_from_path(lw_id->image->collection->name);
1229                         out_string = g_strconcat(image_get_path(lw_id->image), "    Collection: ", collection_name, NULL);
1230                         }
1231                 else
1232                         {
1233                         out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1234                         }
1235                 }
1236         else
1237                 {
1238                 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1239                 }
1240
1241         g_io_channel_write_chars(channel, out_string, -1, nullptr, nullptr);
1242         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1243
1244         g_free(collection_name);
1245         g_free(out_string);
1246 }
1247
1248 static void gr_file_info(const gchar *, GIOChannel *channel, gpointer)
1249 {
1250         gchar *filename;
1251         FileData *fd;
1252         gchar *country_name;
1253         gchar *country_code;
1254         gchar *timezone;
1255         gchar *local_time;
1256         GString *out_string;
1257         FileFormatClass format_class;
1258
1259         if (!layout_valid(&lw_id)) return;
1260
1261         if (image_get_path(lw_id->image))
1262                 {
1263                 filename = g_strdup(image_get_path(lw_id->image));
1264                 fd = file_data_new_group(filename);
1265                 out_string = g_string_new(nullptr);
1266
1267                 if (fd->pixbuf)
1268                         {
1269                         format_class = filter_file_get_class(image_get_path(lw_id->image));
1270                         }
1271                 else
1272                         {
1273                         format_class = FORMAT_CLASS_UNKNOWN;
1274                         }
1275
1276                 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1277
1278                 if (fd->page_total > 1)
1279                         {
1280                         g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1281                         }
1282
1283                 if (fd->exif)
1284                         {
1285                         country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1286                         if (country_name)
1287                                 {
1288                                 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1289                                 g_free(country_name);
1290                                 }
1291
1292                         country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1293                         if (country_name)
1294                                 {
1295                                 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1296                                 g_free(country_code);
1297                                 }
1298
1299                         timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1300                         if (timezone)
1301                                 {
1302                                 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1303                                 g_free(timezone);
1304                                 }
1305
1306                         local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1307                         if (local_time)
1308                                 {
1309                                 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1310                                 g_free(local_time);
1311                                 }
1312                         }
1313
1314                 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1315                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1316
1317                 g_string_free(out_string, TRUE);
1318                 file_data_unref(fd);
1319                 g_free(filename);
1320                 }
1321 }
1322
1323 static gchar *config_file_path(const gchar *param)
1324 {
1325         gchar *path = nullptr;
1326         gchar *full_name = nullptr;
1327
1328         if (file_extension_match(param, ".xml"))
1329                 {
1330                 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1331                 }
1332         else if (file_extension_match(param, nullptr))
1333                 {
1334                 full_name = g_strconcat(param, ".xml", NULL);
1335                 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1336                 }
1337
1338         if (!isfile(path))
1339                 {
1340                 g_free(path);
1341                 path = nullptr;
1342                 }
1343
1344         g_free(full_name);
1345         return path;
1346 }
1347
1348 static gboolean is_config_file(const gchar *param)
1349 {
1350         gchar *name = nullptr;
1351
1352         name = config_file_path(param);
1353         if (name)
1354                 {
1355                 g_free(name);
1356                 return TRUE;
1357                 }
1358         return FALSE;
1359 }
1360
1361 static void gr_config_load(const gchar *text, GIOChannel *, gpointer)
1362 {
1363         gchar *filename = expand_tilde(text);
1364
1365         if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1366                 {
1367                 if (is_config_file(filename))
1368                         {
1369                         gchar *tmp = config_file_path(filename);
1370                         g_free(filename);
1371                         filename = tmp;
1372                         }
1373                 }
1374
1375         if (isfile(filename))
1376                 {
1377                 load_config_from_file(filename, FALSE);
1378                 }
1379         else
1380                 {
1381                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1382                 layout_set_path(nullptr, homedir());
1383                 }
1384
1385         g_free(filename);
1386 }
1387
1388 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer)
1389 {
1390         gchar *filename = expand_tilde(text);
1391         FileData *fd = file_data_new_group(filename);
1392
1393         GList *work;
1394         if (fd->parent) fd = fd->parent;
1395
1396         g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1397         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1398
1399         work = fd->sidecar_files;
1400
1401         while (work)
1402                 {
1403                 fd = static_cast<FileData *>(work->data);
1404                 work = work->next;
1405                 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1406                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1407                 }
1408         g_free(filename);
1409 }
1410
1411 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer)
1412 {
1413         gchar *filename = expand_tilde(text);
1414         FileData *fd = file_data_new_group(filename);
1415
1416         if (fd->change && fd->change->dest)
1417                 {
1418                 g_io_channel_write_chars(channel, fd->change->dest, -1, nullptr, nullptr);
1419                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1420                 }
1421         g_free(filename);
1422 }
1423
1424 static void gr_file_view(const gchar *text, GIOChannel *, gpointer)
1425 {
1426         gchar *filename;
1427         gchar *tilde_filename = expand_tilde(text);
1428
1429         filename = set_pwd(tilde_filename);
1430
1431         view_window_new(file_data_new_group(filename));
1432         g_free(filename);
1433         g_free(tilde_filename);
1434 }
1435
1436 static void gr_list_clear(const gchar *, GIOChannel *, gpointer data)
1437 {
1438         auto remote_data = static_cast<RemoteData *>(data);
1439
1440         remote_data->command_collection = nullptr;
1441         remote_data->file_list = nullptr;
1442         remote_data->single_dir = TRUE;
1443 }
1444
1445 static void gr_list_add(const gchar *text, GIOChannel *, gpointer data)
1446 {
1447         auto remote_data = static_cast<RemoteData *>(data);
1448         gboolean is_new = TRUE;
1449         gchar *path = nullptr;
1450         FileData *fd;
1451         FileData *first;
1452
1453         /** @FIXME Should check if file is in current dir, has tilde or is relative */
1454         if (!isfile(text))
1455                 {
1456                 log_printf("Warning: File does not exist --remote --list-add:%s", text);
1457
1458                 return;
1459                 }
1460
1461         /* If there is a files list on the command line
1462          * check if they are all in the same folder
1463          */
1464         if (remote_data->single_dir)
1465                 {
1466                 GList *work;
1467                 work = remote_data->file_list;
1468                 while (work && remote_data->single_dir)
1469                         {
1470                         gchar *dirname;
1471                         dirname = g_path_get_dirname((static_cast<FileData *>(work->data))->path);
1472                         if (!path)
1473                                 {
1474                                 path = g_strdup(dirname);
1475                                 }
1476                         else
1477                                 {
1478                                 if (g_strcmp0(path, dirname) != 0)
1479                                         {
1480                                         remote_data->single_dir = FALSE;
1481                                         }
1482                                 }
1483                         g_free(dirname);
1484                         work = work->next;
1485                         }
1486                 g_free(path);
1487                 }
1488
1489         gchar *pathname = g_path_get_dirname(text);
1490         layout_set_path(lw_id, pathname);
1491         g_free(pathname);
1492
1493         fd = file_data_new_simple(text);
1494         remote_data->file_list = g_list_append(remote_data->file_list, fd);
1495         file_data_unref(fd);
1496
1497         vf_select_none(lw_id->vf);
1498         remote_data->file_list = g_list_reverse(remote_data->file_list);
1499
1500         layout_select_list(lw_id, remote_data->file_list);
1501         layout_refresh(lw_id);
1502         first = static_cast<FileData *>(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1503         layout_set_fd(lw_id, first);
1504
1505                 CollectionData *cd;
1506                 CollectWindow *cw;
1507
1508         if (!remote_data->command_collection && !remote_data->single_dir)
1509                 {
1510                 cw = collection_window_new(nullptr);
1511                 cd = cw->cd;
1512
1513                 collection_path_changed(cd);
1514
1515                 remote_data->command_collection = cd;
1516                 }
1517         else if (!remote_data->single_dir)
1518                 {
1519                 is_new = (!collection_get_first(remote_data->command_collection));
1520                 }
1521
1522         if (!remote_data->single_dir)
1523                 {
1524                 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1525                 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && is_new)
1526                         {
1527                         layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1528                         }
1529                 }
1530 }
1531
1532 static void gr_action(const gchar *text, GIOChannel *, gpointer)
1533 {
1534         GtkAction *action;
1535
1536         if (!layout_valid(&lw_id))
1537                 {
1538                 return;
1539                 }
1540
1541         if (g_strstr_len(text, -1, ".desktop") != nullptr)
1542                 {
1543                 file_util_start_editor_from_filelist(text, layout_selection_list(lw_id), layout_get_path(lw_id), lw_id->window);
1544                 }
1545         else
1546                 {
1547                 action = gtk_action_group_get_action(lw_id->action_group, text);
1548                 if (action)
1549                         {
1550                         gtk_action_activate(action);
1551                         }
1552                 else
1553                         {
1554                         log_printf("Action %s unknown", text);
1555                         }
1556                 }
1557 }
1558
1559 static gint simple_sort(gconstpointer a, gconstpointer b)
1560 {
1561         return g_strcmp0((gchar *)a, (gchar *)b);
1562 }
1563
1564 static void gr_action_list(const gchar *, GIOChannel *channel, gpointer)
1565 {
1566         const gchar *accel_path;
1567         gchar *action_list;
1568         gchar *action_name;
1569         gchar *comment_list;
1570         gchar *label;
1571         gchar *tooltip;
1572         gint max_length = 0;
1573         GList *actions;
1574         GList *groups;
1575         GList *list_final = nullptr;
1576         GList *list = nullptr;
1577         GList *work;
1578         GString *out_string = g_string_new(nullptr);
1579         GtkAction *action;
1580
1581         if (!layout_valid(&lw_id))
1582                 {
1583                 return;
1584                 }
1585
1586         groups = gtk_ui_manager_get_action_groups(lw_id->ui_manager);
1587         while (groups)
1588                 {
1589                 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
1590                 while (actions)
1591                         {
1592                         action = GTK_ACTION(actions->data);
1593                         accel_path = gtk_action_get_accel_path(action);
1594
1595                                 if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
1596                                         {
1597                                         g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
1598
1599                                         action_name = g_path_get_basename(accel_path);
1600
1601                                         /* Used for output column padding */
1602                                         if (g_utf8_strlen(action_name, -1) > max_length)
1603                                                 {
1604                                                 max_length = g_utf8_strlen(action_name, -1);
1605                                                 }
1606
1607                                         /* Tooltips with newlines affect output format */
1608                                         if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
1609                                                 {
1610                                                 list = g_list_prepend(list, g_strdup(tooltip));
1611                                                 }
1612                                         else
1613                                                 {
1614                                                 list = g_list_prepend(list, g_strdup(label));
1615                                                 }
1616
1617                                         list = g_list_prepend(list, g_strdup(action_name));
1618
1619                                         g_free(action_name);
1620                                         g_free(label);
1621                                         g_free(tooltip);
1622                                         }
1623
1624                         actions = actions->next;
1625                         }
1626
1627                 groups = groups->next;
1628                 }
1629
1630         /* Pad the action names to the same column for readable output */
1631         work = list;
1632         while (work)
1633                 {
1634                 /* Menu actions are irrelevant */
1635                 if (g_strstr_len(static_cast<gchar *>(work->data), -1, "Menu") == nullptr)
1636                         {
1637                         action_list = g_strdup_printf("%-*s", max_length + 4, static_cast<gchar *>(work->data));
1638
1639                         work=work->next;
1640
1641                         comment_list = static_cast<gchar *>(work->data);
1642                         list_final = g_list_prepend(list_final, g_strconcat(action_list, comment_list, nullptr));
1643
1644                         g_free(action_list);
1645                         }
1646                 else
1647                         {
1648                         work = work->next;
1649                         }
1650                 work = work->next;
1651                 }
1652
1653         string_list_free(list);
1654
1655         list_final = g_list_sort(list_final, simple_sort);
1656
1657         work = list_final;
1658         while (work)
1659                 {
1660                 out_string = g_string_append(out_string, static_cast<gchar *>(work->data) );
1661                 out_string = g_string_append(out_string, "\n");
1662                 work = work->next;
1663                 }
1664
1665         string_list_free(list_final);
1666
1667         g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1668         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1669
1670         g_string_free(out_string, TRUE);
1671 }
1672
1673 static void gr_raise(const gchar *, GIOChannel *, gpointer)
1674 {
1675         if (layout_valid(&lw_id))
1676                 {
1677                 gtk_window_present(GTK_WINDOW(lw_id->window));
1678                 }
1679 }
1680
1681 static void gr_pwd(const gchar *text, GIOChannel *, gpointer)
1682 {
1683         LayoutWindow *lw = nullptr;
1684
1685         layout_valid(&lw);
1686
1687         g_free(pwd);
1688         pwd = g_strdup(text);
1689         lw_id = lw;
1690 }
1691
1692 static void gr_print0(const gchar *, GIOChannel *channel, gpointer)
1693 {
1694         g_io_channel_write_chars(channel, "print0", -1, nullptr, nullptr);
1695         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1696 }
1697
1698 #ifdef HAVE_LUA
1699 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer)
1700 {
1701         gchar *result = nullptr;
1702         gchar **lua_command;
1703
1704         lua_command = g_strsplit(text, ",", 2);
1705
1706         if (lua_command[0] && lua_command[1])
1707                 {
1708                 FileData *fd = file_data_new_group(lua_command[0]);
1709                 result = g_strdup(lua_callvalue(fd, lua_command[1], nullptr));
1710                 if (result)
1711                         {
1712                         g_io_channel_write_chars(channel, result, -1, nullptr, nullptr);
1713                         }
1714                 else
1715                         {
1716                         g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1717                         }
1718                 }
1719         else
1720                 {
1721                 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1722                 }
1723
1724         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1725
1726         g_strfreev(lua_command);
1727         g_free(result);
1728 }
1729 #endif
1730
1731 struct RemoteCommandEntry {
1732         const gchar *opt_s;
1733         const gchar *opt_l;
1734         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1735         gboolean needs_extra;
1736         gboolean prefer_command_line;
1737         const gchar *parameter;
1738         const gchar *description;
1739 };
1740
1741 static RemoteCommandEntry remote_commands[] = {
1742         /* short, long                  callback,               extra, prefer, parameter, description */
1743         { nullptr, "--action:",          gr_action,            TRUE,  FALSE, N_("<ACTION>"), N_("execute keyboard action (See Help/Reference/Remote Keyboard Actions)") },
1744         { nullptr, "--action-list",          gr_action_list,    FALSE,  FALSE, nullptr, N_("list available keyboard actions (some are redundant)") },
1745         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, nullptr, N_("previous image") },
1746         { nullptr, "--close-window",       gr_close_window,        FALSE, FALSE, nullptr, N_("close window") },
1747         { nullptr, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1748         { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, nullptr, N_("clean the metadata cache") },
1749         { "-cr:", "--cache-render:",    gr_cache_render,        TRUE, FALSE, N_("<folder>  "), N_(" render thumbnails") },
1750         { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1751         { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1752         { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1753         { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1754         { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1755         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1756         { nullptr, "--first",              gr_image_first,         FALSE, FALSE, nullptr, N_("first image") },
1757         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  nullptr, N_("toggle full screen") },
1758         { nullptr, "--file:",              gr_file_load,           TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1759         { nullptr, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1760         { nullptr, "--File:",              gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1761         { nullptr, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1762         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, nullptr, N_("start full screen") },
1763         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, nullptr, N_("stop full screen") },
1764         { nullptr, "--geometry=",          gr_geometry,            TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1765         { nullptr, "--get-collection:",    gr_collection,          TRUE,  FALSE, N_("<COLLECTION>"), N_("get collection content") },
1766         { nullptr, "--get-collection-list", gr_collection_list,    FALSE, FALSE, nullptr, N_("get collection list") },
1767         { nullptr, "--get-destination:",        gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1768         { nullptr, "--get-file-info",      gr_file_info,           FALSE, FALSE, nullptr, N_("get file info") },
1769         { nullptr, "--get-filelist:",      gr_filelist,            TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1770         { nullptr, "--get-filelist-recurse:", gr_filelist_recurse, TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1771         { nullptr, "--get-rectangle",      gr_rectangle,           FALSE, FALSE, nullptr, N_("get rectangle co-ordinates") },
1772         { nullptr, "--get-render-intent",  gr_render_intent,       FALSE, FALSE, nullptr, N_("get render intent") },
1773         { nullptr, "--get-selection",      gr_get_selection,       FALSE, FALSE, nullptr, N_("get list of selected files") },
1774         { nullptr, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1775         { nullptr, "--id:",                gr_lw_id,               TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1776         { nullptr, "--last",               gr_image_last,          FALSE, FALSE, nullptr, N_("last image") },
1777         { nullptr, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1778         { nullptr, "--list-clear",         gr_list_clear,          FALSE, FALSE, nullptr, N_("clear command line collection list") },
1779 #ifdef HAVE_LUA
1780         { nullptr, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1781 #endif
1782         { nullptr, "--new-window",         gr_new_window,          FALSE, FALSE, nullptr, N_("new window") },
1783         { "-n", "--next",               gr_image_next,          FALSE, FALSE, nullptr, N_("next image") },
1784         { nullptr, "--pixel-info",         gr_pixel_info,          FALSE, FALSE, nullptr, N_("print pixel info of mouse pointer on current image") },
1785         { nullptr, "--print0",             gr_print0,              TRUE, FALSE, nullptr, N_("terminate returned data with null character instead of newline") },
1786         { nullptr, "--PWD:",               gr_pwd,                 TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1787         { "-q", "--quit",               gr_quit,                FALSE, FALSE, nullptr, N_("quit") },
1788         { nullptr, "--raise",              gr_raise,               FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1789         { nullptr, "raise",                gr_raise,               FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1790         { nullptr, "--selection-add:",     gr_selection_add,       TRUE,  FALSE, N_("[<FILE>]"), N_("adds the current file (or the specified file) to the current selection") },
1791         { nullptr, "--selection-clear",    gr_selection_clear,     FALSE, FALSE, nullptr, N_("clears the current selection") },
1792         { nullptr, "--selection-remove:",  gr_selection_remove,    TRUE,  FALSE, N_("[<FILE>]"), N_("removes the current file (or the specified file) from the current selection") },
1793         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  nullptr, N_("toggle slide show") },
1794         { nullptr, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1795         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, nullptr, N_("start slide show") },
1796         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, nullptr, N_("stop slide show") },
1797         { nullptr, "--tell",               gr_file_tell,           FALSE, FALSE, nullptr, N_("print filename [and Collection] of current image") },
1798         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  nullptr, N_("show tools") },
1799         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  nullptr, N_("hide tools") },
1800         { nullptr, "--view:",              gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1801         { nullptr, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1802         { nullptr, nullptr, nullptr, FALSE, FALSE, nullptr, nullptr }
1803 };
1804
1805 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1806 {
1807         gboolean match = FALSE;
1808         gint i;
1809
1810         i = 0;
1811         while (!match && remote_commands[i].func != nullptr)
1812                 {
1813                 if (remote_commands[i].needs_extra)
1814                         {
1815                         if (remote_commands[i].opt_s &&
1816                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1817                                 {
1818                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1819                                 return &remote_commands[i];
1820                                 }
1821                         else if (remote_commands[i].opt_l &&
1822                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1823                                 {
1824                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1825                                 return &remote_commands[i];
1826                                 }
1827                         }
1828                 else
1829                         {
1830                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1831                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1832                                 {
1833                                 if (offset) *offset = text;
1834                                 return &remote_commands[i];
1835                                 }
1836                         }
1837
1838                 i++;
1839                 }
1840
1841         return nullptr;
1842 }
1843
1844 static void remote_cb(RemoteConnection *, const gchar *text, GIOChannel *channel, gpointer data)
1845 {
1846         RemoteCommandEntry *entry;
1847         const gchar *offset;
1848
1849         entry = remote_command_find(text, &offset);
1850         if (entry && entry->func)
1851                 {
1852                 entry->func(offset, channel, data);
1853                 }
1854         else
1855                 {
1856                 log_printf("unknown remote command:%s\n", text);
1857                 }
1858 }
1859
1860 void remote_help()
1861 {
1862         gint i;
1863         gchar *s_opt_param;
1864         gchar *l_opt_param;
1865
1866         print_term(FALSE, _("Remote command list:\n"));
1867
1868         i = 0;
1869         while (remote_commands[i].func != nullptr)
1870                 {
1871                 if (remote_commands[i].description)
1872                         {
1873                         s_opt_param = g_strdup(remote_commands[i].opt_s  ? remote_commands[i].opt_s : "" );
1874                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1875                         printf_term(FALSE, "  %-4s %-40s%-s\n",
1876                                         s_opt_param,
1877                                         l_opt_param,
1878                                         _(remote_commands[i].description));
1879                         g_free(s_opt_param);
1880                         g_free(l_opt_param);
1881                         }
1882                 i++;
1883                 }
1884         printf_term(FALSE, _("\n\n  All other command line parameters are used as plain files if they exist.\n\n  The name of a collection, with or without either path or extension (.gqv) may be used.\n"));
1885 }
1886
1887 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1888 {
1889         gint i;
1890
1891         i = 1;
1892         while (i < argc)
1893                 {
1894                 RemoteCommandEntry *entry;
1895
1896                 entry = remote_command_find(argv[i], nullptr);
1897                 if (entry)
1898                         {
1899                         list = g_list_append(list, argv[i]);
1900                         }
1901                 else if (errors && !isname(argv[i]))
1902                         {
1903                         *errors = g_list_append(*errors, argv[i]);
1904                         }
1905                 i++;
1906                 }
1907
1908         return list;
1909 }
1910
1911 /**
1912  * @param arg_exec Binary (argv0)
1913  * @param remote_list Evaluated and recognized remote commands
1914  * @param path The current path
1915  * @param cmd_list List of all non collections in Path (gchar *path)
1916  * @param collection_list List of all collections in argv
1917  */
1918 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1919                     GList *cmd_list, GList *collection_list)
1920 {
1921         RemoteConnection *rc;
1922         gboolean started = FALSE;
1923         gchar *buf;
1924
1925         buf = g_build_filename(get_rc_dir(), ".command", NULL);
1926         rc = remote_client_open(buf);
1927         if (!rc)
1928                 {
1929                 GString *command;
1930                 GList *work;
1931                 gint retry_count = 12;
1932                 gboolean blank = FALSE;
1933
1934                 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1935
1936                 command = g_string_new(arg_exec);
1937
1938                 work = remote_list;
1939                 while (work)
1940                         {
1941                         gchar *text;
1942                         RemoteCommandEntry *entry;
1943
1944                         text = static_cast<gchar *>(work->data);
1945                         work = work->next;
1946
1947                         entry = remote_command_find(text, nullptr);
1948                         if (entry)
1949                                 {
1950                                 /* If Geeqie is not running, stop the --new-window command opening a second window */
1951                                 if (g_strcmp0(text, "--new-window") == 0)
1952                                         {
1953                                         remote_list = g_list_remove(remote_list, text);
1954                                         }
1955                                 if (entry->prefer_command_line)
1956                                         {
1957                                         remote_list = g_list_remove(remote_list, text);
1958                                         g_string_append(command, " ");
1959                                         g_string_append(command, text);
1960                                         }
1961                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1962                                         {
1963                                         blank = TRUE;
1964                                         }
1965                                 }
1966                         }
1967
1968                 if (blank || cmd_list || path) g_string_append(command, " --blank");
1969                 if (get_debug_level()) g_string_append(command, " --debug");
1970
1971                 g_string_append(command, " &");
1972                 runcmd(command->str);
1973                 g_string_free(command, TRUE);
1974
1975                 while (!rc && retry_count > 0)
1976                         {
1977                         usleep((retry_count > 10) ? 500000 : 1000000);
1978                         rc = remote_client_open(buf);
1979                         if (!rc) print_term(FALSE, ".");
1980                         retry_count--;
1981                         }
1982
1983                 print_term(FALSE, "\n");
1984
1985                 started = TRUE;
1986                 }
1987         g_free(buf);
1988
1989         if (rc)
1990                 {
1991                 GList *work;
1992                 const gchar *prefix;
1993                 gboolean use_path = TRUE;
1994                 gboolean sent = FALSE;
1995
1996                 work = remote_list;
1997                 while (work)
1998                         {
1999                         gchar *text;
2000                         RemoteCommandEntry *entry;
2001
2002                         text = static_cast<gchar *>(work->data);
2003                         work = work->next;
2004
2005                         entry = remote_command_find(text, nullptr);
2006                         if (entry &&
2007                             entry->opt_l &&
2008                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
2009
2010                         remote_client_send(rc, text);
2011
2012                         sent = TRUE;
2013                         }
2014
2015                 if (cmd_list && cmd_list->next)
2016                         {
2017                         prefix = "--list-add:";
2018                         remote_client_send(rc, "--list-clear");
2019                         }
2020                 else
2021                         {
2022                         prefix = "file:";
2023                         }
2024
2025                 work = cmd_list;
2026                 while (work)
2027                         {
2028                         gchar *text;
2029
2030                         text = g_strconcat(prefix, work->data, NULL);
2031                         remote_client_send(rc, text);
2032                         g_free(text);
2033                         work = work->next;
2034
2035                         sent = TRUE;
2036                         }
2037
2038                 if (path && !cmd_list && use_path)
2039                         {
2040                         gchar *text;
2041
2042                         text = g_strdup_printf("file:%s", path);
2043                         remote_client_send(rc, text);
2044                         g_free(text);
2045
2046                         sent = TRUE;
2047                         }
2048
2049                 work = collection_list;
2050                 while (work)
2051                         {
2052                         const gchar *name;
2053                         gchar *text;
2054
2055                         name = static_cast<const gchar *>(work->data);
2056                         work = work->next;
2057
2058                         text = g_strdup_printf("file:%s", name);
2059                         remote_client_send(rc, text);
2060                         g_free(text);
2061
2062                         sent = TRUE;
2063                         }
2064
2065                 if (!started && !sent)
2066                         {
2067                         remote_client_send(rc, "raise");
2068                         }
2069                 }
2070         else
2071                 {
2072                 print_term(TRUE, _("Remote not available\n"));
2073                 }
2074
2075         _exit(0);
2076 }
2077
2078 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
2079 {
2080         RemoteConnection *remote_connection = remote_server_open(path);
2081         auto remote_data = g_new(RemoteData, 1);
2082
2083         remote_data->command_collection = command_collection;
2084
2085         remote_server_subscribe(remote_connection, remote_cb, remote_data);
2086         return remote_connection;
2087 }
2088 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */