Trim trailing white spaces.
[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 *description;
635 };
636
637 static RemoteCommandEntry remote_commands[] = {
638         /* short, long                  callback,               extra, prefer,description */
639         { "-n", "--next",               gr_image_next,          FALSE, FALSE, N_("next image") },
640         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, N_("previous image") },
641         { NULL, "--first",              gr_image_first,         FALSE, FALSE, N_("first image") },
642         { NULL, "--last",               gr_image_last,          FALSE, FALSE, N_("last image") },
643         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  N_("toggle full screen") },
644         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, N_("start full screen") },
645         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, N_("stop full screen") },
646         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  N_("toggle slide show") },
647         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, N_("start slide show") },
648         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, N_("stop slide show") },
649         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("start recursive slide show") },
650         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("set slide show delay in seconds") },
651         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  N_("show tools") },
652         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  N_("hide tools") },
653         { "-q", "--quit",               gr_quit,                FALSE, FALSE, N_("quit") },
654         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("load config file") },
655         { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("get list of sidecars of the given file") },
656         { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("get destination path for the given file") },
657         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("open file") },
658         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("open file in new window") },
659         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL },
660         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, NULL },
661         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL },
662         { NULL, NULL, NULL, FALSE, FALSE, NULL }
663 };
664
665 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
666 {
667         gboolean match = FALSE;
668         gint i;
669
670         i = 0;
671         while (!match && remote_commands[i].func != NULL)
672                 {
673                 if (remote_commands[i].needs_extra)
674                         {
675                         if (remote_commands[i].opt_s &&
676                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
677                                 {
678                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
679                                 return &remote_commands[i];
680                                 }
681                         else if (remote_commands[i].opt_l &&
682                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
683                                 {
684                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
685                                 return &remote_commands[i];
686                                 }
687                         }
688                 else
689                         {
690                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
691                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
692                                 {
693                                 if (offset) *offset = text;
694                                 return &remote_commands[i];
695                                 }
696                         }
697
698                 i++;
699                 }
700
701         return NULL;
702 }
703
704 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
705 {
706         RemoteCommandEntry *entry;
707         const gchar *offset;
708
709         entry = remote_command_find(text, &offset);
710         if (entry && entry->func)
711                 {
712                 entry->func(offset, channel, data);
713                 }
714         else
715                 {
716                 log_printf("unknown remote command:%s\n", text);
717                 }
718 }
719
720 void remote_help(void)
721 {
722         gint i;
723
724         print_term(_("Remote command list:\n"));
725
726         i = 0;
727         while (remote_commands[i].func != NULL)
728                 {
729                 if (remote_commands[i].description)
730                         {
731                         printf_term("  %-3s%s %-20s %s\n",
732                                     (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
733                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
734                                     (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
735                                     _(remote_commands[i].description));
736                         }
737                 i++;
738                 }
739         printf_term(N_("\n  All other command line parameters are used as plain files if they exists.\n"));
740 }
741
742 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
743 {
744         gint i;
745
746         i = 1;
747         while (i < argc)
748                 {
749                 RemoteCommandEntry *entry;
750
751                 entry = remote_command_find(argv[i], NULL);
752                 if (entry)
753                         {
754                         list = g_list_append(list, argv[i]);
755                         }
756                 else if (errors && !isfile(argv[i]))
757                         {
758                         *errors = g_list_append(*errors, argv[i]);
759                         }
760                 i++;
761                 }
762
763         return list;
764 }
765
766 /**
767  * \param arg_exec Binary (argv0)
768  * \param remote_list Evaluated and recognized remote commands
769  * \param path The current path
770  * \param cmd_list List of all non collections in Path
771  * \param collection_list List of all collections in argv
772  */
773 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
774                     GList *cmd_list, GList *collection_list)
775 {
776         RemoteConnection *rc;
777         gboolean started = FALSE;
778         gchar *buf;
779
780         buf = g_build_filename(get_rc_dir(), ".command", NULL);
781         rc = remote_client_open(buf);
782         if (!rc)
783                 {
784                 GString *command;
785                 GList *work;
786                 gint retry_count = 12;
787                 gboolean blank = FALSE;
788
789                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
790
791                 command = g_string_new(arg_exec);
792
793                 work = remote_list;
794                 while (work)
795                         {
796                         gchar *text;
797                         RemoteCommandEntry *entry;
798
799                         text = work->data;
800                         work = work->next;
801
802                         entry = remote_command_find(text, NULL);
803                         if (entry)
804                                 {
805                                 if (entry->prefer_command_line)
806                                         {
807                                         remote_list = g_list_remove(remote_list, text);
808                                         g_string_append(command, " ");
809                                         g_string_append(command, text);
810                                         }
811                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
812                                         {
813                                         blank = TRUE;
814                                         }
815                                 }
816                         }
817
818                 if (blank || cmd_list || path) g_string_append(command, " --blank");
819                 if (get_debug_level()) g_string_append(command, " --debug");
820
821                 g_string_append(command, " &");
822                 runcmd(command->str);
823                 g_string_free(command, TRUE);
824
825                 while (!rc && retry_count > 0)
826                         {
827                         usleep((retry_count > 10) ? 500000 : 1000000);
828                         rc = remote_client_open(buf);
829                         if (!rc) print_term(".");
830                         retry_count--;
831                         }
832
833                 print_term("\n");
834
835                 started = TRUE;
836                 }
837         g_free(buf);
838
839         if (rc)
840                 {
841                 GList *work;
842                 const gchar *prefix;
843                 gboolean use_path = TRUE;
844                 gboolean sent = FALSE;
845
846                 work = remote_list;
847                 while (work)
848                         {
849                         gchar *text;
850                         RemoteCommandEntry *entry;
851
852                         text = work->data;
853                         work = work->next;
854
855                         entry = remote_command_find(text, NULL);
856                         if (entry &&
857                             entry->opt_l &&
858                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
859
860                         remote_client_send(rc, text);
861
862                         sent = TRUE;
863                         }
864
865                 if (cmd_list && cmd_list->next)
866                         {
867                         prefix = "--list-add:";
868                         remote_client_send(rc, "--list-clear");
869                         }
870                 else
871                         {
872                         prefix = "file:";
873                         }
874
875                 work = cmd_list;
876                 while (work)
877                         {
878                         FileData *fd;
879                         gchar *text;
880
881                         fd = work->data;
882                         work = work->next;
883
884                         text = g_strconcat(prefix, fd->path, NULL);
885                         remote_client_send(rc, text);
886                         g_free(text);
887
888                         sent = TRUE;
889                         }
890
891                 if (path && !cmd_list && use_path)
892                         {
893                         gchar *text;
894
895                         text = g_strdup_printf("file:%s", path);
896                         remote_client_send(rc, text);
897                         g_free(text);
898
899                         sent = TRUE;
900                         }
901
902                 work = collection_list;
903                 while (work)
904                         {
905                         const gchar *name;
906                         gchar *text;
907
908                         name = work->data;
909                         work = work->next;
910
911                         text = g_strdup_printf("file:%s", name);
912                         remote_client_send(rc, text);
913                         g_free(text);
914
915                         sent = TRUE;
916                         }
917
918                 if (!started && !sent)
919                         {
920                         remote_client_send(rc, "raise");
921                         }
922                 }
923         else
924                 {
925                 print_term(_("Remote not available\n"));
926                 }
927
928         _exit(0);
929 }
930
931 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
932 {
933         RemoteConnection *remote_connection = remote_server_open(path);
934         RemoteData *remote_data = g_new(RemoteData, 1);
935         
936         remote_data->command_collection = command_collection;
937
938         remote_server_subscribe(remote_connection, remote_cb, remote_data);
939         return remote_connection;
940 }
941 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */