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"
31 #include "layout_image.h"
33 #include "slideshow.h"
34 #include "ui_fileops.h"
37 #include <sys/socket.h>
44 #define SERVER_MAX_CLIENTS 8
46 #define REMOTE_SERVER_BACKLOG 4
50 #define UNIX_PATH_MAX 108
54 static RemoteConnection *remote_client_open(const gchar *path);
55 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
56 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
59 typedef struct _RemoteClient RemoteClient;
60 struct _RemoteClient {
62 guint channel_id; /* event source id */
66 typedef struct _RemoteData RemoteData;
68 CollectionData *command_collection;
72 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
74 RemoteClient *client = data;
76 GIOStatus status = G_IO_STATUS_NORMAL;
80 if (condition & G_IO_IN)
86 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
90 buffer[termpos] = '\0';
92 if (strlen(buffer) > 0)
94 if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
95 g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
96 g_io_channel_flush(source, NULL);
106 log_printf("error reading socket: %s\n", error->message);
111 if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
113 rc->clients = g_list_remove(rc->clients, client);
115 DEBUG_1("HUP detected, closing client.");
116 DEBUG_1("client count %d", g_list_length(rc->clients));
118 g_source_remove(client->channel_id);
126 static void remote_server_client_add(RemoteConnection *rc, gint fd)
128 RemoteClient *client;
131 if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
133 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
138 client = g_new0(RemoteClient, 1);
142 channel = g_io_channel_unix_new(fd);
143 client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
144 remote_server_client_cb, client, NULL);
145 g_io_channel_unref(channel);
147 rc->clients = g_list_append(rc->clients, client);
148 DEBUG_1("client count %d", g_list_length(rc->clients));
151 static void remote_server_clients_close(RemoteConnection *rc)
155 RemoteClient *client = rc->clients->data;
157 rc->clients = g_list_remove(rc->clients, client);
159 g_source_remove(client->channel_id);
165 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
167 RemoteConnection *rc = data;
171 fd = accept(rc->fd, NULL, &alen);
174 log_printf("error accepting socket: %s\n", strerror(errno));
178 remote_server_client_add(rc, fd);
183 static gboolean remote_server_exists(const gchar *path)
185 RemoteConnection *rc;
187 /* verify server up */
188 rc = remote_client_open(path);
193 /* unable to connect, remove socket file to free up address */
198 static RemoteConnection *remote_server_open(const gchar *path)
200 RemoteConnection *rc;
201 struct sockaddr_un addr;
206 if (remote_server_exists(path))
208 log_printf("Address already in use: %s\n", path);
212 fd = socket(PF_UNIX, SOCK_STREAM, 0);
213 if (fd == -1) return NULL;
215 addr.sun_family = AF_UNIX;
216 sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
217 strncpy(addr.sun_path, path, sun_path_len);
218 if (bind(fd, &addr, sizeof(addr)) == -1 ||
219 listen(fd, REMOTE_SERVER_BACKLOG) == -1)
221 log_printf("error subscribing to socket: %s\n", strerror(errno));
226 rc = g_new0(RemoteConnection, 1);
230 rc->path = g_strdup(path);
232 channel = g_io_channel_unix_new(rc->fd);
233 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
235 rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
236 remote_server_read_cb, rc, NULL);
237 g_io_channel_unref(channel);
242 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
244 if (!rc || !rc->server) return;
246 rc->read_func = func;
247 rc->read_data = data;
251 static RemoteConnection *remote_client_open(const gchar *path)
253 RemoteConnection *rc;
255 struct sockaddr_un addr;
259 if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
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 (connect(fd, &addr, sizeof(addr)) == -1)
269 DEBUG_1("error connecting to socket: %s", strerror(errno));
274 rc = g_new0(RemoteConnection, 1);
277 rc->path = g_strdup(path);
282 static sig_atomic_t sigpipe_occured = FALSE;
284 static void sighandler_sigpipe(gint sig)
286 sigpipe_occured = TRUE;
289 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
291 struct sigaction new_action, old_action;
292 gboolean ret = FALSE;
293 GError *error = NULL;
296 if (!rc || rc->server) return FALSE;
297 if (!text) return TRUE;
299 sigpipe_occured = FALSE;
301 new_action.sa_handler = sighandler_sigpipe;
302 sigemptyset(&new_action.sa_mask);
303 new_action.sa_flags = 0;
305 /* setup our signal handler */
306 sigaction(SIGPIPE, &new_action, &old_action);
308 channel = g_io_channel_unix_new(rc->fd);
310 g_io_channel_write_chars(channel, text, -1, NULL, &error);
311 g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
312 g_io_channel_flush(channel, &error);
316 log_printf("error reading socket: %s\n", error->message);
327 gchar *buffer = NULL;
329 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
333 if (buffer[0] == '\n') /* empty line finishes the command */
339 buffer[termpos] = '\0';
340 printf("%s\n", buffer);
348 log_printf("error reading socket: %s\n", error->message);
355 /* restore the original signal handler */
356 sigaction(SIGPIPE, &old_action, NULL);
357 g_io_channel_unref(channel);
361 void remote_close(RemoteConnection *rc)
367 remote_server_clients_close(rc);
369 g_source_remove(rc->channel_id);
374 g_free(rc->read_data);
383 *-----------------------------------------------------------------------------
385 *-----------------------------------------------------------------------------
388 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
390 layout_image_next(NULL);
393 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
395 layout_image_prev(NULL);
398 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
400 layout_image_first(NULL);
403 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
405 layout_image_last(NULL);
408 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
410 layout_image_full_screen_toggle(NULL);
413 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
415 layout_image_full_screen_start(NULL);
418 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
420 layout_image_full_screen_stop(NULL);
423 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
426 FileData *dir_fd = file_data_new_dir(text);
427 list = filelist_recursive(dir_fd);
428 file_data_unref(dir_fd);
430 //printf("length: %d\n", g_list_length(list));
431 layout_image_slideshow_stop(NULL);
432 layout_image_slideshow_start_from_list(NULL, list);
435 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
437 if (!g_strcmp0(text, "clear"))
438 cache_maintain_home_remote(FALSE, TRUE);
439 else if (!g_strcmp0(text, "clean"))
440 cache_maintain_home_remote(FALSE, FALSE);
443 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
445 if (!g_strcmp0(text, "clear"))
446 cache_manager_standard_process_remote(TRUE);
447 else if (!g_strcmp0(text, "clean"))
448 cache_manager_standard_process_remote(FALSE);
451 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
453 cache_maintain_home_remote(TRUE, FALSE);
456 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
458 cache_manager_render_remote(text, FALSE, FALSE);
461 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
463 cache_manager_render_remote(text, TRUE, FALSE);
466 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
468 if(options->thumbnails.spec_standard)
469 cache_manager_render_remote(text, FALSE, TRUE);
472 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
474 if(options->thumbnails.spec_standard)
475 cache_manager_render_remote(text, TRUE, TRUE);
478 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
480 layout_image_slideshow_toggle(NULL);
483 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
485 layout_image_slideshow_start(NULL);
488 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
490 layout_image_slideshow_stop(NULL);
493 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
495 gdouble t1, t2, t3, n;
498 res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
501 n = (t1 * 3600) + (t2 * 60) + t3;
502 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
503 t1 >= 24 || t2 >= 60 || t3 >= 60)
505 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
506 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
513 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
514 t1 >= 60 || t2 >= 60)
516 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
517 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
524 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
526 printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
527 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
536 options->slideshow.delay = (gint)(n * 10.0 + 0.01);
539 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
544 if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
546 layout_tools_float_set(NULL, popped, FALSE);
550 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
555 if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
557 layout_tools_float_set(NULL, popped, TRUE);
561 static gboolean gr_quit_idle_cb(gpointer data)
568 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
570 /* schedule exit when idle, if done from within a
571 * remote handler remote_close will crash
573 g_idle_add(gr_quit_idle_cb, NULL);
576 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
578 gchar *filename = expand_tilde(text);
580 if (isfile(filename))
582 if (file_extension_match(filename, GQ_COLLECTION_EXT))
584 collection_window_new(filename);
588 layout_set_path(NULL, filename);
591 else if (isdir(filename))
593 layout_set_path(NULL, filename);
597 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
598 layout_set_path(NULL, homedir());
604 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
606 gr_file_load_no_raise(text, channel, data);
608 gr_raise(text, channel, data);
611 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
613 LayoutWindow *lw = NULL; /* NULL to force layout_valid() to do some magic */
614 if (!layout_valid(&lw)) return;
615 if (image_get_path(lw->image))
617 g_io_channel_write_chars(channel, image_get_path(lw->image), -1, NULL, NULL);
618 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
622 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
624 gchar *filename = expand_tilde(text);
626 if (isfile(filename))
628 load_config_from_file(filename, FALSE);
632 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
633 layout_set_path(NULL, homedir());
639 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
641 gchar *filename = expand_tilde(text);
642 FileData *fd = file_data_new_group(filename);
645 if (fd->parent) fd = fd->parent;
647 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
648 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
650 work = fd->sidecar_files;
656 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
657 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
662 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
664 gchar *filename = expand_tilde(text);
665 FileData *fd = file_data_new_group(filename);
667 if (fd->change && fd->change->dest)
669 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
670 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
675 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
677 gchar *filename = expand_tilde(text);
679 view_window_new(file_data_new_group(filename));
683 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
685 RemoteData *remote_data = data;
687 if (remote_data->command_collection)
689 collection_unref(remote_data->command_collection);
690 remote_data->command_collection = NULL;
694 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
696 RemoteData *remote_data = data;
699 if (!remote_data->command_collection)
703 cd = collection_new("");
708 cd->name = g_strdup(_("Command line"));
710 remote_data->command_collection = cd;
714 new = (!collection_get_first(remote_data->command_collection));
717 if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
719 layout_image_set_collection(NULL, remote_data->command_collection,
720 collection_get_first(remote_data->command_collection));
724 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
726 LayoutWindow *lw = NULL;
728 if (layout_valid(&lw))
730 gtk_window_present(GTK_WINDOW(lw->window));
735 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
737 gchar *result = NULL;
740 lua_command = g_strsplit(text, ",", 2);
742 if (lua_command[0] && lua_command[1])
744 FileData *fd = file_data_new_group(lua_command[0]);
745 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
748 g_io_channel_write_chars(channel, result, -1, NULL, NULL);
752 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
757 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
760 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
762 g_strfreev(lua_command);
767 typedef struct _RemoteCommandEntry RemoteCommandEntry;
768 struct _RemoteCommandEntry {
771 void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
772 gboolean needs_extra;
773 gboolean prefer_command_line;
778 static RemoteCommandEntry remote_commands[] = {
779 /* short, long callback, extra, prefer, parameter, description */
780 { "-n", "--next", gr_image_next, FALSE, FALSE, NULL, N_("next image") },
781 { "-b", "--back", gr_image_prev, FALSE, FALSE, NULL, N_("previous image") },
782 { NULL, "--first", gr_image_first, FALSE, FALSE, NULL, N_("first image") },
783 { NULL, "--last", gr_image_last, FALSE, FALSE, NULL, N_("last image") },
784 { "-f", "--fullscreen", gr_fullscreen_toggle, FALSE, TRUE, NULL, N_("toggle full screen") },
785 { "-fs","--fullscreen-start", gr_fullscreen_start, FALSE, FALSE, NULL, N_("start full screen") },
786 { "-fS","--fullscreen-stop", gr_fullscreen_stop, FALSE, FALSE, NULL, N_("stop full screen") },
787 { "-s", "--slideshow", gr_slideshow_toggle, FALSE, TRUE, NULL, N_("toggle slide show") },
788 { "-ss","--slideshow-start", gr_slideshow_start, FALSE, FALSE, NULL, N_("start slide show") },
789 { "-sS","--slideshow-stop", gr_slideshow_stop, FALSE, FALSE, NULL, N_("stop slide show") },
790 { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE, FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
791 { "-d", "--delay=", gr_slideshow_delay, TRUE, FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
792 { "+t", "--tools-show", gr_tools_show, FALSE, TRUE, NULL, N_("show tools") },
793 { "-t", "--tools-hide", gr_tools_hide, FALSE, TRUE, NULL, N_("hide tools") },
794 { "-q", "--quit", gr_quit, FALSE, FALSE, NULL, N_("quit") },
795 { NULL, "--config-load:", gr_config_load, TRUE, FALSE, N_("<FILE>"), N_("load configuration from FILE") },
796 { NULL, "--get-sidecars:", gr_get_sidecars, TRUE, FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
797 { NULL, "--get-destination:", gr_get_destination, TRUE, FALSE, N_("<FILE>"), N_("get destination path of FILE") },
798 { NULL, "file:", gr_file_load, TRUE, FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
799 { NULL, "File:", gr_file_load_no_raise, TRUE, FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
800 { NULL, "--tell", gr_file_tell, FALSE, FALSE, NULL, N_("print filename of current image") },
801 { NULL, "view:", gr_file_view, TRUE, FALSE, N_("<FILE>"), N_("open FILE in new window") },
802 { NULL, "--list-clear", gr_list_clear, FALSE, FALSE, NULL, N_("clear command line collection list") },
803 { NULL, "--list-add:", gr_list_add, TRUE, FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
804 { NULL, "raise", gr_raise, FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
805 { "-ct:", "--cache-thumbs:", gr_cache_thumb, TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
806 { "-cs:", "--cache-shared:", gr_cache_shared, TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
807 { "-cm","--cache-metadata", gr_cache_metadata, FALSE, FALSE, NULL, N_(" clean the metadata cache") },
808 { "-cr:", "--cache-render:", gr_cache_render, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails") },
809 { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
810 { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
811 { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
813 { NULL, "--lua:", gr_lua, TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
815 { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
818 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
820 gboolean match = FALSE;
824 while (!match && remote_commands[i].func != NULL)
826 if (remote_commands[i].needs_extra)
828 if (remote_commands[i].opt_s &&
829 strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
831 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
832 return &remote_commands[i];
834 else if (remote_commands[i].opt_l &&
835 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
837 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
838 return &remote_commands[i];
843 if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
844 (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
846 if (offset) *offset = text;
847 return &remote_commands[i];
857 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
859 RemoteCommandEntry *entry;
862 entry = remote_command_find(text, &offset);
863 if (entry && entry->func)
865 entry->func(offset, channel, data);
869 log_printf("unknown remote command:%s\n", text);
873 void remote_help(void)
879 print_term(FALSE, _("Remote command list:\n"));
882 while (remote_commands[i].func != NULL)
884 if (remote_commands[i].description)
886 s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
887 l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
888 printf_term(FALSE, " %-11s%-1s %-30s%-s\n",
889 (remote_commands[i].opt_s) ? s_opt_param : "",
890 (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
891 (remote_commands[i].opt_l) ? l_opt_param : "",
892 _(remote_commands[i].description));
898 printf_term(FALSE, N_("\n All other command line parameters are used as plain files if they exists.\n"));
901 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
908 RemoteCommandEntry *entry;
910 entry = remote_command_find(argv[i], NULL);
913 list = g_list_append(list, argv[i]);
915 else if (errors && !isfile(argv[i]))
917 *errors = g_list_append(*errors, argv[i]);
926 * \param arg_exec Binary (argv0)
927 * \param remote_list Evaluated and recognized remote commands
928 * \param path The current path
929 * \param cmd_list List of all non collections in Path
930 * \param collection_list List of all collections in argv
932 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
933 GList *cmd_list, GList *collection_list)
935 RemoteConnection *rc;
936 gboolean started = FALSE;
939 buf = g_build_filename(get_rc_dir(), ".command", NULL);
940 rc = remote_client_open(buf);
945 gint retry_count = 12;
946 gboolean blank = FALSE;
948 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
950 command = g_string_new(arg_exec);
956 RemoteCommandEntry *entry;
961 entry = remote_command_find(text, NULL);
964 if (entry->prefer_command_line)
966 remote_list = g_list_remove(remote_list, text);
967 g_string_append(command, " ");
968 g_string_append(command, text);
970 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
977 if (blank || cmd_list || path) g_string_append(command, " --blank");
978 if (get_debug_level()) g_string_append(command, " --debug");
980 g_string_append(command, " &");
981 runcmd(command->str);
982 g_string_free(command, TRUE);
984 while (!rc && retry_count > 0)
986 usleep((retry_count > 10) ? 500000 : 1000000);
987 rc = remote_client_open(buf);
988 if (!rc) print_term(FALSE, ".");
992 print_term(FALSE, "\n");
1001 const gchar *prefix;
1002 gboolean use_path = TRUE;
1003 gboolean sent = FALSE;
1009 RemoteCommandEntry *entry;
1014 entry = remote_command_find(text, NULL);
1017 strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1019 remote_client_send(rc, text);
1024 if (cmd_list && cmd_list->next)
1026 prefix = "--list-add:";
1027 remote_client_send(rc, "--list-clear");
1043 text = g_strconcat(prefix, fd->path, NULL);
1044 remote_client_send(rc, text);
1050 if (path && !cmd_list && use_path)
1054 text = g_strdup_printf("file:%s", path);
1055 remote_client_send(rc, text);
1061 work = collection_list;
1070 text = g_strdup_printf("file:%s", name);
1071 remote_client_send(rc, text);
1077 if (!started && !sent)
1079 remote_client_send(rc, "raise");
1084 print_term(TRUE, _("Remote not available\n"));
1090 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1092 RemoteConnection *remote_connection = remote_server_open(path);
1093 RemoteData *remote_data = g_new(RemoteData, 1);
1095 remote_data->command_collection = command_collection;
1097 remote_server_subscribe(remote_connection, remote_cb, remote_data);
1098 return remote_connection;
1100 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */