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