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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* NautilusUndoTransaction - An object for an undoable transaction.
* Used internally by undo machinery.
* Not public.
*
* Copyright (C) 2000 Eazel, Inc.
*
* Author: Gene Z. Ragan <gzr@eazel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include "nautilus-undo-transaction.h"
#include "nautilus-undo-private.h"
#include <bonobo/bonobo-main.h>
#include <gtk/gtksignal.h>
#include <eel/eel-gtk-macros.h>
#define NAUTILUS_UNDO_TRANSACTION_LIST_DATA "Nautilus undo transaction list"
typedef struct {
POA_Nautilus_Undo_Transaction servant;
NautilusUndoTransaction *bonobo_object;
} impl_POA_Nautilus_Undo_Transaction;
static void nautilus_undo_transaction_class_init (NautilusUndoTransactionClass *class);
static void nautilus_undo_transaction_init (NautilusUndoTransaction *item);
static void nautilus_undo_transaction_destroy (GtkObject *object);
static void nautilus_undo_transaction_undo (NautilusUndoTransaction *transaction);
/* CORBA/Bonobo */
static Nautilus_Undo_MenuItem *impl_Nautilus_Undo_Transaction__get_undo_menu_item (PortableServer_Servant servant,
CORBA_Environment *ev);
static Nautilus_Undo_MenuItem *impl_Nautilus_Undo_Transaction__get_redo_menu_item (PortableServer_Servant servant,
CORBA_Environment *ev);
static CORBA_char * impl_Nautilus_Undo_Transaction__get_operation_name (PortableServer_Servant servant,
CORBA_Environment *ev);
static void impl_Nautilus_Undo_Transaction__undo (PortableServer_Servant servant,
CORBA_Environment *ev);
/* undo atoms */
static void undo_atom_list_free (GList *list);
static void undo_atom_list_undo_and_free (GList *list);
EEL_BONOBO_BOILERPLATE_FULL (NautilusUndoTransaction,
Nautilus_Undo_Transaction,
nautilus_undo_transaction,
BONOBO_OBJECT_TYPE)
static Nautilus_Undo_MenuItem *
impl_Nautilus_Undo_Transaction__get_undo_menu_item (PortableServer_Servant servant,
CORBA_Environment *ev)
{
NautilusUndoTransaction *transaction;
Nautilus_Undo_MenuItem *item;
transaction = ((impl_POA_Nautilus_Undo_Transaction *) servant)->bonobo_object;
item = Nautilus_Undo_MenuItem__alloc ();
item->label = CORBA_string_dup (transaction->undo_menu_item_label);
item->hint = CORBA_string_dup (transaction->undo_menu_item_hint);
return item;
}
static Nautilus_Undo_MenuItem *
impl_Nautilus_Undo_Transaction__get_redo_menu_item (PortableServer_Servant servant,
CORBA_Environment *ev)
{
NautilusUndoTransaction *transaction;
Nautilus_Undo_MenuItem *item;
transaction = ((impl_POA_Nautilus_Undo_Transaction *) servant)->bonobo_object;
item = Nautilus_Undo_MenuItem__alloc ();
item->label = CORBA_string_dup (transaction->redo_menu_item_label);
item->hint = CORBA_string_dup (transaction->redo_menu_item_hint);
return item;
}
static CORBA_char *
impl_Nautilus_Undo_Transaction__get_operation_name (PortableServer_Servant servant,
CORBA_Environment *ev)
{
NautilusUndoTransaction *transaction;
transaction = ((impl_POA_Nautilus_Undo_Transaction *) servant)->bonobo_object;
return CORBA_string_dup (transaction->operation_name);
}
static void
impl_Nautilus_Undo_Transaction__undo (PortableServer_Servant servant,
CORBA_Environment *ev)
{
NautilusUndoTransaction *transaction;
transaction = ((impl_POA_Nautilus_Undo_Transaction *) servant)->bonobo_object;
nautilus_undo_transaction_undo (transaction);
}
NautilusUndoTransaction *
nautilus_undo_transaction_new (const char *operation_name,
const char *undo_menu_item_label,
const char *undo_menu_item_hint,
const char *redo_menu_item_label,
const char *redo_menu_item_hint)
{
NautilusUndoTransaction *transaction;
transaction = NAUTILUS_UNDO_TRANSACTION (gtk_object_new (nautilus_undo_transaction_get_type (), NULL));
gtk_object_ref (GTK_OBJECT (transaction));
gtk_object_sink (GTK_OBJECT (transaction));
transaction->operation_name = g_strdup (operation_name);
transaction->undo_menu_item_label = g_strdup (undo_menu_item_label);
transaction->undo_menu_item_hint = g_strdup (undo_menu_item_hint);
transaction->redo_menu_item_label = g_strdup (redo_menu_item_label);
transaction->redo_menu_item_hint = g_strdup (redo_menu_item_hint);
return transaction;
}
static void
nautilus_undo_transaction_init (NautilusUndoTransaction *transaction)
{
}
static void
remove_transaction_from_object (gpointer list_data, gpointer callback_data)
{
NautilusUndoAtom *atom;
NautilusUndoTransaction *transaction;
GList *list;
g_assert (list_data != NULL);
atom = list_data;
transaction = NAUTILUS_UNDO_TRANSACTION (callback_data);
/* Remove the transaction from the list on the atom. */
list = gtk_object_get_data (atom->target, NAUTILUS_UNDO_TRANSACTION_LIST_DATA);
if (list != NULL) {
list = g_list_remove (list, transaction);
gtk_object_set_data (atom->target, NAUTILUS_UNDO_TRANSACTION_LIST_DATA, list);
}
}
static void
remove_transaction_from_atom_targets (NautilusUndoTransaction *transaction)
{
g_list_foreach (transaction->atom_list,
remove_transaction_from_object,
transaction);
}
static void
nautilus_undo_transaction_destroy (GtkObject *object)
{
NautilusUndoTransaction *transaction;
CORBA_Environment ev;
transaction = NAUTILUS_UNDO_TRANSACTION (object);
remove_transaction_from_atom_targets (transaction);
undo_atom_list_free (transaction->atom_list);
g_free (transaction->operation_name);
g_free (transaction->undo_menu_item_label);
g_free (transaction->undo_menu_item_hint);
g_free (transaction->redo_menu_item_label);
g_free (transaction->redo_menu_item_hint);
CORBA_exception_init (&ev);
CORBA_Object_release (transaction->owner, &ev);
CORBA_exception_free (&ev);
EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}
static void
nautilus_undo_transaction_class_init (NautilusUndoTransactionClass *klass)
{
POA_Nautilus_Undo_Transaction__epv *epv = &klass->epv;
GTK_OBJECT_CLASS (klass)->destroy = nautilus_undo_transaction_destroy;
epv->_get_undo_menu_item = &impl_Nautilus_Undo_Transaction__get_undo_menu_item;
epv->_get_redo_menu_item = &impl_Nautilus_Undo_Transaction__get_redo_menu_item;
epv->_get_operation_name = &impl_Nautilus_Undo_Transaction__get_operation_name;
epv->undo = &impl_Nautilus_Undo_Transaction__undo;
}
void
nautilus_undo_transaction_add_atom (NautilusUndoTransaction *transaction,
const NautilusUndoAtom *atom)
{
GList *list;
g_return_if_fail (NAUTILUS_IS_UNDO_TRANSACTION (transaction));
g_return_if_fail (atom != NULL);
g_return_if_fail (GTK_IS_OBJECT (atom->target));
/* Add the atom to the atom list in the transaction. */
transaction->atom_list = g_list_append
(transaction->atom_list, g_memdup (atom, sizeof (*atom)));
/* Add the transaction to the list on the atoms target object. */
list = gtk_object_get_data (atom->target, NAUTILUS_UNDO_TRANSACTION_LIST_DATA);
if (g_list_find (list, transaction) != NULL) {
return;
}
/* If it's not already on that atom, this object is new. */
list = g_list_prepend (list, transaction);
gtk_object_set_data (atom->target, NAUTILUS_UNDO_TRANSACTION_LIST_DATA, list);
/* Connect a signal handler to the atom so it will unregister
* itself when it's destroyed.
*/
gtk_signal_connect (atom->target, "destroy",
GTK_SIGNAL_FUNC (nautilus_undo_transaction_unregister_object),
NULL);
}
void
nautilus_undo_transaction_undo (NautilusUndoTransaction *transaction)
{
g_return_if_fail (NAUTILUS_IS_UNDO_TRANSACTION (transaction));
remove_transaction_from_atom_targets (transaction);
undo_atom_list_undo_and_free (transaction->atom_list);
transaction->atom_list = NULL;
}
void
nautilus_undo_transaction_add_to_undo_manager (NautilusUndoTransaction *transaction,
Nautilus_Undo_Manager manager)
{
CORBA_Environment ev;
g_return_if_fail (NAUTILUS_IS_UNDO_TRANSACTION (transaction));
g_return_if_fail (transaction->owner == CORBA_OBJECT_NIL);
CORBA_exception_init (&ev);
if (!CORBA_Object_is_nil (manager, &ev)) {
Nautilus_Undo_Manager_append
(manager,
bonobo_object_corba_objref (BONOBO_OBJECT (transaction)),
&ev);
transaction->owner = CORBA_Object_duplicate (manager, &ev);
}
CORBA_exception_free (&ev);
}
static void
remove_atoms (NautilusUndoTransaction *transaction,
GtkObject *object)
{
CORBA_Environment ev;
GList *p, *next;
NautilusUndoAtom *atom;
g_assert (NAUTILUS_IS_UNDO_TRANSACTION (transaction));
g_assert (GTK_IS_OBJECT (object));
CORBA_exception_init (&ev);
/* Destroy any atoms for this object. */
for (p = transaction->atom_list; p != NULL; p = next) {
atom = p->data;
next = p->next;
if (atom->target == object) {
transaction->atom_list = g_list_remove_link
(transaction->atom_list, p);
undo_atom_list_free (p);
}
}
/* If all the atoms are gone, forget this transaction.
* This may end up freeing the transaction.
*/
if (transaction->atom_list == NULL) {
Nautilus_Undo_Manager_forget
(transaction->owner,
bonobo_object_corba_objref (BONOBO_OBJECT (transaction)),
&ev);
}
CORBA_exception_free (&ev);
}
static void
remove_atoms_cover (gpointer list_data, gpointer callback_data)
{
if (NAUTILUS_IS_UNDO_TRANSACTION (list_data)) {
remove_atoms (NAUTILUS_UNDO_TRANSACTION (list_data), GTK_OBJECT (callback_data));
}
}
void
nautilus_undo_transaction_unregister_object (GtkObject *object)
{
GList *list;
g_return_if_fail (GTK_IS_OBJECT (object));
/* Remove atoms from each transaction on the list. */
list = gtk_object_get_data (object, NAUTILUS_UNDO_TRANSACTION_LIST_DATA);
if (list != NULL) {
g_list_foreach (list, remove_atoms_cover, object);
g_list_free (list);
gtk_object_remove_data (object, NAUTILUS_UNDO_TRANSACTION_LIST_DATA);
}
}
static void
undo_atom_free (NautilusUndoAtom *atom)
{
/* Call the destroy-notify function if it's present. */
if (atom->callback_data_destroy_notify != NULL) {
(* atom->callback_data_destroy_notify) (atom->callback_data);
}
/* Free the atom storage. */
g_free (atom);
}
static void
undo_atom_undo_and_free (NautilusUndoAtom *atom)
{
/* Call the function that does the actual undo. */
(* atom->callback) (atom->target, atom->callback_data);
/* Get rid of the atom now that it's spent. */
undo_atom_free (atom);
}
static void
undo_atom_free_cover (gpointer atom, gpointer callback_data)
{
g_assert (atom != NULL);
g_assert (callback_data == NULL);
undo_atom_free (atom);
}
static void
undo_atom_undo_and_free_cover (gpointer atom, gpointer callback_data)
{
g_assert (atom != NULL);
g_assert (callback_data == NULL);
undo_atom_undo_and_free (atom);
}
static void
undo_atom_list_free (GList *list)
{
g_list_foreach (list, undo_atom_free_cover, NULL);
g_list_free (list);
}
static void
undo_atom_list_undo_and_free (GList *list)
{
g_list_foreach (list, undo_atom_undo_and_free_cover, NULL);
g_list_free (list);
}
|