1 /* nautilus-file-undo-manager.c - Manages the undo/redo stack
2 *
3 * Copyright (C) 2007-2011 Amos Brocco
4 * Copyright (C) 2010, 2012 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Authors: Amos Brocco <amos.brocco@gmail.com>
20 * Cosimo Cecchi <cosimoc@redhat.com>
21 *
22 */
23
24 #include <config.h>
25
26 #include "nautilus-file-undo-manager.h"
27
28 #include "nautilus-file-operations.h"
29 #include "nautilus-file.h"
30 #include "nautilus-trash-monitor.h"
31
32 #include <glib/gi18n.h>
33
34 #define DEBUG_FLAG NAUTILUS_DEBUG_UNDO
35 #include "nautilus-debug.h"
36
37 enum
38 {
39 SIGNAL_UNDO_CHANGED,
40 NUM_SIGNALS,
41 };
42
43 static guint signals[NUM_SIGNALS] = { 0, };
44
45 struct _NautilusFileUndoManager
46 {
47 GObject parent_instance;
48 NautilusFileUndoInfo *info;
49 NautilusFileUndoManagerState state;
50 NautilusFileUndoManagerState last_state;
51
52 guint is_operating : 1;
53
54 gulong trash_signal_id;
55 };
56
57 G_DEFINE_TYPE (NautilusFileUndoManager, nautilus_file_undo_manager, G_TYPE_OBJECT)
58
59 static NautilusFileUndoManager *undo_singleton = NULL;
60
61 NautilusFileUndoManager *
nautilus_file_undo_manager_new(void)62 nautilus_file_undo_manager_new (void)
63 {
64 if (undo_singleton != NULL)
65 {
66 return g_object_ref (undo_singleton);
67 }
68
69 undo_singleton = g_object_new (NAUTILUS_TYPE_FILE_UNDO_MANAGER, NULL);
70 g_object_add_weak_pointer (G_OBJECT (undo_singleton), (gpointer) & undo_singleton);
71
72 return undo_singleton;
73 }
74
75 static void
file_undo_manager_clear(NautilusFileUndoManager * self)76 file_undo_manager_clear (NautilusFileUndoManager *self)
77 {
78 g_clear_object (&self->info);
79 self->state = NAUTILUS_FILE_UNDO_MANAGER_STATE_NONE;
80 }
81
82 static void
trash_state_changed_cb(NautilusTrashMonitor * monitor,gboolean is_empty,gpointer user_data)83 trash_state_changed_cb (NautilusTrashMonitor *monitor,
84 gboolean is_empty,
85 gpointer user_data)
86 {
87 NautilusFileUndoManager *self = user_data;
88
89 /* A trash operation cannot be undone if the trash is empty */
90 if (is_empty &&
91 self->state == NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO &&
92 NAUTILUS_IS_FILE_UNDO_INFO_TRASH (self->info))
93 {
94 file_undo_manager_clear (self);
95 g_signal_emit (self, signals[SIGNAL_UNDO_CHANGED], 0);
96 }
97 }
98
99 static void
nautilus_file_undo_manager_init(NautilusFileUndoManager * self)100 nautilus_file_undo_manager_init (NautilusFileUndoManager *self)
101 {
102 self->trash_signal_id = g_signal_connect (nautilus_trash_monitor_get (),
103 "trash-state-changed",
104 G_CALLBACK (trash_state_changed_cb), self);
105 }
106
107 static void
nautilus_file_undo_manager_finalize(GObject * object)108 nautilus_file_undo_manager_finalize (GObject *object)
109 {
110 NautilusFileUndoManager *self = NAUTILUS_FILE_UNDO_MANAGER (object);
111
112 g_clear_signal_handler (&self->trash_signal_id, nautilus_trash_monitor_get ());
113
114 file_undo_manager_clear (self);
115
116 G_OBJECT_CLASS (nautilus_file_undo_manager_parent_class)->finalize (object);
117 }
118
119 static void
nautilus_file_undo_manager_class_init(NautilusFileUndoManagerClass * klass)120 nautilus_file_undo_manager_class_init (NautilusFileUndoManagerClass *klass)
121 {
122 GObjectClass *oclass;
123
124 oclass = G_OBJECT_CLASS (klass);
125
126 oclass->finalize = nautilus_file_undo_manager_finalize;
127
128 signals[SIGNAL_UNDO_CHANGED] =
129 g_signal_new ("undo-changed",
130 G_TYPE_FROM_CLASS (klass),
131 G_SIGNAL_RUN_LAST,
132 0, NULL, NULL,
133 g_cclosure_marshal_VOID__VOID,
134 G_TYPE_NONE, 0);
135 }
136
137 static void
undo_info_apply_ready(GObject * source,GAsyncResult * res,gpointer user_data)138 undo_info_apply_ready (GObject *source,
139 GAsyncResult *res,
140 gpointer user_data)
141 {
142 NautilusFileUndoManager *self = user_data;
143 NautilusFileUndoInfo *info = NAUTILUS_FILE_UNDO_INFO (source);
144 gboolean success, user_cancel;
145
146 success = nautilus_file_undo_info_apply_finish (info, res, &user_cancel, NULL);
147
148 self->is_operating = FALSE;
149
150 /* just return in case we got another another operation set */
151 if ((self->info != NULL) &&
152 (self->info != info))
153 {
154 return;
155 }
156
157 if (success)
158 {
159 if (self->last_state == NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO)
160 {
161 self->state = NAUTILUS_FILE_UNDO_MANAGER_STATE_REDO;
162 }
163 else if (self->last_state == NAUTILUS_FILE_UNDO_MANAGER_STATE_REDO)
164 {
165 self->state = NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO;
166 }
167
168 self->info = g_object_ref (info);
169 }
170 else if (user_cancel)
171 {
172 self->state = self->last_state;
173 self->info = g_object_ref (info);
174 }
175 else
176 {
177 file_undo_manager_clear (self);
178 }
179
180 g_signal_emit (self, signals[SIGNAL_UNDO_CHANGED], 0);
181 }
182
183 static void
do_undo_redo(NautilusFileUndoManager * self,GtkWindow * parent_window,NautilusFileOperationsDBusData * dbus_data)184 do_undo_redo (NautilusFileUndoManager *self,
185 GtkWindow *parent_window,
186 NautilusFileOperationsDBusData *dbus_data)
187 {
188 gboolean undo = self->state == NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO;
189
190 self->last_state = self->state;
191
192 self->is_operating = TRUE;
193 nautilus_file_undo_info_apply_async (self->info, undo, parent_window,
194 dbus_data,
195 undo_info_apply_ready, self);
196
197 /* clear actions while undoing */
198 file_undo_manager_clear (self);
199 g_signal_emit (self, signals[SIGNAL_UNDO_CHANGED], 0);
200 }
201
202 void
nautilus_file_undo_manager_redo(GtkWindow * parent_window,NautilusFileOperationsDBusData * dbus_data)203 nautilus_file_undo_manager_redo (GtkWindow *parent_window,
204 NautilusFileOperationsDBusData *dbus_data)
205 {
206 if (undo_singleton->state != NAUTILUS_FILE_UNDO_MANAGER_STATE_REDO)
207 {
208 g_warning ("Called redo, but state is %s!", undo_singleton->state == 0 ?
209 "none" : "undo");
210 return;
211 }
212
213 do_undo_redo (undo_singleton, parent_window, dbus_data);
214 }
215
216 void
nautilus_file_undo_manager_undo(GtkWindow * parent_window,NautilusFileOperationsDBusData * dbus_data)217 nautilus_file_undo_manager_undo (GtkWindow *parent_window,
218 NautilusFileOperationsDBusData *dbus_data)
219 {
220 if (undo_singleton->state != NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO)
221 {
222 g_warning ("Called undo, but state is %s!", undo_singleton->state == 0 ?
223 "none" : "redo");
224 return;
225 }
226
227 do_undo_redo (undo_singleton, parent_window, dbus_data);
228 }
229
230 void
nautilus_file_undo_manager_set_action(NautilusFileUndoInfo * info)231 nautilus_file_undo_manager_set_action (NautilusFileUndoInfo *info)
232 {
233 DEBUG ("Setting undo information %p", info);
234
235 file_undo_manager_clear (undo_singleton);
236
237 if (info != NULL)
238 {
239 undo_singleton->info = g_object_ref (info);
240 undo_singleton->state = NAUTILUS_FILE_UNDO_MANAGER_STATE_UNDO;
241 undo_singleton->last_state = NAUTILUS_FILE_UNDO_MANAGER_STATE_NONE;
242 }
243
244 g_signal_emit (undo_singleton, signals[SIGNAL_UNDO_CHANGED], 0);
245 }
246
247 NautilusFileUndoInfo *
nautilus_file_undo_manager_get_action(void)248 nautilus_file_undo_manager_get_action (void)
249 {
250 return undo_singleton->info;
251 }
252
253 NautilusFileUndoManagerState
nautilus_file_undo_manager_get_state(void)254 nautilus_file_undo_manager_get_state (void)
255 {
256 return undo_singleton->state;
257 }
258
259
260 gboolean
nautilus_file_undo_manager_is_operating()261 nautilus_file_undo_manager_is_operating ()
262 {
263 return undo_singleton->is_operating;
264 }
265
266 NautilusFileUndoManager *
nautilus_file_undo_manager_get()267 nautilus_file_undo_manager_get ()
268 {
269 return undo_singleton;
270 }
271