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"
42 #include <sys/socket.h>
49 #define SERVER_MAX_CLIENTS 8
51 #define REMOTE_SERVER_BACKLOG 4
55 #define UNIX_PATH_MAX 108
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);
63 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
65 typedef struct _RemoteClient RemoteClient;
66 struct _RemoteClient {
68 guint channel_id; /* event source id */
72 typedef struct _RemoteData RemoteData;
74 CollectionData *command_collection;
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
81 static gboolean print0 = FALSE;
83 /* Remote commands from main.c are prepended with the current dir the remote
84 * command was made from. Some remote commands require this. The
85 * value is stored here
87 static gchar *pwd = NULL;
90 * @brief Ensures file path is absolute.
91 * @param[in] filename Filepath, absolute or relative to calling directory
92 * @returns absolute path
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
98 * Return value must be freed with g_free()
100 static gchar *set_pwd(gchar *filename)
104 if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
106 temp = g_build_filename(pwd, filename, NULL);
110 temp = g_strdup(filename);
116 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
118 RemoteClient *client = data;
119 RemoteConnection *rc;
120 GIOStatus status = G_IO_STATUS_NORMAL;
125 if (condition & G_IO_IN)
127 gchar *buffer = NULL;
128 GError *error = NULL;
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, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
136 buffer[termpos] = '\0';
138 if (strlen(buffer) > 0)
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, NULL, NULL); /* empty line finishes the command */
142 g_io_channel_flush(source, NULL);
152 log_printf("error reading socket: %s\n", error->message);
157 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
159 rc->clients = g_list_remove(rc->clients, client);
161 DEBUG_1("HUP detected, closing client.");
162 DEBUG_1("client count %d", g_list_length(rc->clients));
164 g_source_remove(client->channel_id);
172 static void remote_server_client_add(RemoteConnection *rc, gint fd)
174 RemoteClient *client;
177 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
179 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
184 client = g_new0(RemoteClient, 1);
188 channel = g_io_channel_unix_new(fd);
189 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
190 remote_server_client_cb, client, NULL);
191 g_io_channel_unref(channel);
193 rc->clients = g_list_append(rc->clients, client);
194 DEBUG_1("client count %d", g_list_length(rc->clients));
197 static void remote_server_clients_close(RemoteConnection *rc)
201 RemoteClient *client = rc->clients->data;
203 rc->clients = g_list_remove(rc->clients, client);
205 g_source_remove(client->channel_id);
211 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
213 RemoteConnection *rc = data;
217 fd = accept(rc->fd, NULL, &alen);
220 log_printf("error accepting socket: %s\n", strerror(errno));
224 remote_server_client_add(rc, fd);
229 gboolean remote_server_exists(const gchar *path)
231 RemoteConnection *rc;
233 /* verify server up */
234 rc = remote_client_open(path);
239 /* unable to connect, remove socket file to free up address */
244 static RemoteConnection *remote_server_open(const gchar *path)
246 RemoteConnection *rc;
247 struct sockaddr_un addr;
252 if (remote_server_exists(path))
254 log_printf("Address already in use: %s\n", path);
258 fd = socket(PF_UNIX, SOCK_STREAM, 0);
259 if (fd == -1) return NULL;
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, &addr, sizeof(addr)) == -1 ||
265 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
267 log_printf("error subscribing to socket: %s\n", strerror(errno));
272 rc = g_new0(RemoteConnection, 1);
276 rc->path = g_strdup(path);
278 channel = g_io_channel_unix_new(rc->fd);
279 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
281 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
282 remote_server_read_cb, rc, NULL);
283 g_io_channel_unref(channel);
288 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
290 if (!rc || !rc->server) return;
292 rc->read_func = func;
293 rc->read_data = data;
297 static RemoteConnection *remote_client_open(const gchar *path)
299 RemoteConnection *rc;
301 struct sockaddr_un addr;
305 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
307 fd = socket(PF_UNIX, SOCK_STREAM, 0);
308 if (fd == -1) return NULL;
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, &addr, sizeof(addr)) == -1)
315 DEBUG_1("error connecting to socket: %s", strerror(errno));
320 rc = g_new0(RemoteConnection, 1);
323 rc->path = g_strdup(path);
328 static sig_atomic_t sigpipe_occurred = FALSE;
330 static void sighandler_sigpipe(gint sig)
332 sigpipe_occurred = TRUE;
335 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
337 struct sigaction new_action, old_action;
338 gboolean ret = FALSE;
339 GError *error = NULL;
342 if (!rc || rc->server) return FALSE;
343 if (!text) return TRUE;
345 sigpipe_occurred = FALSE;
347 new_action.sa_handler = sighandler_sigpipe;
348 sigemptyset(&new_action.sa_mask);
349 new_action.sa_flags = 0;
351 /* setup our signal handler */
352 sigaction(SIGPIPE, &new_action, &old_action);
354 channel = g_io_channel_unix_new(rc->fd);
356 g_io_channel_write_chars(channel, text, -1, NULL, &error);
357 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, &error);
358 g_io_channel_flush(channel, &error);
362 log_printf("error reading socket: %s\n", error->message);
373 gchar *buffer = NULL;
375 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
376 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
380 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
386 buffer[termpos] = '\0';
387 if (g_strstr_len(buffer, -1, "print0") != 0)
395 printf("%s%c", buffer, 0);
399 printf("%s\n", buffer);
409 log_printf("error reading socket: %s\n", error->message);
416 /* restore the original signal handler */
417 sigaction(SIGPIPE, &old_action, NULL);
418 g_io_channel_unref(channel);
422 void remote_close(RemoteConnection *rc)
428 remote_server_clients_close(rc);
430 g_source_remove(rc->channel_id);
435 g_free(rc->read_data);
444 *-----------------------------------------------------------------------------
446 *-----------------------------------------------------------------------------
449 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
451 layout_image_next(lw_id);
454 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
456 LayoutWindow *lw = NULL;
458 if (!layout_valid(&lw)) return;
460 lw_id = layout_menu_new_window(NULL, lw);
461 layout_set_path(lw_id, pwd);
464 static gboolean gr_close_window_cb()
466 if (!layout_valid(&lw_id)) return FALSE;
468 layout_menu_close_cb(NULL, lw_id);
473 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
475 g_idle_add(gr_close_window_cb, NULL);
478 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
480 layout_image_prev(lw_id);
483 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
485 layout_image_first(lw_id);
488 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
490 layout_image_last(lw_id);
493 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
495 layout_image_full_screen_toggle(lw_id);
498 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
500 layout_image_full_screen_start(lw_id);
503 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
505 layout_image_full_screen_stop(lw_id);
508 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
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 *channel, gpointer data)
521 FileData *dir_fd = file_data_new_dir(text);
523 layout_valid(&lw_id);
524 list = filelist_recursive_full(dir_fd, lw_id->sort_method, lw_id->sort_ascend);
525 file_data_unref(dir_fd);
527 //printf("length: %d\n", g_list_length(list));
528 layout_image_slideshow_stop(lw_id);
529 layout_image_slideshow_start_from_list(lw_id, list);
532 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
534 if (!g_strcmp0(text, "clear"))
535 cache_maintain_home_remote(FALSE, TRUE);
536 else if (!g_strcmp0(text, "clean"))
537 cache_maintain_home_remote(FALSE, FALSE);
540 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
542 if (!g_strcmp0(text, "clear"))
543 cache_manager_standard_process_remote(TRUE);
544 else if (!g_strcmp0(text, "clean"))
545 cache_manager_standard_process_remote(FALSE);
548 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
550 cache_maintain_home_remote(TRUE, FALSE);
553 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
555 cache_manager_render_remote(text, FALSE, FALSE);
558 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
560 cache_manager_render_remote(text, TRUE, FALSE);
563 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
565 if(options->thumbnails.spec_standard)
566 cache_manager_render_remote(text, FALSE, TRUE);
569 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
571 if(options->thumbnails.spec_standard)
572 cache_manager_render_remote(text, TRUE, TRUE);
575 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
577 layout_image_slideshow_toggle(lw_id);
580 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
582 layout_image_slideshow_start(lw_id);
585 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
587 layout_image_slideshow_stop(lw_id);
590 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
592 gdouble t1, t2, t3, n;
595 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
598 n = (t1 * 3600) + (t2 * 60) + t3;
599 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
600 t1 >= 24 || t2 >= 60 || t3 >= 60)
602 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
603 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
610 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
611 t1 >= 60 || t2 >= 60)
613 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
614 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
621 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
623 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
624 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
633 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
636 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
641 if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
643 layout_tools_float_set(lw_id, popped, FALSE);
647 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
652 if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
654 layout_tools_float_set(lw_id, popped, TRUE);
658 static gboolean gr_quit_idle_cb(gpointer data)
665 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
667 /* schedule exit when idle, if done from within a
668 * remote handler remote_close will crash
670 g_idle_add(gr_quit_idle_cb, NULL);
673 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
676 gchar *tilde_filename;
678 if (!download_web_file(text, TRUE, NULL))
680 tilde_filename = expand_tilde(text);
681 filename = set_pwd(tilde_filename);
683 if (isfile(filename))
685 if (file_extension_match(filename, GQ_COLLECTION_EXT))
687 collection_window_new(filename);
691 layout_set_path(lw_id, filename);
694 else if (isdir(filename))
696 layout_set_path(lw_id, filename);
700 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
701 layout_set_path(lw_id, homedir());
705 g_free(tilde_filename);
709 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
711 gr_file_load_no_raise(text, channel, data);
713 gr_raise(text, channel, data);
716 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
719 gint x_pixel, y_pixel;
721 gint r_mouse, g_mouse, b_mouse;
723 LayoutWindow *lw = NULL;
725 if (!layout_valid(&lw_id)) return;
727 pr = (PixbufRenderer*)lw_id->image->pr;
731 pixbuf_renderer_get_image_size(pr, &width, &height);
732 if (width < 1 || height < 1) return;
734 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
736 if (x_pixel >= 0 && y_pixel >= 0)
738 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
739 &r_mouse, &g_mouse, &b_mouse);
741 pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
743 r_mouse, g_mouse, b_mouse);
745 g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
746 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
761 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
763 gchar *rectangle_info;
765 LayoutWindow *lw = NULL;
768 if (!options->draw_rectangle) return;
769 if (!layout_valid(&lw_id)) return;
771 pr = (PixbufRenderer*)lw_id->image->pr;
775 image_get_rectangle(&x1, &y1, &x2, &y2);
776 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
777 (x2 > x1) ? x2 - x1 : x1 - x2,
778 (y2 > y1) ? y2 - y1 : y1 - y2,
780 (y2 > y1) ? y1 : y2);
782 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
783 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
785 g_free(rectangle_info);
789 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
791 gchar *render_intent;
793 switch (options->color_profile.render_intent)
796 render_intent = g_strdup("Perceptual");
799 render_intent = g_strdup("Relative Colorimetric");
802 render_intent = g_strdup("Saturation");
805 render_intent = g_strdup("Absolute Colorimetric");
808 render_intent = g_strdup("none");
812 g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
813 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
815 g_free(render_intent);
818 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
821 FileFormatClass class;
824 GString *out_string = g_string_new(NULL);
826 gchar *tilde_filename;
828 if (strcmp(text, "") == 0)
830 if (layout_valid(&lw_id))
832 dir_fd = file_data_new_dir(lw_id->dir_fd->path);
841 tilde_filename = expand_tilde(text);
842 if (isdir(tilde_filename))
844 dir_fd = file_data_new_dir(tilde_filename);
848 g_free(tilde_filename);
851 g_free(tilde_filename);
856 list = filelist_recursive(dir_fd);
860 filelist_read(dir_fd, &list, NULL);
867 g_string_append_printf(out_string, "%s", fd->path);
868 class = filter_file_get_class(fd->path);
872 case FORMAT_CLASS_IMAGE:
873 out_string = g_string_append(out_string, " Class: Image");
875 case FORMAT_CLASS_RAWIMAGE:
876 out_string = g_string_append(out_string, " Class: RAW image");
878 case FORMAT_CLASS_META:
879 out_string = g_string_append(out_string, " Class: Metadata");
881 case FORMAT_CLASS_VIDEO:
882 out_string = g_string_append(out_string, " Class: Video");
884 case FORMAT_CLASS_COLLECTION:
885 out_string = g_string_append(out_string, " Class: Collection");
887 case FORMAT_CLASS_DOCUMENT:
888 out_string = g_string_append(out_string, " Class: Document");
890 case FORMAT_CLASS_UNKNOWN:
891 out_string = g_string_append(out_string, " Class: Unknown");
894 out_string = g_string_append(out_string, " Class: Unknown");
897 out_string = g_string_append(out_string, "\n");
901 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
902 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
904 g_string_free(out_string, TRUE);
906 file_data_unref(dir_fd);
909 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
911 GString *contents = g_string_new(NULL);
913 if (is_collection(text))
915 collection_contents(text, &contents);
922 g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
923 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
925 g_string_free(contents, TRUE);
928 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
931 GList *collection_list = NULL;
933 GString *out_string = g_string_new(NULL);
935 collect_manager_list(&collection_list, NULL, NULL);
937 work = collection_list;
940 const gchar *collection_name = work->data;
941 out_string = g_string_append(out_string, g_strdup(collection_name));
942 out_string = g_string_append(out_string, "\n");
947 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
948 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
950 string_list_free(collection_list);
951 g_string_free(out_string, TRUE);
955 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
957 get_filelist(text, channel, FALSE);
960 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
962 get_filelist(text, channel, TRUE);
965 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
968 gchar *collection_name = NULL;
970 if (!layout_valid(&lw_id)) return;
972 if (image_get_path(lw_id->image))
974 if (lw_id->image->collection && lw_id->image->collection->name)
976 collection_name = remove_extension_from_path(lw_id->image->collection->name);
977 out_string = g_strconcat(image_get_path(lw_id->image), " Collection: ", collection_name, NULL);
981 out_string = g_strconcat(image_get_path(lw_id->image), NULL);
986 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
989 g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
990 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
992 g_free(collection_name);
996 static void gr_file_info(const gchar *text, GIOChannel *channel, gpointer data)
1000 gchar *country_name;
1001 gchar *country_code;
1004 GString *out_string;
1005 FileFormatClass format_class;
1007 if (!layout_valid(&lw_id)) return;
1009 if (image_get_path(lw_id->image))
1011 filename = g_strdup(image_get_path(lw_id->image));
1012 fd = file_data_new_group(filename);
1013 out_string = g_string_new(NULL);
1015 format_class = filter_file_get_class(image_get_path(lw_id->image));
1018 g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1021 if (fd->page_total > 1)
1023 g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1028 country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1031 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1032 g_free(country_name);
1035 country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1038 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1039 g_free(country_code);
1042 timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1045 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1049 local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1052 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1057 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
1058 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1060 g_string_free(out_string, TRUE);
1061 file_data_unref(fd);
1066 static gchar *config_file_path(const gchar *param)
1069 gchar *full_name = NULL;
1071 if (file_extension_match(param, ".xml"))
1073 path = g_build_filename(get_window_layouts_dir(), param, NULL);
1075 else if (file_extension_match(param, NULL))
1077 full_name = g_strconcat(param, ".xml", NULL);
1078 path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
1091 static gboolean is_config_file(const gchar *param)
1095 name = config_file_path(param);
1104 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
1106 gchar *filename = expand_tilde(text);
1108 if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
1110 if (is_config_file(filename))
1112 gchar *tmp = config_file_path(filename);
1118 if (isfile(filename))
1120 load_config_from_file(filename, FALSE);
1124 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1125 layout_set_path(NULL, homedir());
1131 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
1133 gchar *filename = expand_tilde(text);
1134 FileData *fd = file_data_new_group(filename);
1137 if (fd->parent) fd = fd->parent;
1139 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1140 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1142 work = fd->sidecar_files;
1148 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1149 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1154 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
1156 gchar *filename = expand_tilde(text);
1157 FileData *fd = file_data_new_group(filename);
1159 if (fd->change && fd->change->dest)
1161 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
1162 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1167 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
1170 gchar *tilde_filename = expand_tilde(text);
1172 filename = set_pwd(tilde_filename);
1174 view_window_new(file_data_new_group(filename));
1176 g_free(tilde_filename);
1179 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
1181 RemoteData *remote_data = data;
1183 if (remote_data->command_collection)
1185 collection_unref(remote_data->command_collection);
1186 remote_data->command_collection = NULL;
1190 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
1192 RemoteData *remote_data = data;
1193 gboolean new = TRUE;
1195 if (!remote_data->command_collection)
1199 cd = collection_new("");
1204 cd->name = g_strdup(_("Command line"));
1206 remote_data->command_collection = cd;
1210 new = (!collection_get_first(remote_data->command_collection));
1213 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1215 layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
1219 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1221 LayoutWindow *lw = NULL;
1223 if (layout_valid(&lw_id))
1225 gtk_window_present(GTK_WINDOW(lw_id->window));
1229 static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
1231 LayoutWindow *lw = NULL;
1233 lw = layout_find_by_layout_id("main");
1236 lw = g_list_first(layout_window_list)->data;
1240 pwd = g_strdup(text);
1244 static void gr_print0(const gchar *text, GIOChannel *channel, gpointer data)
1246 LayoutWindow *lw = NULL;
1248 g_io_channel_write_chars(channel, "print0", -1, NULL, NULL);
1249 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1253 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1255 gchar *result = NULL;
1256 gchar **lua_command;
1258 lua_command = g_strsplit(text, ",", 2);
1260 if (lua_command[0] && lua_command[1])
1262 FileData *fd = file_data_new_group(lua_command[0]);
1263 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1266 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1270 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1275 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1278 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1280 g_strfreev(lua_command);
1285 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1286 struct _RemoteCommandEntry {
1289 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1290 gboolean needs_extra;
1291 gboolean prefer_command_line;
1296 static RemoteCommandEntry remote_commands[] = {
1297 /* short, long callback, extra, prefer, parameter, description */
1298 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
1299 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
1300 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
1301 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
1302 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
1303 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
1304 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
1305 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
1306 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
1307 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
1308 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1309 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1310 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
1311 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
1312 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
1313 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
1314 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1315 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
1316 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1317 { NULL, "--file:", gr_file_load, TRUE, FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
1318 { 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") },
1319 { 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") },
1320 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1321 { NULL, "--pixel-info", gr_pixel_info, FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1322 { NULL, "--get-rectangle", gr_rectangle, FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1323 { NULL, "--get-render-intent", gr_render_intent, FALSE, FALSE, NULL, N_("get render intent") },
1324 { NULL, "--get-filelist:", gr_filelist, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1325 { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE, FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1326 { NULL, "--get-collection:", gr_collection, TRUE, FALSE, N_("<COLLECTION>"), N_("get collection content") },
1327 { NULL, "--get-collection-list", gr_collection_list, FALSE, FALSE, NULL, N_("get collection list") },
1328 { NULL, "--get-file-info", gr_file_info, FALSE, FALSE, NULL, N_("get file info") },
1329 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1330 { NULL, "--view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
1331 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
1332 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1333 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1334 { NULL, "--raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1335 { NULL, "--id:", gr_lw_id, TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1336 { NULL, "--new-window", gr_new_window, FALSE, FALSE, NULL, N_("new window") },
1337 { NULL, "--close-window", gr_close_window, FALSE, FALSE, NULL, N_("close window") },
1338 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1339 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1340 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_(" clean the metadata cache") },
1341 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
1342 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1343 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1344 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1346 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1348 { NULL, "--PWD:", gr_pwd, TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1349 { NULL, "--print0", gr_print0, TRUE, FALSE, NULL, N_("terminate returned data with null character instead of newline") },
1350 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1353 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1355 gboolean match = FALSE;
1359 while (!match && remote_commands[i].func != NULL)
1361 if (remote_commands[i].needs_extra)
1363 if (remote_commands[i].opt_s &&
1364 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1366 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1367 return &remote_commands[i];
1369 else if (remote_commands[i].opt_l &&
1370 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1372 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1373 return &remote_commands[i];
1378 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1379 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1381 if (offset) *offset = text;
1382 return &remote_commands[i];
1392 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1394 RemoteCommandEntry *entry;
1395 const gchar *offset;
1397 entry = remote_command_find(text, &offset);
1398 if (entry && entry->func)
1400 entry->func(offset, channel, data);
1404 log_printf("unknown remote command:%s\n", text);
1408 void remote_help(void)
1414 print_term(FALSE, _("Remote command list:\n"));
1417 while (remote_commands[i].func != NULL)
1419 if (remote_commands[i].description)
1421 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1422 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1423 printf_term(FALSE, " %-11s%-1s %-30s%-s\n",
1424 (remote_commands[i].opt_s) ? s_opt_param : "",
1425 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1426 (remote_commands[i].opt_l) ? l_opt_param : "",
1427 _(remote_commands[i].description));
1428 g_free(s_opt_param);
1429 g_free(l_opt_param);
1433 printf_term(FALSE, N_("\n All other command line parameters are used as plain files if they exists.\n"));
1436 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1443 RemoteCommandEntry *entry;
1445 entry = remote_command_find(argv[i], NULL);
1448 list = g_list_append(list, argv[i]);
1450 else if (errors && !isname(argv[i]))
1452 *errors = g_list_append(*errors, argv[i]);
1461 * \param arg_exec Binary (argv0)
1462 * \param remote_list Evaluated and recognized remote commands
1463 * \param path The current path
1464 * \param cmd_list List of all non collections in Path
1465 * \param collection_list List of all collections in argv
1467 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1468 GList *cmd_list, GList *collection_list)
1470 RemoteConnection *rc;
1471 gboolean started = FALSE;
1474 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1475 rc = remote_client_open(buf);
1480 gint retry_count = 12;
1481 gboolean blank = FALSE;
1483 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1485 command = g_string_new(arg_exec);
1491 RemoteCommandEntry *entry;
1496 entry = remote_command_find(text, NULL);
1499 /* If Geeqie is not running, stop the --new-window command opening a second window */
1500 if (g_strcmp0(text, "--new-window") == 0)
1502 remote_list = g_list_remove(remote_list, text);
1504 if (entry->prefer_command_line)
1506 remote_list = g_list_remove(remote_list, text);
1507 g_string_append(command, " ");
1508 g_string_append(command, text);
1510 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1517 if (blank || cmd_list || path) g_string_append(command, " --blank");
1518 if (get_debug_level()) g_string_append(command, " --debug");
1520 g_string_append(command, " &");
1521 runcmd(command->str);
1522 g_string_free(command, TRUE);
1524 while (!rc && retry_count > 0)
1526 usleep((retry_count > 10) ? 500000 : 1000000);
1527 rc = remote_client_open(buf);
1528 if (!rc) print_term(FALSE, ".");
1532 print_term(FALSE, "\n");
1541 const gchar *prefix;
1542 gboolean use_path = TRUE;
1543 gboolean sent = FALSE;
1549 RemoteCommandEntry *entry;
1554 entry = remote_command_find(text, NULL);
1557 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1559 remote_client_send(rc, text);
1564 if (cmd_list && cmd_list->next)
1566 prefix = "--list-add:";
1567 remote_client_send(rc, "--list-clear");
1583 text = g_strconcat(prefix, fd->path, NULL);
1584 remote_client_send(rc, text);
1590 if (path && !cmd_list && use_path)
1594 text = g_strdup_printf("file:%s", path);
1595 remote_client_send(rc, text);
1601 work = collection_list;
1610 text = g_strdup_printf("file:%s", name);
1611 remote_client_send(rc, text);
1617 if (!started && !sent)
1619 remote_client_send(rc, "raise");
1624 print_term(TRUE, _("Remote not available\n"));
1630 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1632 RemoteConnection *remote_connection = remote_server_open(path);
1633 RemoteData *remote_data = g_new(RemoteData, 1);
1635 remote_data->command_collection = command_collection;
1637 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1638 return remote_connection;
1640 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */