1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsITransaction.h"
39 : #include "nsTransactionStack.h"
40 : #include "nsTransactionManager.h"
41 : #include "nsTransactionItem.h"
42 : #include "nsCOMPtr.h"
43 : #include "nsAutoPtr.h"
44 :
45 27 : nsTransactionItem::nsTransactionItem(nsITransaction *aTransaction)
46 27 : : mTransaction(aTransaction), mUndoStack(0), mRedoStack(0)
47 : {
48 27 : }
49 :
50 81 : nsTransactionItem::~nsTransactionItem()
51 : {
52 27 : delete mRedoStack;
53 :
54 27 : delete mUndoStack;
55 108 : }
56 :
57 : nsrefcnt
58 111 : nsTransactionItem::AddRef()
59 : {
60 111 : ++mRefCnt;
61 111 : NS_LOG_ADDREF(this, mRefCnt, "nsTransactionItem",
62 111 : sizeof(nsTransactionItem));
63 111 : return mRefCnt;
64 : }
65 :
66 : nsrefcnt
67 111 : nsTransactionItem::Release() {
68 111 : --mRefCnt;
69 111 : NS_LOG_RELEASE(this, mRefCnt, "nsTransactionItem");
70 111 : if (mRefCnt == 0) {
71 27 : mRefCnt = 1;
72 27 : delete this;
73 27 : return 0;
74 : }
75 84 : return mRefCnt;
76 : }
77 :
78 1396 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsTransactionItem)
79 :
80 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_NATIVE(nsTransactionItem)
81 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mTransaction)
82 0 : if (tmp->mRedoStack) {
83 0 : tmp->mRedoStack->DoUnlink();
84 : }
85 0 : if (tmp->mUndoStack) {
86 0 : tmp->mUndoStack->DoUnlink();
87 : }
88 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
89 :
90 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NATIVE_BEGIN(nsTransactionItem)
91 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTransaction)
92 0 : if (tmp->mRedoStack) {
93 0 : tmp->mRedoStack->DoTraverse(cb);
94 : }
95 0 : if (tmp->mUndoStack) {
96 0 : tmp->mUndoStack->DoTraverse(cb);
97 : }
98 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
99 :
100 0 : NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsTransactionItem, AddRef)
101 0 : NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsTransactionItem, Release)
102 :
103 : nsresult
104 4 : nsTransactionItem::AddChild(nsTransactionItem *aTransactionItem)
105 : {
106 4 : NS_ENSURE_TRUE(aTransactionItem, NS_ERROR_NULL_POINTER);
107 :
108 4 : if (!mUndoStack) {
109 4 : mUndoStack = new nsTransactionStack(nsTransactionStack::FOR_UNDO);
110 : }
111 :
112 4 : mUndoStack->Push(aTransactionItem);
113 :
114 4 : return NS_OK;
115 : }
116 :
117 : nsresult
118 50 : nsTransactionItem::GetTransaction(nsITransaction **aTransaction)
119 : {
120 50 : NS_ENSURE_TRUE(aTransaction, NS_ERROR_NULL_POINTER);
121 :
122 50 : NS_IF_ADDREF(*aTransaction = mTransaction);
123 :
124 50 : return NS_OK;
125 : }
126 :
127 : nsresult
128 0 : nsTransactionItem::GetIsBatch(bool *aIsBatch)
129 : {
130 0 : NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
131 :
132 0 : *aIsBatch = !mTransaction;
133 :
134 0 : return NS_OK;
135 : }
136 :
137 : nsresult
138 1 : nsTransactionItem::GetNumberOfChildren(PRInt32 *aNumChildren)
139 : {
140 : nsresult result;
141 :
142 1 : NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);
143 :
144 1 : *aNumChildren = 0;
145 :
146 1 : PRInt32 ui = 0;
147 1 : PRInt32 ri = 0;
148 :
149 1 : result = GetNumberOfUndoItems(&ui);
150 :
151 1 : NS_ENSURE_SUCCESS(result, result);
152 :
153 1 : result = GetNumberOfRedoItems(&ri);
154 :
155 1 : NS_ENSURE_SUCCESS(result, result);
156 :
157 1 : *aNumChildren = ui + ri;
158 :
159 1 : return NS_OK;
160 : }
161 :
162 : nsresult
163 0 : nsTransactionItem::GetChild(PRInt32 aIndex, nsTransactionItem **aChild)
164 : {
165 0 : NS_ENSURE_TRUE(aChild, NS_ERROR_NULL_POINTER);
166 :
167 0 : *aChild = 0;
168 :
169 0 : PRInt32 numItems = 0;
170 0 : nsresult result = GetNumberOfChildren(&numItems);
171 :
172 0 : NS_ENSURE_SUCCESS(result, result);
173 :
174 0 : if (aIndex < 0 || aIndex >= numItems)
175 0 : return NS_ERROR_FAILURE;
176 :
177 : // Children are expected to be in the order they were added,
178 : // so the child first added would be at the bottom of the undo
179 : // stack, or if there are no items on the undo stack, it would
180 : // be at the top of the redo stack.
181 :
182 0 : result = GetNumberOfUndoItems(&numItems);
183 :
184 0 : NS_ENSURE_SUCCESS(result, result);
185 :
186 0 : if (numItems > 0 && aIndex < numItems) {
187 0 : NS_ENSURE_TRUE(mUndoStack, NS_ERROR_FAILURE);
188 :
189 0 : nsRefPtr<nsTransactionItem> child = mUndoStack->GetItem(aIndex);
190 0 : child.forget(aChild);
191 0 : return *aChild ? NS_OK : NS_ERROR_FAILURE;
192 : }
193 :
194 : // Adjust the index for the redo stack:
195 :
196 0 : aIndex -= numItems;
197 :
198 0 : result = GetNumberOfRedoItems(&numItems);
199 :
200 0 : NS_ENSURE_SUCCESS(result, result);
201 :
202 0 : NS_ENSURE_TRUE(mRedoStack && numItems != 0 && aIndex < numItems, NS_ERROR_FAILURE);
203 :
204 0 : nsRefPtr<nsTransactionItem> child = mRedoStack->GetItem(aIndex);
205 0 : child.forget(aChild);
206 0 : return *aChild ? NS_OK : NS_ERROR_FAILURE;
207 : }
208 :
209 : nsresult
210 27 : nsTransactionItem::DoTransaction()
211 : {
212 27 : if (mTransaction)
213 26 : return mTransaction->DoTransaction();
214 1 : return NS_OK;
215 : }
216 :
217 : nsresult
218 2 : nsTransactionItem::UndoTransaction(nsTransactionManager *aTxMgr)
219 : {
220 2 : nsresult result = UndoChildren(aTxMgr);
221 :
222 2 : if (NS_FAILED(result)) {
223 0 : RecoverFromUndoError(aTxMgr);
224 0 : return result;
225 : }
226 :
227 2 : if (!mTransaction)
228 1 : return NS_OK;
229 :
230 1 : result = mTransaction->UndoTransaction();
231 :
232 1 : if (NS_FAILED(result)) {
233 0 : RecoverFromUndoError(aTxMgr);
234 0 : return result;
235 : }
236 :
237 1 : return NS_OK;
238 : }
239 :
240 : nsresult
241 2 : nsTransactionItem::UndoChildren(nsTransactionManager *aTxMgr)
242 : {
243 4 : nsRefPtr<nsTransactionItem> item;
244 2 : nsresult result = NS_OK;
245 2 : PRInt32 sz = 0;
246 :
247 2 : if (mUndoStack) {
248 1 : if (!mRedoStack && mUndoStack) {
249 1 : mRedoStack = new nsTransactionStack(nsTransactionStack::FOR_REDO);
250 : }
251 :
252 : /* Undo all of the transaction items children! */
253 1 : sz = mUndoStack->GetSize();
254 :
255 3 : while (sz-- > 0) {
256 1 : item = mUndoStack->Peek();
257 :
258 1 : if (!item) {
259 0 : return NS_ERROR_FAILURE;
260 : }
261 :
262 2 : nsCOMPtr<nsITransaction> t;
263 :
264 1 : result = item->GetTransaction(getter_AddRefs(t));
265 :
266 1 : if (NS_FAILED(result)) {
267 0 : return result;
268 : }
269 :
270 1 : bool doInterrupt = false;
271 :
272 1 : result = aTxMgr->WillUndoNotify(t, &doInterrupt);
273 :
274 1 : if (NS_FAILED(result)) {
275 0 : return result;
276 : }
277 :
278 1 : if (doInterrupt) {
279 0 : return NS_OK;
280 : }
281 :
282 1 : result = item->UndoTransaction(aTxMgr);
283 :
284 1 : if (NS_SUCCEEDED(result)) {
285 1 : item = mUndoStack->Pop();
286 1 : mRedoStack->Push(item);
287 : }
288 :
289 1 : nsresult result2 = aTxMgr->DidUndoNotify(t, result);
290 :
291 1 : if (NS_SUCCEEDED(result)) {
292 1 : result = result2;
293 : }
294 : }
295 : }
296 :
297 2 : return result;
298 : }
299 :
300 : nsresult
301 0 : nsTransactionItem::RedoTransaction(nsTransactionManager *aTxMgr)
302 : {
303 : nsresult result;
304 :
305 0 : nsCOMPtr<nsITransaction> kungfuDeathGrip(mTransaction);
306 0 : if (mTransaction) {
307 0 : result = mTransaction->RedoTransaction();
308 :
309 0 : NS_ENSURE_SUCCESS(result, result);
310 : }
311 :
312 0 : result = RedoChildren(aTxMgr);
313 :
314 0 : if (NS_FAILED(result)) {
315 0 : RecoverFromRedoError(aTxMgr);
316 0 : return result;
317 : }
318 :
319 0 : return NS_OK;
320 : }
321 :
322 : nsresult
323 0 : nsTransactionItem::RedoChildren(nsTransactionManager *aTxMgr)
324 : {
325 0 : nsRefPtr<nsTransactionItem> item;
326 0 : nsresult result = NS_OK;
327 :
328 0 : if (!mRedoStack)
329 0 : return NS_OK;
330 :
331 : /* Redo all of the transaction items children! */
332 0 : PRInt32 sz = mRedoStack->GetSize();
333 :
334 0 : while (sz-- > 0) {
335 0 : item = mRedoStack->Peek();
336 :
337 0 : if (!item) {
338 0 : return NS_ERROR_FAILURE;
339 : }
340 :
341 0 : nsCOMPtr<nsITransaction> t;
342 :
343 0 : result = item->GetTransaction(getter_AddRefs(t));
344 :
345 0 : if (NS_FAILED(result)) {
346 0 : return result;
347 : }
348 :
349 0 : bool doInterrupt = false;
350 :
351 0 : result = aTxMgr->WillRedoNotify(t, &doInterrupt);
352 :
353 0 : if (NS_FAILED(result)) {
354 0 : return result;
355 : }
356 :
357 0 : if (doInterrupt) {
358 0 : return NS_OK;
359 : }
360 :
361 0 : result = item->RedoTransaction(aTxMgr);
362 :
363 0 : if (NS_SUCCEEDED(result)) {
364 0 : item = mRedoStack->Pop();
365 0 : mUndoStack->Push(item);
366 : }
367 :
368 0 : nsresult result2 = aTxMgr->DidUndoNotify(t, result);
369 :
370 0 : if (NS_SUCCEEDED(result)) {
371 0 : result = result2;
372 : }
373 : }
374 :
375 0 : return result;
376 : }
377 :
378 : nsresult
379 1 : nsTransactionItem::GetNumberOfUndoItems(PRInt32 *aNumItems)
380 : {
381 1 : NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
382 :
383 1 : if (!mUndoStack) {
384 0 : *aNumItems = 0;
385 0 : return NS_OK;
386 : }
387 :
388 1 : *aNumItems = mUndoStack->GetSize();
389 1 : return *aNumItems ? NS_OK : NS_ERROR_FAILURE;
390 : }
391 :
392 : nsresult
393 1 : nsTransactionItem::GetNumberOfRedoItems(PRInt32 *aNumItems)
394 : {
395 1 : NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
396 :
397 1 : if (!mRedoStack) {
398 1 : *aNumItems = 0;
399 1 : return NS_OK;
400 : }
401 :
402 0 : *aNumItems = mRedoStack->GetSize();
403 0 : return *aNumItems ? NS_OK : NS_ERROR_FAILURE;
404 : }
405 :
406 : nsresult
407 0 : nsTransactionItem::RecoverFromUndoError(nsTransactionManager *aTxMgr)
408 : {
409 : //
410 : // If this method gets called, we never got to the point where we
411 : // successfully called UndoTransaction() for the transaction item itself.
412 : // Just redo any children that successfully called undo!
413 : //
414 0 : return RedoChildren(aTxMgr);
415 : }
416 :
417 : nsresult
418 0 : nsTransactionItem::RecoverFromRedoError(nsTransactionManager *aTxMgr)
419 : {
420 : //
421 : // If this method gets called, we already successfully called
422 : // RedoTransaction() for the transaction item itself. Undo all
423 : // the children that successfully called RedoTransaction(),
424 : // then undo the transaction item itself.
425 : //
426 :
427 : nsresult result;
428 :
429 0 : result = UndoChildren(aTxMgr);
430 :
431 0 : if (NS_FAILED(result)) {
432 0 : return result;
433 : }
434 :
435 0 : if (!mTransaction)
436 0 : return NS_OK;
437 :
438 0 : return mTransaction->UndoTransaction();
439 4188 : }
440 :
|