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