michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/mozalloc.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsDebug.h" michael@0: #include "nsError.h" michael@0: #include "nsID.h" michael@0: #include "nsISupportsUtils.h" michael@0: #include "nsITransactionManager.h" michael@0: #include "nsTransactionItem.h" michael@0: #include "nsTransactionList.h" michael@0: #include "nsTransactionStack.h" michael@0: #include "nscore.h" michael@0: michael@0: NS_IMPL_ISUPPORTS(nsTransactionList, nsITransactionList) michael@0: michael@0: nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionStack *aTxnStack) michael@0: : mTxnStack(aTxnStack) michael@0: , mTxnItem(0) michael@0: { michael@0: if (aTxnMgr) michael@0: mTxnMgr = do_GetWeakReference(aTxnMgr); michael@0: } michael@0: michael@0: nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionItem *aTxnItem) michael@0: : mTxnStack(0) michael@0: , mTxnItem(aTxnItem) michael@0: { michael@0: if (aTxnMgr) michael@0: mTxnMgr = do_GetWeakReference(aTxnMgr); michael@0: } michael@0: michael@0: nsTransactionList::~nsTransactionList() michael@0: { michael@0: mTxnStack = 0; michael@0: mTxnItem = 0; michael@0: } michael@0: michael@0: /* readonly attribute long numItems; */ michael@0: NS_IMETHODIMP nsTransactionList::GetNumItems(int32_t *aNumItems) michael@0: { michael@0: NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aNumItems = 0; michael@0: michael@0: nsCOMPtr txMgr = do_QueryReferent(mTxnMgr); michael@0: michael@0: NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); michael@0: michael@0: nsresult result = NS_OK; michael@0: michael@0: if (mTxnStack) michael@0: *aNumItems = mTxnStack->GetSize(); michael@0: else if (mTxnItem) michael@0: result = mTxnItem->GetNumberOfChildren(aNumItems); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: /* boolean itemIsBatch (in long aIndex); */ michael@0: NS_IMETHODIMP nsTransactionList::ItemIsBatch(int32_t aIndex, bool *aIsBatch) michael@0: { michael@0: NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aIsBatch = false; michael@0: michael@0: nsCOMPtr txMgr = do_QueryReferent(mTxnMgr); michael@0: michael@0: NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr item; michael@0: michael@0: nsresult result = NS_OK; michael@0: michael@0: if (mTxnStack) michael@0: item = mTxnStack->GetItem(aIndex); michael@0: else if (mTxnItem) michael@0: result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); michael@0: michael@0: return item->GetIsBatch(aIsBatch); michael@0: } michael@0: michael@0: /* void getData (in long aIndex, michael@0: [optional] out unsigned long aLength, michael@0: [array, size_is (aLength), retval] michael@0: out nsISupports aData); */ michael@0: NS_IMETHODIMP nsTransactionList::GetData(int32_t aIndex, michael@0: uint32_t *aLength, michael@0: nsISupports ***aData) michael@0: { michael@0: nsCOMPtr txMgr = do_QueryReferent(mTxnMgr); michael@0: michael@0: NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr item; michael@0: michael@0: if (mTxnStack) { michael@0: item = mTxnStack->GetItem(aIndex); michael@0: } else if (mTxnItem) { michael@0: nsresult result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: } michael@0: michael@0: nsCOMArray& data = item->GetData(); michael@0: michael@0: nsISupports** ret = static_cast(NS_Alloc(data.Count() * michael@0: sizeof(nsISupports*))); michael@0: michael@0: for (int32_t i = 0; i < data.Count(); i++) { michael@0: NS_ADDREF(ret[i] = data[i]); michael@0: } michael@0: michael@0: *aLength = data.Count(); michael@0: *aData = ret; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* nsITransaction getItem (in long aIndex); */ michael@0: NS_IMETHODIMP nsTransactionList::GetItem(int32_t aIndex, nsITransaction **aItem) michael@0: { michael@0: NS_ENSURE_TRUE(aItem, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aItem = 0; michael@0: michael@0: nsCOMPtr txMgr = do_QueryReferent(mTxnMgr); michael@0: michael@0: NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr item; michael@0: michael@0: nsresult result = NS_OK; michael@0: michael@0: if (mTxnStack) michael@0: item = mTxnStack->GetItem(aIndex); michael@0: else if (mTxnItem) michael@0: result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); michael@0: michael@0: *aItem = item->GetTransaction().take(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* long getNumChildrenForItem (in long aIndex); */ michael@0: NS_IMETHODIMP nsTransactionList::GetNumChildrenForItem(int32_t aIndex, int32_t *aNumChildren) michael@0: { michael@0: NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aNumChildren = 0; michael@0: michael@0: nsCOMPtr txMgr = do_QueryReferent(mTxnMgr); michael@0: michael@0: NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr item; michael@0: michael@0: nsresult result = NS_OK; michael@0: michael@0: if (mTxnStack) michael@0: item = mTxnStack->GetItem(aIndex); michael@0: else if (mTxnItem) michael@0: result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); michael@0: michael@0: return item->GetNumberOfChildren(aNumChildren); michael@0: } michael@0: michael@0: /* nsITransactionList getChildListForItem (in long aIndex); */ michael@0: NS_IMETHODIMP nsTransactionList::GetChildListForItem(int32_t aIndex, nsITransactionList **aTxnList) michael@0: { michael@0: NS_ENSURE_TRUE(aTxnList, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aTxnList = 0; michael@0: michael@0: nsCOMPtr txMgr = do_QueryReferent(mTxnMgr); michael@0: michael@0: NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr item; michael@0: michael@0: nsresult result = NS_OK; michael@0: michael@0: if (mTxnStack) michael@0: item = mTxnStack->GetItem(aIndex); michael@0: else if (mTxnItem) michael@0: result = mTxnItem->GetChild(aIndex, getter_AddRefs(item)); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: NS_ENSURE_TRUE(item, NS_ERROR_FAILURE); michael@0: michael@0: *aTxnList = (nsITransactionList *)new nsTransactionList(txMgr, item); michael@0: michael@0: NS_ENSURE_TRUE(*aTxnList, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: NS_ADDREF(*aTxnList); michael@0: michael@0: return NS_OK; michael@0: } michael@0: