|
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/. */ |
|
5 |
|
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" |
|
17 |
|
18 NS_IMPL_ISUPPORTS(nsTransactionList, nsITransactionList) |
|
19 |
|
20 nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionStack *aTxnStack) |
|
21 : mTxnStack(aTxnStack) |
|
22 , mTxnItem(0) |
|
23 { |
|
24 if (aTxnMgr) |
|
25 mTxnMgr = do_GetWeakReference(aTxnMgr); |
|
26 } |
|
27 |
|
28 nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionItem *aTxnItem) |
|
29 : mTxnStack(0) |
|
30 , mTxnItem(aTxnItem) |
|
31 { |
|
32 if (aTxnMgr) |
|
33 mTxnMgr = do_GetWeakReference(aTxnMgr); |
|
34 } |
|
35 |
|
36 nsTransactionList::~nsTransactionList() |
|
37 { |
|
38 mTxnStack = 0; |
|
39 mTxnItem = 0; |
|
40 } |
|
41 |
|
42 /* readonly attribute long numItems; */ |
|
43 NS_IMETHODIMP nsTransactionList::GetNumItems(int32_t *aNumItems) |
|
44 { |
|
45 NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER); |
|
46 |
|
47 *aNumItems = 0; |
|
48 |
|
49 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr); |
|
50 |
|
51 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); |
|
52 |
|
53 nsresult result = NS_OK; |
|
54 |
|
55 if (mTxnStack) |
|
56 *aNumItems = mTxnStack->GetSize(); |
|
57 else if (mTxnItem) |
|
58 result = mTxnItem->GetNumberOfChildren(aNumItems); |
|
59 |
|
60 return result; |
|
61 } |
|
62 |
|
63 /* boolean itemIsBatch (in long aIndex); */ |
|
64 NS_IMETHODIMP nsTransactionList::ItemIsBatch(int32_t aIndex, bool *aIsBatch) |
|
65 { |
|
66 NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER); |
|
67 |
|
68 *aIsBatch = false; |
|
69 |
|
70 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr); |
|
71 |
|
72 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); |
|
73 |
|
74 nsRefPtr<nsTransactionItem> item; |
|
75 |
|
76 nsresult result = NS_OK; |
|
77 |
|
78 if (mTxnStack) |
|
79 item = mTxnStack->GetItem(aIndex); |
|
80 else if (mTxnItem) |
|
81 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); |
|
82 |
|
83 NS_ENSURE_SUCCESS(result, result); |
|
84 |
|
85 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); |
|
86 |
|
87 return item->GetIsBatch(aIsBatch); |
|
88 } |
|
89 |
|
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) |
|
97 { |
|
98 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr); |
|
99 |
|
100 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); |
|
101 |
|
102 nsRefPtr<nsTransactionItem> item; |
|
103 |
|
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); |
|
109 } |
|
110 |
|
111 nsCOMArray<nsISupports>& data = item->GetData(); |
|
112 |
|
113 nsISupports** ret = static_cast<nsISupports**>(NS_Alloc(data.Count() * |
|
114 sizeof(nsISupports*))); |
|
115 |
|
116 for (int32_t i = 0; i < data.Count(); i++) { |
|
117 NS_ADDREF(ret[i] = data[i]); |
|
118 } |
|
119 |
|
120 *aLength = data.Count(); |
|
121 *aData = ret; |
|
122 |
|
123 return NS_OK; |
|
124 } |
|
125 |
|
126 /* nsITransaction getItem (in long aIndex); */ |
|
127 NS_IMETHODIMP nsTransactionList::GetItem(int32_t aIndex, nsITransaction **aItem) |
|
128 { |
|
129 NS_ENSURE_TRUE(aItem, NS_ERROR_NULL_POINTER); |
|
130 |
|
131 *aItem = 0; |
|
132 |
|
133 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr); |
|
134 |
|
135 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); |
|
136 |
|
137 nsRefPtr<nsTransactionItem> item; |
|
138 |
|
139 nsresult result = NS_OK; |
|
140 |
|
141 if (mTxnStack) |
|
142 item = mTxnStack->GetItem(aIndex); |
|
143 else if (mTxnItem) |
|
144 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); |
|
145 |
|
146 NS_ENSURE_SUCCESS(result, result); |
|
147 |
|
148 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); |
|
149 |
|
150 *aItem = item->GetTransaction().take(); |
|
151 |
|
152 return NS_OK; |
|
153 } |
|
154 |
|
155 /* long getNumChildrenForItem (in long aIndex); */ |
|
156 NS_IMETHODIMP nsTransactionList::GetNumChildrenForItem(int32_t aIndex, int32_t *aNumChildren) |
|
157 { |
|
158 NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER); |
|
159 |
|
160 *aNumChildren = 0; |
|
161 |
|
162 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr); |
|
163 |
|
164 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); |
|
165 |
|
166 nsRefPtr<nsTransactionItem> item; |
|
167 |
|
168 nsresult result = NS_OK; |
|
169 |
|
170 if (mTxnStack) |
|
171 item = mTxnStack->GetItem(aIndex); |
|
172 else if (mTxnItem) |
|
173 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); |
|
174 |
|
175 NS_ENSURE_SUCCESS(result, result); |
|
176 |
|
177 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); |
|
178 |
|
179 return item->GetNumberOfChildren(aNumChildren); |
|
180 } |
|
181 |
|
182 /* nsITransactionList getChildListForItem (in long aIndex); */ |
|
183 NS_IMETHODIMP nsTransactionList::GetChildListForItem(int32_t aIndex, nsITransactionList **aTxnList) |
|
184 { |
|
185 NS_ENSURE_TRUE(aTxnList, NS_ERROR_NULL_POINTER); |
|
186 |
|
187 *aTxnList = 0; |
|
188 |
|
189 nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr); |
|
190 |
|
191 NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); |
|
192 |
|
193 nsRefPtr<nsTransactionItem> item; |
|
194 |
|
195 nsresult result = NS_OK; |
|
196 |
|
197 if (mTxnStack) |
|
198 item = mTxnStack->GetItem(aIndex); |
|
199 else if (mTxnItem) |
|
200 result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); |
|
201 |
|
202 NS_ENSURE_SUCCESS(result, result); |
|
203 |
|
204 NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); |
|
205 |
|
206 *aTxnList = (nsITransactionList *)new nsTransactionList(txMgr, item); |
|
207 |
|
208 NS_ENSURE_TRUE(*aTxnList, NS_ERROR_OUT_OF_MEMORY); |
|
209 |
|
210 NS_ADDREF(*aTxnList); |
|
211 |
|
212 return NS_OK; |
|
213 } |
|
214 |