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"
34 #include "layout_image.h"
35 #include "layout_util.h"
37 #include "pixbuf-renderer.h"
38 #include "slideshow.h"
39 #include "ui_fileops.h"
41 #include "view_file.h"
43 #include <sys/socket.h>
50 #define SERVER_MAX_CLIENTS 8
52 #define REMOTE_SERVER_BACKLOG 4
56 #define UNIX_PATH_MAX 108
60 static RemoteConnection *remote_client_open(const gchar *path);
61 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
62 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
64 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
66 typedef struct _RemoteClient RemoteClient;
67 struct _RemoteClient {
69 guint channel_id; /* event source id */
73 typedef struct _RemoteData RemoteData;
75 CollectionData *command_collection;
80 /* To enable file names containing newlines to be processed correctly,
81 * the --print0 remote option sets returned data to be terminated with a null
82 * character rather a newline
84 static gboolean print0 = FALSE;
86 /* Remote commands from main.c are prepended with the current dir the remote
87 * command was made from. Some remote commands require this. The
88 * value is stored here
90 static gchar *pwd = NULL;
93 * @brief Ensures file path is absolute.
94 * @param[in] filename Filepath, absolute or relative to calling directory
95 * @returns absolute path
97 * If first character of input filepath is not the directory
98 * separator, assume it as a relative path and prepend
99 * the directory the remote command was initiated from
101 * Return value must be freed with g_free()
103 static gchar *set_pwd(gchar *filename)
107 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
109 temp = g_build_filename(pwd, filename, NULL);
113 temp = g_strdup(filename);
119 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
121 RemoteClient *client = data;
122 RemoteConnection *rc;
123 GIOStatus status = G_IO_STATUS_NORMAL;
128 if (condition & G_IO_IN)
130 gchar *buffer = NULL;
131 GError *error = NULL;
133 /** @FIXME it should be possible to terminate the command with a null character */
134 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
135 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
139 buffer[termpos] = '\0';
141 if (strlen(buffer) > 0)
143 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
144 g_io_channel_write_chars(source, "<gq_end_of_command>", -1, NULL, NULL); /* empty line finishes the command */
145 g_io_channel_flush(source, NULL);
155 log_printf("error reading socket: %s\n", error->message);
160 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
162 rc->clients = g_list_remove(rc->clients, client);
164 DEBUG_1("HUP detected, closing client.");
165 DEBUG_1("client count %d", g_list_length(rc->clients));
167 g_source_remove(client->channel_id);
175 static void remote_server_client_add(RemoteConnection *rc, gint fd)
177 RemoteClient *client;
180 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
182 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
187 client = g_new0(RemoteClient, 1);
191 channel = g_io_channel_unix_new(fd);
192 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
193 remote_server_client_cb, client, NULL);
194 g_io_channel_unref(channel);
196 rc->clients = g_list_append(rc->clients, client);
197 DEBUG_1("client count %d", g_list_length(rc->clients));
200 static void remote_server_clients_close(RemoteConnection *rc)
204 RemoteClient *client = rc->clients->data;
206 rc->clients = g_list_remove(rc->clients, client);
208 g_source_remove(client->channel_id);
214 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
216 RemoteConnection *rc = data;
220 fd = accept(rc->fd, NULL, &alen);
223 log_printf("error accepting socket: %s\n", strerror(errno));
227 remote_server_client_add(rc, fd);
232 gboolean remote_server_exists(const gchar *path)
234 RemoteConnection *rc;
236 /* verify server up */
237 rc = remote_client_open(path);
242 /* unable to connect, remove socket file to free up address */
247 static RemoteConnection *remote_server_open(const gchar *path)
249 RemoteConnection *rc;
250 struct sockaddr_un addr;
255 if (remote_server_exists(path))
257 log_printf("Address already in use: %s\n", path);
261 fd = socket(PF_UNIX, SOCK_STREAM, 0);
262 if (fd == -1) return NULL;
264 addr.sun_family = AF_UNIX;
265 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
266 strncpy(addr.sun_path, path, sun_path_len);
267 if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ||
268 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
270 log_printf("error subscribing to socket: %s\n", strerror(errno));
275 rc = g_new0(RemoteConnection, 1);
279 rc->path = g_strdup(path);
281 channel = g_io_channel_unix_new(rc->fd);
282 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
284 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
285 remote_server_read_cb, rc, NULL);
286 g_io_channel_unref(channel);
291 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
293 if (!rc || !rc->server) return;
295 rc->read_func = func;
296 rc->read_data = data;
300 static RemoteConnection *remote_client_open(const gchar *path)
302 RemoteConnection *rc;
304 struct sockaddr_un addr;
308 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
310 fd = socket(PF_UNIX, SOCK_STREAM, 0);
311 if (fd == -1) return NULL;
313 addr.sun_family = AF_UNIX;
314 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
315 strncpy(addr.sun_path, path, sun_path_len);
316 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
318 DEBUG_1("error connecting to socket: %s", strerror(errno));
323 rc = g_new0(RemoteConnection, 1);
326 rc->path = g_strdup(path);
331 static sig_atomic_t sigpipe_occurred = FALSE;
333 static void sighandler_sigpipe(gint sig)
335 sigpipe_occurred = TRUE;
338 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
340 struct sigaction new_action, old_action;
341 gboolean ret = FALSE;
342 GError *error = NULL;
345 if (!rc || rc->server) return FALSE;
346 if (!text) return TRUE;
348 sigpipe_occurred = FALSE;
350 new_action.sa_handler = sighandler_sigpipe;
351 sigemptyset(&new_action.sa_mask);
352 new_action.sa_flags = 0;
354 /* setup our signal handler */
355 sigaction(SIGPIPE, &new_action, &old_action);
357 channel = g_io_channel_unix_new(rc->fd);
359 g_io_channel_write_chars(channel, text, -1, NULL, &error);
360 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, &error);
361 g_io_channel_flush(channel, &error);
365 log_printf("error reading socket: %s\n", error->message);
376 gchar *buffer = NULL;
378 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
379 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
383 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
389 buffer[termpos] = '\0';
390 if (g_strstr_len(buffer, -1, "print0") != 0)
398 printf("%s%c", buffer, 0);
402 printf("%s\n", buffer);
412 log_printf("error reading socket: %s\n", error->message);
419 /* restore the original signal handler */
420 sigaction(SIGPIPE, &old_action, NULL);
421 g_io_channel_unref(channel);
425 void remote_close(RemoteConnection *rc)
431 remote_server_clients_close(rc);
433 g_source_remove(rc->channel_id);
438 g_free(rc->read_data);
447 *-----------------------------------------------------------------------------
449 *-----------------------------------------------------------------------------
452 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
454 layout_image_next(lw_id);
457 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
459 LayoutWindow *lw = NULL;
461 if (!layout_valid(&lw)) return;
463 lw_id = layout_new_from_default();
465 layout_set_path(lw_id, pwd);
468 static gboolean gr_close_window_cb()
470 if (!layout_valid(&lw_id)) return FALSE;
472 layout_menu_close_cb(NULL, lw_id);
477 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
479 g_idle_add(gr_close_window_cb, NULL);
482 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
484 layout_image_prev(lw_id);
487 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
489 layout_image_first(lw_id);
492 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
494 layout_image_last(lw_id);
497 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
499 layout_image_full_screen_toggle(lw_id);
502 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
504 layout_image_full_screen_start(lw_id);
507 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
509 layout_image_full_screen_stop(lw_id);
512 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
514 lw_id = layout_find_by_layout_id(text);
517 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
519 layout_valid(&lw_id);
522 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
525 gchar *tilde_filename;
527 tilde_filename = expand_tilde(text);
529 FileData *dir_fd = file_data_new_dir(tilde_filename);
530 g_free(tilde_filename);
532 layout_valid(&lw_id);
533 list = filelist_recursive_full(dir_fd, lw_id->sort_method, lw_id->sort_ascend);
534 file_data_unref(dir_fd);
536 //printf("length: %d\n", g_list_length(list));
537 layout_image_slideshow_stop(lw_id);
538 layout_image_slideshow_start_from_list(lw_id, list);
541 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
543 if (!g_strcmp0(text, "clear"))
545 cache_maintain_home_remote(FALSE, TRUE, NULL);
547 else if (!g_strcmp0(text, "clean"))
549 cache_maintain_home_remote(FALSE, FALSE, NULL);
553 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
555 if (!g_strcmp0(text, "clear"))
556 cache_manager_standard_process_remote(TRUE);
557 else if (!g_strcmp0(text, "clean"))
558 cache_manager_standard_process_remote(FALSE);
561 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
563 cache_maintain_home_remote(TRUE, FALSE, NULL);
566 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
568 cache_manager_render_remote(text, FALSE, FALSE, NULL);
571 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
573 cache_manager_render_remote(text, TRUE, FALSE, NULL);
576 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
578 if(options->thumbnails.spec_standard)
580 cache_manager_render_remote(text, FALSE, TRUE, NULL);
584 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
586 if(options->thumbnails.spec_standard)
588 cache_manager_render_remote(text, TRUE, TRUE, NULL);
592 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
594 layout_image_slideshow_toggle(lw_id);
597 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
599 layout_image_slideshow_start(lw_id);
602 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
604 layout_image_slideshow_stop(lw_id);
607 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
609 gdouble t1, t2, t3, n;
612 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
615 n = (t1 * 3600) + (t2 * 60) + t3;
616 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
617 t1 >= 24 || t2 >= 60 || t3 >= 60)
619 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
620 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
627 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
628 t1 >= 60 || t2 >= 60)
630 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
631 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
638 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
640 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
641 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
650 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
653 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
658 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
660 layout_tools_float_set(lw_id, popped, FALSE);
664 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
669 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
671 layout_tools_float_set(lw_id, popped, TRUE);
675 static gboolean gr_quit_idle_cb(gpointer data)
682 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
684 /* schedule exit when idle, if done from within a
685 * remote handler remote_close will crash
687 g_idle_add(gr_quit_idle_cb, NULL);
690 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
693 gchar *tilde_filename;
695 if (!download_web_file(text, TRUE, NULL))
697 tilde_filename = expand_tilde(text);
698 filename = set_pwd(tilde_filename);
700 if (isfile(filename))
702 if (file_extension_match(filename, GQ_COLLECTION_EXT))
704 collection_window_new(filename);
708 layout_set_path(lw_id, filename);
711 else if (isdir(filename))
713 layout_set_path(lw_id, filename);
717 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
718 layout_set_path(lw_id, homedir());
722 g_free(tilde_filename);
726 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
728 gr_file_load_no_raise(text, channel, data);
730 gr_raise(text, channel, data);
733 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
736 gint x_pixel, y_pixel;
738 gint r_mouse, g_mouse, b_mouse;
741 if (!layout_valid(&lw_id)) return;
743 pr = (PixbufRenderer*)lw_id->image->pr;
747 pixbuf_renderer_get_image_size(pr, &width, &height);
748 if (width < 1 || height < 1) return;
750 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
752 if (x_pixel >= 0 && y_pixel >= 0)
754 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
755 &r_mouse, &g_mouse, &b_mouse);
757 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
759 r_mouse, g_mouse, b_mouse);
761 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
762 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
777 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
779 gchar *rectangle_info;
783 if (!options->draw_rectangle) return;
784 if (!layout_valid(&lw_id)) return;
786 pr = (PixbufRenderer*)lw_id->image->pr;
790 image_get_rectangle(&x1, &y1, &x2, &y2);
791 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
792 (x2 > x1) ? x2 - x1 : x1 - x2,
793 (y2 > y1) ? y2 - y1 : y1 - y2,
795 (y2 > y1) ? y1 : y2);
797 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
798 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
800 g_free(rectangle_info);
804 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
806 gchar *render_intent;
808 switch (options->color_profile.render_intent)
811 render_intent = g_strdup("Perceptual");
814 render_intent = g_strdup("Relative Colorimetric");
817 render_intent = g_strdup("Saturation");
820 render_intent = g_strdup("Absolute Colorimetric");
823 render_intent = g_strdup("none");
827 g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
828 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
830 g_free(render_intent);
833 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
836 FileFormatClass class;
839 GString *out_string = g_string_new(NULL);
841 gchar *tilde_filename;
843 if (strcmp(text, "") == 0)
845 if (layout_valid(&lw_id))
847 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
856 tilde_filename = expand_tilde(text);
857 if (isdir(tilde_filename))
859 dir_fd = file_data_new_dir(tilde_filename);
863 g_free(tilde_filename);
866 g_free(tilde_filename);
871 list = filelist_recursive(dir_fd);
875 filelist_read(dir_fd, &list, NULL);
882 g_string_append_printf(out_string, "%s", fd->path);
883 class = filter_file_get_class(fd->path);
887 case FORMAT_CLASS_IMAGE:
888 out_string = g_string_append(out_string, " Class: Image");
890 case FORMAT_CLASS_RAWIMAGE:
891 out_string = g_string_append(out_string, " Class: RAW image");
893 case FORMAT_CLASS_META:
894 out_string = g_string_append(out_string, " Class: Metadata");
896 case FORMAT_CLASS_VIDEO:
897 out_string = g_string_append(out_string, " Class: Video");
899 case FORMAT_CLASS_COLLECTION:
900 out_string = g_string_append(out_string, " Class: Collection");
902 case FORMAT_CLASS_DOCUMENT:
903 out_string = g_string_append(out_string, " Class: Document");
905 case FORMAT_CLASS_ARCHIVE:
906 out_string = g_string_append(out_string, " Class: Archive");
908 case FORMAT_CLASS_UNKNOWN:
909 out_string = g_string_append(out_string, " Class: Unknown");
912 out_string = g_string_append(out_string, " Class: Unknown");
915 out_string = g_string_append(out_string, "\n");
919 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
920 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
922 g_string_free(out_string, TRUE);
924 file_data_unref(dir_fd);
927 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
929 GString *contents = g_string_new(NULL);
931 if (is_collection(text))
933 collection_contents(text, &contents);
940 g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
941 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
943 g_string_free(contents, TRUE);
946 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
949 GList *collection_list = NULL;
951 GString *out_string = g_string_new(NULL);
953 collect_manager_list(&collection_list, NULL, NULL);
955 work = collection_list;
958 const gchar *collection_name = work->data;
959 out_string = g_string_append(out_string, g_strdup(collection_name));
960 out_string = g_string_append(out_string, "\n");
965 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
966 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
968 string_list_free(collection_list);
969 g_string_free(out_string, TRUE);
972 static gboolean wait_cb(const gpointer data)
974 gint position = GPOINTER_TO_INT(data);
975 gint x = position >> 16;
976 gint y = position - (x << 16);
978 gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
983 static void gr_geometry(const gchar *text, GIOChannel *channel, gpointer data)
987 if (!layout_valid(&lw_id) || !text)
994 geometry = g_strsplit_set(text, "+", 3);
995 if (geometry[1] != NULL && geometry[2] != NULL )
997 gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
1002 geometry = g_strsplit_set(text, "+x", 4);
1003 if (geometry[0] != NULL && geometry[1] != NULL)
1005 gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
1007 if (geometry[2] != NULL && geometry[3] != NULL)
1009 /* There is an occasional problem with a window_move immediately after a window_resize */
1010 g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
1013 g_strfreev(geometry);
1016 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
1018 get_filelist(text, channel, FALSE);
1021 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
1023 get_filelist(text, channel, TRUE);
1026 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
1029 gchar *collection_name = NULL;
1031 if (!layout_valid(&lw_id)) return;
1033 if (image_get_path(lw_id->image))
1035 if (lw_id->image->collection && lw_id->image->collection->name)
1037 collection_name = remove_extension_from_path(lw_id->image->collection->name);
1038 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
1042 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
1047 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
1050 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
1051 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1053 g_free(collection_name);
1057 static void gr_file_info(const gchar *text, GIOChannel *channel, gpointer data)
1061 gchar *country_name;
1062 gchar *country_code;
1065 GString *out_string;
1066 FileFormatClass format_class;
1068 if (!layout_valid(&lw_id)) return;
1070 if (image_get_path(lw_id->image))
1072 filename = g_strdup(image_get_path(lw_id->image));
1073 fd = file_data_new_group(filename);
1074 out_string = g_string_new(NULL);
1076 format_class = filter_file_get_class(image_get_path(lw_id->image));
1079 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1082 if (fd->page_total > 1)
1084 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1089 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1092 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1093 g_free(country_name);
1096 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1099 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1100 g_free(country_code);
1103 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1106 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1110 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1113 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1118 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
1119 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1121 g_string_free(out_string, TRUE);
1122 file_data_unref(fd);
1127 static gchar *config_file_path(const gchar *param)
1130 gchar *full_name = NULL;
1132 if (file_extension_match(param, ".xml"))
1134 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1136 else if (file_extension_match(param, NULL))
1138 full_name = g_strconcat(param, ".xml", NULL);
1139 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1152 static gboolean is_config_file(const gchar *param)
1156 name = config_file_path(param);
1165 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
1167 gchar *filename = expand_tilde(text);
1169 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1171 if (is_config_file(filename))
1173 gchar *tmp = config_file_path(filename);
1179 if (isfile(filename))
1181 load_config_from_file(filename, FALSE);
1185 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1186 layout_set_path(NULL, homedir());
1192 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
1194 gchar *filename = expand_tilde(text);
1195 FileData *fd = file_data_new_group(filename);
1198 if (fd->parent) fd = fd->parent;
1200 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1201 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1203 work = fd->sidecar_files;
1209 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1210 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1215 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
1217 gchar *filename = expand_tilde(text);
1218 FileData *fd = file_data_new_group(filename);
1220 if (fd->change && fd->change->dest)
1222 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
1223 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1228 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
1231 gchar *tilde_filename = expand_tilde(text);
1233 filename = set_pwd(tilde_filename);
1235 view_window_new(file_data_new_group(filename));
1237 g_free(tilde_filename);
1240 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
1242 RemoteData *remote_data = data;
1244 remote_data->command_collection = NULL;
1245 remote_data->file_list = NULL;
1246 remote_data->single_dir = TRUE;
1249 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
1251 RemoteData *remote_data = data;
1252 gboolean new = TRUE;
1257 /* If there is a files list on the command line
1258 * check if they are all in the same folder
1260 if (remote_data->single_dir)
1263 work = remote_data->file_list;
1264 while (work && remote_data->single_dir)
1267 dirname = g_path_get_dirname(((FileData *)work->data)->path);
1270 path = g_strdup(dirname);
1274 if (g_strcmp0(path, dirname) != 0)
1276 remote_data->single_dir = FALSE;
1285 gchar *pathname = g_path_get_dirname(text);
1286 layout_set_path(lw_id, pathname);
1289 fd = file_data_new_simple(text);
1290 remote_data->file_list = g_list_append(remote_data->file_list, fd);
1291 file_data_unref(fd);
1293 vf_select_none(lw_id->vf);
1294 remote_data->file_list = g_list_reverse(remote_data->file_list);
1296 layout_select_list(lw_id, remote_data->file_list);
1297 layout_refresh(lw_id);
1298 first = (FileData *)(g_list_first(vf_selection_get_list(lw_id->vf))->data);
1299 layout_set_fd(lw_id, first);
1304 if (!remote_data->command_collection && !remote_data->single_dir)
1306 cw = collection_window_new(NULL);
1309 collection_path_changed(cd);
1311 remote_data->command_collection = cd;
1313 else if (!remote_data->single_dir)
1315 new = (!collection_get_first(remote_data->command_collection));
1318 if (!remote_data->single_dir)
1320 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1321 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1323 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1328 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1330 if (layout_valid(&lw_id))
1332 gtk_window_present(GTK_WINDOW(lw_id->window));
1336 static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
1338 LayoutWindow *lw = NULL;
1343 pwd = g_strdup(text);
1347 static void gr_print0(const gchar *text, GIOChannel *channel, gpointer data)
1349 g_io_channel_write_chars(channel, "print0", -1, NULL, NULL);
1350 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1354 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1356 gchar *result = NULL;
1357 gchar **lua_command;
1359 lua_command = g_strsplit(text, ",", 2);
1361 if (lua_command[0] && lua_command[1])
1363 FileData *fd = file_data_new_group(lua_command[0]);
1364 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1367 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1371 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1376 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1379 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1381 g_strfreev(lua_command);
1386 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1387 struct _RemoteCommandEntry {
1390 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1391 gboolean needs_extra;
1392 gboolean prefer_command_line;
1397 static RemoteCommandEntry remote_commands[] = {
1398 /* short, long callback, extra, prefer, parameter, description */
1399 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
1400 { NULL, "--close-window", gr_close_window, FALSE, FALSE, NULL, N_("close window") },
1401 { NULL, "--config-load: ", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_(" load configuration from FILE") },
1402 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_("clean the metadata cache") },
1403 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1404 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1405 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1406 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1407 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1408 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1409 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1410 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
1411 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
1412 { NULL, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1413 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1414 { NULL, "--File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1415 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
1416 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
1417 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
1418 { NULL, "--geometry=", gr_geometry, TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
1419 { NULL, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1420 { NULL, "--get-collection-list", gr_collection_list, FALSE, FALSE, NULL, N_("get collection list") },
1421 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE (See Plugins Configuration)") },
1422 { NULL, "--get-file-info", gr_file_info, FALSE, FALSE, NULL, N_("get file info") },
1423 { NULL, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1424 { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1425 { NULL, "--get-rectangle", gr_rectangle, FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1426 { NULL, "--get-render-intent", gr_render_intent, FALSE, FALSE, NULL, N_("get render intent") },
1427 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1428 { NULL, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1429 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
1430 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1431 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
1433 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1435 { NULL, "--new-window", gr_new_window, FALSE, FALSE, NULL, N_("new window") },
1436 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
1437 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1438 { NULL, "--print0", gr_print0, TRUE, FALSE, NULL, N_("terminate returned data with null character instead of newline") },
1439 { NULL, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1440 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
1441 { NULL, "--raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1442 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1443 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
1444 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1445 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
1446 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
1447 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1448 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
1449 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
1450 { NULL, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1451 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1452 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1455 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1457 gboolean match = FALSE;
1461 while (!match && remote_commands[i].func != NULL)
1463 if (remote_commands[i].needs_extra)
1465 if (remote_commands[i].opt_s &&
1466 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1468 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1469 return &remote_commands[i];
1471 else if (remote_commands[i].opt_l &&
1472 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1474 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1475 return &remote_commands[i];
1480 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1481 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1483 if (offset) *offset = text;
1484 return &remote_commands[i];
1494 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1496 RemoteCommandEntry *entry;
1497 const gchar *offset;
1499 entry = remote_command_find(text, &offset);
1500 if (entry && entry->func)
1502 entry->func(offset, channel, data);
1506 log_printf("unknown remote command:%s\n", text);
1510 void remote_help(void)
1516 print_term(FALSE, _("Remote command list:\n"));
1519 while (remote_commands[i].func != NULL)
1521 if (remote_commands[i].description)
1523 s_opt_param = g_strdup(remote_commands[i].opt_s ? remote_commands[i].opt_s : "" );
1524 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1525 printf_term(FALSE, " %-4s %-40s%-s\n",
1528 remote_commands[i].description);
1529 g_free(s_opt_param);
1530 g_free(l_opt_param);
1534 printf_term(FALSE, N_("\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"));
1537 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1544 RemoteCommandEntry *entry;
1546 entry = remote_command_find(argv[i], NULL);
1549 list = g_list_append(list, argv[i]);
1551 else if (errors && !isname(argv[i]))
1553 *errors = g_list_append(*errors, argv[i]);
1562 * @param arg_exec Binary (argv0)
1563 * @param remote_list Evaluated and recognized remote commands
1564 * @param path The current path
1565 * @param cmd_list List of all non collections in Path (gchar *path)
1566 * @param collection_list List of all collections in argv
1568 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1569 GList *cmd_list, GList *collection_list)
1571 RemoteConnection *rc;
1572 gboolean started = FALSE;
1575 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1576 rc = remote_client_open(buf);
1581 gint retry_count = 12;
1582 gboolean blank = FALSE;
1584 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1586 command = g_string_new(arg_exec);
1592 RemoteCommandEntry *entry;
1597 entry = remote_command_find(text, NULL);
1600 /* If Geeqie is not running, stop the --new-window command opening a second window */
1601 if (g_strcmp0(text, "--new-window") == 0)
1603 remote_list = g_list_remove(remote_list, text);
1605 if (entry->prefer_command_line)
1607 remote_list = g_list_remove(remote_list, text);
1608 g_string_append(command, " ");
1609 g_string_append(command, text);
1611 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1618 if (blank || cmd_list || path) g_string_append(command, " --blank");
1619 if (get_debug_level()) g_string_append(command, " --debug");
1621 g_string_append(command, " &");
1622 runcmd(command->str);
1623 g_string_free(command, TRUE);
1625 while (!rc && retry_count > 0)
1627 usleep((retry_count > 10) ? 500000 : 1000000);
1628 rc = remote_client_open(buf);
1629 if (!rc) print_term(FALSE, ".");
1633 print_term(FALSE, "\n");
1642 const gchar *prefix;
1643 gboolean use_path = TRUE;
1644 gboolean sent = FALSE;
1650 RemoteCommandEntry *entry;
1655 entry = remote_command_find(text, NULL);
1658 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1660 remote_client_send(rc, text);
1665 if (cmd_list && cmd_list->next)
1667 prefix = "--list-add:";
1668 remote_client_send(rc, "--list-clear");
1680 text = g_strconcat(prefix, work->data, NULL);
1681 remote_client_send(rc, text);
1688 if (path && !cmd_list && use_path)
1692 text = g_strdup_printf("file:%s", path);
1693 remote_client_send(rc, text);
1699 work = collection_list;
1708 text = g_strdup_printf("file:%s", name);
1709 remote_client_send(rc, text);
1715 if (!started && !sent)
1717 remote_client_send(rc, "raise");
1722 print_term(TRUE, _("Remote not available\n"));
1728 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1730 RemoteConnection *remote_connection = remote_server_open(path);
1731 RemoteData *remote_data = g_new(RemoteData, 1);
1733 remote_data->command_collection = command_collection;
1735 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1736 return remote_connection;
1738 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */