2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
25 #include "cache-maint.h"
27 #include "collect-io.h"
30 #include "filefilter.h"
33 #include "layout-image.h"
34 #include "layout-util.h"
36 #include "pixbuf-renderer.h"
37 #include "slideshow.h"
38 #include "ui-fileops.h"
40 #include "view-file.h"
43 #include <sys/socket.h>
48 #define SERVER_MAX_CLIENTS 8
50 #define REMOTE_SERVER_BACKLOG 4
54 #define UNIX_PATH_MAX 108
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);
62 static LayoutWindow *lw_id = nullptr; /* points to the window set by the --id option */
66 guint channel_id; /* event source id */
71 CollectionData *command_collection;
76 /* To enable file names containing newlines to be processed correctly,
77 * the --print0 remote option sets returned data to be terminated with a null
78 * character rather a newline
80 static gboolean print0 = FALSE;
82 /* Remote commands from main.cc are prepended with the current dir the remote
83 * command was made from. Some remote commands require this. The
84 * value is stored here
86 static gchar *pwd = nullptr;
89 * @brief Ensures file path is absolute.
90 * @param[in] filename Filepath, absolute or relative to calling directory
91 * @returns absolute path
93 * If first character of input filepath is not the directory
94 * separator, assume it as a relative path and prepend
95 * the directory the remote command was initiated from
97 * Return value must be freed with g_free()
99 static gchar *set_pwd(gchar *filename)
103 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
105 temp = g_build_filename(pwd, filename, NULL);
109 temp = g_strdup(filename);
115 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
117 auto client = static_cast<RemoteClient *>(data);
118 RemoteConnection *rc;
119 GIOStatus status = G_IO_STATUS_NORMAL;
124 if (condition & G_IO_IN)
126 gchar *buffer = nullptr;
127 GError *error = nullptr;
129 /** @FIXME it should be possible to terminate the command with a null character */
130 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
131 while ((status = g_io_channel_read_line(source, &buffer, nullptr, &termpos, &error)) == G_IO_STATUS_NORMAL)
135 buffer[termpos] = '\0';
137 if (strlen(buffer) > 0)
139 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
140 g_io_channel_write_chars(source, "<gq_end_of_command>", -1, nullptr, nullptr); /* empty line finishes the command */
141 g_io_channel_flush(source, nullptr);
151 log_printf("error reading socket: %s\n", error->message);
156 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
158 rc->clients = g_list_remove(rc->clients, client);
160 DEBUG_1("HUP detected, closing client.");
161 DEBUG_1("client count %d", g_list_length(rc->clients));
163 g_source_remove(client->channel_id);
171 static void remote_server_client_add(RemoteConnection *rc, gint fd)
173 RemoteClient *client;
176 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
178 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
183 client = g_new0(RemoteClient, 1);
187 channel = g_io_channel_unix_new(fd);
188 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, static_cast<GIOCondition>(G_IO_IN | G_IO_HUP),
189 remote_server_client_cb, client, nullptr);
190 g_io_channel_unref(channel);
192 rc->clients = g_list_append(rc->clients, client);
193 DEBUG_1("client count %d", g_list_length(rc->clients));
196 static void remote_server_clients_close(RemoteConnection *rc)
200 auto client = static_cast<RemoteClient *>(rc->clients->data);
202 rc->clients = g_list_remove(rc->clients, client);
204 g_source_remove(client->channel_id);
210 static gboolean remote_server_read_cb(GIOChannel *, GIOCondition, gpointer data)
212 auto rc = static_cast<RemoteConnection *>(data);
216 fd = accept(rc->fd, nullptr, &alen);
219 log_printf("error accepting socket: %s\n", strerror(errno));
223 remote_server_client_add(rc, fd);
228 gboolean remote_server_exists(const gchar *path)
230 RemoteConnection *rc;
232 /* verify server up */
233 rc = remote_client_open(path);
238 /* unable to connect, remove socket file to free up address */
243 static RemoteConnection *remote_server_open(const gchar *path)
245 RemoteConnection *rc;
246 struct sockaddr_un addr;
251 if (remote_server_exists(path))
253 log_printf("Address already in use: %s\n", path);
257 fd = socket(PF_UNIX, SOCK_STREAM, 0);
258 if (fd == -1) return nullptr;
260 addr.sun_family = AF_UNIX;
261 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
262 strncpy(addr.sun_path, path, sun_path_len);
263 if (bind(fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == -1 ||
264 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
266 log_printf("error subscribing to socket: %s\n", strerror(errno));
271 rc = g_new0(RemoteConnection, 1);
275 rc->path = g_strdup(path);
277 channel = g_io_channel_unix_new(rc->fd);
278 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, nullptr);
280 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
281 remote_server_read_cb, rc, nullptr);
282 g_io_channel_unref(channel);
287 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
289 if (!rc || !rc->server) return;
291 rc->read_func = func;
292 rc->read_data = data;
296 static RemoteConnection *remote_client_open(const gchar *path)
298 RemoteConnection *rc;
300 struct sockaddr_un addr;
304 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return nullptr;
306 fd = socket(PF_UNIX, SOCK_STREAM, 0);
307 if (fd == -1) return nullptr;
309 addr.sun_family = AF_UNIX;
310 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
311 strncpy(addr.sun_path, path, sun_path_len);
312 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
314 DEBUG_1("error connecting to socket: %s", strerror(errno));
319 rc = g_new0(RemoteConnection, 1);
322 rc->path = g_strdup(path);
327 static sig_atomic_t sigpipe_occurred = FALSE;
329 static void sighandler_sigpipe(gint)
331 sigpipe_occurred = TRUE;
334 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
336 struct sigaction new_action, old_action;
337 gboolean ret = FALSE;
338 GError *error = nullptr;
341 if (!rc || rc->server) return FALSE;
342 if (!text) return TRUE;
344 sigpipe_occurred = FALSE;
346 new_action.sa_handler = sighandler_sigpipe;
347 sigemptyset(&new_action.sa_mask);
348 new_action.sa_flags = 0;
350 /* setup our signal handler */
351 sigaction(SIGPIPE, &new_action, &old_action);
353 channel = g_io_channel_unix_new(rc->fd);
355 g_io_channel_write_chars(channel, text, -1, nullptr, &error);
356 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, &error);
357 g_io_channel_flush(channel, &error);
361 log_printf("error reading socket: %s\n", error->message);
372 gchar *buffer = nullptr;
374 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
375 while (g_io_channel_read_line(channel, &buffer, nullptr, &termpos, &error) == G_IO_STATUS_NORMAL)
379 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
385 buffer[termpos] = '\0';
386 if (g_strstr_len(buffer, -1, "print0") != nullptr)
394 printf("%s%c", buffer, 0);
398 printf("%s\n", buffer);
408 log_printf("error reading socket: %s\n", error->message);
415 /* restore the original signal handler */
416 sigaction(SIGPIPE, &old_action, nullptr);
417 g_io_channel_unref(channel);
421 void remote_close(RemoteConnection *rc)
427 remote_server_clients_close(rc);
429 g_source_remove(rc->channel_id);
434 g_free(rc->read_data);
443 *-----------------------------------------------------------------------------
445 *-----------------------------------------------------------------------------
448 static void gr_image_next(const gchar *, GIOChannel *, gpointer)
450 layout_image_next(lw_id);
453 static void gr_new_window(const gchar *, GIOChannel *, gpointer)
455 LayoutWindow *lw = nullptr;
457 if (!layout_valid(&lw)) return;
459 lw_id = layout_new_from_default();
461 layout_set_path(lw_id, pwd);
464 static gboolean gr_close_window_cb(gpointer)
466 if (!layout_valid(&lw_id)) return FALSE;
468 layout_menu_close_cb(nullptr, lw_id);
470 return G_SOURCE_REMOVE;
473 static void gr_close_window(const gchar *, GIOChannel *, gpointer)
475 g_idle_add((gr_close_window_cb), nullptr);
478 static void gr_image_prev(const gchar *, GIOChannel *, gpointer)
480 layout_image_prev(lw_id);
483 static void gr_image_first(const gchar *, GIOChannel *, gpointer)
485 layout_image_first(lw_id);
488 static void gr_image_last(const gchar *, GIOChannel *, gpointer)
490 layout_image_last(lw_id);
493 static void gr_fullscreen_toggle(const gchar *, GIOChannel *, gpointer)
495 layout_image_full_screen_toggle(lw_id);
498 static void gr_fullscreen_start(const gchar *, GIOChannel *, gpointer)
500 layout_image_full_screen_start(lw_id);
503 static void gr_fullscreen_stop(const gchar *, GIOChannel *, gpointer)
505 layout_image_full_screen_stop(lw_id);
508 static void gr_lw_id(const gchar *text, GIOChannel *, gpointer)
510 lw_id = layout_find_by_layout_id(text);
513 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
515 layout_valid(&lw_id);
518 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *, gpointer)
521 gchar *tilde_filename;
523 tilde_filename = expand_tilde(text);
525 FileData *dir_fd = file_data_new_dir(tilde_filename);
526 g_free(tilde_filename);
528 layout_valid(&lw_id);
529 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);
530 file_data_unref(dir_fd);
533 layout_image_slideshow_stop(lw_id);
534 layout_image_slideshow_start_from_list(lw_id, list);
537 static void gr_cache_thumb(const gchar *text, GIOChannel *, gpointer)
539 if (!g_strcmp0(text, "clear"))
541 cache_maintain_home_remote(FALSE, TRUE, nullptr);
543 else if (!g_strcmp0(text, "clean"))
545 cache_maintain_home_remote(FALSE, FALSE, nullptr);
549 static void gr_cache_shared(const gchar *text, GIOChannel *, gpointer)
551 if (!g_strcmp0(text, "clear"))
552 cache_manager_standard_process_remote(TRUE);
553 else if (!g_strcmp0(text, "clean"))
554 cache_manager_standard_process_remote(FALSE);
557 static void gr_cache_metadata(const gchar *, GIOChannel *, gpointer)
559 cache_maintain_home_remote(TRUE, FALSE, nullptr);
562 static void gr_cache_render(const gchar *text, GIOChannel *, gpointer)
564 cache_manager_render_remote(text, FALSE, FALSE, nullptr);
567 static void gr_cache_render_recurse(const gchar *text, GIOChannel *, gpointer)
569 cache_manager_render_remote(text, TRUE, FALSE, nullptr);
572 static void gr_cache_render_standard(const gchar *text, GIOChannel *, gpointer)
574 if(options->thumbnails.spec_standard)
576 cache_manager_render_remote(text, FALSE, TRUE, nullptr);
580 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *, gpointer)
582 if(options->thumbnails.spec_standard)
584 cache_manager_render_remote(text, TRUE, TRUE, nullptr);
588 static void gr_slideshow_toggle(const gchar *, GIOChannel *, gpointer)
590 layout_image_slideshow_toggle(lw_id);
593 static void gr_slideshow_start(const gchar *, GIOChannel *, gpointer)
595 layout_image_slideshow_start(lw_id);
598 static void gr_slideshow_stop(const gchar *, GIOChannel *, gpointer)
600 layout_image_slideshow_stop(lw_id);
603 static void gr_slideshow_delay(const gchar *text, GIOChannel *, gpointer)
605 gdouble t1, t2, t3, n;
608 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
611 n = (t1 * 3600) + (t2 * 60) + t3;
612 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
613 t1 >= 24 || t2 >= 60 || t3 >= 60)
615 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
616 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
623 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
624 t1 >= 60 || t2 >= 60)
626 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
627 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
634 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
636 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
637 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
646 options->slideshow.delay = static_cast<gint>(n * 10.0 + 0.01);
649 static void gr_tools_show(const gchar *, GIOChannel *, gpointer)
654 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
656 layout_tools_float_set(lw_id, popped, FALSE);
660 static void gr_tools_hide(const gchar *, GIOChannel *, gpointer)
665 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
667 layout_tools_float_set(lw_id, popped, TRUE);
671 static gboolean gr_quit_idle_cb(gpointer)
675 return G_SOURCE_REMOVE;
678 static void gr_quit(const gchar *, GIOChannel *, gpointer)
680 /* schedule exit when idle, if done from within a
681 * remote handler remote_close will crash
683 g_idle_add(gr_quit_idle_cb, nullptr);
686 static void gr_file_load_no_raise(const gchar *text, GIOChannel *, gpointer)
689 gchar *tilde_filename;
691 if (!download_web_file(text, TRUE, nullptr))
693 tilde_filename = expand_tilde(text);
694 filename = set_pwd(tilde_filename);
696 if (isfile(filename))
698 if (file_extension_match(filename, GQ_COLLECTION_EXT))
700 collection_window_new(filename);
704 layout_set_path(lw_id, filename);
707 else if (isdir(filename))
709 layout_set_path(lw_id, filename);
713 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
714 layout_set_path(lw_id, homedir());
718 g_free(tilde_filename);
722 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
724 gr_file_load_no_raise(text, channel, data);
726 gr_raise(text, channel, data);
729 static void gr_pixel_info(const gchar *, GIOChannel *channel, gpointer)
732 gint x_pixel, y_pixel;
734 gint r_mouse, g_mouse, b_mouse;
737 if (!layout_valid(&lw_id)) return;
739 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
743 pixbuf_renderer_get_image_size(pr, &width, &height);
744 if (width < 1 || height < 1) return;
746 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
748 if (x_pixel >= 0 && y_pixel >= 0)
750 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
751 &r_mouse, &g_mouse, &b_mouse);
753 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
755 r_mouse, g_mouse, b_mouse);
757 g_io_channel_write_chars(channel, pixel_info, -1, nullptr, nullptr);
758 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
773 static void gr_rectangle(const gchar *, GIOChannel *channel, gpointer)
775 gchar *rectangle_info;
779 if (!options->draw_rectangle) return;
780 if (!layout_valid(&lw_id)) return;
782 pr = reinterpret_cast<PixbufRenderer*>(lw_id->image->pr);
786 image_get_rectangle(&x1, &y1, &x2, &y2);
787 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
788 (x2 > x1) ? x2 - x1 : x1 - x2,
789 (y2 > y1) ? y2 - y1 : y1 - y2,
791 (y2 > y1) ? y1 : y2);
793 g_io_channel_write_chars(channel, rectangle_info, -1, nullptr, nullptr);
794 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
796 g_free(rectangle_info);
800 static void gr_render_intent(const gchar *, GIOChannel *channel, gpointer)
802 gchar *render_intent;
804 switch (options->color_profile.render_intent)
807 render_intent = g_strdup("Perceptual");
810 render_intent = g_strdup("Relative Colorimetric");
813 render_intent = g_strdup("Saturation");
816 render_intent = g_strdup("Absolute Colorimetric");
819 render_intent = g_strdup("none");
823 g_io_channel_write_chars(channel, render_intent, -1, nullptr, nullptr);
824 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
826 g_free(render_intent);
829 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
831 GList *list = nullptr;
832 FileFormatClass format_class;
836 gchar *tilde_filename;
838 if (strcmp(text, "") == 0)
840 if (layout_valid(&lw_id))
842 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
851 tilde_filename = expand_tilde(text);
852 if (isdir(tilde_filename))
854 dir_fd = file_data_new_dir(tilde_filename);
858 g_free(tilde_filename);
861 g_free(tilde_filename);
866 list = filelist_recursive(dir_fd);
870 filelist_read(dir_fd, &list, nullptr);
873 GString *out_string = g_string_new(nullptr);
877 fd = static_cast<FileData *>(work->data);
878 g_string_append_printf(out_string, "%s", fd->path);
879 format_class = filter_file_get_class(fd->path);
881 switch (format_class)
883 case FORMAT_CLASS_IMAGE:
884 out_string = g_string_append(out_string, " Class: Image");
886 case FORMAT_CLASS_RAWIMAGE:
887 out_string = g_string_append(out_string, " Class: RAW image");
889 case FORMAT_CLASS_META:
890 out_string = g_string_append(out_string, " Class: Metadata");
892 case FORMAT_CLASS_VIDEO:
893 out_string = g_string_append(out_string, " Class: Video");
895 case FORMAT_CLASS_COLLECTION:
896 out_string = g_string_append(out_string, " Class: Collection");
898 case FORMAT_CLASS_DOCUMENT:
899 out_string = g_string_append(out_string, " Class: Document");
901 case FORMAT_CLASS_ARCHIVE:
902 out_string = g_string_append(out_string, " Class: Archive");
904 case FORMAT_CLASS_UNKNOWN:
905 out_string = g_string_append(out_string, " Class: Unknown");
908 out_string = g_string_append(out_string, " Class: Unknown");
911 out_string = g_string_append(out_string, "\n");
915 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
916 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
918 g_string_free(out_string, TRUE);
920 file_data_unref(dir_fd);
923 static void gr_get_selection(const gchar *, GIOChannel *channel, gpointer)
925 if (!layout_valid(&lw_id)) return;
927 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
928 GString *out_string = g_string_new(nullptr);
930 GList *work = selected;
933 auto fd = static_cast<FileData *>(work->data);
934 g_assert(fd->magick == FD_MAGICK);
936 g_string_append_printf(out_string, "%s %s\n",
938 format_class_list[filter_file_get_class(fd->path)]);
943 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
944 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
946 filelist_free(selected);
947 g_string_free(out_string, TRUE);
950 static void gr_selection_add(const gchar *text, GIOChannel *, gpointer)
952 if (!layout_valid(&lw_id)) return;
954 FileData *fd_to_select = nullptr;
955 if (strcmp(text, "") == 0)
957 // No file specified, use current fd.
958 fd_to_select = layout_image_get_fd(lw_id);
962 // Search through the current file list for a file matching the specified path.
963 // "Match" is either a basename match or a file path match.
964 gchar *path = expand_tilde(text);
965 gchar *filename = g_path_get_basename(path);
966 gchar *slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
968 GList *file_list = layout_list(lw_id);
969 for (GList *work = file_list; work && !fd_to_select; work = work->next)
971 auto fd = static_cast<FileData *>(work->data);
972 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
974 fd_to_select = file_data_ref(fd);
975 continue; // will exit loop.
978 for (GList *sidecar = fd->sidecar_files; sidecar && !fd_to_select; sidecar = sidecar->next)
980 auto side_fd = static_cast<FileData *>(sidecar->data);
981 if (!strcmp(path, side_fd->path)
982 || g_str_has_suffix(side_fd->path, slash_plus_filename))
984 fd_to_select = file_data_ref(side_fd);
985 continue; // will exit both nested loops.
992 log_printf("remote sent --selection-add filename that could not be found: \"%s\"\n",
996 filelist_free(file_list);
997 g_free(slash_plus_filename);
1004 GList *to_select = g_list_append(nullptr, fd_to_select);
1005 // Using the "_list" variant doesn't clear the existing selection.
1006 layout_select_list(lw_id, to_select);
1007 filelist_free(to_select);
1011 static void gr_selection_clear(const gchar *, GIOChannel *, gpointer)
1013 layout_select_none(lw_id); // Checks lw_id validity internally.
1016 static void gr_selection_remove(const gchar *text, GIOChannel *, gpointer)
1018 if (!layout_valid(&lw_id)) return;
1020 GList *selected = layout_selection_list(lw_id); // Keep copy to free.
1023 log_printf("remote sent --selection-remove with empty selection.");
1027 FileData *fd_to_deselect = nullptr;
1028 gchar *path = nullptr;
1029 gchar *filename = nullptr;
1030 gchar *slash_plus_filename = nullptr;
1031 if (strcmp(text, "") == 0)
1033 // No file specified, use current fd.
1034 fd_to_deselect = layout_image_get_fd(lw_id);
1035 if (!fd_to_deselect)
1037 log_printf("remote sent \"--selection-remove:\" with no current image");
1038 filelist_free(selected);
1044 // Search through the selection list for a file matching the specified path.
1045 // "Match" is either a basename match or a file path match.
1046 path = expand_tilde(text);
1047 filename = g_path_get_basename(path);
1048 slash_plus_filename = g_strdup_printf("%s%s", G_DIR_SEPARATOR_S, filename);
1051 GList *prior_link = nullptr; // Stash base for link removal to avoid a second traversal.
1052 GList *link_to_remove = nullptr;
1053 for (GList *work = selected; work; prior_link = work, work = work->next)
1055 auto fd = static_cast<FileData *>(work->data);
1058 if (fd == fd_to_deselect)
1060 link_to_remove = work;
1066 // path, filename, and slash_plus_filename should be defined.
1068 if (!strcmp(path, fd->path) || g_str_has_suffix(fd->path, slash_plus_filename))
1070 link_to_remove = work;
1076 if (!link_to_remove)
1080 log_printf("remote sent \"--selection-remove:\" but current image is not selected");
1084 log_printf("remote sent \"--selection-remove:%s\" but that filename is not selected",
1090 if (link_to_remove == selected)
1092 // Remove first link.
1093 selected = g_list_remove_link(selected, link_to_remove);
1094 filelist_free(link_to_remove);
1095 link_to_remove = nullptr;
1099 // Remove a subsequent link.
1100 prior_link = g_list_remove_link(prior_link, link_to_remove);
1101 filelist_free(link_to_remove);
1102 link_to_remove = nullptr;
1105 // Re-select all but the deselected item.
1106 layout_select_none(lw_id);
1107 layout_select_list(lw_id, selected);
1110 filelist_free(selected);
1111 file_data_unref(fd_to_deselect);
1112 g_free(slash_plus_filename);
1117 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer)
1119 GString *contents = g_string_new(nullptr);
1121 if (is_collection(text))
1123 collection_contents(text, &contents);
1130 g_io_channel_write_chars(channel, contents->str, -1, nullptr, nullptr);
1131 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1133 g_string_free(contents, TRUE);
1136 static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
1139 GList *collection_list = nullptr;
1141 GString *out_string = g_string_new(nullptr);
1143 collect_manager_list(&collection_list, nullptr, nullptr);
1145 work = collection_list;
1148 auto collection_name = static_cast<const gchar *>(work->data);
1149 out_string = g_string_append(out_string, g_strdup(collection_name));
1150 out_string = g_string_append(out_string, "\n");
1155 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1156 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1158 g_list_free_full(collection_list, g_free);
1159 g_string_free(out_string, TRUE);
1162 static gboolean wait_cb(gpointer data)
1164 gint position = GPOINTER_TO_INT(data);
1165 gint x = position >> 16;
1166 gint y = position - (x << 16);
1168 gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
1170 return G_SOURCE_REMOVE;
1173 static void gr_geometry(const gchar *text, GIOChannel *, gpointer)
1177 if (!layout_valid(&lw_id) || !text)
1184 geometry = g_strsplit_set(text, "+", 3);
1185 if (geometry[1] != nullptr && geometry[2] != nullptr )
1187 gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1192 geometry = g_strsplit_set(text, "+x", 4);
1193 if (geometry[0] != nullptr && geometry[1] != nullptr)
1195 gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1197 if (geometry[2] != nullptr && geometry[3] != nullptr)
1199 /* There is an occasional problem with a window_move immediately after a window_resize */
1200 g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1203 g_strfreev(geometry);
1206 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer)
1208 get_filelist(text, channel, FALSE);
1211 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer)
1213 get_filelist(text, channel, TRUE);
1216 static void gr_file_tell(const gchar *, GIOChannel *channel, gpointer)
1219 gchar *collection_name = nullptr;
1221 if (!layout_valid(&lw_id)) return;
1223 if (image_get_path(lw_id->image))
1225 if (lw_id->image->collection && lw_id->image->collection->name)
1227 collection_name = remove_extension_from_path(lw_id->image->collection->name);
1228 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
1232 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1237 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1240 g_io_channel_write_chars(channel, out_string, -1, nullptr, nullptr);
1241 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1243 g_free(collection_name);
1247 static void gr_file_info(const gchar *, GIOChannel *channel, gpointer)
1251 gchar *country_name;
1252 gchar *country_code;
1255 GString *out_string;
1256 FileFormatClass format_class;
1258 if (!layout_valid(&lw_id)) return;
1260 if (image_get_path(lw_id->image))
1262 filename = g_strdup(image_get_path(lw_id->image));
1263 fd = file_data_new_group(filename);
1264 out_string = g_string_new(nullptr);
1268 format_class = filter_file_get_class(image_get_path(lw_id->image));
1272 format_class = FORMAT_CLASS_UNKNOWN;
1275 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1277 if (fd->page_total > 1)
1279 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1284 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1287 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1288 g_free(country_name);
1291 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1294 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1295 g_free(country_code);
1298 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1301 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1305 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1308 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1313 g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
1314 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1316 g_string_free(out_string, TRUE);
1317 file_data_unref(fd);
1322 static gchar *config_file_path(const gchar *param)
1324 gchar *path = nullptr;
1325 gchar *full_name = nullptr;
1327 if (file_extension_match(param, ".xml"))
1329 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1331 else if (file_extension_match(param, nullptr))
1333 full_name = g_strconcat(param, ".xml", NULL);
1334 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1347 static gboolean is_config_file(const gchar *param)
1349 gchar *name = nullptr;
1351 name = config_file_path(param);
1360 static void gr_config_load(const gchar *text, GIOChannel *, gpointer)
1362 gchar *filename = expand_tilde(text);
1364 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1366 if (is_config_file(filename))
1368 gchar *tmp = config_file_path(filename);
1374 if (isfile(filename))
1376 load_config_from_file(filename, FALSE);
1380 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1381 layout_set_path(nullptr, homedir());
1387 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer)
1389 gchar *filename = expand_tilde(text);
1390 FileData *fd = file_data_new_group(filename);
1393 if (fd->parent) fd = fd->parent;
1395 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1396 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1398 work = fd->sidecar_files;
1402 fd = static_cast<FileData *>(work->data);
1404 g_io_channel_write_chars(channel, fd->path, -1, nullptr, nullptr);
1405 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1410 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer)
1412 gchar *filename = expand_tilde(text);
1413 FileData *fd = file_data_new_group(filename);
1415 if (fd->change && fd->change->dest)
1417 g_io_channel_write_chars(channel, fd->change->dest, -1, nullptr, nullptr);
1418 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1423 static void gr_file_view(const gchar *text, GIOChannel *, gpointer)
1426 gchar *tilde_filename = expand_tilde(text);
1428 filename = set_pwd(tilde_filename);
1430 view_window_new(file_data_new_group(filename));
1432 g_free(tilde_filename);
1435 static void gr_list_clear(const gchar *, GIOChannel *, gpointer data)
1437 auto remote_data = static_cast<RemoteData *>(data);
1439 remote_data->command_collection = nullptr;
1440 remote_data->file_list = nullptr;
1441 remote_data->single_dir = TRUE;
1444 static void gr_list_add(const gchar *text, GIOChannel *, gpointer data)
1446 auto remote_data = static_cast<RemoteData *>(data);
1447 gboolean is_new = TRUE;
1448 gchar *path = nullptr;
1452 /** @FIXME Should check if file is in current dir, has tilde or is relative */
1455 log_printf("Warning: File does not exist --remote --list-add:%s", text);
1460 /* If there is a files list on the command line
1461 * check if they are all in the same folder
1463 if (remote_data->single_dir)
1466 work = remote_data->file_list;
1467 while (work && remote_data->single_dir)
1470 dirname = g_path_get_dirname((static_cast<FileData *>(work->data))->path);
1473 path = g_strdup(dirname);
1477 if (g_strcmp0(path, dirname) != 0)
1479 remote_data->single_dir = FALSE;
1488 gchar *pathname = g_path_get_dirname(text);
1489 layout_set_path(lw_id, pathname);
1492 fd = file_data_new_simple(text);
1493 remote_data->file_list = g_list_append(remote_data->file_list, fd);
1494 file_data_unref(fd);
1496 vf_select_none(lw_id->vf);
1497 remote_data->file_list = g_list_reverse(remote_data->file_list);
1499 layout_select_list(lw_id, remote_data->file_list);
1500 layout_refresh(lw_id);
1501 first = static_cast<FileData *>(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1502 layout_set_fd(lw_id, first);
1507 if (!remote_data->command_collection && !remote_data->single_dir)
1509 cw = collection_window_new(nullptr);
1512 collection_path_changed(cd);
1514 remote_data->command_collection = cd;
1516 else if (!remote_data->single_dir)
1518 is_new = (!collection_get_first(remote_data->command_collection));
1521 if (!remote_data->single_dir)
1523 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1524 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && is_new)
1526 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1531 static void gr_raise(const gchar *, GIOChannel *, gpointer)
1533 if (layout_valid(&lw_id))
1535 gtk_window_present(GTK_WINDOW(lw_id->window));
1539 static void gr_pwd(const gchar *text, GIOChannel *, gpointer)
1541 LayoutWindow *lw = nullptr;
1546 pwd = g_strdup(text);
1550 static void gr_print0(const gchar *, GIOChannel *channel, gpointer)
1552 g_io_channel_write_chars(channel, "print0", -1, nullptr, nullptr);
1553 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1557 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer)
1559 gchar *result = nullptr;
1560 gchar **lua_command;
1562 lua_command = g_strsplit(text, ",", 2);
1564 if (lua_command[0] && lua_command[1])
1566 FileData *fd = file_data_new_group(lua_command[0]);
1567 result = g_strdup(lua_callvalue(fd, lua_command[1], nullptr));
1570 g_io_channel_write_chars(channel, result, -1, nullptr, nullptr);
1574 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1579 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, nullptr, nullptr);
1582 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
1584 g_strfreev(lua_command);
1589 struct RemoteCommandEntry {
1592 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1593 gboolean needs_extra;
1594 gboolean prefer_command_line;
1595 const gchar *parameter;
1596 const gchar *description;
1599 static RemoteCommandEntry remote_commands[] = {
1600 /* short, long callback, extra, prefer, parameter, description */
1601 { "-b", "--back", gr_image_prev, FALSE, FALSE, nullptr, N_("previous image") },
1602 { nullptr, "--close-window", gr_close_window, FALSE, FALSE, nullptr, N_("close window") },
1603 { nullptr, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1604 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, nullptr, N_("clean the metadata cache") },
1605 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1606 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1607 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1608 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1609 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1610 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1611 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1612 { nullptr, "--first", gr_image_first, FALSE, FALSE, nullptr, N_("first image") },
1613 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, nullptr, N_("toggle full screen") },
1614 { nullptr, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1615 { nullptr, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1616 { 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") },
1617 { 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") },
1618 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, nullptr, N_("start full screen") },
1619 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, nullptr, N_("stop full screen") },
1620 { nullptr, "--geometry=", gr_geometry, TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1621 { nullptr, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1622 { nullptr, "--get-collection-list", gr_collection_list, FALSE, FALSE, nullptr, N_("get collection list") },
1623 { nullptr, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1624 { nullptr, "--get-file-info", gr_file_info, FALSE, FALSE, nullptr, N_("get file info") },
1625 { nullptr, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1626 { nullptr, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1627 { nullptr, "--get-rectangle", gr_rectangle, FALSE, FALSE, nullptr, N_("get rectangle co-ordinates") },
1628 { nullptr, "--get-render-intent", gr_render_intent, FALSE, FALSE, nullptr, N_("get render intent") },
1629 { nullptr, "--get-selection", gr_get_selection, FALSE, FALSE, nullptr, N_("get list of selected files") },
1630 { nullptr, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1631 { nullptr, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1632 { nullptr, "--last", gr_image_last, FALSE, FALSE, nullptr, N_("last image") },
1633 { nullptr, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1634 { nullptr, "--list-clear", gr_list_clear, FALSE, FALSE, nullptr, N_("clear command line collection list") },
1636 { nullptr, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1638 { nullptr, "--new-window", gr_new_window, FALSE, FALSE, nullptr, N_("new window") },
1639 { "-n", "--next", gr_image_next, FALSE, FALSE, nullptr, N_("next image") },
1640 { nullptr, "--pixel-info", gr_pixel_info, FALSE, FALSE, nullptr, N_("print pixel info of mouse pointer on current image") },
1641 { nullptr, "--print0", gr_print0, TRUE, FALSE, nullptr, N_("terminate returned data with null character instead of newline") },
1642 { nullptr, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1643 { "-q", "--quit", gr_quit, FALSE, FALSE, nullptr, N_("quit") },
1644 { nullptr, "--raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1645 { nullptr, "raise", gr_raise, FALSE, FALSE, nullptr, N_("bring the Geeqie window to the top") },
1646 { nullptr, "--selection-add:", gr_selection_add, TRUE, FALSE, N_("[<FILE>]"), N_("adds the current file (or the specified file) to the current selection") },
1647 { nullptr, "--selection-clear", gr_selection_clear, FALSE, FALSE, nullptr, N_("clears the current selection") },
1648 { nullptr, "--selection-remove:", gr_selection_remove, TRUE, FALSE, N_("[<FILE>]"), N_("removes the current file (or the specified file) from the current selection") },
1649 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, nullptr, N_("toggle slide show") },
1650 { nullptr, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1651 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, nullptr, N_("start slide show") },
1652 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, nullptr, N_("stop slide show") },
1653 { nullptr, "--tell", gr_file_tell, FALSE, FALSE, nullptr, N_("print filename [and Collection] of current image") },
1654 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, nullptr, N_("show tools") },
1655 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, nullptr, N_("hide tools") },
1656 { nullptr, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1657 { nullptr, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1658 { nullptr, nullptr, nullptr, FALSE, FALSE, nullptr, nullptr }
1661 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1663 gboolean match = FALSE;
1667 while (!match && remote_commands[i].func != nullptr)
1669 if (remote_commands[i].needs_extra)
1671 if (remote_commands[i].opt_s &&
1672 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1674 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1675 return &remote_commands[i];
1677 else if (remote_commands[i].opt_l &&
1678 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1680 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1681 return &remote_commands[i];
1686 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1687 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1689 if (offset) *offset = text;
1690 return &remote_commands[i];
1700 static void remote_cb(RemoteConnection *, const gchar *text, GIOChannel *channel, gpointer data)
1702 RemoteCommandEntry *entry;
1703 const gchar *offset;
1705 entry = remote_command_find(text, &offset);
1706 if (entry && entry->func)
1708 entry->func(offset, channel, data);
1712 log_printf("unknown remote command:%s\n", text);
1722 print_term(FALSE, _("Remote command list:\n"));
1725 while (remote_commands[i].func != nullptr)
1727 if (remote_commands[i].description)
1729 s_opt_param = g_strdup(remote_commands[i].opt_s ? remote_commands[i].opt_s : "" );
1730 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1731 printf_term(FALSE, " %-4s %-40s%-s\n",
1734 _(remote_commands[i].description));
1735 g_free(s_opt_param);
1736 g_free(l_opt_param);
1740 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"));
1743 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1750 RemoteCommandEntry *entry;
1752 entry = remote_command_find(argv[i], nullptr);
1755 list = g_list_append(list, argv[i]);
1757 else if (errors && !isname(argv[i]))
1759 *errors = g_list_append(*errors, argv[i]);
1768 * @param arg_exec Binary (argv0)
1769 * @param remote_list Evaluated and recognized remote commands
1770 * @param path The current path
1771 * @param cmd_list List of all non collections in Path (gchar *path)
1772 * @param collection_list List of all collections in argv
1774 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1775 GList *cmd_list, GList *collection_list)
1777 RemoteConnection *rc;
1778 gboolean started = FALSE;
1781 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1782 rc = remote_client_open(buf);
1787 gint retry_count = 12;
1788 gboolean blank = FALSE;
1790 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1792 command = g_string_new(arg_exec);
1798 RemoteCommandEntry *entry;
1800 text = static_cast<gchar *>(work->data);
1803 entry = remote_command_find(text, nullptr);
1806 /* If Geeqie is not running, stop the --new-window command opening a second window */
1807 if (g_strcmp0(text, "--new-window") == 0)
1809 remote_list = g_list_remove(remote_list, text);
1811 if (entry->prefer_command_line)
1813 remote_list = g_list_remove(remote_list, text);
1814 g_string_append(command, " ");
1815 g_string_append(command, text);
1817 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1824 if (blank || cmd_list || path) g_string_append(command, " --blank");
1825 if (get_debug_level()) g_string_append(command, " --debug");
1827 g_string_append(command, " &");
1828 runcmd(command->str);
1829 g_string_free(command, TRUE);
1831 while (!rc && retry_count > 0)
1833 usleep((retry_count > 10) ? 500000 : 1000000);
1834 rc = remote_client_open(buf);
1835 if (!rc) print_term(FALSE, ".");
1839 print_term(FALSE, "\n");
1848 const gchar *prefix;
1849 gboolean use_path = TRUE;
1850 gboolean sent = FALSE;
1856 RemoteCommandEntry *entry;
1858 text = static_cast<gchar *>(work->data);
1861 entry = remote_command_find(text, nullptr);
1864 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1866 remote_client_send(rc, text);
1871 if (cmd_list && cmd_list->next)
1873 prefix = "--list-add:";
1874 remote_client_send(rc, "--list-clear");
1886 text = g_strconcat(prefix, work->data, NULL);
1887 remote_client_send(rc, text);
1894 if (path && !cmd_list && use_path)
1898 text = g_strdup_printf("file:%s", path);
1899 remote_client_send(rc, text);
1905 work = collection_list;
1911 name = static_cast<const gchar *>(work->data);
1914 text = g_strdup_printf("file:%s", name);
1915 remote_client_send(rc, text);
1921 if (!started && !sent)
1923 remote_client_send(rc, "raise");
1928 print_term(TRUE, _("Remote not available\n"));
1934 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1936 RemoteConnection *remote_connection = remote_server_open(path);
1937 auto remote_data = g_new(RemoteData, 1);
1939 remote_data->command_collection = command_collection;
1941 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1942 return remote_connection;
1944 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */