Fix #269: External image marks - (--remote --lua)
[geeqie.git] / src / remote.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "remote.h"
24
25 #include "cache_maint.h"
26 #include "collect.h"
27 #include "filedata.h"
28 #include "image.h"
29 #include "img-view.h"
30 #include "layout.h"
31 #include "layout_image.h"
32 #include "misc.h"
33 #include "slideshow.h"
34 #include "ui_fileops.h"
35 #include "rcfile.h"
36
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <signal.h>
40 #include <errno.h>
41
42 #include "glua.h"
43
44 #define SERVER_MAX_CLIENTS 8
45
46 #define REMOTE_SERVER_BACKLOG 4
47
48
49 #ifndef UNIX_PATH_MAX
50 #define UNIX_PATH_MAX 108
51 #endif
52
53
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);
57
58
59 typedef struct _RemoteClient RemoteClient;
60 struct _RemoteClient {
61         gint fd;
62         guint channel_id; /* event source id */
63         RemoteConnection *rc;
64 };
65
66 typedef struct _RemoteData RemoteData;
67 struct _RemoteData {
68         CollectionData *command_collection;
69 };
70
71
72 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
73 {
74         RemoteClient *client = data;
75         RemoteConnection *rc;
76         GIOStatus status = G_IO_STATUS_NORMAL;
77
78         rc = client->rc;
79
80         if (condition & G_IO_IN)
81                 {
82                 gchar *buffer = NULL;
83                 GError *error = NULL;
84                 gsize termpos;
85
86                 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
87                         {
88                         if (buffer)
89                                 {
90                                 buffer[termpos] = '\0';
91
92                                 if (strlen(buffer) > 0)
93                                         {
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);
97                                         }
98                                 g_free(buffer);
99
100                                 buffer = NULL;
101                                 }
102                         }
103
104                 if (error)
105                         {
106                         log_printf("error reading socket: %s\n", error->message);
107                         g_error_free(error);
108                         }
109                 }
110
111         if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
112                 {
113                 rc->clients = g_list_remove(rc->clients, client);
114
115                 DEBUG_1("HUP detected, closing client.");
116                 DEBUG_1("client count %d", g_list_length(rc->clients));
117
118                 g_source_remove(client->channel_id);
119                 close(client->fd);
120                 g_free(client);
121                 }
122
123         return TRUE;
124 }
125
126 static void remote_server_client_add(RemoteConnection *rc, gint fd)
127 {
128         RemoteClient *client;
129         GIOChannel *channel;
130
131         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
132                 {
133                 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
134                 close(fd);
135                 return;
136                 }
137
138         client = g_new0(RemoteClient, 1);
139         client->rc = rc;
140         client->fd = fd;
141
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);
146
147         rc->clients = g_list_append(rc->clients, client);
148         DEBUG_1("client count %d", g_list_length(rc->clients));
149 }
150
151 static void remote_server_clients_close(RemoteConnection *rc)
152 {
153         while (rc->clients)
154                 {
155                 RemoteClient *client = rc->clients->data;
156
157                 rc->clients = g_list_remove(rc->clients, client);
158
159                 g_source_remove(client->channel_id);
160                 close(client->fd);
161                 g_free(client);
162                 }
163 }
164
165 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
166 {
167         RemoteConnection *rc = data;
168         gint fd;
169         guint alen;
170
171         fd = accept(rc->fd, NULL, &alen);
172         if (fd == -1)
173                 {
174                 log_printf("error accepting socket: %s\n", strerror(errno));
175                 return TRUE;
176                 }
177
178         remote_server_client_add(rc, fd);
179
180         return TRUE;
181 }
182
183 static gboolean remote_server_exists(const gchar *path)
184 {
185         RemoteConnection *rc;
186
187         /* verify server up */
188         rc = remote_client_open(path);
189         remote_close(rc);
190
191         if (rc) return TRUE;
192
193         /* unable to connect, remove socket file to free up address */
194         unlink(path);
195         return FALSE;
196 }
197
198 static RemoteConnection *remote_server_open(const gchar *path)
199 {
200         RemoteConnection *rc;
201         struct sockaddr_un addr;
202         gint sun_path_len;
203         gint fd;
204         GIOChannel *channel;
205
206         if (remote_server_exists(path))
207                 {
208                 log_printf("Address already in use: %s\n", path);
209                 return NULL;
210                 }
211
212         fd = socket(PF_UNIX, SOCK_STREAM, 0);
213         if (fd == -1) return NULL;
214
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)
220                 {
221                 log_printf("error subscribing to socket: %s\n", strerror(errno));
222                 close(fd);
223                 return NULL;
224                 }
225
226         rc = g_new0(RemoteConnection, 1);
227
228         rc->server = TRUE;
229         rc->fd = fd;
230         rc->path = g_strdup(path);
231
232         channel = g_io_channel_unix_new(rc->fd);
233         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
234
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);
238
239         return rc;
240 }
241
242 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
243 {
244         if (!rc || !rc->server) return;
245
246         rc->read_func = func;
247         rc->read_data = data;
248 }
249
250
251 static RemoteConnection *remote_client_open(const gchar *path)
252 {
253         RemoteConnection *rc;
254         struct stat st;
255         struct sockaddr_un addr;
256         gint sun_path_len;
257         gint fd;
258
259         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
260
261         fd = socket(PF_UNIX, SOCK_STREAM, 0);
262         if (fd == -1) return NULL;
263
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)
268                 {
269                 DEBUG_1("error connecting to socket: %s", strerror(errno));
270                 close(fd);
271                 return NULL;
272                 }
273
274         rc = g_new0(RemoteConnection, 1);
275         rc->server = FALSE;
276         rc->fd = fd;
277         rc->path = g_strdup(path);
278
279         return rc;
280 }
281
282 static sig_atomic_t sigpipe_occured = FALSE;
283
284 static void sighandler_sigpipe(gint sig)
285 {
286         sigpipe_occured = TRUE;
287 }
288
289 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
290 {
291         struct sigaction new_action, old_action;
292         gboolean ret = FALSE;
293         GError *error = NULL;
294         GIOChannel *channel;
295
296         if (!rc || rc->server) return FALSE;
297         if (!text) return TRUE;
298
299         sigpipe_occured = FALSE;
300
301         new_action.sa_handler = sighandler_sigpipe;
302         sigemptyset(&new_action.sa_mask);
303         new_action.sa_flags = 0;
304
305         /* setup our signal handler */
306         sigaction(SIGPIPE, &new_action, &old_action);
307
308         channel = g_io_channel_unix_new(rc->fd);
309
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);
313
314         if (error)
315                 {
316                 log_printf("error reading socket: %s\n", error->message);
317                 g_error_free(error);
318                 ret = FALSE;;
319                 }
320         else
321                 {
322                 ret = TRUE;
323                 }
324
325         if (ret)
326                 {
327                 gchar *buffer = NULL;
328                 gsize termpos;
329                 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
330                         {
331                         if (buffer)
332                                 {
333                                 if (buffer[0] == '\n') /* empty line finishes the command */
334                                         {
335                                         g_free(buffer);
336                                         fflush(stdout);
337                                         break;
338                                         }
339                                 buffer[termpos] = '\0';
340                                 printf("%s\n", buffer);
341                                 g_free(buffer);
342                                 buffer = NULL;
343                                 }
344                         }
345
346                 if (error)
347                         {
348                         log_printf("error reading socket: %s\n", error->message);
349                         g_error_free(error);
350                         ret = FALSE;
351                         }
352                 }
353
354
355         /* restore the original signal handler */
356         sigaction(SIGPIPE, &old_action, NULL);
357         g_io_channel_unref(channel);
358         return ret;
359 }
360
361 void remote_close(RemoteConnection *rc)
362 {
363         if (!rc) return;
364
365         if (rc->server)
366                 {
367                 remote_server_clients_close(rc);
368
369                 g_source_remove(rc->channel_id);
370                 unlink(rc->path);
371                 }
372
373         if (rc->read_data)
374                 g_free(rc->read_data);
375
376         close(rc->fd);
377
378         g_free(rc->path);
379         g_free(rc);
380 }
381
382 /*
383  *-----------------------------------------------------------------------------
384  * remote functions
385  *-----------------------------------------------------------------------------
386  */
387
388 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
389 {
390         layout_image_next(NULL);
391 }
392
393 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
394 {
395         layout_image_prev(NULL);
396 }
397
398 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
399 {
400         layout_image_first(NULL);
401 }
402
403 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
404 {
405         layout_image_last(NULL);
406 }
407
408 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
409 {
410         layout_image_full_screen_toggle(NULL);
411 }
412
413 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
414 {
415         layout_image_full_screen_start(NULL);
416 }
417
418 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
419 {
420         layout_image_full_screen_stop(NULL);
421 }
422
423 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
424 {
425         GList *list;
426         FileData *dir_fd = file_data_new_dir(text);
427         list = filelist_recursive(dir_fd);
428         file_data_unref(dir_fd);
429         if (!list) return;
430 //printf("length: %d\n", g_list_length(list));
431         layout_image_slideshow_stop(NULL);
432         layout_image_slideshow_start_from_list(NULL, list);
433 }
434
435 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
436 {
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);
441 }
442
443 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
444 {
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);
449 }
450
451 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
452 {
453         cache_maintain_home_remote(TRUE, FALSE);
454 }
455
456 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
457 {
458         cache_manager_render_remote(text, FALSE, FALSE);
459 }
460
461 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
462 {
463         cache_manager_render_remote(text, TRUE, FALSE);
464 }
465
466 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
467 {
468         if(options->thumbnails.spec_standard)
469                 cache_manager_render_remote(text, FALSE, TRUE);
470 }
471
472 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
473 {
474         if(options->thumbnails.spec_standard)
475                 cache_manager_render_remote(text, TRUE, TRUE);
476 }
477
478 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
479 {
480         layout_image_slideshow_toggle(NULL);
481 }
482
483 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
484 {
485         layout_image_slideshow_start(NULL);
486 }
487
488 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
489 {
490         layout_image_slideshow_stop(NULL);
491 }
492
493 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
494 {
495         gdouble n;
496
497         n = g_ascii_strtod(text, NULL);
498         if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
499                 {
500                 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
501                             SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
502                 return;
503                 }
504         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
505 }
506
507 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
508 {
509         gboolean popped;
510         gboolean hidden;
511
512         if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
513                 {
514                 layout_tools_float_set(NULL, popped, FALSE);
515                 }
516 }
517
518 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
519 {
520         gboolean popped;
521         gboolean hidden;
522
523         if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
524                 {
525                 layout_tools_float_set(NULL, popped, TRUE);
526                 }
527 }
528
529 static gboolean gr_quit_idle_cb(gpointer data)
530 {
531         exit_program();
532
533         return FALSE;
534 }
535
536 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
537 {
538         /* schedule exit when idle, if done from within a
539          * remote handler remote_close will crash
540          */
541         g_idle_add(gr_quit_idle_cb, NULL);
542 }
543
544 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
545 {
546         gchar *filename = expand_tilde(text);
547
548         if (isfile(filename))
549                 {
550                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
551                         {
552                         collection_window_new(filename);
553                         }
554                 else
555                         {
556                         layout_set_path(NULL, filename);
557                         }
558                 }
559         else if (isdir(filename))
560                 {
561                 layout_set_path(NULL, filename);
562                 }
563         else
564                 {
565                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
566                 layout_set_path(NULL, homedir());
567                 }
568
569         g_free(filename);
570 }
571
572 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
573 {
574         gr_file_load_no_raise(text, channel, data);
575
576         gr_raise(text, channel, data);
577 }
578
579 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
580 {
581         LayoutWindow *lw = NULL; /* NULL to force layout_valid() to do some magic */
582         if (!layout_valid(&lw)) return;
583         if (image_get_path(lw->image))
584                 {
585                 g_io_channel_write_chars(channel, image_get_path(lw->image), -1, NULL, NULL);
586                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
587                 }
588 }
589
590 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
591 {
592         gchar *filename = expand_tilde(text);
593
594         if (isfile(filename))
595                 {
596                 load_config_from_file(filename, FALSE);
597                 }
598         else
599                 {
600                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
601                 layout_set_path(NULL, homedir());
602                 }
603
604         g_free(filename);
605 }
606
607 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
608 {
609         gchar *filename = expand_tilde(text);
610         FileData *fd = file_data_new_group(filename);
611
612         GList *work;
613         if (fd->parent) fd = fd->parent;
614
615         g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
616         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
617
618         work = fd->sidecar_files;
619
620         while (work)
621                 {
622                 fd = work->data;
623                 work = work->next;
624                 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
625                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
626                 }
627         g_free(filename);
628 }
629
630 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
631 {
632         gchar *filename = expand_tilde(text);
633         FileData *fd = file_data_new_group(filename);
634
635         if (fd->change && fd->change->dest)
636                 {
637                 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
638                 g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
639                 }
640         g_free(filename);
641 }
642
643 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
644 {
645         gchar *filename = expand_tilde(text);
646
647         view_window_new(file_data_new_group(filename));
648         g_free(filename);
649 }
650
651 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
652 {
653         RemoteData *remote_data = data;
654
655         if (remote_data->command_collection)
656                 {
657                 collection_unref(remote_data->command_collection);
658                 remote_data->command_collection = NULL;
659                 }
660 }
661
662 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
663 {
664         RemoteData *remote_data = data;
665         gboolean new = TRUE;
666
667         if (!remote_data->command_collection)
668                 {
669                 CollectionData *cd;
670
671                 cd = collection_new("");
672
673                 g_free(cd->path);
674                 cd->path = NULL;
675                 g_free(cd->name);
676                 cd->name = g_strdup(_("Command line"));
677
678                 remote_data->command_collection = cd;
679                 }
680         else
681                 {
682                 new = (!collection_get_first(remote_data->command_collection));
683                 }
684
685         if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
686                 {
687                 layout_image_set_collection(NULL, remote_data->command_collection,
688                                             collection_get_first(remote_data->command_collection));
689                 }
690 }
691
692 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
693 {
694         LayoutWindow *lw = NULL;
695
696         if (layout_valid(&lw))
697                 {
698                 gtk_window_present(GTK_WINDOW(lw->window));
699                 }
700 }
701
702 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
703 {
704         gchar *result = NULL;
705         gchar **lua_command;
706
707         lua_command = g_strsplit(text, ",", 2);
708
709         if (lua_command[0] && lua_command[1])
710                 {
711                 FileData *fd = file_data_new_group(lua_command[0]);
712                 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
713                 if (result)
714                         {
715                         g_io_channel_write_chars(channel, result, -1, NULL, NULL);
716                         }
717                 else
718                         {
719                         g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
720                         }
721                 }
722         else
723                 {
724                 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
725                 }
726
727         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
728
729         g_strfreev(lua_command);
730         g_free(result);
731 }
732
733 typedef struct _RemoteCommandEntry RemoteCommandEntry;
734 struct _RemoteCommandEntry {
735         gchar *opt_s;
736         gchar *opt_l;
737         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
738         gboolean needs_extra;
739         gboolean prefer_command_line;
740         gchar *parameter;
741         gchar *description;
742 };
743
744 static RemoteCommandEntry remote_commands[] = {
745         /* short, long                  callback,               extra, prefer, parameter, description */
746         { "-n", "--next",               gr_image_next,          FALSE, FALSE, NULL, N_("next image") },
747         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, NULL, N_("previous image") },
748         { NULL, "--first",              gr_image_first,         FALSE, FALSE, NULL, N_("first image") },
749         { NULL, "--last",               gr_image_last,          FALSE, FALSE, NULL, N_("last image") },
750         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  NULL, N_("toggle full screen") },
751         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, NULL, N_("start full screen") },
752         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, NULL, N_("stop full screen") },
753         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  NULL, N_("toggle slide show") },
754         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, NULL, N_("start slide show") },
755         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, NULL, N_("stop slide show") },
756         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
757         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
758         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
759         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
760         { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
761         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
762         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
763         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
764         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
765         { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
766         { NULL, "--tell",               gr_file_tell,           FALSE, FALSE, NULL, N_("print filename of current image") },
767         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
768         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL, N_("clear command line collection list") },
769         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
770         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
771         { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
772         { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
773         { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, NULL, N_("    clean the metadata cache") },
774         { "-cr:", "--cache-render:",    gr_cache_render,        TRUE, FALSE, N_("<folder>  "), N_(" render thumbnails") },
775         { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
776         { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
777         { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
778         { NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
779         { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
780 };
781
782 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
783 {
784         gboolean match = FALSE;
785         gint i;
786
787         i = 0;
788         while (!match && remote_commands[i].func != NULL)
789                 {
790                 if (remote_commands[i].needs_extra)
791                         {
792                         if (remote_commands[i].opt_s &&
793                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
794                                 {
795                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
796                                 return &remote_commands[i];
797                                 }
798                         else if (remote_commands[i].opt_l &&
799                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
800                                 {
801                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
802                                 return &remote_commands[i];
803                                 }
804                         }
805                 else
806                         {
807                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
808                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
809                                 {
810                                 if (offset) *offset = text;
811                                 return &remote_commands[i];
812                                 }
813                         }
814
815                 i++;
816                 }
817
818         return NULL;
819 }
820
821 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
822 {
823         RemoteCommandEntry *entry;
824         const gchar *offset;
825
826         entry = remote_command_find(text, &offset);
827         if (entry && entry->func)
828                 {
829                 entry->func(offset, channel, data);
830                 }
831         else
832                 {
833                 log_printf("unknown remote command:%s\n", text);
834                 }
835 }
836
837 void remote_help(void)
838 {
839         gint i;
840         gchar *s_opt_param;
841         gchar *l_opt_param;
842
843         print_term(_("Remote command list:\n"));
844
845         i = 0;
846         while (remote_commands[i].func != NULL)
847                 {
848                 if (remote_commands[i].description)
849                         {
850                         s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
851                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
852                         printf_term("  %-11s%-1s %-30s%-s\n",
853                                     (remote_commands[i].opt_s) ? s_opt_param : "",
854                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
855                                     (remote_commands[i].opt_l) ? l_opt_param : "",
856                                     _(remote_commands[i].description));
857                         g_free(s_opt_param);
858                         g_free(l_opt_param);
859                         }
860                 i++;
861                 }
862         printf_term(N_("\n  All other command line parameters are used as plain files if they exists.\n"));
863 }
864
865 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
866 {
867         gint i;
868
869         i = 1;
870         while (i < argc)
871                 {
872                 RemoteCommandEntry *entry;
873
874                 entry = remote_command_find(argv[i], NULL);
875                 if (entry)
876                         {
877                         list = g_list_append(list, argv[i]);
878                         }
879                 else if (errors && !isfile(argv[i]))
880                         {
881                         *errors = g_list_append(*errors, argv[i]);
882                         }
883                 i++;
884                 }
885
886         return list;
887 }
888
889 /**
890  * \param arg_exec Binary (argv0)
891  * \param remote_list Evaluated and recognized remote commands
892  * \param path The current path
893  * \param cmd_list List of all non collections in Path
894  * \param collection_list List of all collections in argv
895  */
896 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
897                     GList *cmd_list, GList *collection_list)
898 {
899         RemoteConnection *rc;
900         gboolean started = FALSE;
901         gchar *buf;
902
903         buf = g_build_filename(get_rc_dir(), ".command", NULL);
904         rc = remote_client_open(buf);
905         if (!rc)
906                 {
907                 GString *command;
908                 GList *work;
909                 gint retry_count = 12;
910                 gboolean blank = FALSE;
911
912                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
913
914                 command = g_string_new(arg_exec);
915
916                 work = remote_list;
917                 while (work)
918                         {
919                         gchar *text;
920                         RemoteCommandEntry *entry;
921
922                         text = work->data;
923                         work = work->next;
924
925                         entry = remote_command_find(text, NULL);
926                         if (entry)
927                                 {
928                                 if (entry->prefer_command_line)
929                                         {
930                                         remote_list = g_list_remove(remote_list, text);
931                                         g_string_append(command, " ");
932                                         g_string_append(command, text);
933                                         }
934                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
935                                         {
936                                         blank = TRUE;
937                                         }
938                                 }
939                         }
940
941                 if (blank || cmd_list || path) g_string_append(command, " --blank");
942                 if (get_debug_level()) g_string_append(command, " --debug");
943
944                 g_string_append(command, " &");
945                 runcmd(command->str);
946                 g_string_free(command, TRUE);
947
948                 while (!rc && retry_count > 0)
949                         {
950                         usleep((retry_count > 10) ? 500000 : 1000000);
951                         rc = remote_client_open(buf);
952                         if (!rc) print_term(".");
953                         retry_count--;
954                         }
955
956                 print_term("\n");
957
958                 started = TRUE;
959                 }
960         g_free(buf);
961
962         if (rc)
963                 {
964                 GList *work;
965                 const gchar *prefix;
966                 gboolean use_path = TRUE;
967                 gboolean sent = FALSE;
968
969                 work = remote_list;
970                 while (work)
971                         {
972                         gchar *text;
973                         RemoteCommandEntry *entry;
974
975                         text = work->data;
976                         work = work->next;
977
978                         entry = remote_command_find(text, NULL);
979                         if (entry &&
980                             entry->opt_l &&
981                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
982
983                         remote_client_send(rc, text);
984
985                         sent = TRUE;
986                         }
987
988                 if (cmd_list && cmd_list->next)
989                         {
990                         prefix = "--list-add:";
991                         remote_client_send(rc, "--list-clear");
992                         }
993                 else
994                         {
995                         prefix = "file:";
996                         }
997
998                 work = cmd_list;
999                 while (work)
1000                         {
1001                         FileData *fd;
1002                         gchar *text;
1003
1004                         fd = work->data;
1005                         work = work->next;
1006
1007                         text = g_strconcat(prefix, fd->path, NULL);
1008                         remote_client_send(rc, text);
1009                         g_free(text);
1010
1011                         sent = TRUE;
1012                         }
1013
1014                 if (path && !cmd_list && use_path)
1015                         {
1016                         gchar *text;
1017
1018                         text = g_strdup_printf("file:%s", path);
1019                         remote_client_send(rc, text);
1020                         g_free(text);
1021
1022                         sent = TRUE;
1023                         }
1024
1025                 work = collection_list;
1026                 while (work)
1027                         {
1028                         const gchar *name;
1029                         gchar *text;
1030
1031                         name = work->data;
1032                         work = work->next;
1033
1034                         text = g_strdup_printf("file:%s", name);
1035                         remote_client_send(rc, text);
1036                         g_free(text);
1037
1038                         sent = TRUE;
1039                         }
1040
1041                 if (!started && !sent)
1042                         {
1043                         remote_client_send(rc, "raise");
1044                         }
1045                 }
1046         else
1047                 {
1048                 print_term(_("Remote not available\n"));
1049                 }
1050
1051         _exit(0);
1052 }
1053
1054 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1055 {
1056         RemoteConnection *remote_connection = remote_server_open(path);
1057         RemoteData *remote_data = g_new(RemoteData, 1);
1058
1059         remote_data->command_collection = command_collection;
1060
1061         remote_server_subscribe(remote_connection, remote_cb, remote_data);
1062         return remote_connection;
1063 }
1064 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */