Bumping manifests a=b2g-bump
[gecko.git] / editor / txmgr / nsTransactionList.cpp
blob57c0a5b2a47cf2d6c281768c246bee37551eda4d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/mozalloc.h"
7 #include "nsCOMPtr.h"
8 #include "nsDebug.h"
9 #include "nsError.h"
10 #include "nsID.h"
11 #include "nsISupportsUtils.h"
12 #include "nsITransactionManager.h"
13 #include "nsTransactionItem.h"
14 #include "nsTransactionList.h"
15 #include "nsTransactionStack.h"
16 #include "nscore.h"
18 NS_IMPL_ISUPPORTS(nsTransactionList, nsITransactionList)
20 nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionStack *aTxnStack)
21 : mTxnStack(aTxnStack)
22 , mTxnItem(0)
24 if (aTxnMgr)
25 mTxnMgr = do_GetWeakReference(aTxnMgr);
28 nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionItem *aTxnItem)
29 : mTxnStack(0)
30 , mTxnItem(aTxnItem)
32 if (aTxnMgr)
33 mTxnMgr = do_GetWeakReference(aTxnMgr);
36 nsTransactionList::~nsTransactionList()
38 mTxnStack = 0;
39 mTxnItem = 0;
42 /* readonly attribute long numItems; */
43 NS_IMETHODIMP nsTransactionList::GetNumItems(int32_t *aNumItems)
45 NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
47 *aNumItems = 0;
49 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
51 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
53 nsresult result = NS_OK;
55 if (mTxnStack)
56 *aNumItems = mTxnStack->GetSize();
57 else if (mTxnItem)
58 result = mTxnItem->GetNumberOfChildren(aNumItems);
60 return result;
63 /* boolean itemIsBatch (in long aIndex); */
64 NS_IMETHODIMP nsTransactionList::ItemIsBatch(int32_t aIndex, bool *aIsBatch)
66 NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
68 *aIsBatch = false;
70 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
72 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
74 nsRefPtr<nsTransactionItem> item;
76 nsresult result = NS_OK;
78 if (mTxnStack)
79 item = mTxnStack->GetItem(aIndex);
80 else if (mTxnItem)
81 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
83 NS_ENSURE_SUCCESS(result, result);
85 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
87 return item->GetIsBatch(aIsBatch);
90 /* void getData (in long aIndex,
91 [optional] out unsigned long aLength,
92 [array, size_is (aLength), retval]
93 out nsISupports aData); */
94 NS_IMETHODIMP nsTransactionList::GetData(int32_t aIndex,
95 uint32_t *aLength,
96 nsISupports ***aData)
98 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
100 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
102 nsRefPtr<nsTransactionItem> item;
104 if (mTxnStack) {
105 item = mTxnStack->GetItem(aIndex);
106 } else if (mTxnItem) {
107 nsresult result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
108 NS_ENSURE_SUCCESS(result, result);
111 nsCOMArray<nsISupports>& data = item->GetData();
113 nsISupports** ret = static_cast<nsISupports**>(NS_Alloc(data.Count() *
114 sizeof(nsISupports*)));
116 for (int32_t i = 0; i < data.Count(); i++) {
117 NS_ADDREF(ret[i] = data[i]);
120 *aLength = data.Count();
121 *aData = ret;
123 return NS_OK;
126 /* nsITransaction getItem (in long aIndex); */
127 NS_IMETHODIMP nsTransactionList::GetItem(int32_t aIndex, nsITransaction **aItem)
129 NS_ENSURE_TRUE(aItem, NS_ERROR_NULL_POINTER);
131 *aItem = 0;
133 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
135 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
137 nsRefPtr<nsTransactionItem> item;
139 nsresult result = NS_OK;
141 if (mTxnStack)
142 item = mTxnStack->GetItem(aIndex);
143 else if (mTxnItem)
144 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
146 NS_ENSURE_SUCCESS(result, result);
148 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
150 *aItem = item->GetTransaction().take();
152 return NS_OK;
155 /* long getNumChildrenForItem (in long aIndex); */
156 NS_IMETHODIMP nsTransactionList::GetNumChildrenForItem(int32_t aIndex, int32_t *aNumChildren)
158 NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);
160 *aNumChildren = 0;
162 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
164 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
166 nsRefPtr<nsTransactionItem> item;
168 nsresult result = NS_OK;
170 if (mTxnStack)
171 item = mTxnStack->GetItem(aIndex);
172 else if (mTxnItem)
173 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
175 NS_ENSURE_SUCCESS(result, result);
177 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
179 return item->GetNumberOfChildren(aNumChildren);
182 /* nsITransactionList getChildListForItem (in long aIndex); */
183 NS_IMETHODIMP nsTransactionList::GetChildListForItem(int32_t aIndex, nsITransactionList **aTxnList)
185 NS_ENSURE_TRUE(aTxnList, NS_ERROR_NULL_POINTER);
187 *aTxnList = 0;
189 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
191 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
193 nsRefPtr<nsTransactionItem> item;
195 nsresult result = NS_OK;
197 if (mTxnStack)
198 item = mTxnStack->GetItem(aIndex);
199 else if (mTxnItem)
200 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
202 NS_ENSURE_SUCCESS(result, result);
204 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
206 *aTxnList = (nsITransactionList *)new nsTransactionList(txMgr, item);
208 NS_ENSURE_TRUE(*aTxnList, NS_ERROR_OUT_OF_MEMORY);
210 NS_ADDREF(*aTxnList);
212 return NS_OK;