Back to index

lightning-sunbird  0.9+nobinonly
EditAggregateTxn.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "EditAggregateTxn.h"
00039 #include "nsCOMPtr.h"
00040 
00041 EditAggregateTxn::EditAggregateTxn()
00042   : EditTxn()
00043 {
00044   nsresult res = NS_NewISupportsArray(getter_AddRefs(mChildren));
00045   NS_POSTCONDITION(NS_SUCCEEDED(res), "EditAggregateTxn failed in constructor");
00046 }
00047 
00048 EditAggregateTxn::~EditAggregateTxn()
00049 {
00050   // nsISupportsArray cleans up array for us at destruct time
00051 }
00052 
00053 NS_IMETHODIMP EditAggregateTxn::DoTransaction(void)
00054 {
00055   nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
00056   if (mChildren)
00057   {
00058     PRInt32 i;
00059     PRUint32 count;
00060     mChildren->Count(&count);
00061     for (i=0; i<((PRInt32)count); i++)
00062     {
00063       nsCOMPtr<nsITransaction> txn (do_QueryElementAt(mChildren, i));
00064       if (!txn) { return NS_ERROR_NULL_POINTER; }
00065       result = txn->DoTransaction();
00066       if (NS_FAILED(result))
00067         break;
00068     }  
00069   }
00070   return result;
00071 }
00072 
00073 NS_IMETHODIMP EditAggregateTxn::UndoTransaction(void)
00074 {
00075   nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
00076   if (mChildren)
00077   {
00078     PRInt32 i;
00079     PRUint32 count;
00080     mChildren->Count(&count);
00081     // undo goes through children backwards
00082     for (i=count-1; i>=0; i--)
00083     {
00084       nsCOMPtr<nsITransaction> txn (do_QueryElementAt(mChildren, i));
00085       if (!txn) { return NS_ERROR_NULL_POINTER; }
00086       result = txn->UndoTransaction();
00087       if (NS_FAILED(result))
00088         break;
00089     }  
00090   }
00091   return result;
00092 }
00093 
00094 NS_IMETHODIMP EditAggregateTxn::RedoTransaction(void)
00095 {
00096   nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
00097   if (mChildren)
00098   {
00099     PRInt32 i;
00100     PRUint32 count;
00101     mChildren->Count(&count);
00102     for (i=0; i<((PRInt32)count); i++)
00103     {
00104       nsCOMPtr<nsITransaction> txn (do_QueryElementAt(mChildren, i));
00105       if (!txn) { return NS_ERROR_NULL_POINTER; }
00106       result = txn->RedoTransaction();
00107       if (NS_FAILED(result))
00108         break;
00109     }  
00110   }
00111   return result;
00112 }
00113 
00114 NS_IMETHODIMP EditAggregateTxn::GetIsTransient(PRBool *aIsTransient)
00115 {
00116   if (aIsTransient)
00117     *aIsTransient = PR_FALSE;
00118   return NS_OK;
00119 }
00120 
00121 NS_IMETHODIMP EditAggregateTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
00122 {
00123   nsresult result=NS_OK;  // it's legal (but not very useful) to have an empty child list
00124   if (aDidMerge)
00125     *aDidMerge = PR_FALSE;
00126   if (mChildren)
00127   {
00128     PRInt32 i=0;
00129     PRUint32 count;
00130     mChildren->Count(&count);
00131     NS_ASSERTION(count>0, "bad count");
00132     if (0<count)
00133     {
00134       nsCOMPtr<nsITransaction> txn (do_QueryElementAt(mChildren, i));
00135       if (!txn) { return NS_ERROR_NULL_POINTER; }
00136       result = txn->Merge(aTransaction, aDidMerge);
00137     }
00138   }
00139   return result;
00140 }
00141 
00142 NS_IMETHODIMP EditAggregateTxn::GetTxnDescription(nsAString& aString)
00143 {
00144   aString.AssignLiteral("EditAggregateTxn: ");
00145 
00146   if (mName)
00147   {
00148     nsAutoString name;
00149     mName->ToString(name);
00150     aString += name;
00151   }
00152 
00153   return NS_OK;
00154 }
00155 
00156 NS_IMETHODIMP EditAggregateTxn::AppendChild(EditTxn *aTxn)
00157 {
00158   if (mChildren && aTxn)
00159   {
00160     // aaahhhh! broken interfaces drive me crazy!!!
00161     nsCOMPtr<nsISupports> isupports;
00162     aTxn->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(isupports));
00163     mChildren->AppendElement(isupports);
00164     return NS_OK;
00165   }
00166   return NS_ERROR_NULL_POINTER;
00167 }
00168 
00169 NS_IMETHODIMP EditAggregateTxn::GetCount(PRUint32 *aCount)
00170 {
00171   if (!aCount) {
00172     return NS_ERROR_NULL_POINTER;
00173   }
00174   *aCount=0;
00175   if (mChildren) {
00176     mChildren->Count(aCount);
00177   }
00178   return NS_OK;
00179 }
00180 
00181 NS_IMETHODIMP EditAggregateTxn::GetTxnAt(PRInt32 aIndex, EditTxn **aTxn)
00182 {
00183   // preconditions
00184   NS_PRECONDITION(aTxn, "null out param");
00185   NS_PRECONDITION(mChildren, "bad internal state");
00186 
00187   if (!aTxn) {
00188     return NS_ERROR_NULL_POINTER;
00189   }
00190   *aTxn = nsnull; // initialize out param as soon as we know it's a valid pointer
00191   if (!mChildren) {
00192     return NS_ERROR_UNEXPECTED;
00193   }
00194 
00195   // get the transaction at aIndex
00196   PRUint32 txnCount;
00197   mChildren->Count(&txnCount);
00198   if (0>aIndex || ((PRInt32)txnCount)<=aIndex) {
00199     return NS_ERROR_UNEXPECTED;
00200   }
00201   // ugh, this is all wrong - what a mess we have with editor transaction interfaces
00202   mChildren->QueryElementAt(aIndex, EditTxn::GetCID(), (void**)aTxn);
00203   if (!*aTxn)
00204     return NS_ERROR_UNEXPECTED;
00205   return NS_OK;
00206 }
00207 
00208 
00209 NS_IMETHODIMP EditAggregateTxn::SetName(nsIAtom *aName)
00210 {
00211   mName = do_QueryInterface(aName);
00212   return NS_OK;
00213 }
00214 
00215 NS_IMETHODIMP EditAggregateTxn::GetName(nsIAtom **aName)
00216 {
00217   if (aName && mName)
00218   {
00219     *aName = mName;
00220     NS_ADDREF(*aName);
00221     return NS_OK;
00222   }
00223   return NS_ERROR_NULL_POINTER;
00224 }
00225 
00226 NS_IMETHODIMP_(nsrefcnt) EditAggregateTxn::AddRef(void)
00227 {
00228   return EditTxn::AddRef();
00229 }
00230 
00231 //NS_IMPL_RELEASE_INHERITED(Class, Super)
00232 NS_IMETHODIMP_(nsrefcnt) EditAggregateTxn::Release(void)
00233 {
00234   return EditTxn::Release();
00235 }
00236 
00237 //NS_IMPL_QUERY_INTERFACE_INHERITED1(Class, Super, AdditionalInterface)
00238 NS_IMETHODIMP EditAggregateTxn::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00239 {
00240   if (!aInstancePtr) return NS_ERROR_NULL_POINTER;
00241  
00242   if (aIID.Equals(EditAggregateTxn::GetCID())) {
00243     *aInstancePtr = NS_STATIC_CAST(EditAggregateTxn*, this);
00244     NS_ADDREF_THIS();
00245     return NS_OK;
00246   }
00247   return EditTxn::QueryInterface(aIID, aInstancePtr);
00248 }
00249