improved remote protocol to allow bidirectional communication
[geeqie.git] / src / remote.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 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                                         break;
325                                         }
326                                 buffer[termpos] = '\0';
327                                 printf("%s\n", buffer);
328                                 g_free(buffer);
329                                 buffer = NULL;
330                                 }
331                         }
332
333                 if (error)
334                         {
335                         log_printf("error reading socket: %s\n", error->message);
336                         g_error_free(error);
337                         ret = FALSE;
338                         }
339                 }
340                 
341
342         /* restore the original signal handler */
343         sigaction(SIGPIPE, &old_action, NULL);
344         g_io_channel_unref(channel);
345         return ret;
346 }
347
348 void remote_close(RemoteConnection *rc)
349 {
350         if (!rc) return;
351
352         if (rc->server)
353                 {
354                 remote_server_clients_close(rc);
355
356                 g_source_remove(rc->channel_id);
357                 unlink(rc->path);
358                 }
359
360         if (rc->read_data)
361                 g_free(rc->read_data);
362
363         close(rc->fd);
364
365         g_free(rc->path);
366         g_free(rc);
367 }
368
369 /*
370  *-----------------------------------------------------------------------------
371  * remote functions
372  *-----------------------------------------------------------------------------
373  */
374
375 static void gr_image_next(const gchar *text, GIOChannel *channel, gpointer data)
376 {
377         layout_image_next(NULL);
378 }
379
380 static void gr_image_prev(const gchar *text, GIOChannel *channel, gpointer data)
381 {
382         layout_image_prev(NULL);
383 }
384
385 static void gr_image_first(const gchar *text, GIOChannel *channel, gpointer data)
386 {
387         layout_image_first(NULL);
388 }
389
390 static void gr_image_last(const gchar *text, GIOChannel *channel, gpointer data)
391 {
392         layout_image_last(NULL);
393 }
394
395 static void gr_fullscreen_toggle(const gchar *text, GIOChannel *channel, gpointer data)
396 {
397         layout_image_full_screen_toggle(NULL);
398 }
399
400 static void gr_fullscreen_start(const gchar *text, GIOChannel *channel, gpointer data)
401 {
402         layout_image_full_screen_start(NULL);
403 }
404
405 static void gr_fullscreen_stop(const gchar *text, GIOChannel *channel, gpointer data)
406 {
407         layout_image_full_screen_stop(NULL);
408 }
409
410 static void gr_slideshow_start_rec(const gchar *text, GIOChannel *channel, gpointer data)
411 {
412         GList *list;
413         FileData *dir_fd = file_data_new_simple(text);
414         list = filelist_recursive(dir_fd);
415         file_data_unref(dir_fd);
416         if (!list) return;
417 //printf("length: %d\n", g_list_length(list));
418         layout_image_slideshow_stop(NULL);
419         layout_image_slideshow_start_from_list(NULL, list);
420 }
421
422 static void gr_slideshow_toggle(const gchar *text, GIOChannel *channel, gpointer data)
423 {
424         layout_image_slideshow_toggle(NULL);
425 }
426
427 static void gr_slideshow_start(const gchar *text, GIOChannel *channel, gpointer data)
428 {
429         layout_image_slideshow_start(NULL);
430 }
431
432 static void gr_slideshow_stop(const gchar *text, GIOChannel *channel, gpointer data)
433 {
434         layout_image_slideshow_stop(NULL);
435 }
436
437 static void gr_slideshow_delay(const gchar *text, GIOChannel *channel, gpointer data)
438 {
439         gdouble n;
440
441         n = g_ascii_strtod(text, NULL);
442         if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
443                 {
444                 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
445                             SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
446                 return;
447                 }
448         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
449 }
450
451 static void gr_tools_show(const gchar *text, GIOChannel *channel, gpointer data)
452 {
453         gboolean popped;
454         gboolean hidden;
455
456         if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
457                 {
458                 layout_tools_float_set(NULL, popped, FALSE);
459                 }
460 }
461
462 static void gr_tools_hide(const gchar *text, GIOChannel *channel, gpointer data)
463 {
464         gboolean popped;
465         gboolean hidden;
466
467         if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
468                 {
469                 layout_tools_float_set(NULL, popped, TRUE);
470                 }
471 }
472
473 static gboolean gr_quit_idle_cb(gpointer data)
474 {
475         exit_program();
476
477         return FALSE;
478 }
479
480 static void gr_quit(const gchar *text, GIOChannel *channel, gpointer data)
481 {
482         /* schedule exit when idle, if done from within a
483          * remote handler remote_close will crash
484          */
485         g_idle_add(gr_quit_idle_cb, NULL);
486 }
487
488 static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
489 {
490         gchar *filename = expand_tilde(text);
491
492         if (isfile(filename))
493                 {
494                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
495                         {
496                         collection_window_new(filename);
497                         }
498                 else
499                         {
500                         layout_set_path(NULL, filename);
501                         }
502                 }
503         else if (isdir(filename))
504                 {
505                 layout_set_path(NULL, filename);
506                 }
507         else
508                 {
509                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
510                 }
511
512         g_free(filename);
513 }
514
515 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
516 {
517         gchar *filename = expand_tilde(text);
518
519         if (isfile(filename))
520                 {
521                 load_config_from_file(filename, FALSE);
522                 }
523         else
524                 {
525                 log_printf("remote sent filename that does not exist:\"%s\"\n", filename);
526                 }
527
528         g_free(filename);
529 }
530
531 static void gr_file_view(const gchar *text, GIOChannel *channel, gpointer data)
532 {
533         gchar *filename = expand_tilde(text);
534
535         view_window_new(file_data_new_simple(filename));
536         g_free(filename);
537 }
538
539 static void gr_list_clear(const gchar *text, GIOChannel *channel, gpointer data)
540 {
541         RemoteData *remote_data = data;
542
543         if (remote_data->command_collection)
544                 {
545                 collection_unref(remote_data->command_collection);
546                 remote_data->command_collection = NULL;
547                 }
548 }
549
550 static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
551 {
552         RemoteData *remote_data = data;
553         gboolean new = TRUE;
554
555         if (!remote_data->command_collection)
556                 {
557                 CollectionData *cd;
558
559                 cd = collection_new("");
560
561                 g_free(cd->path);
562                 cd->path = NULL;
563                 g_free(cd->name);
564                 cd->name = g_strdup(_("Command line"));
565
566                 remote_data->command_collection = cd;
567                 }
568         else
569                 {
570                 new = (!collection_get_first(remote_data->command_collection));
571                 }
572
573         if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new)
574                 {
575                 layout_image_set_collection(NULL, remote_data->command_collection,
576                                             collection_get_first(remote_data->command_collection));
577                 }
578 }
579
580 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
581 {
582         LayoutWindow *lw = NULL;
583
584         if (layout_valid(&lw))
585                 {
586                 gtk_window_present(GTK_WINDOW(lw->window));
587                 }
588 }
589
590 typedef struct _RemoteCommandEntry RemoteCommandEntry;
591 struct _RemoteCommandEntry {
592         gchar *opt_s;
593         gchar *opt_l;
594         void (*func)(const gchar *text, GIOChannel *channel, gpointer data);
595         gboolean needs_extra;
596         gboolean prefer_command_line;
597         gchar *description;
598 };
599
600 static RemoteCommandEntry remote_commands[] = {
601         /* short, long                  callback,               extra, prefer,description */
602         { "-n", "--next",               gr_image_next,          FALSE, FALSE, N_("next image") },
603         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, N_("previous image") },
604         { NULL, "--first",              gr_image_first,         FALSE, FALSE, N_("first image") },
605         { NULL, "--last",               gr_image_last,          FALSE, FALSE, N_("last image") },
606         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  N_("toggle full screen") },
607         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, N_("start full screen") },
608         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, N_("stop full screen") },
609         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  N_("toggle slide show") },
610         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, N_("start slide show") },
611         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, N_("stop slide show") },
612         { NULL, "--slideshow-recurse:", gr_slideshow_start_rec, TRUE,  FALSE, N_("start recursive slide show") },
613         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("set slide show delay in seconds") },
614         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  N_("show tools") },
615         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  N_("hide tools") },
616         { "-q", "--quit",               gr_quit,                FALSE, FALSE, N_("quit") },
617         { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("load config file") },
618         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("open file") },
619         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("open file in new window") },
620         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL },
621         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, NULL },
622         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL },
623         { NULL, NULL, NULL, FALSE, FALSE, NULL }
624 };
625
626 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
627 {
628         gboolean match = FALSE;
629         gint i;
630
631         i = 0;
632         while (!match && remote_commands[i].func != NULL)
633                 {
634                 if (remote_commands[i].needs_extra)
635                         {
636                         if (remote_commands[i].opt_s &&
637                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
638                                 {
639                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
640                                 return &remote_commands[i];
641                                 }
642                         else if (remote_commands[i].opt_l &&
643                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
644                                 {
645                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
646                                 return &remote_commands[i];
647                                 }
648                         }
649                 else
650                         {
651                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
652                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
653                                 {
654                                 if (offset) *offset = text;
655                                 return &remote_commands[i];
656                                 }
657                         }
658
659                 i++;
660                 }
661
662         return NULL;
663 }
664
665 static void remote_cb(RemoteConnection *rc, const gchar *text, GIOChannel *channel, gpointer data)
666 {
667         RemoteCommandEntry *entry;
668         const gchar *offset;
669
670         entry = remote_command_find(text, &offset);
671         if (entry && entry->func)
672                 {
673                 entry->func(offset, channel, data);
674                 }
675         else
676                 {
677                 log_printf("unknown remote command:%s\n", text);
678                 }
679 }
680
681 void remote_help(void)
682 {
683         gint i;
684
685         print_term(_("Remote command list:\n"));
686
687         i = 0;
688         while (remote_commands[i].func != NULL)
689                 {
690                 if (remote_commands[i].description)
691                         {
692                         printf_term("  %-3s%s %-20s %s\n",
693                                     (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
694                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
695                                     (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
696                                     _(remote_commands[i].description));
697                         }
698                 i++;
699                 }
700 }
701
702 GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
703 {
704         gint i;
705
706         i = 1;
707         while (i < argc)
708                 {
709                 RemoteCommandEntry *entry;
710
711                 entry = remote_command_find(argv[i], NULL);
712                 if (entry)
713                         {
714                         list = g_list_append(list, argv[i]);
715                         }
716                 else if (errors)
717                         {
718                         *errors = g_list_append(*errors, argv[i]);
719                         }
720                 i++;
721                 }
722
723         return list;
724 }
725
726 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
727                     GList *cmd_list, GList *collection_list)
728 {
729         RemoteConnection *rc;
730         gboolean started = FALSE;
731         gchar *buf;
732
733         buf = g_build_filename(get_rc_dir(), ".command", NULL);
734         rc = remote_client_open(buf);
735         if (!rc)
736                 {
737                 GString *command;
738                 GList *work;
739                 gint retry_count = 12;
740                 gboolean blank = FALSE;
741
742                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
743
744                 command = g_string_new(arg_exec);
745
746                 work = remote_list;
747                 while (work)
748                         {
749                         gchar *text;
750                         RemoteCommandEntry *entry;
751
752                         text = work->data;
753                         work = work->next;
754
755                         entry = remote_command_find(text, NULL);
756                         if (entry)
757                                 {
758                                 if (entry->prefer_command_line)
759                                         {
760                                         remote_list = g_list_remove(remote_list, text);
761                                         g_string_append(command, " ");
762                                         g_string_append(command, text);
763                                         }
764                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
765                                         {
766                                         blank = TRUE;
767                                         }
768                                 }
769                         }
770
771                 if (blank || cmd_list || path) g_string_append(command, " --blank");
772                 if (get_debug_level()) g_string_append(command, " --debug");
773
774                 g_string_append(command, " &");
775                 runcmd(command->str);
776                 g_string_free(command, TRUE);
777
778                 while (!rc && retry_count > 0)
779                         {
780                         usleep((retry_count > 10) ? 500000 : 1000000);
781                         rc = remote_client_open(buf);
782                         if (!rc) print_term(".");
783                         retry_count--;
784                         }
785
786                 print_term("\n");
787
788                 started = TRUE;
789                 }
790         g_free(buf);
791
792         if (rc)
793                 {
794                 GList *work;
795                 const gchar *prefix;
796                 gboolean use_path = TRUE;
797                 gboolean sent = FALSE;
798
799                 work = remote_list;
800                 while (work)
801                         {
802                         gchar *text;
803                         RemoteCommandEntry *entry;
804
805                         text = work->data;
806                         work = work->next;
807
808                         entry = remote_command_find(text, NULL);
809                         if (entry &&
810                             entry->opt_l &&
811                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
812
813                         remote_client_send(rc, text);
814
815                         sent = TRUE;
816                         }
817
818                 if (cmd_list && cmd_list->next)
819                         {
820                         prefix = "--list-add:";
821                         remote_client_send(rc, "--list-clear");
822                         }
823                 else
824                         {
825                         prefix = "file:";
826                         }
827
828                 work = cmd_list;
829                 while (work)
830                         {
831                         FileData *fd;
832                         gchar *text;
833
834                         fd = work->data;
835                         work = work->next;
836
837                         text = g_strconcat(prefix, fd->path, NULL);
838                         remote_client_send(rc, text);
839                         g_free(text);
840
841                         sent = TRUE;
842                         }
843
844                 if (path && !cmd_list && use_path)
845                         {
846                         gchar *text;
847
848                         text = g_strdup_printf("file:%s", path);
849                         remote_client_send(rc, text);
850                         g_free(text);
851
852                         sent = TRUE;
853                         }
854
855                 work = collection_list;
856                 while (work)
857                         {
858                         const gchar *name;
859                         gchar *text;
860
861                         name = work->data;
862                         work = work->next;
863
864                         text = g_strdup_printf("file:%s", name);
865                         remote_client_send(rc, text);
866                         g_free(text);
867
868                         sent = TRUE;
869                         }
870
871                 if (!started && !sent)
872                         {
873                         remote_client_send(rc, "raise");
874                         }
875                 }
876         else
877                 {
878                 print_term(_("Remote not available\n"));
879                 }
880
881         _exit(0);
882 }
883
884 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
885 {
886         RemoteConnection *remote_connection = remote_server_open(path);
887         RemoteData *remote_data = g_new(RemoteData, 1);
888         
889         remote_data->command_collection = command_collection;
890
891         remote_server_subscribe(remote_connection, remote_cb, remote_data);
892         return remote_connection;
893 }
894 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */