1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 
3 /*
4    caja-trash-monitor.c: Caja trash state watcher.
5 
6    Copyright (C) 2000, 2001 Eazel, Inc.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (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 GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public
19    License along with this program; if not, write to the
20    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21    Boston, MA 02110-1301, USA.
22 
23    Author: Pavel Cisler <pavel@eazel.com>
24 */
25 
26 #include <config.h>
27 
28 #include <gio/gio.h>
29 #include <string.h>
30 
31 #include <eel/eel-debug.h>
32 
33 #include "caja-trash-monitor.h"
34 #include "caja-directory-notify.h"
35 #include "caja-directory.h"
36 #include "caja-file-attributes.h"
37 #include "caja-icon-names.h"
38 
39 struct _CajaTrashMonitorPrivate
40 {
41     gboolean empty;
42     GIcon *icon;
43     GFileMonitor *file_monitor;
44 };
45 
46 enum
47 {
48     TRASH_STATE_CHANGED,
49     LAST_SIGNAL
50 };
51 
52 static guint signals[LAST_SIGNAL] = { 0 };
53 static CajaTrashMonitor *caja_trash_monitor = NULL;
54 
G_DEFINE_TYPE_WITH_PRIVATE(CajaTrashMonitor,caja_trash_monitor,G_TYPE_OBJECT)55 G_DEFINE_TYPE_WITH_PRIVATE (CajaTrashMonitor, caja_trash_monitor, G_TYPE_OBJECT)
56 
57 static void
58 caja_trash_monitor_finalize (GObject *object)
59 {
60     CajaTrashMonitor *trash_monitor;
61 
62     trash_monitor = CAJA_TRASH_MONITOR (object);
63 
64     if (trash_monitor->details->icon)
65     {
66         g_object_unref (trash_monitor->details->icon);
67     }
68     if (trash_monitor->details->file_monitor)
69     {
70         g_object_unref (trash_monitor->details->file_monitor);
71     }
72 
73     G_OBJECT_CLASS (caja_trash_monitor_parent_class)->finalize (object);
74 }
75 
76 static void
caja_trash_monitor_class_init(CajaTrashMonitorClass * klass)77 caja_trash_monitor_class_init (CajaTrashMonitorClass *klass)
78 {
79     GObjectClass *object_class;
80 
81     object_class = G_OBJECT_CLASS (klass);
82 
83     object_class->finalize = caja_trash_monitor_finalize;
84 
85     signals[TRASH_STATE_CHANGED] = g_signal_new
86                                    ("trash_state_changed",
87                                     G_TYPE_FROM_CLASS (object_class),
88                                     G_SIGNAL_RUN_LAST,
89                                     G_STRUCT_OFFSET (CajaTrashMonitorClass, trash_state_changed),
90                                     NULL, NULL,
91                                     g_cclosure_marshal_VOID__BOOLEAN,
92                                     G_TYPE_NONE, 1,
93                                     G_TYPE_BOOLEAN);
94 }
95 
96 static void
update_info_cb(GObject * source_object,GAsyncResult * res,gpointer user_data)97 update_info_cb (GObject *source_object,
98                 GAsyncResult *res,
99                 gpointer user_data)
100 {
101     CajaTrashMonitor *trash_monitor;
102     GFileInfo *info;
103     gboolean empty;
104 
105     trash_monitor = CAJA_TRASH_MONITOR (user_data);
106 
107     info = g_file_query_info_finish (G_FILE (source_object),
108                                      res, NULL);
109 
110     if (info != NULL)
111     {
112         GIcon *icon;
113 
114         icon = g_file_info_get_icon (info);
115 
116         if (icon)
117         {
118             g_object_unref (trash_monitor->details->icon);
119             trash_monitor->details->icon = g_object_ref (icon);
120             empty = TRUE;
121             if (G_IS_THEMED_ICON (icon))
122             {
123                 const char * const *names;
124                 int i;
125 
126                 names = g_themed_icon_get_names (G_THEMED_ICON (icon));
127                 for (i = 0; names[i] != NULL; i++)
128                 {
129                     if (strcmp (names[i], CAJA_ICON_TRASH_FULL) == 0)
130                     {
131                         empty = FALSE;
132                         break;
133                     }
134                 }
135             }
136             if (trash_monitor->details->empty != empty)
137             {
138                 trash_monitor->details->empty = empty;
139 
140                 /* trash got empty or full, notify everyone who cares */
141                 g_signal_emit (trash_monitor,
142                                signals[TRASH_STATE_CHANGED], 0,
143                                trash_monitor->details->empty);
144             }
145         }
146         g_object_unref (info);
147     }
148 
149     g_object_unref (trash_monitor);
150 }
151 
152 static void
schedule_update_info(CajaTrashMonitor * trash_monitor)153 schedule_update_info (CajaTrashMonitor *trash_monitor)
154 {
155     GFile *location;
156 
157     location = g_file_new_for_uri ("trash:///");
158 
159     g_file_query_info_async (location,
160                              G_FILE_ATTRIBUTE_STANDARD_ICON,
161                              0, 0, NULL,
162                              update_info_cb, g_object_ref (trash_monitor));
163 
164     g_object_unref (location);
165 }
166 
167 static void
file_changed(GFileMonitor * monitor,GFile * child,GFile * other_file,GFileMonitorEvent event_type,gpointer user_data)168 file_changed (GFileMonitor* monitor,
169               GFile *child,
170               GFile *other_file,
171               GFileMonitorEvent event_type,
172               gpointer user_data)
173 {
174     CajaTrashMonitor *trash_monitor;
175 
176     trash_monitor = CAJA_TRASH_MONITOR (user_data);
177 
178     schedule_update_info (trash_monitor);
179 }
180 
181 static void
caja_trash_monitor_init(CajaTrashMonitor * trash_monitor)182 caja_trash_monitor_init (CajaTrashMonitor *trash_monitor)
183 {
184     GFile *location;
185 
186     trash_monitor->details = caja_trash_monitor_get_instance_private (trash_monitor);
187 
188     trash_monitor->details->empty = TRUE;
189     trash_monitor->details->icon = g_themed_icon_new (CAJA_ICON_TRASH);
190 
191     location = g_file_new_for_uri ("trash:///");
192 
193     trash_monitor->details->file_monitor = g_file_monitor_file (location, 0, NULL, NULL);
194 
195     g_signal_connect (trash_monitor->details->file_monitor, "changed",
196                       (GCallback)file_changed, trash_monitor);
197 
198     g_object_unref (location);
199 
200     schedule_update_info (trash_monitor);
201 }
202 
203 static void
unref_trash_monitor(void)204 unref_trash_monitor (void)
205 {
206     g_object_unref (caja_trash_monitor);
207 }
208 
209 CajaTrashMonitor *
caja_trash_monitor_get(void)210 caja_trash_monitor_get (void)
211 {
212     if (caja_trash_monitor == NULL)
213     {
214         /* not running yet, start it up */
215 
216         caja_trash_monitor = CAJA_TRASH_MONITOR
217                              (g_object_new (CAJA_TYPE_TRASH_MONITOR, NULL));
218         eel_debug_call_at_shutdown (unref_trash_monitor);
219     }
220 
221     return caja_trash_monitor;
222 }
223 
224 gboolean
caja_trash_monitor_is_empty(void)225 caja_trash_monitor_is_empty (void)
226 {
227     CajaTrashMonitor *monitor;
228 
229     monitor = caja_trash_monitor_get ();
230     return monitor->details->empty;
231 }
232 
233 GIcon *
caja_trash_monitor_get_icon(void)234 caja_trash_monitor_get_icon (void)
235 {
236     CajaTrashMonitor *monitor;
237 
238     monitor = caja_trash_monitor_get ();
239     if (monitor->details->icon)
240     {
241         return g_object_ref (monitor->details->icon);
242     }
243     return NULL;
244 }
245