summaryrefslogtreecommitdiff
path: root/src/nautilus-network-address-bar.c
blob: ff81bb65b1705e1b571720d925d756f7c4091f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Copyright (C) 2015 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
 * Copyright (C) 2022 António Fernandes <antoniof@gnome.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "nautilus-network-address-bar.h"

#include <glib/gi18n.h>
#include <adwaita.h>

#include "nautilus-recent-servers.h"

struct _NautilusNetworkAddressBar
{
    GtkBox parent_instance;

    GtkWidget *address_entry;
    GtkWidget *connect_button;
    GtkWidget *available_protocols_grid;

    gboolean should_open_location;
    gboolean should_pulse_entry;
    gboolean connecting_to_server;
    guint entry_pulse_timeout_id;

    GCancellable *cancellable;
};

G_DEFINE_FINAL_TYPE (NautilusNetworkAddressBar, nautilus_network_address_bar, GTK_TYPE_BOX)

const char *unsupported_protocols[] =
{
    "afc", "archive", "burn", "computer", "file", "http", "localtest", "obex", "recent", "trash", NULL
};


static void
show_error_message (NautilusNetworkAddressBar *self,
                    const gchar               *primary,
                    const gchar               *secondary)
{
    AdwDialog *dialog;

    dialog = adw_alert_dialog_new (primary, secondary);
    adw_alert_dialog_add_response (ADW_ALERT_DIALOG (dialog), "close", _("_Close"));

    adw_dialog_present (dialog, GTK_WIDGET (self));
}

static void
server_mount_ready_cb (GObject      *source_file,
                       GAsyncResult *res,
                       gpointer      user_data)
{
    g_autoptr (NautilusNetworkAddressBar) self = NAUTILUS_NETWORK_ADDRESS_BAR (user_data);
    gboolean should_show = TRUE;
    g_autoptr (GError) error = NULL;
    GFile *location = G_FILE (source_file);

    g_file_mount_enclosing_volume_finish (location, res, &error);
    if (error != NULL)
    {
        should_show = FALSE;

        if (error->code == G_IO_ERROR_ALREADY_MOUNTED)
        {
            /*
             * Already mounted volume is not a critical error
             * and we can still continue with the operation.
             */
            should_show = TRUE;
        }
        else if (error->domain != G_IO_ERROR ||
                 (error->code != G_IO_ERROR_CANCELLED &&
                  error->code != G_IO_ERROR_FAILED_HANDLED))
        {
            /* if it wasn't cancelled show a dialog */
            show_error_message (self, _("Unable to access location"), error->message);
        }

        /* The operation got cancelled by the user or dispose() and or the error
         *  has been handled already. */
    }

    self->should_pulse_entry = FALSE;
    gtk_entry_set_progress_fraction (GTK_ENTRY (self->address_entry), 0);

    /* Restore from Cancel to Connect */
    gtk_button_set_label (GTK_BUTTON (self->connect_button), _("Con_nect"));
    gtk_widget_set_sensitive (self->address_entry, TRUE);
    self->connecting_to_server = FALSE;

    if (should_show)
    {
        nautilus_add_recent_server (location);

        /*
         * Only clear the entry if it successfully connects to the server.
         * Otherwise, the user would lost the typed address even if it fails
         * to connect.
         */
        gtk_editable_set_text (GTK_EDITABLE (self->address_entry), "");

        if (self->should_open_location)
        {
            g_autofree char *uri_to_open = NULL;
            g_autoptr (GMount) mount = g_file_find_enclosing_mount (location, self->cancellable, NULL);

            /*
             * If the mount is not found at this point, it is probably user-
             * invisible, which happens e.g for smb-browse, but the location
             * should be opened anyway...
             */
            if (mount != NULL)
            {
                g_autoptr (GFile) root = g_mount_get_default_location (mount);

                uri_to_open = g_file_get_uri (root);
            }
            else
            {
                uri_to_open = g_file_get_uri (location);
            }

            gtk_widget_activate_action (GTK_WIDGET (self),
                                        "slot.open-location", "s", uri_to_open);
        }
    }
}

static gboolean
pulse_entry_cb (gpointer user_data)
{
    NautilusNetworkAddressBar *self = NAUTILUS_NETWORK_ADDRESS_BAR (user_data);

    if (self->should_pulse_entry)
    {
        gtk_entry_progress_pulse (GTK_ENTRY (self->address_entry));

        return G_SOURCE_CONTINUE;
    }
    else
    {
        gtk_entry_set_progress_fraction (GTK_ENTRY (self->address_entry), 0);
        self->entry_pulse_timeout_id = 0;

        return G_SOURCE_REMOVE;
    }
}


static void
mount_server (NautilusNetworkAddressBar *self,
              GFile                     *location)
{
    GtkWidget *toplevel = GTK_WIDGET (gtk_widget_get_root (GTK_WIDGET (self)));
    g_autoptr (GMountOperation) operation = gtk_mount_operation_new (GTK_WINDOW (toplevel));

    g_cancellable_cancel (self->cancellable);
    g_clear_object (&self->cancellable);
    /* User cliked when the operation was ongoing, so wanted to cancel it */
    if (self->connecting_to_server)
    {
        return;
    }

    self->cancellable = g_cancellable_new ();

    self->should_pulse_entry = TRUE;
    gtk_entry_set_progress_pulse_step (GTK_ENTRY (self->address_entry), 0.1);
    gtk_entry_set_progress_fraction (GTK_ENTRY (self->address_entry), 0.1);
    /* Allow to cancel the operation */
    gtk_button_set_label (GTK_BUTTON (self->connect_button), _("Cance_l"));
    gtk_widget_set_sensitive (self->address_entry, FALSE);
    self->connecting_to_server = TRUE;

    if (self->entry_pulse_timeout_id == 0)
    {
        self->entry_pulse_timeout_id = g_timeout_add (100, (GSourceFunc) pulse_entry_cb, self);
    }

    g_mount_operation_set_password_save (operation, G_PASSWORD_SAVE_FOR_SESSION);

    /* make sure we keep the view around for as long as we are running */
    g_file_mount_enclosing_volume (location,
                                   0,
                                   operation,
                                   self->cancellable,
                                   server_mount_ready_cb,
                                   g_object_ref (self));
}

static void
on_connect_button_clicked (NautilusNetworkAddressBar *self)
{
    /* Since the 'Connect' button is updated whenever the typed
     * address changes, it is sufficient to check if it's sensitive
     * or not, in order to determine if the given address is valid.
     */
    if (!gtk_widget_get_sensitive (self->connect_button))
    {
        return;
    }

    const char *uri = gtk_editable_get_text (GTK_EDITABLE (self->address_entry));

    if (uri != NULL && uri[0] != '\0')
    {
        g_autoptr (GFile) file = g_file_new_for_commandline_arg (uri);

        self->should_open_location = TRUE;

        mount_server (self, file);
    }
    else
    {
        show_error_message (self, _("Unable to get remote server location"), NULL);
    }
}

static void
on_entry_icon_press (GtkEntry             *entry,
                     GtkEntryIconPosition  icon_pos,
                     gpointer              user_data)
{
    g_return_if_fail (icon_pos == GTK_ENTRY_ICON_SECONDARY);

    gtk_editable_set_text (GTK_EDITABLE (entry), "");
}

static void
on_address_entry_text_changed (NautilusNetworkAddressBar *self)
{
    const char * const *supported_protocols = g_vfs_get_supported_uri_schemes (g_vfs_get_default ());
    const char *address = gtk_editable_get_text (GTK_EDITABLE (self->address_entry));
    g_autofree char *scheme = g_uri_parse_scheme (address);
    gboolean is_empty = (address == NULL || *address == '\0');
    gboolean supported = FALSE;

    if (supported_protocols != NULL && scheme != NULL)
    {
        supported = g_strv_contains (supported_protocols, scheme) &&
                    !g_strv_contains (unsupported_protocols, scheme);
    }

    gtk_entry_set_icon_from_icon_name (GTK_ENTRY (self->address_entry),
                                       GTK_ENTRY_ICON_SECONDARY,
                                       is_empty ? NULL : "edit-clear-symbolic");

    gtk_widget_set_sensitive (self->connect_button, supported);
    if (scheme != NULL && !supported)
    {
        gtk_widget_add_css_class (self->address_entry, "error");
        gtk_accessible_update_state (GTK_ACCESSIBLE (self->address_entry),
                                     GTK_ACCESSIBLE_STATE_INVALID,
                                     GTK_ACCESSIBLE_INVALID_TRUE,
                                     -1);
    }
    else
    {
        gtk_widget_remove_css_class (self->address_entry, "error");
        gtk_accessible_update_state (GTK_ACCESSIBLE (self->address_entry),
                                     GTK_ACCESSIBLE_STATE_INVALID,
                                     GTK_ACCESSIBLE_INVALID_FALSE,
                                     -1);
    }
}

static void
nautilus_network_address_bar_map (GtkWidget *widget)
{
    NautilusNetworkAddressBar *self = NAUTILUS_NETWORK_ADDRESS_BAR (widget);

    gtk_editable_set_text (GTK_EDITABLE (self->address_entry), "");

    GTK_WIDGET_CLASS (nautilus_network_address_bar_parent_class)->map (widget);
}

static void
attach_protocol_row_to_grid (GtkGrid    *grid,
                             const char *protocol_name,
                             const char *protocol_prefix)
{
    GtkWidget *name_label = gtk_label_new (protocol_name);
    GtkWidget *prefix_label = gtk_label_new (protocol_prefix);

    gtk_widget_set_halign (name_label, GTK_ALIGN_START);
    gtk_grid_attach_next_to (grid, name_label, NULL, GTK_POS_BOTTOM, 1, 1);

    gtk_widget_set_halign (prefix_label, GTK_ALIGN_START);
    gtk_grid_attach_next_to (grid, prefix_label, name_label, GTK_POS_RIGHT, 1, 1);
}

static void
populate_available_protocols_grid (GtkGrid *grid)
{
    const char * const *supported_protocols = g_vfs_get_supported_uri_schemes (g_vfs_get_default ());
    gboolean has_any = FALSE;

    if (g_strv_contains (supported_protocols, "afp"))
    {
        attach_protocol_row_to_grid (grid, _("AppleTalk"), "afp://");
        has_any = TRUE;
    }

    if (g_strv_contains (supported_protocols, "ftp"))
    {
        attach_protocol_row_to_grid (grid, _("File Transfer Protocol"),
                                     /* Translators: do not translate ftp:// and ftps:// */
                                     _("ftp:// or ftps://"));
        has_any = TRUE;
    }

    if (g_strv_contains (supported_protocols, "nfs"))
    {
        attach_protocol_row_to_grid (grid, _("Network File System"), "nfs://");
        has_any = TRUE;
    }

    if (g_strv_contains (supported_protocols, "smb"))
    {
        attach_protocol_row_to_grid (grid, _("Samba"), "smb://");
        has_any = TRUE;
    }

    if (g_strv_contains (supported_protocols, "ssh"))
    {
        attach_protocol_row_to_grid (grid, _("SSH File Transfer Protocol"),
                                     /* Translators: do not translate sftp:// and ssh:// */
                                     _("sftp:// or ssh://"));
        has_any = TRUE;
    }

    if (g_strv_contains (supported_protocols, "dav"))
    {
        attach_protocol_row_to_grid (grid, _("WebDAV"),
                                     /* Translators: do not translate dav:// and davs:// */
                                     _("dav:// or davs://"));
        has_any = TRUE;
    }

    if (!has_any)
    {
        gtk_widget_set_visible (GTK_WIDGET (grid), FALSE);
    }
}

static void
nautilus_network_address_bar_dispose (GObject *object)
{
    NautilusNetworkAddressBar *self = NAUTILUS_NETWORK_ADDRESS_BAR (object);

    g_cancellable_cancel (self->cancellable);
    g_clear_object (&self->cancellable);
    g_clear_handle_id (&self->entry_pulse_timeout_id, g_source_remove);

    G_OBJECT_CLASS (nautilus_network_address_bar_parent_class)->dispose (object);
}

static void
nautilus_network_address_bar_finalize (GObject *object)
{
    G_OBJECT_CLASS (nautilus_network_address_bar_parent_class)->finalize (object);
}

static void
nautilus_network_address_bar_init (NautilusNetworkAddressBar *self)
{
    gtk_widget_init_template (GTK_WIDGET (self));

    g_signal_connect (self->address_entry, "icon-press", G_CALLBACK (on_entry_icon_press), NULL);

    populate_available_protocols_grid (GTK_GRID (self->available_protocols_grid));
}

static void
nautilus_network_address_bar_class_init (NautilusNetworkAddressBarClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    object_class->finalize = nautilus_network_address_bar_finalize;
    object_class->dispose = nautilus_network_address_bar_dispose;

    widget_class->map = nautilus_network_address_bar_map;

    /* Bind class to template */
    gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/nautilus/ui/nautilus-network-address-bar.ui");

    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkAddressBar, address_entry);
    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkAddressBar, connect_button);
    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkAddressBar, available_protocols_grid);

    gtk_widget_class_bind_template_callback (widget_class, on_address_entry_text_changed);
    gtk_widget_class_bind_template_callback (widget_class, on_connect_button_clicked);
}

NautilusNetworkAddressBar *
nautilus_network_address_bar_new (void)
{
    return g_object_new (NAUTILUS_TYPE_NETWORK_ADDRESS_BAR, NULL);
}