editor/txmgr/src/nsTransactionList.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/txmgr/src/nsTransactionList.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,214 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/mozalloc.h"
    1.10 +#include "nsCOMPtr.h"
    1.11 +#include "nsDebug.h"
    1.12 +#include "nsError.h"
    1.13 +#include "nsID.h"
    1.14 +#include "nsISupportsUtils.h"
    1.15 +#include "nsITransactionManager.h"
    1.16 +#include "nsTransactionItem.h"
    1.17 +#include "nsTransactionList.h"
    1.18 +#include "nsTransactionStack.h"
    1.19 +#include "nscore.h"
    1.20 +
    1.21 +NS_IMPL_ISUPPORTS(nsTransactionList, nsITransactionList)
    1.22 +
    1.23 +nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionStack *aTxnStack)
    1.24 +  : mTxnStack(aTxnStack)
    1.25 +  , mTxnItem(0)
    1.26 +{
    1.27 +  if (aTxnMgr)
    1.28 +    mTxnMgr = do_GetWeakReference(aTxnMgr);
    1.29 +}
    1.30 +
    1.31 +nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionItem *aTxnItem)
    1.32 +  : mTxnStack(0)
    1.33 +  , mTxnItem(aTxnItem)
    1.34 +{
    1.35 +  if (aTxnMgr)
    1.36 +    mTxnMgr = do_GetWeakReference(aTxnMgr);
    1.37 +}
    1.38 +
    1.39 +nsTransactionList::~nsTransactionList()
    1.40 +{
    1.41 +  mTxnStack = 0;
    1.42 +  mTxnItem  = 0;
    1.43 +}
    1.44 +
    1.45 +/* readonly attribute long numItems; */
    1.46 +NS_IMETHODIMP nsTransactionList::GetNumItems(int32_t *aNumItems)
    1.47 +{
    1.48 +  NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
    1.49 +
    1.50 +  *aNumItems = 0;
    1.51 +
    1.52 +  nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
    1.53 +
    1.54 +  NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
    1.55 +
    1.56 +  nsresult result = NS_OK;
    1.57 +
    1.58 +  if (mTxnStack)
    1.59 +    *aNumItems = mTxnStack->GetSize();
    1.60 +  else if (mTxnItem)
    1.61 +    result = mTxnItem->GetNumberOfChildren(aNumItems);
    1.62 +
    1.63 +  return result;
    1.64 +}
    1.65 +
    1.66 +/* boolean itemIsBatch (in long aIndex); */
    1.67 +NS_IMETHODIMP nsTransactionList::ItemIsBatch(int32_t aIndex, bool *aIsBatch)
    1.68 +{
    1.69 +  NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
    1.70 +
    1.71 +  *aIsBatch = false;
    1.72 +
    1.73 +  nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
    1.74 +
    1.75 +  NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
    1.76 +
    1.77 +  nsRefPtr<nsTransactionItem> item;
    1.78 +
    1.79 +  nsresult result = NS_OK;
    1.80 +
    1.81 +  if (mTxnStack)
    1.82 +    item = mTxnStack->GetItem(aIndex);
    1.83 +  else if (mTxnItem)
    1.84 +    result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
    1.85 +
    1.86 +  NS_ENSURE_SUCCESS(result, result);
    1.87 +
    1.88 +  NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
    1.89 +
    1.90 +  return item->GetIsBatch(aIsBatch);
    1.91 +}
    1.92 +
    1.93 +/* void getData (in long aIndex,
    1.94 +                 [optional] out unsigned long aLength,
    1.95 +                 [array, size_is (aLength), retval]
    1.96 +                 out nsISupports aData); */
    1.97 +NS_IMETHODIMP nsTransactionList::GetData(int32_t aIndex,
    1.98 +                                         uint32_t *aLength,
    1.99 +                                         nsISupports ***aData)
   1.100 +{
   1.101 +  nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   1.102 +
   1.103 +  NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   1.104 +
   1.105 +  nsRefPtr<nsTransactionItem> item;
   1.106 +
   1.107 +  if (mTxnStack) {
   1.108 +    item = mTxnStack->GetItem(aIndex);
   1.109 +  } else if (mTxnItem) {
   1.110 +    nsresult result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   1.111 +    NS_ENSURE_SUCCESS(result, result);
   1.112 +  }
   1.113 +
   1.114 +  nsCOMArray<nsISupports>& data = item->GetData();
   1.115 +
   1.116 +  nsISupports** ret = static_cast<nsISupports**>(NS_Alloc(data.Count() *
   1.117 +    sizeof(nsISupports*)));
   1.118 +
   1.119 +  for (int32_t i = 0; i < data.Count(); i++) {
   1.120 +    NS_ADDREF(ret[i] = data[i]);
   1.121 +  }
   1.122 +
   1.123 +  *aLength = data.Count();
   1.124 +  *aData = ret;
   1.125 +
   1.126 +  return NS_OK;
   1.127 +}
   1.128 +
   1.129 +/* nsITransaction getItem (in long aIndex); */
   1.130 +NS_IMETHODIMP nsTransactionList::GetItem(int32_t aIndex, nsITransaction **aItem)
   1.131 +{
   1.132 +  NS_ENSURE_TRUE(aItem, NS_ERROR_NULL_POINTER);
   1.133 +
   1.134 +  *aItem = 0;
   1.135 +
   1.136 +  nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   1.137 +
   1.138 +  NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   1.139 +
   1.140 +  nsRefPtr<nsTransactionItem> item;
   1.141 +
   1.142 +  nsresult result = NS_OK;
   1.143 +
   1.144 +  if (mTxnStack)
   1.145 +    item = mTxnStack->GetItem(aIndex);
   1.146 +  else if (mTxnItem)
   1.147 +    result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   1.148 +
   1.149 +  NS_ENSURE_SUCCESS(result, result);
   1.150 +
   1.151 +  NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
   1.152 +
   1.153 +  *aItem = item->GetTransaction().take();
   1.154 +
   1.155 +  return NS_OK;
   1.156 +}
   1.157 +
   1.158 +/* long getNumChildrenForItem (in long aIndex); */
   1.159 +NS_IMETHODIMP nsTransactionList::GetNumChildrenForItem(int32_t aIndex, int32_t *aNumChildren)
   1.160 +{
   1.161 +  NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);
   1.162 +
   1.163 +  *aNumChildren = 0;
   1.164 +
   1.165 +  nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   1.166 +
   1.167 +  NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   1.168 +
   1.169 +  nsRefPtr<nsTransactionItem> item;
   1.170 +
   1.171 +  nsresult result = NS_OK;
   1.172 +
   1.173 +  if (mTxnStack)
   1.174 +    item = mTxnStack->GetItem(aIndex);
   1.175 +  else if (mTxnItem)
   1.176 +    result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   1.177 +
   1.178 +  NS_ENSURE_SUCCESS(result, result);
   1.179 +
   1.180 +  NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
   1.181 +
   1.182 +  return item->GetNumberOfChildren(aNumChildren);
   1.183 +}
   1.184 +
   1.185 +/* nsITransactionList getChildListForItem (in long aIndex); */
   1.186 +NS_IMETHODIMP nsTransactionList::GetChildListForItem(int32_t aIndex, nsITransactionList **aTxnList)
   1.187 +{
   1.188 +  NS_ENSURE_TRUE(aTxnList, NS_ERROR_NULL_POINTER);
   1.189 +
   1.190 +  *aTxnList = 0;
   1.191 +
   1.192 +  nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   1.193 +
   1.194 +  NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   1.195 +
   1.196 +  nsRefPtr<nsTransactionItem> item;
   1.197 +
   1.198 +  nsresult result = NS_OK;
   1.199 +
   1.200 +  if (mTxnStack)
   1.201 +    item = mTxnStack->GetItem(aIndex);
   1.202 +  else if (mTxnItem)
   1.203 +    result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   1.204 +
   1.205 +  NS_ENSURE_SUCCESS(result, result);
   1.206 +
   1.207 +  NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
   1.208 +
   1.209 +  *aTxnList = (nsITransactionList *)new nsTransactionList(txMgr, item);
   1.210 +
   1.211 +  NS_ENSURE_TRUE(*aTxnList, NS_ERROR_OUT_OF_MEMORY);
   1.212 +
   1.213 +  NS_ADDREF(*aTxnList);
   1.214 +
   1.215 +  return NS_OK;
   1.216 +}
   1.217 +

mercurial