Back to index

lightning-sunbird  0.9+nobinonly
nsImapMailDatabase.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-2003
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 #ifdef XP_MAC
00039 #include <stat.h>
00040 #else
00041 #include <sys/stat.h>
00042 #endif
00043 
00044 #include "msgCore.h"
00045 #include "nsImapMailDatabase.h"
00046 #include "nsDBFolderInfo.h"
00047 #include "nsLocalFolderSummarySpec.h"
00048 #include "nsIFileSpec.h"
00049 
00050 const char *kPendingHdrsScope = "ns:msg:db:row:scope:pending:all";    // scope for all offine ops table
00051 const char *kPendingHdrsTableKind = "ns:msg:db:table:kind:pending";
00052 struct mdbOid gAllPendingHdrsTableOID;
00053 
00054 nsImapMailDatabase::nsImapMailDatabase()
00055 {
00056   m_mdbAllPendingHdrsTable = nsnull;
00057 }
00058 
00059 nsImapMailDatabase::~nsImapMailDatabase()
00060 {
00061 }
00062 
00063 NS_IMETHODIMP nsImapMailDatabase::GetSummaryValid(PRBool *aResult)
00064 {
00065   NS_ENSURE_ARG_POINTER(aResult);
00066   if (m_dbFolderInfo)
00067   {
00068     PRUint32 version;
00069     m_dbFolderInfo->GetVersion(&version);
00070     *aResult = (GetCurVersion() == version);
00071   }
00072   else
00073       *aResult = PR_FALSE;
00074 
00075   return NS_OK;
00076 }
00077 
00078 NS_IMETHODIMP nsImapMailDatabase::SetSummaryValid(PRBool valid)
00079 {
00080   if (m_dbFolderInfo)
00081   {
00082     m_dbFolderInfo->SetVersion(valid ? GetCurVersion() : 0);
00083     Commit(nsMsgDBCommitType::kLargeCommit);
00084   }
00085   return NS_OK;
00086 }
00087 
00088 // IMAP does not set local file flags, override does nothing
00089 void   nsImapMailDatabase::UpdateFolderFlag(nsIMsgDBHdr * /* msgHdr */, PRBool /* bSet */, 
00090                                               MsgFlags /* flag */, nsIOFileStream ** /* ppFileStream */)
00091 {
00092 }
00093 
00094 PRBool nsImapMailDatabase::SetHdrFlag(nsIMsgDBHdr *msgHdr, PRBool bSet, MsgFlags flag)
00095 {
00096   return nsMsgDatabase::SetHdrFlag(msgHdr, bSet, flag);
00097 }
00098 
00099 // override so nsMailDatabase methods that deal with m_folderStream are *not* called
00100 NS_IMETHODIMP nsImapMailDatabase::StartBatch()
00101 {
00102   return NS_OK;
00103 }
00104 
00105 NS_IMETHODIMP nsImapMailDatabase::EndBatch()
00106 {
00107   return NS_OK;
00108 }
00109 
00110 NS_IMETHODIMP nsImapMailDatabase::DeleteMessages(nsMsgKeyArray* nsMsgKeys, nsIDBChangeListener *instigator)
00111 {
00112   return nsMsgDatabase::DeleteMessages(nsMsgKeys, instigator);
00113 }
00114 
00115 nsresult nsImapMailDatabase::AdjustExpungedBytesOnDelete(nsIMsgDBHdr *msgHdr)
00116 {
00117   PRUint32 msgFlags;
00118   msgHdr->GetFlags(&msgFlags);
00119   if (msgFlags & MSG_FLAG_OFFLINE && m_dbFolderInfo)
00120   {
00121     PRUint32 size = 0;
00122     (void)msgHdr->GetOfflineMessageSize(&size);
00123     return m_dbFolderInfo->ChangeExpungedBytes (size);
00124   }
00125   return NS_OK;
00126 }
00127 
00128 NS_IMETHODIMP nsImapMailDatabase::ForceClosed()
00129 {
00130   m_mdbAllPendingHdrsTable = nsnull;
00131   return nsMailDatabase::ForceClosed();
00132 }
00133 
00134 NS_IMETHODIMP nsImapMailDatabase::GetFolderStream(nsIOFileStream **aFileStream)
00135 {
00136   return NS_ERROR_NOT_IMPLEMENTED;
00137 }
00138 
00139 NS_IMETHODIMP nsImapMailDatabase::SetFolderStream(nsIOFileStream *aFileStream)
00140 {
00141   NS_ASSERTION(0, "Trying to set folderStream, not implemented");
00142   return NS_ERROR_NOT_IMPLEMENTED;
00143 }
00144 
00145 nsresult nsImapMailDatabase::GetAllPendingHdrsTable()
00146 {
00147   nsresult rv = NS_OK;
00148   if (!m_mdbAllPendingHdrsTable)
00149     rv = GetTableCreateIfMissing(kPendingHdrsScope, kPendingHdrsTableKind, getter_AddRefs(m_mdbAllPendingHdrsTable),
00150                                                 m_pendingHdrsRowScopeToken, m_pendingHdrsTableKindToken) ;
00151   return rv;
00152 }
00153 
00154 static const char *kFlagsName = "flags";
00155 
00156 NS_IMETHODIMP nsImapMailDatabase::AddNewHdrToDB(nsIMsgDBHdr *newHdr, PRBool notify)
00157 {
00158   nsresult rv = nsMsgDatabase::AddNewHdrToDB(newHdr, notify);
00159   if (NS_SUCCEEDED(rv))
00160   {
00161     rv = GetAllPendingHdrsTable();
00162     NS_ENSURE_SUCCESS(rv, rv);
00163     mdb_count numPendingHdrs = 0;
00164     m_mdbAllPendingHdrsTable->GetCount(GetEnv(), &numPendingHdrs);
00165     if (numPendingHdrs > 0)
00166     {
00167       mdbYarn messageIdYarn;
00168       nsCOMPtr <nsIMdbRow> pendingRow;
00169       mdbOid  outRowId;
00170   
00171       nsXPIDLCString messageId;
00172       newHdr->GetMessageId(getter_Copies(messageId));
00173       messageIdYarn.mYarn_Buf = (void*)messageId.get();
00174       messageIdYarn.mYarn_Fill = messageId.Length();
00175       messageIdYarn.mYarn_Form = 0;
00176       messageIdYarn.mYarn_Size = messageIdYarn.mYarn_Fill;
00177   
00178       m_mdbStore->FindRow(GetEnv(), m_pendingHdrsRowScopeToken, 
00179                 m_messageIdColumnToken, &messageIdYarn, &outRowId, getter_AddRefs(pendingRow));
00180       if (pendingRow)
00181       {
00182         mdb_count numCells;
00183         mdbYarn cellYarn;
00184         mdb_column cellColumn;
00185 
00186         pendingRow->GetCount(GetEnv(), &numCells);
00187         // iterate over the cells in the pending hdr setting properties on the newHdr.
00188         // we skip cell 0, which is the messageId;
00189         for (mdb_count cellIndex = 1; cellIndex < numCells; cellIndex++)
00190         {
00191           mdb_err err = pendingRow->SeekCellYarn(GetEnv(), cellIndex, &cellColumn, nsnull);
00192           if (err == 0)
00193           {
00194             err = pendingRow->AliasCellYarn(GetEnv(), cellColumn, &cellYarn);
00195             if (err == 0)
00196             {
00197               nsMsgHdr* msgHdr = NS_STATIC_CAST(nsMsgHdr*, newHdr);      // closed system, cast ok
00198               nsIMdbRow *row = msgHdr->GetMDBRow();
00199               if (row)
00200                 row->AddColumn(GetEnv(), cellColumn, &cellYarn);
00201             }
00202           }
00203         }
00204         m_mdbAllPendingHdrsTable->CutRow(GetEnv(), pendingRow);
00205         pendingRow->CutAllColumns(GetEnv());
00206       }
00207     }
00208   }
00209   return rv;
00210 }
00211 
00212 NS_IMETHODIMP nsImapMailDatabase::SetAttributesOnPendingHdr(nsIMsgDBHdr *pendingHdr, const char *property, 
00213                                   const char *propertyVal, PRInt32 flags)
00214 {
00215   NS_ENSURE_ARG_POINTER(pendingHdr);
00216   nsresult rv = GetAllPendingHdrsTable();
00217   NS_ENSURE_SUCCESS(rv, rv);
00218 
00219   mdbYarn messageIdYarn;
00220   nsCOMPtr <nsIMdbRow> pendingRow;
00221   nsIMdbRow *newHdrRow;
00222   mdbOid  outRowId;
00223   mdb_err err;
00224   nsXPIDLCString messageId;
00225   pendingHdr->GetMessageId(getter_Copies(messageId));
00226   messageIdYarn.mYarn_Buf = (void*)messageId.get();
00227   messageIdYarn.mYarn_Fill = messageId.Length();
00228   messageIdYarn.mYarn_Form = 0;
00229   messageIdYarn.mYarn_Size = messageIdYarn.mYarn_Fill;
00230 
00231   err = m_mdbStore->FindRow(GetEnv(), m_pendingHdrsRowScopeToken, 
00232             m_messageIdColumnToken, &messageIdYarn, &outRowId, getter_AddRefs(pendingRow));
00233 
00234   if (!pendingRow)
00235   {
00236     err  = m_mdbStore->NewRow(GetEnv(), m_pendingHdrsRowScopeToken, &newHdrRow);
00237     pendingRow = do_QueryInterface(newHdrRow);
00238   }
00239   NS_ENSURE_SUCCESS(err, err);
00240   if (pendingRow)
00241   {
00242     // now we need to add cells to the row to remember the messageid, property and property value, and flags. 
00243     // Then, when hdrs are added to the db, we'll check if they have a matching message-id, and if so,
00244     // set the property and flags
00245     nsXPIDLCString messageId;
00246     pendingHdr->GetMessageId(getter_Copies(messageId));
00247     // we're just going to ignore messages without a message-id. They should be rare. If SPAM messages often
00248     // didn't have message-id's, they'd be filtered on the server, most likely, and spammers would then
00249     // start putting in message-id's.
00250     if (!messageId.IsEmpty())
00251     {
00252        extern const char *kMessageIdColumnName;
00253        m_mdbAllPendingHdrsTable->AddRow(GetEnv(), pendingRow);
00254        // make sure this is the first cell so that when we ignore the first
00255        // cell in nsImapMailDatabase::AddNewHdrToDB, we're ignoring the right one
00256       (void) SetProperty(pendingRow, kMessageIdColumnName, messageId.get());
00257       (void) SetProperty(pendingRow, property, propertyVal);
00258       (void) SetUint32Property(pendingRow, kFlagsName, (PRUint32) flags);
00259     }
00260     else 
00261       return NS_ERROR_FAILURE; 
00262   }
00263   return rv;
00264 }
00265