summaryrefslogtreecommitdiff
path: root/src/nautilus-network-cell.c
blob: a185781095eb4693a053e4ce6fbb5f8b2d8c40ad (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
/*
 * Copyright (C) 2024 The GNOME project contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "nautilus-network-cell.h"

#include <glib/gi18n.h>

#include "nautilus-directory.h"
#include "nautilus-file.h"
#include "nautilus-file-utilities.h"
#include "nautilus-scheme.h"
#include "nautilus-view-item.h"

struct _NautilusNetworkCell
{
    NautilusViewCell parent_instance;

    GSignalGroup *item_signal_group;

    GtkWidget *top_child;
    GtkWidget *icon;
    GtkWidget *target_uri;
    GtkWidget *unmount_button;
};

G_DEFINE_FINAL_TYPE (NautilusNetworkCell, nautilus_network_cell, NAUTILUS_TYPE_VIEW_CELL)

static void
update_labels (NautilusNetworkCell *self)
{
    g_autoptr (NautilusViewItem) item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));

    g_return_if_fail (item != NULL);

    NautilusFile *file = nautilus_view_item_get_file (item);
    g_autofree char *target_uri = nautilus_file_get_activation_uri (file);

    if (g_str_has_prefix (target_uri, SCHEME_COMPUTER ":///"))
    {
        /* Online accounts do not currently have a target URI. */
        g_set_str (&target_uri, _("Online Account"));
    }

    gtk_label_set_label (GTK_LABEL (self->target_uri), target_uri);
}

static void
update_icon (NautilusNetworkCell *self)
{
    g_autoptr (NautilusViewItem) item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));

    g_return_if_fail (item != NULL);

    g_autoptr (GIcon) icon = nautilus_file_get_gicon (nautilus_view_item_get_file (item),
                                                      NAUTILUS_FILE_ICON_FLAGS_NONE);

    gtk_image_set_from_gicon (GTK_IMAGE (self->icon), icon);
}

static void
on_file_changed (NautilusNetworkCell *self)
{
    g_autoptr (NautilusViewItem) item = nautilus_view_cell_get_item (NAUTILUS_VIEW_CELL (self));

    g_return_if_fail (item != NULL);

    update_icon (self);
    update_labels (self);

    NautilusFile *file = nautilus_view_item_get_file (item);

    gtk_widget_set_visible (self->unmount_button, nautilus_file_can_unmount (file));
}

static void
on_unmount_clicked (NautilusNetworkCell *self)
{
    /* Select item first, because view.unmount-volume acts on selection. */
    gtk_widget_activate_action (GTK_WIDGET (self), "listitem.select", "(bb)", FALSE, FALSE);
    gtk_widget_activate_action (GTK_WIDGET (self), "view.unmount-volume", NULL);
}

static void
nautilus_network_cell_init (NautilusNetworkCell *self)
{
    gtk_widget_init_template (GTK_WIDGET (self));

    /* Connect automatically to an item. */
    self->item_signal_group = g_signal_group_new (NAUTILUS_TYPE_VIEW_ITEM);
    g_signal_group_connect_swapped (self->item_signal_group, "notify::loading",
                                    (GCallback) on_file_changed, self);
    g_signal_group_connect_swapped (self->item_signal_group, "file-changed",
                                    (GCallback) on_file_changed, self);
    g_signal_connect_object (self->item_signal_group, "bind",
                             (GCallback) on_file_changed, self,
                             G_CONNECT_SWAPPED);

    g_object_bind_property (self, "item",
                            self->item_signal_group, "target",
                            G_BINDING_SYNC_CREATE);
}

static void
nautilus_network_cell_dispose (GObject *object)
{
    NautilusNetworkCell *self = (NautilusNetworkCell *) object;

    gtk_widget_dispose_template (GTK_WIDGET (self), NAUTILUS_TYPE_NETWORK_CELL);

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

static void
nautilus_network_cell_finalize (GObject *object)
{
    NautilusNetworkCell *self = (NautilusNetworkCell *) object;

    g_clear_object (&self->item_signal_group);
    G_OBJECT_CLASS (nautilus_network_cell_parent_class)->finalize (object);
}

static void
nautilus_network_cell_class_init (NautilusNetworkCellClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    object_class->dispose = nautilus_network_cell_dispose;
    object_class->finalize = nautilus_network_cell_finalize;

    gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/nautilus/ui/nautilus-network-cell.ui");

    /* Needs to add the direct child of the template widget to dispose it since
     * a plain GtkWidget doesn't dispose it's child automatically. */
    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkCell, top_child);
    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkCell, icon);
    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkCell, target_uri);
    gtk_widget_class_bind_template_child (widget_class, NautilusNetworkCell, unmount_button);
    gtk_widget_class_bind_template_callback (widget_class, on_unmount_clicked);

    gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
}

NautilusViewCell *
nautilus_network_cell_new (NautilusListBase *view)
{
    return NAUTILUS_VIEW_CELL (g_object_new (NAUTILUS_TYPE_NETWORK_CELL,
                                             "view", view,
                                             NULL));
}