Back to index

lightning-sunbird  0.9+nobinonly
nsNNTPArticleList.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  *
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"    // precompiled header...
00039 
00040 #include "nsCOMPtr.h"
00041 #include "nsNNTPArticleList.h"
00042 #include "nsIMsgFolder.h"
00043 
00044 NS_IMPL_ISUPPORTS1(nsNNTPArticleList, nsINNTPArticleList)
00045 
00046 nsNNTPArticleList::nsNNTPArticleList()
00047 {
00048 }
00049 
00050 nsNNTPArticleList::~nsNNTPArticleList()
00051 {
00052   if (m_newsDB) {
00053     m_newsDB->Commit(nsMsgDBCommitType::kSessionCommit);
00054     m_newsDB->Close(PR_TRUE);
00055     m_newsDB = nsnull;
00056   }
00057   
00058   m_newsFolder = nsnull;
00059 }
00060 
00061 NS_IMETHODIMP
00062 nsNNTPArticleList::Initialize(nsIMsgNewsFolder *newsFolder)
00063 {
00064     nsresult rv;
00065     NS_ENSURE_ARG_POINTER(newsFolder);
00066     
00067     m_dbIndex = 0;
00068 
00069     m_newsFolder = newsFolder;
00070 
00071     nsCOMPtr <nsIMsgFolder> folder = do_QueryInterface(m_newsFolder, &rv);
00072     NS_ENSURE_SUCCESS(rv,rv);
00073 
00074     rv = folder->GetMsgDatabase(nsnull /* msgWindow */, getter_AddRefs(m_newsDB));
00075     NS_ENSURE_SUCCESS(rv,rv);
00076     if (!m_newsDB) return NS_ERROR_UNEXPECTED;
00077 
00078     rv = m_newsDB->ListAllKeys(m_idsInDB);
00079     NS_ENSURE_SUCCESS(rv,rv);
00080 
00081     return NS_OK;
00082 }
00083 
00084 NS_IMETHODIMP
00085 nsNNTPArticleList::AddArticleKey(PRInt32 key)
00086 {
00087 #ifdef DEBUG
00088   m_idsOnServer.Add(key);
00089 #endif
00090   
00091   if (m_dbIndex < m_idsInDB.GetSize())
00092   {
00093     PRInt32 idInDBToCheck = m_idsInDB.GetAt(m_dbIndex);
00094     // if there are keys in the database that aren't in the newsgroup
00095     // on the server, remove them. We probably shouldn't do this if
00096     // we have a copy of the article offline.
00097     while (idInDBToCheck < key)
00098     {
00099       m_newsFolder->RemoveMessage(idInDBToCheck);
00100 #ifdef DEBUG
00101       m_idsDeleted.Add(idInDBToCheck);
00102 #endif
00103       if (m_dbIndex >= m_idsInDB.GetSize())
00104         break;
00105       idInDBToCheck = m_idsInDB.GetAt(++m_dbIndex);
00106     }
00107     if (idInDBToCheck == key)
00108       m_dbIndex++;
00109   }
00110   return NS_OK;
00111 }
00112 
00113 NS_IMETHODIMP
00114 nsNNTPArticleList::FinishAddingArticleKeys()
00115 {
00116   // if the last n messages in the group are cancelled, they won't have gotten removed
00117   // so we have to go an removed them now.
00118   PRUint32 totalCount = m_idsInDB.GetSize();
00119   PRUint32 i;
00120 
00121   for (i = m_dbIndex; i < totalCount; i++) {
00122     m_newsFolder->RemoveMessage(m_idsInDB.GetAt(i));
00123 #ifdef DEBUG
00124     m_idsDeleted.Add(m_idsInDB.GetAt(i));
00125 #endif
00126   }
00127 
00128 #ifdef DEBUG
00129        // make sure none of the deleted turned up on the idsOnServer list
00130        for (i = 0; i < m_idsDeleted.GetSize(); i++) {
00131               NS_ASSERTION(m_idsOnServer.FindIndex((nsMsgKey)(m_idsDeleted.GetAt(i)), 0) == nsMsgViewIndex_None, "a deleted turned up on the idsOnServer list");
00132   }
00133 #endif
00134        return NS_OK;
00135 }