From 399c89c16445038c451f04403fe0f0e07e0ee5dc Mon Sep 17 00:00:00 2001 From: "dmose%netscape.com" Date: Thu, 26 Sep 2002 00:22:53 +0000 Subject: [PATCH] Make it possible to get an nsMsgKeyArray from an nsMsgKeySet, and to get the list of NEW messages from nsIMsgDatabase (bug 170841). r=naving@netscape.com, sr=bienvenu@netscape.com --- mailnews/base/util/nsMsgKeySet.cpp | 64 ++++++++++++++++++++- mailnews/base/util/nsMsgKeySet.h | 10 ++++ mailnews/db/msgdb/public/nsIMsgDatabase.idl | 12 +++- mailnews/db/msgdb/src/nsMsgDatabase.cpp | 17 ++++++ 4 files changed, 100 insertions(+), 3 deletions(-) diff --git a/mailnews/base/util/nsMsgKeySet.cpp b/mailnews/base/util/nsMsgKeySet.cpp index 00f245acc524..4d2725af1bd2 100644 --- a/mailnews/base/util/nsMsgKeySet.cpp +++ b/mailnews/base/util/nsMsgKeySet.cpp @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ***** BEGIN LICENSE BLOCK ***** * Version: NPL 1.1/GPL 2.0/LGPL 2.1 * @@ -16,7 +16,7 @@ * * The Initial Developer of the Original Code is * Netscape Communications Corporation. - * Portions created by the Initial Developer are Copyright (C) 1998 + * Portions created by the Initial Developer are Copyright (C) 1998, 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): @@ -42,6 +42,7 @@ #include "nsMsgKeySet.h" #include "prprf.h" #include "prmem.h" +#include "nsMsgKeyArray.h" #if defined(DEBUG_seth_) || defined(DEBUG_sspitzer_) #define DEBUG_MSGKEYSET 1 @@ -1174,7 +1175,66 @@ nsMsgKeySet::LastMissingRange(PRInt32 min, PRInt32 max, return 0; } +/** + * Return a copy of this as an nsMsgKeyArray, which is much easier for + * callers to manipulate. Normal XPCOM calling conventions, although the + * array itself isn't refcounted, so the caller should free when done + * using NS_DELETEXPCOM(). + */ +nsresult +nsMsgKeySet::ToMsgKeyArray(nsMsgKeyArray **aArray) +{ + PRInt32 size; + PRInt32 *head; + PRInt32 *tail; + PRInt32 *end; + PRInt32 last_art = -1; + nsMsgKeyArray *array; + NS_NEWXPCOM(array, nsMsgKeyArray); + if (!array) + return NS_ERROR_OUT_OF_MEMORY; + + size = m_length; + head = m_data; + tail = head; + end = head + size; + + while (tail < end) { + PRInt32 from; + PRInt32 to; + + if (*tail < 0) { + /* it's a range */ + from = tail[1]; + to = from + (-(tail[0])); + tail += 2; + } + else /* it's a literal */ + { + from = *tail; + to = from; + tail++; + } + if (from == 0) { + from = 1; /* See 'hack' comment above ### */ + } + if (from <= last_art) from = last_art + 1; + if (from <= to) { + if (from < to) { + for (PRInt32 i = from; i < to ; ++i ) { + array->Add(i); + } + } else { + array->Add(from); + } + last_art = to; + } + } + + *aArray = array; + return NS_OK; +} #ifdef DEBUG /* A lot of test cases for the above */ diff --git a/mailnews/base/util/nsMsgKeySet.h b/mailnews/base/util/nsMsgKeySet.h index 302968db38df..9693c3425cb1 100644 --- a/mailnews/base/util/nsMsgKeySet.h +++ b/mailnews/base/util/nsMsgKeySet.h @@ -40,6 +40,8 @@ #include "msgCore.h" +class nsMsgKeyArray; + // nsMsgKeySet represents a set of articles. Typically, it is the set of // read articles from a .newsrc file, but it can be used for other purposes // too. @@ -99,6 +101,14 @@ public: void SetLastMember(PRInt32 highWaterMark); // For debugging only... PRInt32 getLength() {return m_length;} + + /** + * Return a copy of this as an nsMsgKeyArray, which is much easier for + * callers to manipulate. Normal XPCOM calling conventions, although the + * array itself isn't refcounted, so the caller should free when done + * using NS_DELETEXPCOM(). + */ + nsresult ToMsgKeyArray(nsMsgKeyArray **aArray); #ifdef DEBUG static void RunTests(); diff --git a/mailnews/db/msgdb/public/nsIMsgDatabase.idl b/mailnews/db/msgdb/public/nsIMsgDatabase.idl index 32ea22201f2d..8dc10116314d 100644 --- a/mailnews/db/msgdb/public/nsIMsgDatabase.idl +++ b/mailnews/db/msgdb/public/nsIMsgDatabase.idl @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* ***** BEGIN LICENSE BLOCK ***** * Version: NPL 1.1/GPL 2.0/LGPL 2.1 * @@ -273,5 +273,15 @@ interface nsIMsgDatabase : nsIDBChangeAnnouncer { attribute unsigned long msgHdrCacheSize; void setFolderStream(in nsIOFileStream aFileStream); + + /** + * The list of messages currently in the NEW state. + * + * If there are no such messages, a null pointer may be returned. + * Note that the since the structure itself is not refcounted, + * the caller should free when done using NS_DELETEXPCOM(). + */ + [noscript] + readonly attribute nsMsgKeyArrayPtr newList; }; diff --git a/mailnews/db/msgdb/src/nsMsgDatabase.cpp b/mailnews/db/msgdb/src/nsMsgDatabase.cpp index 22ae6412b001..712fcf6f4167 100644 --- a/mailnews/db/msgdb/src/nsMsgDatabase.cpp +++ b/mailnews/db/msgdb/src/nsMsgDatabase.cpp @@ -4425,3 +4425,20 @@ NS_IMETHODIMP nsMsgDatabase::SetFolderStream(nsIOFileStream *aFileStream) return NS_ERROR_NOT_IMPLEMENTED; } +/** + * [noscript] readonly attribute nsMsgKeySetPtr newList; + */ +NS_IMETHODIMP +nsMsgDatabase::GetNewList(nsMsgKeyArray * *aNewList) +{ + NS_ENSURE_ARG_POINTER(aNewList); + + if (m_newSet) { + return m_newSet->ToMsgKeyArray(aNewList); + } + + // if there were no new messages, signal this by returning a null pointer + // + *aNewList = 0; + return NS_OK; +}