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