Back to index

lightning-sunbird  0.9+nobinonly
nsMsgFolderCacheElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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) 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 "msgCore.h"
00039 #include "nsMsgFolderCacheElement.h"
00040 #include "prmem.h"
00041 #include "nsISupportsObsolete.h"
00042 
00043 nsMsgFolderCacheElement::nsMsgFolderCacheElement()
00044 {
00045   m_mdbRow = nsnull;
00046   m_owningCache = nsnull;
00047   m_folderKey = nsnull;
00048 }
00049 
00050 nsMsgFolderCacheElement::~nsMsgFolderCacheElement()
00051 {
00052   NS_IF_RELEASE(m_mdbRow);
00053   // circular reference, don't do it.
00054   // NS_IF_RELEASE(m_owningCache);
00055   PR_FREEIF(m_folderKey);
00056 }
00057 
00058 
00059 NS_IMPL_ISUPPORTS1(nsMsgFolderCacheElement, nsIMsgFolderCacheElement)
00060 
00061 NS_IMPL_GETTER_STR(nsMsgFolderCacheElement::GetKey, m_folderKey)
00062 NS_IMPL_SETTER_STR(nsMsgFolderCacheElement::SetKey, m_folderKey)
00063 
00064 void nsMsgFolderCacheElement::SetOwningCache(nsMsgFolderCache *owningCache)
00065 {
00066   m_owningCache = owningCache;
00067   // circular reference, don't do it.
00068   //  if (owningCache)
00069   //    NS_ADDREF(owningCache);
00070 }
00071 
00072 NS_IMETHODIMP         nsMsgFolderCacheElement::GetStringProperty(const char *propertyName, char **result)
00073 {
00074   if (!propertyName || !result || !m_mdbRow || !m_owningCache)
00075     return NS_ERROR_NULL_POINTER;
00076   
00077   mdb_token   property_token;
00078   
00079   nsresult ret = m_owningCache->GetStore()->StringToToken(m_owningCache->GetEnv(),  propertyName, &property_token);
00080   if (ret == NS_OK)
00081   {
00082     ret = m_owningCache->RowCellColumnToCharPtr(m_mdbRow, property_token, result);
00083   }
00084   return ret;
00085 }
00086 
00087 NS_IMETHODIMP nsMsgFolderCacheElement::GetInt32Property(const char *propertyName, PRInt32 *aResult)
00088 {
00089   if (!propertyName || !aResult || !m_mdbRow)
00090     return NS_ERROR_NULL_POINTER;
00091   
00092   char *resultStr = nsnull;
00093   GetStringProperty(propertyName, &resultStr);
00094   if (!resultStr)
00095     return NS_ERROR_NULL_POINTER;
00096   
00097   PRInt32 result;
00098   char *p = resultStr;
00099   
00100   for (result = 0; *p; p++)
00101   {
00102     char C = *p;
00103     
00104     PRInt8 unhex = ((C >= '0' && C <= '9') ? C - '0' :
00105     ((C >= 'A' && C <= 'F') ? C - 'A' + 10 :
00106      ((C >= 'a' && C <= 'f') ? C - 'a' + 10 : -1)));
00107     if (unhex < 0)
00108      break;
00109     result = (result << 4) | unhex;
00110   }
00111   
00112   PR_Free(resultStr);
00113   *aResult = result;
00114   
00115   return NS_OK;
00116 }
00117 
00118 NS_IMETHODIMP nsMsgFolderCacheElement::SetStringProperty(const char *propertyName, const char *propertyValue)
00119 {
00120   if (!propertyName || !m_mdbRow)
00121     return NS_ERROR_NULL_POINTER;
00122   nsresult err = NS_OK;
00123   mdb_token   property_token;
00124   
00125   if (m_owningCache)
00126   {
00127     err = m_owningCache->GetStore()->StringToToken(m_owningCache->GetEnv(),  propertyName, &property_token);
00128     if (err == NS_OK)
00129     {
00130       struct mdbYarn yarn;
00131       
00132       yarn.mYarn_Grow = NULL;
00133       if (m_mdbRow)
00134       {
00135         yarn.mYarn_Buf = (void *) propertyValue;
00136         yarn.mYarn_Size = PL_strlen((const char *) yarn.mYarn_Buf) + 1;
00137         yarn.mYarn_Fill = yarn.mYarn_Size - 1;
00138         yarn.mYarn_Form = 0;       // what to do with this? we're storing csid in the msg hdr...
00139         err = m_mdbRow->AddColumn(m_owningCache->GetEnv(), property_token, &yarn);
00140         return err;
00141       }
00142     }
00143   }
00144   return err;
00145 }
00146 
00147 NS_IMETHODIMP nsMsgFolderCacheElement::SetInt32Property(const char *propertyName, PRInt32 propertyValue)
00148 {
00149   if (!propertyName || !m_mdbRow)
00150     return NS_ERROR_NULL_POINTER;
00151   nsCAutoString propertyStr;
00152   propertyStr.AppendInt(propertyValue, 16);
00153   return SetStringProperty(propertyName, propertyStr.get());
00154   
00155 }
00156 
00157 void nsMsgFolderCacheElement::SetMDBRow(nsIMdbRow       *row) 
00158 {
00159   if (m_mdbRow)
00160     NS_RELEASE(m_mdbRow);
00161   m_mdbRow = row;
00162   if (row)
00163     NS_ADDREF(row);
00164 }