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
|
/* GTK - The GIMP Toolkit
* gtktrashmonitor.h: Monitor the trash:/// folder to see if there is trash or not
* Copyright (C) 2011 Suse
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Authors: Federico Mena Quintero <federico@gnome.org>
*/
#include "config.h"
#include "gtkprivate.h"
#include "gtkmarshalers.h"
#include "gtktrashmonitorprivate.h"
#define UPDATE_RATE_SECONDS 1
struct _GtkTrashMonitor
{
GObject parent;
GFileMonitor *file_monitor;
gulong file_monitor_changed_id;
gboolean pending;
int timeout_id;
guint has_trash : 1;
};
struct _GtkTrashMonitorClass
{
GObjectClass parent_class;
void (* trash_state_changed) (GtkTrashMonitor *monitor);
};
enum {
TRASH_STATE_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
G_DEFINE_TYPE (GtkTrashMonitor, _gtk_trash_monitor, G_TYPE_OBJECT)
static GtkTrashMonitor *the_trash_monitor;
#define ICON_NAME_TRASH_EMPTY "user-trash-symbolic"
#define ICON_NAME_TRASH_FULL "user-trash-full-symbolic"
static void
gtk_trash_monitor_dispose (GObject *object)
{
GtkTrashMonitor *monitor;
monitor = GTK_TRASH_MONITOR (object);
if (monitor->file_monitor)
{
g_signal_handler_disconnect (monitor->file_monitor, monitor->file_monitor_changed_id);
monitor->file_monitor_changed_id = 0;
g_clear_object (&monitor->file_monitor);
}
if (monitor->timeout_id > 0)
g_source_remove (monitor->timeout_id);
monitor->timeout_id = 0;
G_OBJECT_CLASS (_gtk_trash_monitor_parent_class)->dispose (object);
}
static void
_gtk_trash_monitor_class_init (GtkTrashMonitorClass *class)
{
GObjectClass *gobject_class;
gobject_class = (GObjectClass *) class;
gobject_class->dispose = gtk_trash_monitor_dispose;
signals[TRASH_STATE_CHANGED] =
g_signal_new (I_("trash-state-changed"),
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkTrashMonitorClass, trash_state_changed),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
}
/* Updates the internal has_trash flag and emits the "trash-state-changed" signal */
static void
update_has_trash_and_notify (GtkTrashMonitor *monitor,
gboolean has_trash)
{
if (monitor->has_trash == !!has_trash)
return;
monitor->has_trash = !!has_trash;
g_signal_emit (monitor, signals[TRASH_STATE_CHANGED], 0);
}
/* Use G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT since we only want to know whether the
* trash is empty or not, not access its children. This is available for the
* trash backend since it uses a cache. In this way we prevent flooding the
* trash backend with enumeration requests when trashing > 1000 files
*/
static void
trash_query_info_cb (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
GtkTrashMonitor *monitor = GTK_TRASH_MONITOR (user_data);
GFileInfo *info;
guint32 item_count;
gboolean has_trash = FALSE;
info = g_file_query_info_finish (G_FILE (source), result, NULL);
if (info != NULL)
{
item_count = g_file_info_get_attribute_uint32 (info,
G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT);
has_trash = item_count > 0;
g_object_unref (info);
}
update_has_trash_and_notify (monitor, has_trash);
g_object_unref (monitor); /* was reffed in recompute_trash_state() */
}
static void recompute_trash_state (GtkTrashMonitor *monitor);
static gboolean
recompute_trash_state_cb (gpointer data)
{
GtkTrashMonitor *monitor = data;
monitor->timeout_id = 0;
if (monitor->pending)
{
monitor->pending = FALSE;
recompute_trash_state (monitor);
}
return G_SOURCE_REMOVE;
}
/* Asynchronously recomputes whether there is trash or not */
static void
recompute_trash_state (GtkTrashMonitor *monitor)
{
GFile *file;
/* Rate limit the updates to not flood the gvfsd-trash when too many changes
* happened in a short time.
*/
if (monitor->timeout_id > 0)
{
monitor->pending = TRUE;
return;
}
file = g_file_new_for_uri ("trash:///");
g_file_query_info_async (file,
G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_DEFAULT, NULL,
trash_query_info_cb, g_object_ref (monitor));
monitor->timeout_id = g_timeout_add_seconds (UPDATE_RATE_SECONDS,
recompute_trash_state_cb,
monitor);
gdk_source_set_static_name_by_id (monitor->timeout_id, "[gtk] recompute_trash_state_cb");
g_object_unref (file);
}
/* Callback used when the "trash:///" file monitor changes; we just recompute the trash state
* whenever something happens.
*/
static void
file_monitor_changed_cb (GFileMonitor *file_monitor,
GFile *child,
GFile *other_file,
GFileMonitorEvent event_type,
GtkTrashMonitor *monitor)
{
recompute_trash_state (monitor);
}
static void
_gtk_trash_monitor_init (GtkTrashMonitor *monitor)
{
GFile *file;
file = g_file_new_for_uri ("trash:///");
monitor->file_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
monitor->pending = FALSE;
monitor->timeout_id = 0;
g_object_unref (file);
if (monitor->file_monitor)
monitor->file_monitor_changed_id = g_signal_connect (monitor->file_monitor, "changed",
G_CALLBACK (file_monitor_changed_cb), monitor);
recompute_trash_state (monitor);
}
/*
* _gtk_trash_monitor_get:
*
* Returns: (transfer full): a new reference to the singleton
* GtkTrashMonitor object. Be sure to call g_object_unref() on it when you are
* done with the trash monitor.
*/
GtkTrashMonitor *
_gtk_trash_monitor_get (void)
{
if (the_trash_monitor != NULL)
{
g_object_ref (the_trash_monitor);
}
else
{
the_trash_monitor = g_object_new (GTK_TYPE_TRASH_MONITOR, NULL);
g_object_add_weak_pointer (G_OBJECT (the_trash_monitor), (gpointer *) &the_trash_monitor);
}
return the_trash_monitor;
}
/*
* _gtk_trash_monitor_get_icon:
* @monitor: a GtkTrashMonitor
*
* Returns: (transfer full): the GIcon that should be used to represent
* the state of the trash folder on screen, based on whether there is trash or
* not.
*/
GIcon *
_gtk_trash_monitor_get_icon (GtkTrashMonitor *monitor)
{
const char *icon_name;
g_return_val_if_fail (GTK_IS_TRASH_MONITOR (monitor), NULL);
if (monitor->has_trash)
icon_name = ICON_NAME_TRASH_FULL;
else
icon_name = ICON_NAME_TRASH_EMPTY;
return g_themed_icon_new (icon_name);
}
/*
* _gtk_trash_monitor_get_has_trash:
* @monitor: a GtkTrashMonitor
*
* Returns: #TRUE if there is trash in the trash:/// folder, or #FALSE otherwise.
*/
gboolean
_gtk_trash_monitor_get_has_trash (GtkTrashMonitor *monitor)
{
g_return_val_if_fail (GTK_IS_TRASH_MONITOR (monitor), FALSE);
return monitor->has_trash;
}
|