Expand tilde with file: and view: remote parameters.
[geeqie.git] / src / remote.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 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 "debug.h"
19 #include "filedata.h"
20 #include "img-view.h"
21 #include "layout.h"
22 #include "layout_image.h"
23 #include "slideshow.h"
24 #include "ui_fileops.h"
25
26 #include <sys/types.h>
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
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                 guint termpos;
74
75                 while (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                         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)
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, int fd)
129 {
130         RemoteClient *client;
131         GIOChannel *channel;
132
133         if (g_list_length(rc->clients) > SERVER_MAX_CLIENTS)
134                 {
135                 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         int fd;
171         unsigned int alen;
172
173         fd = accept(rc->fd, NULL, &alen);
174         if (fd == -1)
175                 {
176                 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         int fd;
206         GIOChannel *channel;
207
208         if (remote_server_exists(path))
209                 {
210                 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                 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         rc->channel_id = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, G_IO_IN,
240                                              remote_server_read_cb, rc, NULL);
241         g_io_channel_unref(channel);
242
243         return rc;
244 }
245
246 static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
247 {
248         if (!rc || !rc->server) return;
249
250         rc->read_func = func;
251         rc->read_data = data;
252 }
253
254
255 static RemoteConnection *remote_client_open(const gchar *path)
256 {
257         RemoteConnection *rc;
258         struct stat st;
259         struct sockaddr_un addr;
260         gint sun_path_len;
261         int fd;
262
263         if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return NULL;
264
265         fd = socket(PF_UNIX, SOCK_STREAM, 0);
266         if (fd == -1) return NULL;
267
268         addr.sun_family = AF_UNIX;
269         sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
270         strncpy(addr.sun_path, path, sun_path_len);
271         if (connect(fd, &addr, sizeof(addr)) == -1)
272                 {
273                 DEBUG_1("error connecting to socket: %s", strerror(errno));
274                 close(fd);
275                 return NULL;
276                 }
277
278         rc = g_new0(RemoteConnection, 1);
279         rc->server = FALSE;
280         rc->fd = fd;
281         rc->path = g_strdup(path);
282
283         /* this might fix the freezes on freebsd, solaris, etc. - completely untested */
284         remote_client_send(rc, "\n");
285
286         return rc;
287 }
288
289 static sig_atomic_t sigpipe_occured = FALSE;
290
291 static void sighandler_sigpipe(int sig)
292 {
293         sigpipe_occured = TRUE;
294 }
295
296 static gint remote_client_send(RemoteConnection *rc, const gchar *text)
297 {
298         struct sigaction new_action, old_action;
299         gint ret = FALSE;
300
301         if (!rc || rc->server) return FALSE;
302         if (!text) return TRUE;
303
304         sigpipe_occured = FALSE;
305
306         new_action.sa_handler = sighandler_sigpipe;
307         sigemptyset(&new_action.sa_mask);
308         new_action.sa_flags = 0;
309
310         /* setup our signal handler */
311         sigaction(SIGPIPE, &new_action, &old_action);
312
313         if (write(rc->fd, text, strlen(text)) == -1 ||
314             write(rc->fd, "\n", 1) == -1)
315                 {
316                 if (sigpipe_occured)
317                         {
318                         printf("SIGPIPE writing to socket: %s\n", rc->path);
319                         }
320                 else
321                         {
322                         printf("error writing to socket: %s\n", strerror(errno));
323                         }
324                 ret = FALSE;;
325                 }
326         else
327                 {
328                 ret = TRUE;
329                 }
330
331         /* restore the original signal handler */
332         sigaction(SIGPIPE, &old_action, NULL);
333
334         return ret;
335 }
336
337 void remote_close(RemoteConnection *rc)
338 {
339         if (!rc) return;
340
341         if (rc->server)
342                 {
343                 remote_server_clients_close(rc);
344
345                 g_source_remove(rc->channel_id);
346                 unlink(rc->path);
347                 }
348
349         if (rc->read_data)
350                 g_free(rc->read_data);
351
352         close(rc->fd);
353
354         g_free(rc->path);
355         g_free(rc);
356 }
357
358 /*
359  *-----------------------------------------------------------------------------
360  * remote functions
361  *-----------------------------------------------------------------------------
362  */
363
364 static void gr_image_next(const gchar *text, gpointer data)
365 {
366         layout_image_next(NULL);
367 }
368
369 static void gr_image_prev(const gchar *text, gpointer data)
370 {
371         layout_image_prev(NULL);
372 }
373
374 static void gr_image_first(const gchar *text, gpointer data)
375 {
376         layout_image_first(NULL);
377 }
378
379 static void gr_image_last(const gchar *text, gpointer data)
380 {
381         layout_image_last(NULL);
382 }
383
384 static void gr_fullscreen_toggle(const gchar *text, gpointer data)
385 {
386         layout_image_full_screen_toggle(NULL);
387 }
388
389 static void gr_fullscreen_start(const gchar *text, gpointer data)
390 {
391         layout_image_full_screen_start(NULL);
392 }
393
394 static void gr_fullscreen_stop(const gchar *text, gpointer data)
395 {
396         layout_image_full_screen_stop(NULL);
397 }
398
399 static void gr_slideshow_start_rec(const gchar *text, gpointer data)
400 {
401         GList *list;
402
403         list = filelist_recursive(text);
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 = 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         gint popped;
442         gint 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         gint popped;
453         gint 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 gint 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, ".gqv"))
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                 printf("remote sent filename that does not exist:\"%s\"\n", filename);
498                 }
499
500         g_free(filename);
501 }
502
503 static void gr_file_view(const gchar *text, gpointer data)
504 {
505         gchar *filename = expand_tilde(text);
506
507         view_window_new(file_data_new_simple(filename));
508         g_free(filename);
509 }
510
511 static void gr_list_clear(const gchar *text, gpointer data)
512 {
513         RemoteData *remote_data = data;
514
515         if (remote_data->command_collection)
516                 {
517                 collection_unref(remote_data->command_collection);
518                 remote_data->command_collection = NULL;
519                 }
520 }               
521
522 static void gr_list_add(const gchar *text, gpointer data)
523 {
524         RemoteData *remote_data = data;
525         gint new = TRUE;
526
527         if (!remote_data->command_collection)
528                 {
529                 CollectionData *cd;
530
531                 cd = collection_new("");
532
533                 g_free(cd->path);
534                 cd->path = NULL;
535                 g_free(cd->name);
536                 cd->name = g_strdup(_("Command line"));
537
538                 remote_data->command_collection = cd;
539                 }
540         else
541                 {
542                 new = (!collection_get_first(remote_data->command_collection));
543                 }
544
545         if (collection_add(remote_data->command_collection, file_data_new_simple(text), FALSE) && new)
546                 {
547                 layout_image_set_collection(NULL, remote_data->command_collection,
548                                             collection_get_first(remote_data->command_collection));
549                 }
550 }
551
552 static void gr_raise(const gchar *text, gpointer data)
553 {
554         LayoutWindow *lw = NULL;
555
556         if (layout_valid(&lw))
557                 {
558                 gtk_window_present(GTK_WINDOW(lw->window));
559                 }
560 }
561
562 typedef struct _RemoteCommandEntry RemoteCommandEntry;
563 struct _RemoteCommandEntry {
564         gchar *opt_s;
565         gchar *opt_l;
566         void (*func)(const gchar *text, gpointer data);
567         gint needs_extra;
568         gint prefer_command_line;
569         gchar *description;
570 };
571
572 static RemoteCommandEntry remote_commands[] = {
573         /* short, long                  callback,               extra, prefer,description */
574         { "-n", "--next",               gr_image_next,          FALSE, FALSE, N_("next image") },
575         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, N_("previous image") },
576         { NULL, "--first",              gr_image_first,         FALSE, FALSE, N_("first image") },
577         { NULL, "--last",               gr_image_last,          FALSE, FALSE, N_("last image") },
578         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  N_("toggle full screen") },
579         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, N_("start full screen") },
580         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, N_("stop full screen") },
581         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  N_("toggle slide show") },
582         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, N_("start slide show") },
583         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, N_("stop slide show") },
584         { "-sr","--slideshow-recurse",  gr_slideshow_start_rec, TRUE,  FALSE, N_("start recursive slide show") },
585         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("set slide show delay in seconds") },
586         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  N_("show tools") },
587         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  N_("hide tools") },
588         { "-q", "--quit",               gr_quit,                FALSE, FALSE, N_("quit") },
589         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("open file") },
590         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("open file in new window") },
591         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL },
592         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, NULL },
593         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL },
594         { NULL, NULL, NULL, FALSE, FALSE, NULL }
595 };
596
597 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
598 {
599         gint match = FALSE;
600         gint i;
601
602         i = 0;
603         while (!match && remote_commands[i].func != NULL)
604                 {
605                 if (remote_commands[i].needs_extra)
606                         {
607                         if (remote_commands[i].opt_s &&
608                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
609                                 {
610                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
611                                 return &remote_commands[i];
612                                 }
613                         else if (remote_commands[i].opt_l &&
614                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
615                                 {
616                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
617                                 return &remote_commands[i];
618                                 }
619                         }
620                 else
621                         {
622                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
623                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
624                                 {
625                                 if (offset) *offset = text;
626                                 return &remote_commands[i];
627                                 }
628                         }
629
630                 i++;
631                 }
632
633         return NULL;
634 }
635
636 static void remote_cb(RemoteConnection *rc, const gchar *text, gpointer data)
637 {
638         RemoteCommandEntry *entry;
639         const gchar *offset;
640
641         entry = remote_command_find(text, &offset);
642         if (entry && entry->func)
643                 {
644                 entry->func(offset, data);
645                 }
646         else
647                 {
648                 printf("unknown remote command:%s\n", text);
649                 }
650 }
651
652 void remote_help(void)
653 {
654         gint i;
655
656         print_term(_("Remote command list:\n"));
657
658         i = 0;
659         while (remote_commands[i].func != NULL)
660                 {
661                 if (remote_commands[i].description)
662                         {
663                         printf_term("  %-3s%s %-20s %s\n",
664                                     (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
665                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
666                                     (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
667                                     _(remote_commands[i].description));
668                         }
669                 i++;
670                 }
671 }
672
673 GList *remote_build_list(GList *list, int argc, char *argv[], GList **errors)
674 {
675         gint i;
676
677         i = 1;
678         while (i < argc)
679                 {
680                 RemoteCommandEntry *entry;
681
682                 entry = remote_command_find(argv[i], NULL);
683                 if (entry)
684                         {
685                         list = g_list_append(list, argv[i]);
686                         }
687                 else if (errors)
688                         {
689                         *errors = g_list_append(*errors, argv[i]);
690                         }
691                 i++;
692                 }
693
694         return list;
695 }
696
697 void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
698                     GList *cmd_list, GList *collection_list)
699 {
700         RemoteConnection *rc;
701         gint started = FALSE;
702         gchar *buf;
703
704         buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
705         rc = remote_client_open(buf);
706         if (!rc)
707                 {
708                 GString *command;
709                 GList *work;
710                 gint retry_count = 12;
711                 gint blank = FALSE;
712
713                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
714
715                 command = g_string_new(arg_exec);
716
717                 work = remote_list;
718                 while (work)
719                         {
720                         gchar *text;
721                         RemoteCommandEntry *entry;
722
723                         text = work->data;
724                         work = work->next;
725
726                         entry = remote_command_find(text, NULL);
727                         if (entry)
728                                 {
729                                 if (entry->prefer_command_line)
730                                         {
731                                         remote_list = g_list_remove(remote_list, text);
732                                         g_string_append(command, " ");
733                                         g_string_append(command, text);
734                                         }
735                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
736                                         {
737                                         blank = TRUE;
738                                         }
739                                 }
740                         }
741
742                 if (blank || cmd_list || path) g_string_append(command, " --blank");
743                 if (get_debug_level()) g_string_append(command, " --debug");
744
745                 g_string_append(command, " &");
746                 system(command->str);
747                 g_string_free(command, TRUE);
748
749                 while (!rc && retry_count > 0)
750                         {
751                         usleep((retry_count > 10) ? 500000 : 1000000);
752                         rc = remote_client_open(buf);
753                         if (!rc) print_term(".");
754                         retry_count--;
755                         }
756
757                 print_term("\n");
758
759                 started = TRUE;
760                 }
761         g_free(buf);
762
763         if (rc)
764                 {
765                 GList *work;
766                 const gchar *prefix;
767                 gint use_path = TRUE;
768                 gint sent = FALSE;
769
770                 work = remote_list;
771                 while (work)
772                         {
773                         gchar *text;
774                         RemoteCommandEntry *entry;
775
776                         text = work->data;
777                         work = work->next;
778
779                         entry = remote_command_find(text, NULL);
780                         if (entry &&
781                             entry->opt_l &&
782                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
783
784                         remote_client_send(rc, text);
785
786                         sent = TRUE;
787                         }
788
789                 if (cmd_list && cmd_list->next)
790                         {
791                         prefix = "--list-add:";
792                         remote_client_send(rc, "--list-clear");
793                         }
794                 else
795                         {
796                         prefix = "file:";
797                         }
798
799                 work = cmd_list;
800                 while (work)
801                         {
802                         FileData *fd;
803                         gchar *text;
804
805                         fd = work->data;
806                         work = work->next;
807
808                         text = g_strconcat(prefix, fd->path, NULL);
809                         remote_client_send(rc, text);
810                         g_free(text);
811
812                         sent = TRUE;
813                         }
814
815                 if (path && !cmd_list && use_path)
816                         {
817                         gchar *text;
818
819                         text = g_strdup_printf("file:%s", path);
820                         remote_client_send(rc, text);
821                         g_free(text);
822
823                         sent = TRUE;
824                         }
825
826                 work = collection_list;
827                 while (work)
828                         {
829                         const gchar *name;
830                         gchar *text;
831
832                         name = work->data;
833                         work = work->next;
834
835                         text = g_strdup_printf("file:%s", name);
836                         remote_client_send(rc, text);
837                         g_free(text);
838
839                         sent = TRUE;
840                         }
841
842                 if (!started && !sent)
843                         {
844                         remote_client_send(rc, "raise");
845                         }
846                 }
847         else
848                 {
849                 print_term(_("Remote not available\n"));
850                 }
851
852         _exit(0);
853 }
854
855 RemoteConnection *remote_server_init(gchar *path, CollectionData *command_collection)
856 {
857         RemoteConnection *remote_connection = remote_server_open(path);
858         RemoteData *remote_data = g_new(RemoteData, 1);
859         
860         remote_data->command_collection = command_collection;
861
862         remote_server_subscribe(remote_connection, remote_cb, remote_data);
863         return remote_connection;
864 }