Addl fix 269: Image marks "save" feature
[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 #ifdef HAVE_LUA
703 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
704 {
705         gchar *result = NULL;
706         gchar **lua_command;
707
708         lua_command = g_strsplit(text, ",", 2);
709
710         if (lua_command[0] && lua_command[1])
711                 {
712                 FileData *fd = file_data_new_group(lua_command[0]);
713                 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
714                 if (result)
715                         {
716                         g_io_channel_write_chars(channel, result, -1, NULL, NULL);
717                         }
718                 else
719                         {
720                         g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
721                         }
722                 }
723         else
724                 {
725                 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
726                 }
727
728         g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
729
730         g_strfreev(lua_command);
731         g_free(result);
732 }
733 #endif
734
735 typedef struct _RemoteCommandEntry RemoteCommandEntry;
736 struct _RemoteCommandEntry {
737         gchar *opt_s;
738         gchar *opt_l;
739         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
740         gboolean needs_extra;
741         gboolean prefer_command_line;
742         gchar *parameter;
743         gchar *description;
744 };
745
746 static RemoteCommandEntry remote_commands[] = {
747         /* short, long                  callback,               extra, prefer, parameter, description */
748         { "-n", "--next",               gr_image_next,          FALSE, FALSE, NULL, N_("next image") },
749         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, NULL, N_("previous image") },
750         { NULL, "--first",              gr_image_first,         FALSE, FALSE, NULL, N_("first image") },
751         { NULL, "--last",               gr_image_last,          FALSE, FALSE, NULL, N_("last image") },
752         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  NULL, N_("toggle full screen") },
753         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, NULL, N_("start full screen") },
754         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, NULL, N_("stop full screen") },
755         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  NULL, N_("toggle slide show") },
756         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, NULL, N_("start slide show") },
757         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, NULL, N_("stop slide show") },
758         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
759         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[N][.M]>"), N_("set slide show delay to N.M seconds") },
760         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
761         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
762         { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
763         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
764         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
765         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
766         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
767         { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
768         { NULL, "--tell",               gr_file_tell,           FALSE, FALSE, NULL, N_("print filename of current image") },
769         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
770         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL, N_("clear command line collection list") },
771         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
772         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
773         { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
774         { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
775         { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, NULL, N_("    clean the metadata cache") },
776         { "-cr:", "--cache-render:",    gr_cache_render,        TRUE, FALSE, N_("<folder>  "), N_(" render thumbnails") },
777         { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
778         { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
779         { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
780 #ifdef HAVE_LUA
781         { NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
782 #endif
783         { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
784 };
785
786 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
787 {
788         gboolean match = FALSE;
789         gint i;
790
791         i = 0;
792         while (!match && remote_commands[i].func != NULL)
793                 {
794                 if (remote_commands[i].needs_extra)
795                         {
796                         if (remote_commands[i].opt_s &&
797                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
798                                 {
799                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
800                                 return &remote_commands[i];
801                                 }
802                         else if (remote_commands[i].opt_l &&
803                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
804                                 {
805                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
806                                 return &remote_commands[i];
807                                 }
808                         }
809                 else
810                         {
811                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
812                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
813                                 {
814                                 if (offset) *offset = text;
815                                 return &remote_commands[i];
816                                 }
817                         }
818
819                 i++;
820                 }
821
822         return NULL;
823 }
824
825 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
826 {
827         RemoteCommandEntry *entry;
828         const gchar *offset;
829
830         entry = remote_command_find(text, &offset);
831         if (entry && entry->func)
832                 {
833                 entry->func(offset, channel, data);
834                 }
835         else
836                 {
837                 log_printf("unknown remote command:%s\n", text);
838                 }
839 }
840
841 void remote_help(void)
842 {
843         gint i;
844         gchar *s_opt_param;
845         gchar *l_opt_param;
846
847         print_term(_("Remote command list:\n"));
848
849         i = 0;
850         while (remote_commands[i].func != NULL)
851                 {
852                 if (remote_commands[i].description)
853                         {
854                         s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
855                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
856                         printf_term("  %-11s%-1s %-30s%-s\n",
857                                     (remote_commands[i].opt_s) ? s_opt_param : "",
858                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
859                                     (remote_commands[i].opt_l) ? l_opt_param : "",
860                                     _(remote_commands[i].description));
861                         g_free(s_opt_param);
862                         g_free(l_opt_param);
863                         }
864                 i++;
865                 }
866         printf_term(N_("\n  All other command line parameters are used as plain files if they exists.\n"));
867 }
868
869 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
870 {
871         gint i;
872
873         i = 1;
874         while (i < argc)
875                 {
876                 RemoteCommandEntry *entry;
877
878                 entry = remote_command_find(argv[i], NULL);
879                 if (entry)
880                         {
881                         list = g_list_append(list, argv[i]);
882                         }
883                 else if (errors && !isfile(argv[i]))
884                         {
885                         *errors = g_list_append(*errors, argv[i]);
886                         }
887                 i++;
888                 }
889
890         return list;
891 }
892
893 /**
894  * \param arg_exec Binary (argv0)
895  * \param remote_list Evaluated and recognized remote commands
896  * \param path The current path
897  * \param cmd_list List of all non collections in Path
898  * \param collection_list List of all collections in argv
899  */
900 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
901                     GList *cmd_list, GList *collection_list)
902 {
903         RemoteConnection *rc;
904         gboolean started = FALSE;
905         gchar *buf;
906
907         buf = g_build_filename(get_rc_dir(), ".command", NULL);
908         rc = remote_client_open(buf);
909         if (!rc)
910                 {
911                 GString *command;
912                 GList *work;
913                 gint retry_count = 12;
914                 gboolean blank = FALSE;
915
916                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
917
918                 command = g_string_new(arg_exec);
919
920                 work = remote_list;
921                 while (work)
922                         {
923                         gchar *text;
924                         RemoteCommandEntry *entry;
925
926                         text = work->data;
927                         work = work->next;
928
929                         entry = remote_command_find(text, NULL);
930                         if (entry)
931                                 {
932                                 if (entry->prefer_command_line)
933                                         {
934                                         remote_list = g_list_remove(remote_list, text);
935                                         g_string_append(command, " ");
936                                         g_string_append(command, text);
937                                         }
938                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
939                                         {
940                                         blank = TRUE;
941                                         }
942                                 }
943                         }
944
945                 if (blank || cmd_list || path) g_string_append(command, " --blank");
946                 if (get_debug_level()) g_string_append(command, " --debug");
947
948                 g_string_append(command, " &");
949                 runcmd(command->str);
950                 g_string_free(command, TRUE);
951
952                 while (!rc && retry_count > 0)
953                         {
954                         usleep((retry_count > 10) ? 500000 : 1000000);
955                         rc = remote_client_open(buf);
956                         if (!rc) print_term(".");
957                         retry_count--;
958                         }
959
960                 print_term("\n");
961
962                 started = TRUE;
963                 }
964         g_free(buf);
965
966         if (rc)
967                 {
968                 GList *work;
969                 const gchar *prefix;
970                 gboolean use_path = TRUE;
971                 gboolean sent = FALSE;
972
973                 work = remote_list;
974                 while (work)
975                         {
976                         gchar *text;
977                         RemoteCommandEntry *entry;
978
979                         text = work->data;
980                         work = work->next;
981
982                         entry = remote_command_find(text, NULL);
983                         if (entry &&
984                             entry->opt_l &&
985                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
986
987                         remote_client_send(rc, text);
988
989                         sent = TRUE;
990                         }
991
992                 if (cmd_list && cmd_list->next)
993                         {
994                         prefix = "--list-add:";
995                         remote_client_send(rc, "--list-clear");
996                         }
997                 else
998                         {
999                         prefix = "file:";
1000                         }
1001
1002                 work = cmd_list;
1003                 while (work)
1004                         {
1005                         FileData *fd;
1006                         gchar *text;
1007
1008                         fd = work->data;
1009                         work = work->next;
1010
1011                         text = g_strconcat(prefix, fd->path, NULL);
1012                         remote_client_send(rc, text);
1013                         g_free(text);
1014
1015                         sent = TRUE;
1016                         }
1017
1018                 if (path && !cmd_list && use_path)
1019                         {
1020                         gchar *text;
1021
1022                         text = g_strdup_printf("file:%s", path);
1023                         remote_client_send(rc, text);
1024                         g_free(text);
1025
1026                         sent = TRUE;
1027                         }
1028
1029                 work = collection_list;
1030                 while (work)
1031                         {
1032                         const gchar *name;
1033                         gchar *text;
1034
1035                         name = work->data;
1036                         work = work->next;
1037
1038                         text = g_strdup_printf("file:%s", name);
1039                         remote_client_send(rc, text);
1040                         g_free(text);
1041
1042                         sent = TRUE;
1043                         }
1044
1045                 if (!started && !sent)
1046                         {
1047                         remote_client_send(rc, "raise");
1048                         }
1049                 }
1050         else
1051                 {
1052                 print_term(_("Remote not available\n"));
1053                 }
1054
1055         _exit(0);
1056 }
1057
1058 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1059 {
1060         RemoteConnection *remote_connection = remote_server_open(path);
1061         RemoteData *remote_data = g_new(RemoteData, 1);
1062
1063         remote_data->command_collection = command_collection;
1064
1065         remote_server_subscribe(remote_connection, remote_cb, remote_data);
1066         return remote_connection;
1067 }
1068 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */