Back to index

lightning-sunbird  0.9+nobinonly
nsEditorTxnLog.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
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 #include "nsHTMLEditorLog.h"
00041 #include "nsEditorTxnLog.h"
00042 #include "nsPIEditorTransaction.h"
00043 
00044 #define LOCK_LOG(doc)
00045 #define UNLOCK_LOG(doc)
00046 
00047 
00048 nsEditorTxnLog::nsEditorTxnLog(nsHTMLEditorLog *aEditorLog)
00049 {
00050   mRefCnt      = 0;
00051   mIndentLevel = 0;
00052   mBatchCount  = 0;
00053   mEditorLog   = aEditorLog;
00054 }
00055 
00056 nsEditorTxnLog::~nsEditorTxnLog()
00057 {
00058 }
00059 
00060 // #define DEBUG_EDITOR_TXN_LOG_REFCNT 1
00061 
00062 #ifdef DEBUG_EDITOR_TXN_LOG_REFCNT
00063 
00064 nsrefcnt nsEditorTxnLog::AddRef(void)
00065 {
00066   return ++mRefCnt;
00067 }
00068 
00069 nsrefcnt nsEditorTxnLog::Release(void)
00070 {
00071   NS_PRECONDITION(0 != mRefCnt, "dup release");
00072   if (--mRefCnt == 0) {
00073     NS_DELETEXPCOM(this);
00074     return 0;
00075   }
00076   return mRefCnt;
00077 }
00078 
00079 #else
00080 
00081 NS_IMPL_ADDREF(nsEditorTxnLog)
00082 NS_IMPL_RELEASE(nsEditorTxnLog)
00083 
00084 #endif
00085 
00086 NS_IMPL_QUERY_INTERFACE1(nsEditorTxnLog, nsITransactionListener)
00087 
00088 NS_IMETHODIMP
00089 nsEditorTxnLog::WillDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, PRBool *aInterrupt)
00090 {
00091   LOCK_LOG(this);
00092 
00093   PrintIndent(mIndentLevel++);
00094   Write("WillDo:   ");
00095   WriteTransaction(aTransaction);
00096   Write("\n");
00097   Flush();
00098 
00099   UNLOCK_LOG(this);
00100 
00101   return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP
00105 nsEditorTxnLog::DidDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aDoResult)
00106 {
00107   LOCK_LOG(this);
00108 
00109   PrintIndent(--mIndentLevel);
00110   Write("DidDo:    ");
00111   WriteTransaction(aTransaction);
00112   Write("(");
00113   WriteInt(aDoResult);
00114   Write(")\n");
00115   Flush();
00116 
00117   UNLOCK_LOG(this);
00118 
00119   return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP
00123 nsEditorTxnLog::WillUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, PRBool *aInterrupt)
00124 {
00125   LOCK_LOG(this);
00126 
00127   PrintIndent(mIndentLevel++);
00128 
00129   if (aTransaction)
00130   {
00131     Write("WillUndo:   ");
00132     WriteTransaction(aTransaction);
00133     Write("\n");
00134   }
00135   else
00136     Write("WillUndoBatch\n");
00137 
00138   Flush();
00139 
00140   UNLOCK_LOG(this);
00141 
00142   return NS_OK;
00143 }
00144 
00145 NS_IMETHODIMP
00146 nsEditorTxnLog::DidUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aUndoResult)
00147 {
00148   LOCK_LOG(this);
00149 
00150   PrintIndent(--mIndentLevel);
00151 
00152   if (aTransaction)
00153   {
00154     Write("DidUndo:  ");
00155     WriteTransaction(aTransaction);
00156     Write("(");
00157     WriteInt(aUndoResult);
00158     Write(")\n");
00159   }
00160   else
00161   {
00162     Write("EndUndoBatch (");
00163     WriteInt(aUndoResult);
00164     Write(")\n");
00165   }
00166 
00167   Flush();
00168 
00169   UNLOCK_LOG(this);
00170 
00171   return NS_OK;
00172 }
00173 
00174 NS_IMETHODIMP
00175 nsEditorTxnLog::WillRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, PRBool *aInterrupt)
00176 {
00177   LOCK_LOG(this);
00178 
00179   PrintIndent(mIndentLevel++);
00180 
00181   if (aTransaction)
00182   {
00183     Write("WillRedo: ");
00184     WriteTransaction(aTransaction);
00185     Write("\n");
00186   }
00187   else
00188     Write("WillRedoBatch\n");
00189 
00190   Flush();
00191 
00192   UNLOCK_LOG(this);
00193 
00194   return NS_OK;
00195 }
00196 
00197 NS_IMETHODIMP
00198 nsEditorTxnLog::DidRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aRedoResult)
00199 {
00200   LOCK_LOG(this);
00201 
00202   PrintIndent(--mIndentLevel);
00203 
00204   if (aTransaction)
00205   {
00206     Write("DidRedo:  ");
00207     WriteTransaction(aTransaction);
00208     Write(" (");
00209     WriteInt(aRedoResult);
00210     Write(")\n");
00211   }
00212   else
00213   {
00214     Write("DidRedoBatch (");
00215     WriteInt(aRedoResult);
00216     Write(")\n");
00217   }
00218 
00219   Flush();
00220 
00221   UNLOCK_LOG(this);
00222 
00223   return NS_OK;
00224 }
00225 
00226 NS_IMETHODIMP
00227 nsEditorTxnLog::WillBeginBatch(nsITransactionManager *aTxMgr, PRBool *aInterrupt)
00228 {
00229   LOCK_LOG(this);
00230 
00231   PrintIndent(mIndentLevel);
00232   Write("WillBeginBatch: ");
00233   WriteInt(mBatchCount);
00234   Write("\n");
00235   Flush();
00236 
00237   UNLOCK_LOG(this);
00238 
00239   return NS_OK;
00240 }
00241 
00242 NS_IMETHODIMP
00243 nsEditorTxnLog::DidBeginBatch(nsITransactionManager *aTxMgr, nsresult aResult)
00244 {
00245   LOCK_LOG(this);
00246 
00247   PrintIndent(mIndentLevel++);
00248   Write("DidBeginBatch:  ");
00249   WriteInt(mBatchCount++);
00250   Write(" (");
00251   WriteInt(aResult);
00252   Write(")\n");
00253   Flush();
00254 
00255   UNLOCK_LOG(this);
00256 
00257   return NS_OK;
00258 }
00259 
00260 NS_IMETHODIMP
00261 nsEditorTxnLog::WillEndBatch(nsITransactionManager *aTxMgr, PRBool *aInterrupt)
00262 {
00263   LOCK_LOG(this);
00264 
00265   PrintIndent(--mIndentLevel);
00266   Write("WillEndBatch:   ");
00267   WriteInt(--mBatchCount);
00268   Write("\n");
00269   Flush();
00270 
00271   UNLOCK_LOG(this);
00272 
00273   return NS_OK;
00274 }
00275 
00276 NS_IMETHODIMP
00277 nsEditorTxnLog::DidEndBatch(nsITransactionManager *aTxMgr, nsresult aResult)
00278 {
00279   LOCK_LOG(this);
00280 
00281   PrintIndent(mIndentLevel);
00282   Write("DidEndBatch:    ");
00283   WriteInt(mBatchCount);
00284   Write(" (");
00285   WriteInt(aResult);
00286   Write(")\n");
00287   Flush();
00288 
00289   UNLOCK_LOG(this);
00290 
00291   return NS_OK;
00292 }
00293 
00294 NS_IMETHODIMP
00295 nsEditorTxnLog::WillMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool *aInterrupt)
00296 {
00297   LOCK_LOG(this);
00298 
00299   PrintIndent(mIndentLevel);
00300   Write("WillMerge:   ");
00301   WriteTransaction(aTopTransaction);
00302   Write(" <-- ");
00303   WriteTransaction(aTransaction);
00304   Write("\n");
00305   Flush();
00306 
00307   UNLOCK_LOG(this);
00308 
00309   return NS_OK;
00310 }
00311 
00312 NS_IMETHODIMP
00313 nsEditorTxnLog::DidMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool aDidMerge, nsresult aMergeResult)
00314 {
00315   LOCK_LOG(this);
00316 
00317   PrintIndent(mIndentLevel);
00318   Write("DidMerge:    ");
00319   WriteTransaction(aTopTransaction);
00320   Write(" <-- ");
00321   WriteTransaction(aTransaction);
00322   Write(" (");
00323   Write(aDidMerge ? "TRUE" : "FALSE");
00324   Write(", ");
00325   WriteInt(aMergeResult);
00326   Write(")\n");
00327   Flush();
00328 
00329   UNLOCK_LOG(this);
00330 
00331   return NS_OK;
00332 }
00333 
00334 nsresult
00335 nsEditorTxnLog::WriteTransaction(nsITransaction *aTransaction)
00336 {
00337   nsString str;
00338 
00339   nsCOMPtr<nsPIEditorTransaction> txn = do_QueryInterface(aTransaction);
00340   if (txn) {
00341     txn->GetTxnDescription(str);
00342     if (str.IsEmpty())
00343       str.AssignLiteral("<NULL>");
00344   }
00345 
00346   return Write(NS_LossyConvertUCS2toASCII(str).get());
00347 }
00348 
00349 nsresult
00350 nsEditorTxnLog::PrintIndent(PRInt32 aIndentLevel)
00351 {
00352   PRInt32 i;
00353 
00354   Write("    // ");
00355 
00356   for (i = 0; i < aIndentLevel; i++)
00357     Write("  ");
00358 
00359   return NS_OK;
00360 }
00361 
00362 nsresult
00363 nsEditorTxnLog::Write(const char *aBuffer)
00364 {
00365   if (!aBuffer)
00366     return NS_ERROR_NULL_POINTER;
00367 
00368   if (mEditorLog)
00369     mEditorLog->Write(aBuffer);
00370   else
00371   {
00372     PRInt32 len = strlen(aBuffer);
00373     if (len > 0)
00374       fwrite(aBuffer, 1, len, stdout);
00375   }
00376 
00377   return NS_OK;
00378 }
00379 
00380 nsresult
00381 nsEditorTxnLog::WriteInt(PRInt32 aInt)
00382 {
00383   if (mEditorLog)
00384     mEditorLog->WriteInt(aInt);
00385   else
00386     printf("%d", aInt);
00387 
00388   return NS_OK;
00389 }
00390 
00391 nsresult
00392 nsEditorTxnLog::Flush()
00393 {
00394   nsresult result = NS_OK;
00395 
00396 #ifdef SLOWS_THINGS_WAY_DOWN
00397 
00398   if (mEditorLog)
00399     result = mEditorLog->Flush();
00400   else
00401     fflush(stdout);
00402 
00403 #endif // SLOWS_THINGS_WAY_DOWN
00404 
00405   return result;
00406 }