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