]> git.meshlink.io Git - catta/blob - avahi-ui/bssh.c
add i18n support
[catta] / avahi-ui / bssh.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30
31 #include <gtk/gtk.h>
32
33 #include <avahi-client/client.h>
34 #include <avahi-common/strlst.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/domain.h>
37 #include <avahi-common/i18n.h>
38
39 #include "avahi-ui.h"
40
41 int main(int argc, char*argv[]) {
42     GtkWidget *d;
43
44     avahi_init_i18n();
45
46     gtk_init(&argc, &argv);
47
48     if (g_str_has_suffix(argv[0], "bshell")) {
49         d = aui_service_dialog_new(_("Choose Shell Server"), NULL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_CONNECT, GTK_RESPONSE_ACCEPT, NULL);
50         aui_service_dialog_set_browse_service_types(AUI_SERVICE_DIALOG(d), "_rfb._tcp", "_ssh._tcp", NULL);
51         aui_service_dialog_set_service_type_name(AUI_SERVICE_DIALOG(d), "_rfb._tcp", _("Desktop"));
52         aui_service_dialog_set_service_type_name(AUI_SERVICE_DIALOG(d), "_ssh._tcp", _("Terminal"));
53     } else if (g_str_has_suffix(argv[0], "bvnc")) {
54         d = aui_service_dialog_new(_("Choose VNC server"), NULL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_CONNECT, GTK_RESPONSE_ACCEPT, NULL);
55         aui_service_dialog_set_browse_service_types(AUI_SERVICE_DIALOG(d), "_rfb._tcp", NULL);
56     } else {
57         d = aui_service_dialog_new(_("Choose SSH server"), NULL, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_CONNECT, GTK_RESPONSE_ACCEPT, NULL);
58         aui_service_dialog_set_browse_service_types(AUI_SERVICE_DIALOG(d), "_ssh._tcp", NULL);
59     }
60
61     aui_service_dialog_set_resolve_service(AUI_SERVICE_DIALOG(d), TRUE);
62     aui_service_dialog_set_resolve_host_name(AUI_SERVICE_DIALOG(d), !avahi_nss_support());
63
64     gtk_window_present(GTK_WINDOW(d));
65
66     if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_ACCEPT) {
67         char a[AVAHI_ADDRESS_STR_MAX], *u = NULL, *n = NULL;
68         char *h = NULL, *t = NULL;
69         const AvahiStringList *txt;
70
71         t = g_strdup(aui_service_dialog_get_service_type(AUI_SERVICE_DIALOG(d)));
72         n = g_strdup(aui_service_dialog_get_service_name(AUI_SERVICE_DIALOG(d)));
73
74         if (avahi_nss_support())
75             h = g_strdup(aui_service_dialog_get_host_name(AUI_SERVICE_DIALOG(d)));
76         else
77             h = g_strdup(avahi_address_snprint(a, sizeof(a), aui_service_dialog_get_address(AUI_SERVICE_DIALOG(d))));
78
79         g_print(_("Connecting to '%s' ...\n"), n);
80
81         if (avahi_domain_equal(t, "_rfb._tcp")) {
82             char p[AVAHI_DOMAIN_NAME_MAX+16];
83             snprintf(p, sizeof(p), "%s:%u", h, aui_service_dialog_get_port(AUI_SERVICE_DIALOG(d))-5900);
84
85             gtk_widget_destroy(d);
86
87             g_print("xvncviewer %s\n", p);
88             execlp("xvncviewer", "xvncviewer", p, NULL);
89
90         } else {
91             char p[16];
92
93             snprintf(p, sizeof(p), "%u", aui_service_dialog_get_port(AUI_SERVICE_DIALOG(d)));
94
95             for (txt = aui_service_dialog_get_txt_data(AUI_SERVICE_DIALOG(d)); txt; txt = txt->next) {
96                 char *key, *value;
97
98                 if (avahi_string_list_get_pair((AvahiStringList*) txt, &key, &value, NULL) < 0)
99                     break;
100
101                 if (strcmp(key, "u") == 0)
102                     u = g_strdup(value);
103
104                 avahi_free(key);
105                 avahi_free(value);
106             }
107
108             gtk_widget_destroy(d);
109
110             if (u) {
111                 g_print("ssh -p %s -l %s %s\n", p, u, h);
112
113                 if (isatty(0) || !getenv("DISPLAY"))
114                     execlp("ssh", "ssh", "-p", p, "-l", u, h, NULL);
115                 else {
116                     execlp("x-terminal-emulator", "x-terminal-emulator", "-T", n, "-e", "ssh", "-p", p, "-l", u, h, NULL);
117                     execlp("gnome-terminal", "gnome-terminal", "-t", n, "-x", "ssh", "-p", p, "-l", u, h, NULL);
118                     execlp("xterm", "xterm", "-T", n, "-e", "ssh", "-p", p, "-l", u, h, NULL);
119                 }
120             } else {
121                 g_print("ssh -p %s %s\n", p, h);
122
123                 if (isatty(0) || !getenv("DISPLAY"))
124                     execlp("ssh", "ssh", "-p", p, h, NULL);
125                 else {
126                     execlp("x-terminal-emulator", "x-terminal-emulator", "-T", n, "-e", "ssh", "-p", p, h, NULL);
127                     execlp("gnome-terminal", "gnome-terminal", "-t", n, "-x", "ssh", "-p", p, h, NULL);
128                     execlp("xterm", "xterm", "-T", n, "-e", "ssh", "-p", p, h, NULL);
129                 }
130             }
131         }
132
133         g_warning(_("execlp() failed: %s\n"), strerror(errno));
134
135         g_free(h);
136         g_free(u);
137         g_free(t);
138         g_free(n);
139
140     } else {
141         gtk_widget_destroy(d);
142
143         g_print(_("Canceled.\n"));
144     }
145
146     return 1;
147 }