Commit from LXDE Pootle server by user sotrud_nik.: 217 of 295 strings translated...
[lxde/pcmanfm.git] / src / connect-server.c
1 /*
2 * connect-server.c
3 *
4 * This file is a part of the PCManFM project.
5 *
6 * Copyright 2013 Andriy Grytsenko (LStranger) <andrej@rep.kiev.ua>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23
24 #include "connect-server.h"
25
26 #include <string.h>
27
28 #include "main-win.h"
29
30 typedef struct
31 {
32 GtkWindow *dlg;
33 GtkWidget *ok;
34 GtkComboBox *server_type;
35 GtkEntry *server_host;
36 GtkSpinButton *server_port;
37 GtkEntry *server_path;
38 GtkToggleButton *user_anonymous;
39 GtkToggleButton *user_user;
40 GtkEntry *login_entry;
41 } ConnectDlg;
42
43 static void on_response(GtkDialog *dialog, gint resp, ConnectDlg *dlg)
44 {
45 GtkWindow *parent = gtk_window_get_transient_for(dlg->dlg);
46 GString *str;
47 GtkTreeIter iter;
48 char *scheme = NULL;
49 const char *text;
50 FmPath *path;
51
52 if (resp == GTK_RESPONSE_OK && gtk_combo_box_get_active_iter(dlg->server_type, &iter))
53 gtk_tree_model_get(gtk_combo_box_get_model(dlg->server_type), &iter, 1, &scheme, -1);
54 if (scheme)
55 {
56 /* make an URI from the data */
57 str = g_string_new(scheme);
58 g_free(scheme);
59 g_string_append(str, "://");
60 if (gtk_toggle_button_get_active(dlg->user_user))
61 g_string_append_printf(str, "%s@", gtk_entry_get_text(dlg->login_entry));
62 g_string_append_printf(str, "%s:%d", gtk_entry_get_text(dlg->server_host),
63 (int)gtk_spin_button_get_value(dlg->server_port));
64 text = gtk_entry_get_text(dlg->server_path);
65 if (text[0] != '/')
66 g_string_append_c(str, '/');
67 g_string_append(str, text);
68 path = fm_path_new_for_uri(str->str);
69 /* g_debug("created URI: %s",str->str); */
70 g_string_free(str, TRUE);
71 /* create new tab for the URI */
72 if (parent)
73 fm_main_win_add_tab(FM_MAIN_WIN(parent), path);
74 else
75 fm_main_win_add_win(NULL, path);
76 fm_path_unref(path);
77 }
78 gtk_widget_destroy(GTK_WIDGET(dlg->dlg));
79 g_slice_free(ConnectDlg, dlg);
80 pcmanfm_unref();
81 }
82
83 static void on_server_type(GtkComboBox *box, ConnectDlg *dlg)
84 {
85 GtkTreeIter iter;
86 char *scheme;
87
88 if (!gtk_combo_box_get_active_iter(box, &iter))
89 return;
90 gtk_tree_model_get(gtk_combo_box_get_model(box), &iter, 1, &scheme, -1);
91 if (scheme == NULL)
92 return;
93 if (strcmp(scheme, "sftp") == 0)
94 {
95 /* disable anonymous */
96 gtk_widget_set_sensitive(GTK_WIDGET(dlg->user_anonymous), FALSE);
97 gtk_toggle_button_set_active(dlg->user_user, TRUE);
98 /* set user to user's login */
99 gtk_entry_set_text(dlg->login_entry, g_get_user_name());
100 /* set port to 22 */
101 gtk_spin_button_set_value(dlg->server_port, 22.0);
102 }
103 else if (strcmp(scheme, "ftp") == 0)
104 {
105 /* enable anonymous and set default */
106 gtk_widget_set_sensitive(GTK_WIDGET(dlg->user_anonymous), TRUE);
107 gtk_toggle_button_set_active(dlg->user_anonymous, TRUE);
108 /* set port to 21 */
109 gtk_spin_button_set_value(dlg->server_port, 21.0);
110 }
111 else if (strcmp(scheme, "dav") == 0)
112 {
113 /* enable anonymous */
114 gtk_widget_set_sensitive(GTK_WIDGET(dlg->user_anonymous), TRUE);
115 /* set port to 80 */
116 gtk_spin_button_set_value(dlg->server_port, 80.0);
117 }
118 g_free(scheme);
119 }
120
121 static void on_server_host(GtkEditable *editable, ConnectDlg *dlg)
122 {
123 const char *text = gtk_entry_get_text(GTK_ENTRY(editable));
124 gboolean ready;
125
126 /* disable OK if entry is empty */
127 if (!text || !text[0])
128 ready = FALSE;
129 /* disable OK if type not selected */
130 else if (gtk_combo_box_get_active(dlg->server_type) == -1)
131 ready = FALSE;
132 /* enable OK if anonymous is enabled */
133 else if (gtk_toggle_button_get_active(dlg->user_anonymous))
134 ready = TRUE;
135 /* enable OK if login isn't empty */
136 else
137 {
138 text = gtk_entry_get_text(dlg->login_entry);
139 ready = (text && text[0] != '\0');
140 }
141 gtk_widget_set_sensitive(dlg->ok, ready);
142 }
143
144 static void on_user_type(GtkToggleButton *user_anonymous, ConnectDlg *dlg)
145 {
146 gboolean anonymous = gtk_toggle_button_get_active(user_anonymous);
147 gboolean ready;
148 const char *text;
149
150 if (anonymous)
151 {
152 /* disable login_entry */
153 gtk_widget_set_sensitive(GTK_WIDGET(dlg->login_entry), FALSE);
154 /* enable OK if host isn't empty and type is selected */
155 text = gtk_entry_get_text(dlg->server_host);
156 if (!text || !text[0])
157 ready = FALSE;
158 else
159 ready = (gtk_combo_box_get_active(dlg->server_type) != -1);
160 }
161 else
162 {
163 /* enable login_entry */
164 gtk_widget_set_sensitive(GTK_WIDGET(dlg->login_entry), TRUE);
165 /* enable OK if both host and login aren't empty and type is selected */
166 text = gtk_entry_get_text(dlg->server_host);
167 if (!text || !text[0])
168 ready = FALSE;
169 else if (gtk_combo_box_get_active(dlg->server_type) == -1)
170 ready = FALSE;
171 else
172 {
173 text = gtk_entry_get_text(dlg->login_entry);
174 if (!text || !text[0])
175 ready = FALSE;
176 else
177 ready = TRUE;
178 }
179 }
180 gtk_widget_set_sensitive(dlg->ok, ready);
181 }
182
183 void open_connect_dialog(GtkWindow *parent)
184 {
185 GtkBuilder *builder = gtk_builder_new();
186 ConnectDlg *dlg = g_slice_new(ConnectDlg);
187
188 gtk_builder_add_from_file(builder, PACKAGE_UI_DIR "/connect.ui", NULL);
189 dlg->dlg = GTK_WINDOW(gtk_builder_get_object(builder, "dlg"));
190 g_signal_connect(dlg->dlg, "response", G_CALLBACK(on_response), dlg);
191 dlg->ok = GTK_WIDGET(gtk_builder_get_object(builder, "ok"));
192 gtk_widget_set_sensitive((GtkWidget*)dlg->ok, FALSE);
193 dlg->server_type = GTK_COMBO_BOX(gtk_builder_get_object(builder, "server_type"));
194 g_signal_connect(dlg->server_type, "changed", G_CALLBACK(on_server_type), dlg);
195 dlg->server_host = GTK_ENTRY(gtk_builder_get_object(builder, "server_host"));
196 g_signal_connect(dlg->server_host, "changed", G_CALLBACK(on_server_host), dlg);
197 dlg->server_port = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "server_port"));
198 dlg->server_path = GTK_ENTRY(gtk_builder_get_object(builder, "server_path"));
199 dlg->user_anonymous = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "user_anonymous"));
200 g_signal_connect(dlg->user_anonymous, "toggled", G_CALLBACK(on_user_type), dlg);
201 dlg->user_user = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "user_user"));
202 dlg->login_entry = GTK_ENTRY(gtk_builder_get_object(builder, "login_entry"));
203 g_object_unref(builder);
204
205 pcmanfm_ref();
206 if(parent)
207 gtk_window_set_transient_for(dlg->dlg, parent);
208 gtk_window_present(dlg->dlg);
209 }