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