Amend remote --tell option
[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 "collect-io.h"
28 #include "exif.h"
29 #include "filedata.h"
30 #include "filefilter.h"
31 #include "image.h"
32 #include "img-view.h"
33 #include "layout.h"
34 #include "layout_image.h"
35 #include "layout_util.h"
36 #include "misc.h"
37 #include "pixbuf-renderer.h"
38 #include "slideshow.h"
39 #include "ui_fileops.h"
40 #include "rcfile.h"
41
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <signal.h>
45 #include <errno.h>
46
47 #include "glua.h"
48
49 #define SERVER_MAX_CLIENTS 8
50
51 #define REMOTE_SERVER_BACKLOG 4
52
53
54 #ifndef UNIX_PATH_MAX
55 #define UNIX_PATH_MAX 108
56 #endif
57
58
59 static RemoteConnection *remote_client_open(const gchar *path);
60 static gint remote_client_send(RemoteConnection *rc, const gchar *text);
61 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data);
62
63 static LayoutWindow *lw_id = NULL; /* points to the window set by the --id option */
64
65 typedef struct _RemoteClient RemoteClient;
66 struct _RemoteClient {
67         gint fd;
68         guint channel_id; /* event source id */
69         RemoteConnection *rc;
70 };
71
72 typedef struct _RemoteData RemoteData;
73 struct _RemoteData {
74         CollectionData *command_collection;
75 };
76
77 /* To enable file names containing newlines to be processed correctly,
78  * the --print0 remote option sets returned data to be terminated with a null
79  * character rather a newline
80  */
81 static gboolean print0 = FALSE;
82
83 /* Remote commands from main.c are prepended with the current dir the remote
84  * command was made from. Some remote commands require this. The
85  * value is stored here
86  */
87 static gchar *pwd = NULL;
88
89 /**
90  * @brief Ensures file path is absolute.
91  * @param[in] filename Filepath, absolute or relative to calling directory
92  * @returns absolute path
93  * 
94  * If first character of input filepath is not the directory
95  * separator, assume it as a relative path and prepend
96  * the directory the remote command was initiated from
97  * 
98  * Return value must be freed with g_free()
99  */
100 static gchar *set_pwd(gchar *filename)
101 {
102         gchar *temp;
103
104         if (strncmp(filename, G_DIR_SEPARATOR_S, 1) != 0)
105                 {
106                 temp = g_build_filename(pwd, filename, NULL);
107                 }
108         else
109                 {
110                 temp = g_strdup(filename);
111                 }
112
113         return temp;
114 }
115
116 static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition condition, gpointer data)
117 {
118         RemoteClient *client = data;
119         RemoteConnection *rc;
120         GIOStatus status = G_IO_STATUS_NORMAL;
121
122         lw_id = NULL;
123         rc = client->rc;
124
125         if (condition & G_IO_IN)
126                 {
127                 gchar *buffer = NULL;
128                 GError *error = NULL;
129                 gsize termpos;
130                 /* FIXME: it should be possible to terminate the command with a null character */
131                 g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
132                 while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
133                         {
134                         if (buffer)
135                                 {
136                                 buffer[termpos] = '\0';
137
138                                 if (strlen(buffer) > 0)
139                                         {
140                                         if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
141                                         g_io_channel_write_chars(source, "<gq_end_of_command>", -1, NULL, NULL); /* empty line finishes the command */
142                                         g_io_channel_flush(source, NULL);
143                                         }
144                                 g_free(buffer);
145
146                                 buffer = NULL;
147                                 }
148                         }
149
150                 if (error)
151                         {
152                         log_printf("error reading socket: %s\n", error->message);
153                         g_error_free(error);
154                         }
155                 }
156
157         if (condition & G_IO_HUP || status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
158                 {
159                 rc->clients = g_list_remove(rc->clients, client);
160
161                 DEBUG_1("HUP detected, closing client.");
162                 DEBUG_1("client count %d", g_list_length(rc->clients));
163
164                 g_source_remove(client->channel_id);
165                 close(client->fd);
166                 g_free(client);
167                 }
168
169         return TRUE;
170 }
171
172 static void remote_server_client_add(RemoteConnection *rc, gint fd)
173 {
174         RemoteClient *client;
175         GIOChannel *channel;
176
177         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
178                 {
179                 log_printf("maximum remote clients of %d exceeded, closing connection\n", SERVER_MAX_CLIENTS);
180                 close(fd);
181                 return;
182                 }
183
184         client = g_new0(RemoteClient, 1);
185         client->rc = rc;
186         client->fd = fd;
187
188         channel = g_io_channel_unix_new(fd);
189         client->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP,
190                                                  remote_server_client_cb, client, NULL);
191         g_io_channel_unref(channel);
192
193         rc->clients = g_list_append(rc->clients, client);
194         DEBUG_1("client count %d", g_list_length(rc->clients));
195 }
196
197 static void remote_server_clients_close(RemoteConnection *rc)
198 {
199         while (rc->clients)
200                 {
201                 RemoteClient *client = rc->clients->data;
202
203                 rc->clients = g_list_remove(rc->clients, client);
204
205                 g_source_remove(client->channel_id);
206                 close(client->fd);
207                 g_free(client);
208                 }
209 }
210
211 static gboolean remote_server_read_cb(GIOChannel *source, GIOCondition condition, gpointer data)
212 {
213         RemoteConnection *rc = data;
214         gint fd;
215         guint alen;
216
217         fd = accept(rc->fd, NULL, &alen);
218         if (fd == -1)
219                 {
220                 log_printf("error accepting socket: %s\n", strerror(errno));
221                 return TRUE;
222                 }
223
224         remote_server_client_add(rc, fd);
225
226         return TRUE;
227 }
228
229 gboolean remote_server_exists(const gchar *path)
230 {
231         RemoteConnection *rc;
232
233         /* verify server up */
234         rc = remote_client_open(path);
235         remote_close(rc);
236
237         if (rc) return TRUE;
238
239         /* unable to connect, remove socket file to free up address */
240         unlink(path);
241         return FALSE;
242 }
243
244 static RemoteConnection *remote_server_open(const gchar *path)
245 {
246         RemoteConnection *rc;
247         struct sockaddr_un addr;
248         gint sun_path_len;
249         gint fd;
250         GIOChannel *channel;
251
252         if (remote_server_exists(path))
253                 {
254                 log_printf("Address already in use: %s\n", path);
255                 return NULL;
256                 }
257
258         fd = socket(PF_UNIX, SOCK_STREAM, 0);
259         if (fd == -1) return NULL;
260
261         addr.sun_family = AF_UNIX;
262         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
263         strncpy(addr.sun_path, path, sun_path_len);
264         if (bind(fd, &addr, sizeof(addr)) == -1 ||
265             listen(fd, REMOTE_SERVER_BACKLOG) == -1)
266                 {
267                 log_printf("error subscribing to socket: %s\n", strerror(errno));
268                 close(fd);
269                 return NULL;
270                 }
271
272         rc = g_new0(RemoteConnection, 1);
273
274         rc->server = TRUE;
275         rc->fd = fd;
276         rc->path = g_strdup(path);
277
278         channel = g_io_channel_unix_new(rc->fd);
279         g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
280
281         rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
282                                              remote_server_read_cb, rc, NULL);
283         g_io_channel_unref(channel);
284
285         return rc;
286 }
287
288 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
289 {
290         if (!rc || !rc->server) return;
291
292         rc->read_func = func;
293         rc->read_data = data;
294 }
295
296
297 static RemoteConnection *remote_client_open(const gchar *path)
298 {
299         RemoteConnection *rc;
300         struct stat st;
301         struct sockaddr_un addr;
302         gint sun_path_len;
303         gint fd;
304
305         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
306
307         fd = socket(PF_UNIX, SOCK_STREAM, 0);
308         if (fd == -1) return NULL;
309
310         addr.sun_family = AF_UNIX;
311         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
312         strncpy(addr.sun_path, path, sun_path_len);
313         if (connect(fd, &addr, sizeof(addr)) == -1)
314                 {
315                 DEBUG_1("error connecting to socket: %s", strerror(errno));
316                 close(fd);
317                 return NULL;
318                 }
319
320         rc = g_new0(RemoteConnection, 1);
321         rc->server = FALSE;
322         rc->fd = fd;
323         rc->path = g_strdup(path);
324
325         return rc;
326 }
327
328 static sig_atomic_t sigpipe_occurred = FALSE;
329
330 static void sighandler_sigpipe(gint sig)
331 {
332         sigpipe_occurred = TRUE;
333 }
334
335 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
336 {
337         struct sigaction new_action, old_action;
338         gboolean ret = FALSE;
339         GError *error = NULL;
340         GIOChannel *channel;
341
342         if (!rc || rc->server) return FALSE;
343         if (!text) return TRUE;
344
345         sigpipe_occurred = FALSE;
346
347         new_action.sa_handler = sighandler_sigpipe;
348         sigemptyset(&new_action.sa_mask);
349         new_action.sa_flags = 0;
350
351         /* setup our signal handler */
352         sigaction(SIGPIPE, &new_action, &old_action);
353
354         channel = g_io_channel_unix_new(rc->fd);
355
356         g_io_channel_write_chars(channel, text, -1, NULL, &error);
357         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, &error);
358         g_io_channel_flush(channel, &error);
359
360         if (error)
361                 {
362                 log_printf("error reading socket: %s\n", error->message);
363                 g_error_free(error);
364                 ret = FALSE;;
365                 }
366         else
367                 {
368                 ret = TRUE;
369                 }
370
371         if (ret)
372                 {
373                 gchar *buffer = NULL;
374                 gsize termpos;
375                 g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
376                 while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
377                         {
378                         if (buffer)
379                                 {
380                                 if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
381                                         {
382                                         g_free(buffer);
383                                         fflush(stdout);
384                                         break;
385                                         }
386                                 buffer[termpos] = '\0';
387                                 if (g_strstr_len(buffer, -1, "print0") != 0)
388                                         {
389                                         print0 = TRUE;
390                                         }
391                                 else
392                                         {
393                                         if (print0)
394                                                 {
395                                                 printf("%s%c", buffer, 0);
396                                                 }
397                                         else
398                                                 {
399                                                 printf("%s\n", buffer);
400                                                 }
401                                         }
402                                 g_free(buffer);
403                                 buffer = NULL;
404                                 }
405                         }
406
407                 if (error)
408                         {
409                         log_printf("error reading socket: %s\n", error->message);
410                         g_error_free(error);
411                         ret = FALSE;
412                         }
413                 }
414
415
416         /* restore the original signal handler */
417         sigaction(SIGPIPE, &old_action, NULL);
418         g_io_channel_unref(channel);
419         return ret;
420 }
421
422 void remote_close(RemoteConnection *rc)
423 {
424         if (!rc) return;
425
426         if (rc->server)
427                 {
428                 remote_server_clients_close(rc);
429
430                 g_source_remove(rc->channel_id);
431                 unlink(rc->path);
432                 }
433
434         if (rc->read_data)
435                 g_free(rc->read_data);
436
437         close(rc->fd);
438
439         g_free(rc->path);
440         g_free(rc);
441 }
442
443 /*
444  *-----------------------------------------------------------------------------
445  * remote functions
446  *-----------------------------------------------------------------------------
447  */
448
449 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
450 {
451         layout_image_next(lw_id);
452 }
453
454 static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
455 {
456         LayoutWindow *lw = NULL;
457
458         if (!layout_valid(&lw)) return;
459
460         lw_id = layout_menu_new_window(NULL, lw);
461 }
462
463 static gboolean gr_close_window_cb()
464 {
465         if (!layout_valid(&lw_id)) return FALSE;
466
467         layout_menu_close_cb(NULL, lw_id);
468
469         return FALSE;
470 }
471
472 static void gr_close_window(const gchar *text, GIOChannel *channel, gpointer data)
473 {
474         g_idle_add(gr_close_window_cb, NULL);
475 }
476
477 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
478 {
479         layout_image_prev(lw_id);
480 }
481
482 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
483 {
484         layout_image_first(lw_id);
485 }
486
487 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
488 {
489         layout_image_last(lw_id);
490 }
491
492 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
493 {
494         layout_image_full_screen_toggle(lw_id);
495 }
496
497 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
498 {
499         layout_image_full_screen_start(lw_id);
500 }
501
502 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
503 {
504         layout_image_full_screen_stop(lw_id);
505 }
506
507 static void gr_lw_id(const gchar *text, GIOChannel *channel, gpointer data)
508 {
509         lw_id = layout_find_by_layout_id(text);
510         if (!lw_id)
511                 {
512                 log_printf("remote sent window ID that does not exist:\"%s\"\n",text);
513                 }
514         layout_valid(&lw_id);
515 }
516
517 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
518 {
519         GList *list;
520         FileData *dir_fd = file_data_new_dir(text);
521
522         layout_valid(&lw_id);
523         list = filelist_recursive_full(dir_fd, lw_id->sort_method, lw_id->sort_ascend);
524         file_data_unref(dir_fd);
525         if (!list) return;
526 //printf("length: %d\n", g_list_length(list));
527         layout_image_slideshow_stop(lw_id);
528         layout_image_slideshow_start_from_list(lw_id, list);
529 }
530
531 static void gr_cache_thumb(const gchar *text, GIOChannel *channel, gpointer data)
532 {
533         if (!g_strcmp0(text, "clear"))
534                 cache_maintain_home_remote(FALSE, TRUE);
535         else if (!g_strcmp0(text, "clean"))
536                 cache_maintain_home_remote(FALSE, FALSE);
537 }
538
539 static void gr_cache_shared(const gchar *text, GIOChannel *channel, gpointer data)
540 {
541         if (!g_strcmp0(text, "clear"))
542                 cache_manager_standard_process_remote(TRUE);
543         else if (!g_strcmp0(text, "clean"))
544                 cache_manager_standard_process_remote(FALSE);
545 }
546
547 static void gr_cache_metadata(const gchar *text, GIOChannel *channel, gpointer data)
548 {
549         cache_maintain_home_remote(TRUE, FALSE);
550 }
551
552 static void gr_cache_render(const gchar *text, GIOChannel *channel, gpointer data)
553 {
554         cache_manager_render_remote(text, FALSE, FALSE);
555 }
556
557 static void gr_cache_render_recurse(const gchar *text, GIOChannel *channel, gpointer data)
558 {
559         cache_manager_render_remote(text, TRUE, FALSE);
560 }
561
562 static void gr_cache_render_standard(const gchar *text, GIOChannel *channel, gpointer data)
563 {
564         if(options->thumbnails.spec_standard)
565                 cache_manager_render_remote(text, FALSE, TRUE);
566 }
567
568 static void gr_cache_render_standard_recurse(const gchar *text, GIOChannel *channel, gpointer data)
569 {
570         if(options->thumbnails.spec_standard)
571                 cache_manager_render_remote(text, TRUE, TRUE);
572 }
573
574 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
575 {
576         layout_image_slideshow_toggle(lw_id);
577 }
578
579 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
580 {
581         layout_image_slideshow_start(lw_id);
582 }
583
584 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
585 {
586         layout_image_slideshow_stop(lw_id);
587 }
588
589 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
590 {
591         gdouble t1, t2, t3, n;
592         gint res;
593
594         res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
595         if (res == 3)
596                 {
597                 n = (t1 * 3600) + (t2 * 60) + t3;
598                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
599                                 t1 >= 24 || t2 >= 60 || t3 >= 60)
600                         {
601                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
602                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
603                         return;
604                         }
605                 }
606         else if (res == 2)
607                 {
608                 n = t1 * 60 + t2;
609                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS ||
610                                 t1 >= 60 || t2 >= 60)
611                         {
612                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
613                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
614                         return;
615                         }
616                 }
617         else if (res == 1)
618                 {
619                 n = t1;
620                 if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
621                         {
622                         printf_term(TRUE, "Remote slideshow delay out of range (%.1f to %.1f)\n",
623                                                                 SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
624                         return;
625                         }
626                 }
627         else
628                 {
629                 n = 0;
630                 }
631
632         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
633 }
634
635 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
636 {
637         gboolean popped;
638         gboolean hidden;
639
640         if (layout_tools_float_get(lw_id, &popped, &hidden) && hidden)
641                 {
642                 layout_tools_float_set(lw_id, popped, FALSE);
643                 }
644 }
645
646 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
647 {
648         gboolean popped;
649         gboolean hidden;
650
651         if (layout_tools_float_get(lw_id, &popped, &hidden) && !hidden)
652                 {
653                 layout_tools_float_set(lw_id, popped, TRUE);
654                 }
655 }
656
657 static gboolean gr_quit_idle_cb(gpointer data)
658 {
659         exit_program();
660
661         return FALSE;
662 }
663
664 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
665 {
666         /* schedule exit when idle, if done from within a
667          * remote handler remote_close will crash
668          */
669         g_idle_add(gr_quit_idle_cb, NULL);
670 }
671
672 static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpointer data)
673 {
674         gchar *filename;
675         gchar *tilde_filename;
676
677         if (!download_web_file(text, TRUE, NULL))
678                 {
679                 tilde_filename = expand_tilde(text);
680                 filename = set_pwd(tilde_filename);
681
682                 if (isfile(filename))
683                         {
684                         if (file_extension_match(filename, GQ_COLLECTION_EXT))
685                                 {
686                                 collection_window_new(filename);
687                                 }
688                         else
689                                 {
690                                 layout_set_path(lw_id, filename);
691                                 }
692                         }
693                 else if (isdir(filename))
694                         {
695                         layout_set_path(lw_id, filename);
696                         }
697                 else
698                         {
699                         log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
700                         layout_set_path(lw_id, homedir());
701                         }
702
703                 g_free(filename);
704                 g_free(tilde_filename);
705                 }
706 }
707
708 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
709 {
710         gr_file_load_no_raise(text, channel, data);
711
712         gr_raise(text, channel, data);
713 }
714
715 static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
716 {
717         gchar *pixel_info;
718         gint x_pixel, y_pixel;
719         gint width, height;
720         gint r_mouse, g_mouse, b_mouse;
721         PixbufRenderer *pr;
722         LayoutWindow *lw = NULL;
723
724         if (!layout_valid(&lw_id)) return;
725
726         pr = (PixbufRenderer*)lw_id->image->pr;
727
728         if (pr)
729                 {
730                 pixbuf_renderer_get_image_size(pr, &width, &height);
731                 if (width < 1 || height < 1) return;
732
733                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
734
735                 if (x_pixel >= 0 && y_pixel >= 0)
736                         {
737                         pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
738                                                          &r_mouse, &g_mouse, &b_mouse);
739
740                         pixel_info = g_strdup_printf(_("[%d,%d]: RGB(%3d,%3d,%3d)"),
741                                                  x_pixel, y_pixel,
742                                                  r_mouse, g_mouse, b_mouse);
743
744                         g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
745                         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
746
747                         g_free(pixel_info);
748                         }
749                 else
750                         {
751                         return;
752                         }
753                 }
754         else
755                 {
756                 return;
757                 }
758 }
759
760 static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
761 {
762         gchar *rectangle_info;
763         PixbufRenderer *pr;
764         LayoutWindow *lw = NULL;
765         gint x1, y1, x2, y2;
766
767         if (!options->draw_rectangle) return;
768         if (!layout_valid(&lw_id)) return;
769
770         pr = (PixbufRenderer*)lw_id->image->pr;
771
772         if (pr)
773                 {
774                 image_get_rectangle(&x1, &y1, &x2, &y2);
775                 rectangle_info = g_strdup_printf(_("%dx%d+%d+%d"),
776                                         (x2 > x1) ? x2 - x1 : x1 - x2,
777                                         (y2 > y1) ? y2 - y1 : y1 - y2,
778                                         (x2 > x1) ? x1 : x2,
779                                         (y2 > y1) ? y1 : y2);
780
781                 g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
782                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
783
784                 g_free(rectangle_info);
785                 }
786 }
787
788 static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer data)
789 {
790         gchar *render_intent;
791
792         switch (options->color_profile.render_intent)
793                 {
794                 case 0:
795                         render_intent = g_strdup("Perceptual");
796                         break;
797                 case 1:
798                         render_intent = g_strdup("Relative Colorimetric");
799                         break;
800                 case 2:
801                         render_intent = g_strdup("Saturation");
802                         break;
803                 case 3:
804                         render_intent = g_strdup("Absolute Colorimetric");
805                         break;
806                 default:
807                         render_intent = g_strdup("none");
808                         break;
809                 }
810
811         g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
812         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
813
814         g_free(render_intent);
815 }
816
817 static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurse)
818 {
819         GList *list = NULL;
820         FileFormatClass class;
821         FileData *dir_fd;
822         FileData *fd;
823         GString *out_string = g_string_new(NULL);
824         GList *work;
825
826         if (strcmp(text, "") == 0)
827                 {
828                 if (layout_valid(&lw_id))
829                         {
830                         dir_fd = file_data_new_dir(lw_id->dir_fd->path);
831                         }
832                 else
833                         {
834                         return;
835                         }
836                 }
837         else
838                 {
839                 if (isdir(text))
840                         {
841                         dir_fd = file_data_new_dir(text);
842                         }
843                 else
844                         {
845                         return;
846                         }
847                 }
848
849         if (recurse)
850                 {
851                 list = filelist_recursive(dir_fd);
852                 }
853         else
854                 {
855                 filelist_read(dir_fd, &list, NULL);
856                 }
857
858         work = list;
859         while (work)
860                 {
861                 fd = work->data;
862                 g_string_append_printf(out_string, "%s", fd->path);
863                 class = filter_file_get_class(fd->path);
864
865                 switch (class)
866                         {
867                         case FORMAT_CLASS_IMAGE:
868                                 out_string = g_string_append(out_string, "    Class: Image");
869                                 break;
870                         case FORMAT_CLASS_RAWIMAGE:
871                                 out_string = g_string_append(out_string, "    Class: RAW image");
872                                 break;
873                         case FORMAT_CLASS_META:
874                                 out_string = g_string_append(out_string, "    Class: Metadata");
875                                 break;
876                         case FORMAT_CLASS_VIDEO:
877                                 out_string = g_string_append(out_string, "    Class: Video");
878                                 break;
879                         case FORMAT_CLASS_COLLECTION:
880                                 out_string = g_string_append(out_string, "    Class: Collection");
881                                 break;
882                         case FORMAT_CLASS_DOCUMENT:
883                                 out_string = g_string_append(out_string, "    Class: Document");
884                                 break;
885                         case FORMAT_CLASS_UNKNOWN:
886                                 out_string = g_string_append(out_string, "    Class: Unknown");
887                                 break;
888                         default:
889                                 out_string = g_string_append(out_string, "    Class: Unknown");
890                                 break;
891                         }
892                 out_string = g_string_append(out_string, "\n");
893                 work = work->next;
894                 }
895
896         g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
897         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
898
899         g_string_free(out_string, TRUE);
900         filelist_free(list);
901         file_data_unref(dir_fd);
902 }
903
904 static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
905 {
906         GString *contents = g_string_new(NULL);
907
908         if (is_collection(text))
909                 {
910                 collection_contents(text, &contents);
911                 }
912         else
913                 {
914                 return;
915                 }
916
917         g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
918         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
919
920         g_string_free(contents, TRUE);
921 }
922
923 static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer data)
924 {
925
926         GList *collection_list = NULL;
927         GList *work;
928         GString *out_string = g_string_new(NULL);
929
930         collect_manager_list(&collection_list, NULL, NULL);
931
932         work = collection_list;
933         while (work)
934                 {
935                 const gchar *collection_name = work->data;
936                 out_string = g_string_append(out_string, g_strdup(collection_name));
937                 out_string = g_string_append(out_string, "\n");
938
939                 work = work->next;
940                 }
941
942         g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
943         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
944
945         string_list_free(collection_list);
946         g_string_free(out_string, TRUE);
947 }
948
949
950 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
951 {
952         get_filelist(text, channel, FALSE);
953 }
954
955 static void gr_filelist_recurse(const gchar *text, GIOChannel *channel, gpointer data)
956 {
957         get_filelist(text, channel, TRUE);
958 }
959
960 static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
961 {
962         gchar *out_string;
963         gchar *collection_name = NULL;
964
965         if (!layout_valid(&lw_id)) return;
966
967         if (image_get_path(lw_id->image))
968                 {
969                 if (lw_id->image->collection && lw_id->image->collection->name)
970                         {
971                         collection_name = remove_extension_from_path(lw_id->image->collection->name);
972                         out_string = g_strconcat(image_get_path(lw_id->image), "    Collection: ", collection_name, NULL);
973                         }
974                 else
975                         {
976                         out_string = g_strconcat(image_get_path(lw_id->image), NULL);
977                         }
978                 }
979         else
980                 {
981                 out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
982                 }
983
984         g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
985         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
986
987         g_free(collection_name);
988         g_free(out_string);
989 }
990
991 static void gr_file_info(const gchar *text, GIOChannel *channel, gpointer data)
992 {
993         gchar *filename;
994         FileData *fd;
995         gchar *country_name;
996         gchar *country_code;
997         gchar *timezone;
998         gchar *local_time;
999         GString *out_string;
1000         FileFormatClass format_class;
1001
1002         if (!layout_valid(&lw_id)) return;
1003
1004         if (image_get_path(lw_id->image))
1005                 {
1006                 filename = g_strdup(image_get_path(lw_id->image));
1007                 fd = file_data_new_group(filename);
1008                 out_string = g_string_new(NULL);
1009
1010                 format_class = filter_file_get_class(image_get_path(lw_id->image));
1011                 if (format_class)
1012                         {
1013                         g_string_append_printf(out_string, _("Class: %s\n"), format_class_list[format_class]);
1014                         }
1015
1016                 if (fd->page_total > 1)
1017                         {
1018                         g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
1019                         }
1020
1021                 if (fd->exif)
1022                         {
1023                         country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
1024                         if (country_name)
1025                                 {
1026                                 g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
1027                                 g_free(country_name);
1028                                 }
1029
1030                         country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
1031                         if (country_name)
1032                                 {
1033                                 g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
1034                                 g_free(country_code);
1035                                 }
1036
1037                         timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
1038                         if (timezone)
1039                                 {
1040                                 g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
1041                                 g_free(timezone);
1042                                 }
1043
1044                         local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
1045                         if (local_time)
1046                                 {
1047                                 g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
1048                                 g_free(local_time);
1049                                 }
1050                         }
1051
1052                 g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
1053                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1054
1055                 g_string_free(out_string, TRUE);
1056                 file_data_unref(fd);
1057                 g_free(filename);
1058                 }
1059 }
1060
1061 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
1062 {
1063         gchar *filename = expand_tilde(text);
1064
1065         if (isfile(filename))
1066                 {
1067                 load_config_from_file(filename, FALSE);
1068                 }
1069         else
1070                 {
1071                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
1072                 layout_set_path(NULL, homedir());
1073                 }
1074
1075         g_free(filename);
1076 }
1077
1078 static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer data)
1079 {
1080         gchar *filename = expand_tilde(text);
1081         FileData *fd = file_data_new_group(filename);
1082
1083         GList *work;
1084         if (fd->parent) fd = fd->parent;
1085
1086         g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1087         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1088
1089         work = fd->sidecar_files;
1090
1091         while (work)
1092                 {
1093                 fd = work->data;
1094                 work = work->next;
1095                 g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
1096                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1097                 }
1098         g_free(filename);
1099 }
1100
1101 static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer data)
1102 {
1103         gchar *filename = expand_tilde(text);
1104         FileData *fd = file_data_new_group(filename);
1105
1106         if (fd->change && fd->change->dest)
1107                 {
1108                 g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
1109                 g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1110                 }
1111         g_free(filename);
1112 }
1113
1114 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
1115 {
1116         gchar *filename;
1117         gchar *tilde_filename = expand_tilde(text);
1118
1119         filename = set_pwd(tilde_filename);
1120
1121         view_window_new(file_data_new_group(filename));
1122         g_free(filename);
1123         g_free(tilde_filename);
1124 }
1125
1126 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
1127 {
1128         RemoteData *remote_data = data;
1129
1130         if (remote_data->command_collection)
1131                 {
1132                 collection_unref(remote_data->command_collection);
1133                 remote_data->command_collection = NULL;
1134                 }
1135 }
1136
1137 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
1138 {
1139         RemoteData *remote_data = data;
1140         gboolean new = TRUE;
1141
1142         if (!remote_data->command_collection)
1143                 {
1144                 CollectionData *cd;
1145
1146                 cd = collection_new("");
1147
1148                 g_free(cd->path);
1149                 cd->path = NULL;
1150                 g_free(cd->name);
1151                 cd->name = g_strdup(_("Command line"));
1152
1153                 remote_data->command_collection = cd;
1154                 }
1155         else
1156                 {
1157                 new = (!collection_get_first(remote_data->command_collection));
1158                 }
1159
1160         if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
1161                 {
1162                 layout_image_set_collection(NULL, remote_data->command_collection,
1163                                             collection_get_first(remote_data->command_collection));
1164                 }
1165 }
1166
1167 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
1168 {
1169         LayoutWindow *lw = NULL;
1170
1171         if (layout_valid(&lw_id))
1172                 {
1173                 gtk_window_present(GTK_WINDOW(lw_id->window));
1174                 }
1175 }
1176
1177 static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
1178 {
1179         LayoutWindow *lw = NULL;
1180
1181         g_free(pwd);
1182         pwd = g_strdup(text);
1183 }
1184
1185 static void gr_print0(const gchar *text, GIOChannel *channel, gpointer data)
1186 {
1187         LayoutWindow *lw = NULL;
1188
1189         g_io_channel_write_chars(channel, "print0", -1, NULL, NULL);
1190         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1191 }
1192
1193 #ifdef HAVE_LUA
1194 static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
1195 {
1196         gchar *result = NULL;
1197         gchar **lua_command;
1198
1199         lua_command = g_strsplit(text, ",", 2);
1200
1201         if (lua_command[0] && lua_command[1])
1202                 {
1203                 FileData *fd = file_data_new_group(lua_command[0]);
1204                 result = g_strdup(lua_callvalue(fd, lua_command[1], NULL));
1205                 if (result)
1206                         {
1207                         g_io_channel_write_chars(channel, result, -1, NULL, NULL);
1208                         }
1209                 else
1210                         {
1211                         g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1212                         }
1213                 }
1214         else
1215                 {
1216                 g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
1217                 }
1218
1219         g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
1220
1221         g_strfreev(lua_command);
1222         g_free(result);
1223 }
1224 #endif
1225
1226 typedef struct _RemoteCommandEntry RemoteCommandEntry;
1227 struct _RemoteCommandEntry {
1228         gchar *opt_s;
1229         gchar *opt_l;
1230         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
1231         gboolean needs_extra;
1232         gboolean prefer_command_line;
1233         gchar *parameter;
1234         gchar *description;
1235 };
1236
1237 static RemoteCommandEntry remote_commands[] = {
1238         /* short, long                  callback,               extra, prefer, parameter, description */
1239         { "-n", "--next",               gr_image_next,          FALSE, FALSE, NULL, N_("next image") },
1240         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, NULL, N_("previous image") },
1241         { NULL, "--first",              gr_image_first,         FALSE, FALSE, NULL, N_("first image") },
1242         { NULL, "--last",               gr_image_last,          FALSE, FALSE, NULL, N_("last image") },
1243         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  NULL, N_("toggle full screen") },
1244         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, NULL, N_("start full screen") },
1245         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, NULL, N_("stop full screen") },
1246         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  NULL, N_("toggle slide show") },
1247         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, NULL, N_("start slide show") },
1248         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, NULL, N_("stop slide show") },
1249         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("<FOLDER>"), N_("start recursive slide show in FOLDER") },
1250         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("<[H:][M:][N][.M]>"), N_("set slide show delay to Hrs Mins N.M seconds") },
1251         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
1252         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
1253         { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
1254         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
1255         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
1256         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
1257         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1258         { NULL, "--file:",              gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
1259         { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1260         { NULL, "--File:",              gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
1261         { NULL, "--tell",               gr_file_tell,           FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
1262         { NULL, "--pixel-info",         gr_pixel_info,          FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
1263         { NULL, "--get-rectangle",      gr_rectangle,           FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
1264         { NULL, "--get-render-intent",  gr_render_intent,       FALSE, FALSE, NULL, N_("get render intent") },
1265         { NULL, "--get-filelist:",      gr_filelist,            TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class") },
1266         { NULL, "--get-filelist-recurse:", gr_filelist_recurse, TRUE,  FALSE, N_("[<FOLDER>]"), N_("get list of files and class recursive") },
1267         { NULL, "--get-collection:",    gr_collection,          TRUE,  FALSE, N_("<COLLECTION>"), N_("get collection content") },
1268         { NULL, "--get-collection-list", gr_collection_list,    FALSE, FALSE, NULL, N_("get collection list") },
1269         { NULL, "--get-file-info",      gr_file_info,           FALSE, FALSE, NULL, N_("get file info") },
1270         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1271         { NULL, "--view:",              gr_file_view,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE in new window") },
1272         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL, N_("clear command line collection list") },
1273         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, N_("<FILE>"), N_("add FILE to command line collection list") },
1274         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1275         { NULL, "--raise",              gr_raise,               FALSE, FALSE, NULL, N_("bring the Geeqie window to the top") },
1276         { NULL, "--id:",                gr_lw_id,               TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
1277         { NULL, "--new-window",         gr_new_window,          FALSE, FALSE, NULL, N_("new window") },
1278         { NULL, "--close-window",       gr_close_window,        FALSE, FALSE, NULL, N_("close window") },
1279         { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
1280         { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
1281         { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, NULL, N_("    clean the metadata cache") },
1282         { "-cr:", "--cache-render:",    gr_cache_render,        TRUE, FALSE, N_("<folder>  "), N_(" render thumbnails") },
1283         { "-crr:", "--cache-render-recurse:", gr_cache_render_recurse, TRUE, FALSE, N_("<folder> "), N_("render thumbnails recursively") },
1284         { "-crs:", "--cache-render-shared:", gr_cache_render_standard, TRUE, FALSE, N_("<folder> "), N_(" render thumbnails (see Help)") },
1285         { "-crsr:", "--cache-render-shared-recurse:", gr_cache_render_standard_recurse, TRUE, FALSE, N_("<folder>"), N_(" render thumbnails recursively (see Help)") },
1286 #ifdef HAVE_LUA
1287         { NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
1288 #endif
1289         { NULL, "--PWD:",               gr_pwd,                 TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
1290         { NULL, "--print0",             gr_print0,              TRUE, FALSE, NULL, N_("terminate returned data with null character instead of newline") },
1291         { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
1292 };
1293
1294 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
1295 {
1296         gboolean match = FALSE;
1297         gint i;
1298
1299         i = 0;
1300         while (!match && remote_commands[i].func != NULL)
1301                 {
1302                 if (remote_commands[i].needs_extra)
1303                         {
1304                         if (remote_commands[i].opt_s &&
1305                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
1306                                 {
1307                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
1308                                 return &remote_commands[i];
1309                                 }
1310                         else if (remote_commands[i].opt_l &&
1311                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
1312                                 {
1313                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
1314                                 return &remote_commands[i];
1315                                 }
1316                         }
1317                 else
1318                         {
1319                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
1320                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
1321                                 {
1322                                 if (offset) *offset = text;
1323                                 return &remote_commands[i];
1324                                 }
1325                         }
1326
1327                 i++;
1328                 }
1329
1330         return NULL;
1331 }
1332
1333 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
1334 {
1335         RemoteCommandEntry *entry;
1336         const gchar *offset;
1337
1338         entry = remote_command_find(text, &offset);
1339         if (entry && entry->func)
1340                 {
1341                 entry->func(offset, channel, data);
1342                 }
1343         else
1344                 {
1345                 log_printf("unknown remote command:%s\n", text);
1346                 }
1347 }
1348
1349 void remote_help(void)
1350 {
1351         gint i;
1352         gchar *s_opt_param;
1353         gchar *l_opt_param;
1354
1355         print_term(FALSE, _("Remote command list:\n"));
1356
1357         i = 0;
1358         while (remote_commands[i].func != NULL)
1359                 {
1360                 if (remote_commands[i].description)
1361                         {
1362                         s_opt_param = g_strconcat(remote_commands[i].opt_s, remote_commands[i].parameter, NULL);
1363                         l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
1364                         printf_term(FALSE, "  %-11s%-1s %-30s%-s\n",
1365                                     (remote_commands[i].opt_s) ? s_opt_param : "",
1366                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
1367                                     (remote_commands[i].opt_l) ? l_opt_param : "",
1368                                     _(remote_commands[i].description));
1369                         g_free(s_opt_param);
1370                         g_free(l_opt_param);
1371                         }
1372                 i++;
1373                 }
1374         printf_term(FALSE, N_("\n  All other command line parameters are used as plain files if they exists.\n"));
1375 }
1376
1377 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
1378 {
1379         gint i;
1380
1381         i = 1;
1382         while (i < argc)
1383                 {
1384                 RemoteCommandEntry *entry;
1385
1386                 entry = remote_command_find(argv[i], NULL);
1387                 if (entry)
1388                         {
1389                         list = g_list_append(list, argv[i]);
1390                         }
1391                 else if (errors && !isfile(argv[i]))
1392                         {
1393                         *errors = g_list_append(*errors, argv[i]);
1394                         }
1395                 i++;
1396                 }
1397
1398         return list;
1399 }
1400
1401 /**
1402  * \param arg_exec Binary (argv0)
1403  * \param remote_list Evaluated and recognized remote commands
1404  * \param path The current path
1405  * \param cmd_list List of all non collections in Path
1406  * \param collection_list List of all collections in argv
1407  */
1408 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
1409                     GList *cmd_list, GList *collection_list)
1410 {
1411         RemoteConnection *rc;
1412         gboolean started = FALSE;
1413         gchar *buf;
1414
1415         buf = g_build_filename(get_rc_dir(), ".command", NULL);
1416         rc = remote_client_open(buf);
1417         if (!rc)
1418                 {
1419                 GString *command;
1420                 GList *work;
1421                 gint retry_count = 12;
1422                 gboolean blank = FALSE;
1423
1424                 printf_term(FALSE, _("Remote %s not running, starting..."), GQ_APPNAME);
1425
1426                 command = g_string_new(arg_exec);
1427
1428                 work = remote_list;
1429                 while (work)
1430                         {
1431                         gchar *text;
1432                         RemoteCommandEntry *entry;
1433
1434                         text = work->data;
1435                         work = work->next;
1436
1437                         entry = remote_command_find(text, NULL);
1438                         if (entry)
1439                                 {
1440                                 /* If Geeqie is not running, stop the --new-window command opening a second window */
1441                                 if (g_strcmp0(text, "--new-window") == 0)
1442                                         {
1443                                         remote_list = g_list_remove(remote_list, text);
1444                                         }
1445                                 if (entry->prefer_command_line)
1446                                         {
1447                                         remote_list = g_list_remove(remote_list, text);
1448                                         g_string_append(command, " ");
1449                                         g_string_append(command, text);
1450                                         }
1451                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
1452                                         {
1453                                         blank = TRUE;
1454                                         }
1455                                 }
1456                         }
1457
1458                 if (blank || cmd_list || path) g_string_append(command, " --blank");
1459                 if (get_debug_level()) g_string_append(command, " --debug");
1460
1461                 g_string_append(command, " &");
1462                 runcmd(command->str);
1463                 g_string_free(command, TRUE);
1464
1465                 while (!rc && retry_count > 0)
1466                         {
1467                         usleep((retry_count > 10) ? 500000 : 1000000);
1468                         rc = remote_client_open(buf);
1469                         if (!rc) print_term(FALSE, ".");
1470                         retry_count--;
1471                         }
1472
1473                 print_term(FALSE, "\n");
1474
1475                 started = TRUE;
1476                 }
1477         g_free(buf);
1478
1479         if (rc)
1480                 {
1481                 GList *work;
1482                 const gchar *prefix;
1483                 gboolean use_path = TRUE;
1484                 gboolean sent = FALSE;
1485
1486                 work = remote_list;
1487                 while (work)
1488                         {
1489                         gchar *text;
1490                         RemoteCommandEntry *entry;
1491
1492                         text = work->data;
1493                         work = work->next;
1494
1495                         entry = remote_command_find(text, NULL);
1496                         if (entry &&
1497                             entry->opt_l &&
1498                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
1499
1500                         remote_client_send(rc, text);
1501
1502                         sent = TRUE;
1503                         }
1504
1505                 if (cmd_list && cmd_list->next)
1506                         {
1507                         prefix = "--list-add:";
1508                         remote_client_send(rc, "--list-clear");
1509                         }
1510                 else
1511                         {
1512                         prefix = "file:";
1513                         }
1514
1515                 work = cmd_list;
1516                 while (work)
1517                         {
1518                         FileData *fd;
1519                         gchar *text;
1520
1521                         fd = work->data;
1522                         work = work->next;
1523
1524                         text = g_strconcat(prefix, fd->path, NULL);
1525                         remote_client_send(rc, text);
1526                         g_free(text);
1527
1528                         sent = TRUE;
1529                         }
1530
1531                 if (path && !cmd_list && use_path)
1532                         {
1533                         gchar *text;
1534
1535                         text = g_strdup_printf("file:%s", path);
1536                         remote_client_send(rc, text);
1537                         g_free(text);
1538
1539                         sent = TRUE;
1540                         }
1541
1542                 work = collection_list;
1543                 while (work)
1544                         {
1545                         const gchar *name;
1546                         gchar *text;
1547
1548                         name = work->data;
1549                         work = work->next;
1550
1551                         text = g_strdup_printf("file:%s", name);
1552                         remote_client_send(rc, text);
1553                         g_free(text);
1554
1555                         sent = TRUE;
1556                         }
1557
1558                 if (!started && !sent)
1559                         {
1560                         remote_client_send(rc, "raise");
1561                         }
1562                 }
1563         else
1564                 {
1565                 print_term(TRUE, _("Remote not available\n"));
1566                 }
1567
1568         _exit(0);
1569 }
1570
1571 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
1572 {
1573         RemoteConnection *remote_connection = remote_server_open(path);
1574         RemoteData *remote_data = g_new(RemoteData, 1);
1575
1576         remote_data->command_collection = command_collection;
1577
1578         remote_server_subscribe(remote_connection, remote_cb, remote_data);
1579         return remote_connection;
1580 }
1581 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */