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