summaryrefslogtreecommitdiff
path: root/src-ng/nautilus-file-changes.c
blob: 0f9752fab88e23275107adc383732580db473d70 (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
/* Copyright (C) 2017 Ernestas Kulik <ernestask@gnome.org>
 *
 * This file is part of Nautilus.
 *
 * Nautilus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Nautilus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nautilus.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "nautilus-file-changes.h"

#include "nautilus-file.h"
#include "nautilus-signal-utilities.h"

typedef struct
{
    NautilusFileChange type;
    GFile *location;
} Change;

typedef struct
{
    NautilusFileChange type;
    GFile *location_from;
    GFile *location_to;
} MoveChange;

static void move_change_free (MoveChange *change);

G_DEFINE_AUTOPTR_CLEANUP_FUNC (MoveChange, move_change_free)

static guint source = 0;
static GMutex source_mutex;

static void
move_change_free (MoveChange *change)
{
    g_clear_object (&change->location_to);
    g_clear_object (&change->location_from);
    g_free (change);
}

static gpointer
init_default_queue (gpointer data)
{
    return g_async_queue_new ();
}

static GAsyncQueue *
get_default_queue (void)
{
    static GOnce once = G_ONCE_INIT;

    g_once (&once, init_default_queue, NULL);

    return once.retval;
}

static gboolean
emit_signals (gpointer user_data)
{
    GAsyncQueue *queue;
    Change *change;

    queue = user_data;

    g_async_queue_lock (queue);

    while ((change = g_async_queue_try_pop_unlocked (queue)) != NULL)
    {
        g_autoptr (NautilusFile) file = NULL;
        g_autoptr (NautilusFile) parent = NULL;

        file = nautilus_file_new (change->location);
        if (file == NULL)
        {
            continue;
        }
        parent = nautilus_file_get_parent (file);

        switch (change->type)
        {
            case NAUTILUS_FILE_CHANGE_RENAMED:
            {
                g_autoptr (MoveChange) move_change = NULL;

                move_change = (MoveChange *) change;

                nautilus_emit_signal_in_main_context_by_name (file,
                                                              NULL,
                                                              "renamed",
                                                              move_change->location_to);

                if (parent == NULL)
                {
                    break;
                }

                nautilus_emit_signal_in_main_context_by_name (parent,
                                                              NULL,
                                                              "children-changed");
            }
            break;
        }
    }

    g_async_queue_unlock (queue);

    g_mutex_lock (&source_mutex);
    source = 0;
    g_mutex_unlock (&source_mutex);

    return G_SOURCE_REMOVE;
}

static void
schedule_signal_emission (void)
{
    g_mutex_lock (&source_mutex);

    if (source == 0)
    {
        source = g_timeout_add (100, emit_signals, get_default_queue ());
    }
    else
    {
        g_source_remove (source);
    }

    g_mutex_unlock (&source_mutex);
}

static void
notify_file_moved_or_renamed (GFile    *from,
                              GFile    *to,
                              gboolean  move_is_rename)
{
    MoveChange *change;
    GAsyncQueue *queue;

    change = g_new0 (MoveChange, 1);
    queue = get_default_queue ();

    change->type = move_is_rename? NAUTILUS_FILE_CHANGE_RENAMED
                                 : NAUTILUS_FILE_CHANGE_MOVED;
    change->location_from = g_object_ref (from);
    change->location_to = g_object_ref (to);

    g_async_queue_push (queue, change);

    schedule_signal_emission ();
}

void
nautilus_notify_file_renamed (GFile *location,
                              GFile *new_location)
{
    notify_file_moved_or_renamed (location, new_location, TRUE);
}