checkpoint filter work, get filters loading

This commit is contained in:
bienvenu%netscape.com 1999-05-14 00:57:19 +00:00
parent 4a637cc152
commit a167dc8e88
8 changed files with 379 additions and 33 deletions

View File

@ -43,7 +43,7 @@ public:
static const nsIID& GetIID() { static nsIID iid = NS_IMSGFILTERLIST_IID; return iid; }
NS_IMETHOD GetFolderForFilterList(nsIMsgFolder **aFolder)= 0;
NS_IMETHOD GetFilterCount(PRInt32 *pCount)= 0;
NS_IMETHOD GetFilterCount(PRUint32 *pCount)= 0;
NS_IMETHOD GetFilterAt(PRUint32 filterIndex, nsIMsgFilter **filter)= 0;
/* these methods don't delete filters - they just change the list. FE still must
call MSG_DestroyFilter to delete a filter.

View File

@ -29,6 +29,7 @@ static const char *kImapPrefix = "//imap:";
nsMsgFilter::nsMsgFilter()
{
m_filterList = nsnull;
NS_INIT_REFCNT();
}
nsMsgFilter::~nsMsgFilter()

View File

@ -40,6 +40,7 @@ nsMsgFilterList::nsMsgFilterList(nsIOFileStream *fileStream)
nsresult rv = NS_NewISupportsArray(getter_AddRefs(m_filters));
m_loggingEnabled = PR_FALSE;
m_curFilter = nsnull;
NS_INIT_REFCNT();
}
NS_IMPL_ADDREF(nsMsgFilterList)
@ -285,7 +286,7 @@ const char *nsMsgFilterList::GetStringForAttrib(nsMsgFilterFileAttrib attrib)
nsresult nsMsgFilterList::LoadValue(nsString2 &value)
{
nsString2 valueStr;
nsString2 valueStr(eOneByte);
char curChar;
value = "";
curChar = SkipWhitespace();
@ -295,7 +296,7 @@ nsresult nsMsgFilterList::LoadValue(nsString2 &value)
return NS_MSG_FILTER_PARSE_ERROR;
}
curChar = ReadChar();
for (int i = 0; i + 2 < sizeof(valueStr); )
do
{
if (curChar == '\\')
{
@ -308,7 +309,7 @@ nsresult nsMsgFilterList::LoadValue(nsString2 &value)
}
else
{
valueStr.SetCharAt(curChar, i++) ;
valueStr += curChar;
curChar = nextChar;
}
}
@ -316,20 +317,14 @@ nsresult nsMsgFilterList::LoadValue(nsString2 &value)
{
if (curChar == (char) -1 || curChar == '"' || curChar == '\n' || curChar == '\r')
{
valueStr.SetCharAt(0, i);
value += valueStr;
break;
}
}
valueStr.SetCharAt(curChar, i++);
valueStr += curChar;
curChar = ReadChar();
if (i + 2 >= sizeof(valueStr))
{
valueStr.SetCharAt(0, i);
value += valueStr;
i = 0;
}
}
while (!m_fileStream->eof());
return NS_OK;
}
@ -538,12 +533,13 @@ nsresult nsMsgFilterList::SaveTextFilters()
{
nsresult err = NS_OK;
const char *attribStr;
int32 filterCount = m_filters->Count();
PRUint32 filterCount;
m_filters->Count(&filterCount);
attribStr = GetStringForAttrib(nsMsgFilterAttribVersion);
err = WriteIntAttr(nsMsgFilterAttribVersion, kFileVersion);
err = WriteBoolAttr(nsMsgFilterAttribLogging, m_loggingEnabled);
for (int i = 0; i < filterCount; i ++)
for (PRUint32 i = 0; i < filterCount; i ++)
{
nsMsgFilter *filter;
if (GetMsgFilterAt(i, &filter) == NS_OK && filter != nsnull)
@ -560,7 +556,10 @@ nsresult nsMsgFilterList::SaveTextFilters()
nsMsgFilterList::~nsMsgFilterList()
{
for (PRUint32 i = 0; i < m_filters->Count(); i++)
PRUint32 filterCount;
m_filters->Count(&filterCount);
for (PRUint32 i = 0; i < filterCount; i++)
{
nsIMsgFilter *filter;
if (GetFilterAt(i, &filter) == NS_OK)
@ -604,18 +603,17 @@ nsresult nsMsgFilterList::Close()
#endif
}
nsresult nsMsgFilterList::GetFilterCount(PRInt32 *pCount)
nsresult nsMsgFilterList::GetFilterCount(PRUint32 *pCount)
{
if (pCount == nsnull)
return NS_ERROR_NULL_POINTER;
*pCount = m_filters->Count();
return NS_OK;
return m_filters->Count(pCount);
}
nsresult nsMsgFilterList::GetMsgFilterAt(PRUint32 filterIndex, nsMsgFilter **filter)
{
if (! (m_filters->Count() >= filterIndex))
PRUint32 filterCount;
m_filters->Count(&filterCount);
if (! (filterCount >= filterIndex))
return NS_ERROR_INVALID_ARG;
if (filter == nsnull)
return NS_ERROR_NULL_POINTER;
@ -625,7 +623,9 @@ nsresult nsMsgFilterList::GetMsgFilterAt(PRUint32 filterIndex, nsMsgFilter **fil
nsresult nsMsgFilterList::GetFilterAt(PRUint32 filterIndex, nsIMsgFilter **filter)
{
if (! (m_filters->Count() >= filterIndex))
PRUint32 filterCount;
m_filters->Count(&filterCount);
if (! (filterCount >= filterIndex))
return NS_ERROR_INVALID_ARG;
if (filter == nsnull)
return NS_ERROR_NULL_POINTER;
@ -660,7 +660,9 @@ nsresult nsMsgFilterList::MoveFilterAt(PRUint32 filterIndex,
{
nsIMsgFilter *tempFilter;
if (! (m_filters->Count() >= filterIndex))
PRUint32 filterCount;
m_filters->Count(&filterCount);
if (! (filterCount >= filterIndex))
return NS_ERROR_INVALID_ARG;
tempFilter = (nsIMsgFilter *) m_filters->ElementAt(filterIndex);
@ -673,7 +675,7 @@ nsresult nsMsgFilterList::MoveFilterAt(PRUint32 filterIndex,
}
else if (motion == nsMsgFilterDown)
{
if (filterIndex + 1 > (PRUint32) (m_filters->Count() - 1))
if (filterIndex + 1 > filterCount - 1)
return NS_OK;
m_filters->ReplaceElementAt(m_filters->ElementAt(filterIndex + 1), filterIndex);
m_filters->ReplaceElementAt(tempFilter, filterIndex + 1);
@ -688,9 +690,11 @@ nsresult nsMsgFilterList::MoveFilterAt(PRUint32 filterIndex,
#ifdef DEBUG
void nsMsgFilterList::Dump()
{
printf("%d filters\n", m_filters->Count());
PRUint32 filterCount;
m_filters->Count(&filterCount);
printf("%d filters\n", filterCount);
for (int32 i = 0; i < m_filters->Count(); i++)
for (PRUint32 i = 0; i < filterCount; i++)
{
nsMsgFilter *filter;
if (GetMsgFilterAt(i, &filter) == NS_OK)

View File

@ -60,7 +60,7 @@ public:
virtual ~nsMsgFilterList();
NS_IMETHOD GetFolderForFilterList(nsIMsgFolder **aFolder);
NS_IMETHOD GetFilterCount(PRInt32 *pCount);
NS_IMETHOD GetFilterCount(PRUint32 *pCount);
NS_IMETHOD GetFilterAt(PRUint32 filterIndex, nsIMsgFilter **filter);
/* these methods don't delete filters - they just change the list. FE still must
call MSG_DestroyFilter to delete a filter.

View File

@ -38,6 +38,15 @@ NS_NewMsgFilterService(const nsIID& iid, void **result)
NS_END_EXTERN_C
nsMsgFilterService::nsMsgFilterService()
{
NS_INIT_REFCNT();
}
nsMsgFilterService::~nsMsgFilterService()
{
}
NS_IMETHODIMP nsMsgFilterService::QueryInterface(REFNSIID aIID, void** aResult)
{
if (aResult == NULL)
@ -55,7 +64,7 @@ NS_IMETHODIMP nsMsgFilterService::QueryInterface(REFNSIID aIID, void** aResult)
NS_IMETHODIMP nsMsgFilterService::OpenFilterList(nsFileSpec *filterFile, nsIMsgFilterList **resultFilterList)
{
nsresult ret;
nsresult ret = NS_OK;
nsIOFileStream *fileStream = new nsIOFileStream(*filterFile);
if (!fileStream)
@ -64,7 +73,9 @@ NS_IMETHODIMP nsMsgFilterService::OpenFilterList(nsFileSpec *filterFile, nsIMsgF
nsMsgFilterList *filterList = new nsMsgFilterList(fileStream);
if (!filterList)
return NS_ERROR_OUT_OF_MEMORY;
ret = filterList->LoadTextFilters();
NS_ADDREF(filterList);
if (filterFile->GetFileSize() > 0)
ret = filterList->LoadTextFilters();
if (NS_SUCCEEDED(ret))
*resultFilterList = filterList;
else

View File

@ -21,10 +21,6 @@
#include "nsIMsgFilterService.h"
//f5aacb00-072d-11d3-8d70-00805f8a6617
#define NS_MSGFILTERSERVICE_CID \
{ 0xf5aacb00, 0x072d, 0x11d3, \
{ 0x8d, 0x70, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x17 } }
NS_BEGIN_EXTERN_C
@ -39,6 +35,9 @@ class nsMsgFilterService : public nsIMsgFilterService
{
public:
nsMsgFilterService();
virtual ~nsMsgFilterService();
NS_DECL_ISUPPORTS
/* clients call OpenFilterList to get a handle to a FilterList, of existing nsMsgFilter *.
These are manipulated by the front end as a result of user interaction

View File

@ -0,0 +1,270 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
* Reserved.
*/
///////////////////////////////////////////////////////////////////////////////
// This is at test harness for mail filters.
//
// For command (1) parsing a filter file,
// You are prompted for the name of a filter file you wish to parse
// CURRENTLY this file name must be in the form of a file url:
// i.e. "D|/mozilla/MailboxFile".
#include "msgCore.h"
#include "prprf.h"
#include <stdio.h>
#include <assert.h>
#ifdef XP_PC
#include <windows.h>
#endif
#include "nsFileSpec.h"
#include "nsIPref.h"
#include "plstr.h"
#include "plevent.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsString.h"
#include "nsXPComCIID.h"
#include "nsMsgFilterService.h"
#include "nsCOMPtr.h"
#include "nsMsgBaseCID.h"
#include "nsIRDFService.h"
#include "nsIRDFDataSource.h"
#include "nsRDFCID.h"
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
/////////////////////////////////////////////////////////////////////////////////
// Define keys for all of the interfaces we are going to require for this test
/////////////////////////////////////////////////////////////////////////////////
static NS_DEFINE_CID(kMsgFilterServiceCID, NS_MSGFILTERSERVICE_CID);
/////////////////////////////////////////////////////////////////////////////////
// Define default values to be used to drive the test
/////////////////////////////////////////////////////////////////////////////////
/* strip out non-printable characters */
static void strip_nonprintable(char *string) {
char *dest, *src;
dest=src=string;
while (*src) {
if (isprint(*src)) {
(*src)=(*dest);
src++; dest++;
} else {
src++;
}
}
(*dest)='\0';
}
//////////////////////////////////////////////////////////////////////////////////
// The nsFilterTestDriver is a class that I envision could be generalized to form the
// building block of a protocol test harness. To configure it, you would list all of
// the events you know how to handle and when one of those events is triggered, you
// would be asked to process it....right now it is just Mailbox specific....
///////////////////////////////////////////////////////////////////////////////////
class nsFilterTestDriver
{
public:
nsFilterTestDriver(nsIMsgFilterService *);
virtual ~nsFilterTestDriver();
// run driver initializes the instance, lists the commands, runs the command and when
// the command is finished, it reads in the next command and continues...theoretically,
// the client should only ever have to call RunDriver(). It should do the rest of the
// work....
nsresult RunDriver();
// User drive commands
void InitializeTestDriver(); // will end up prompting the user for things like host, port, etc.
nsresult ListCommands(); // will list all available commands to the user...i.e. "get groups, get article, etc."
nsresult ReadAndDispatchCommand(); // reads a command number in from the user and calls the appropriate command generator
nsresult PromptForUserData(const char * userPrompt);
nsresult OnOpenFilterFile();
nsresult OnExit();
nsresult OnWriteFilterList();
protected:
char m_userData[500]; // generic string buffer for storing the current user entered data...
nsFileSpec m_folderSpec;
PRBool m_runTestHarness;
nsCOMPtr<nsIMsgFilterService> m_filterService;
};
nsFilterTestDriver::nsFilterTestDriver(nsIMsgFilterService *filterService) : m_folderSpec("")
{
m_runTestHarness = PR_TRUE;
m_filterService = filterService;
InitializeTestDriver(); // prompts user for initialization information...
}
nsFilterTestDriver::~nsFilterTestDriver()
{
}
nsresult nsFilterTestDriver::RunDriver()
{
nsresult status = NS_OK;
while (m_runTestHarness)
{
status = ReadAndDispatchCommand();
} // until the user has asked us to stop
return status;
}
void nsFilterTestDriver::InitializeTestDriver()
{
nsresult rv;
}
// prints the userPrompt and then reads in the user data. Assumes urlData has already been allocated.
// it also reconstructs the url string in m_urlString but does NOT reload it....
nsresult nsFilterTestDriver::PromptForUserData(const char * userPrompt)
{
char tempBuffer[500];
tempBuffer[0] = '\0';
if (userPrompt)
printf(userPrompt);
else
printf("Enter data for command: ");
fgets(tempBuffer, sizeof(tempBuffer), stdin);
strip_nonprintable(tempBuffer);
// only replace m_userData if the user actually entered a valid line...
// this allows the command function to set a default value on m_userData before
// calling this routine....
if (tempBuffer && *tempBuffer)
PL_strcpy(m_userData, tempBuffer);
return NS_OK;
}
nsresult nsFilterTestDriver::ReadAndDispatchCommand()
{
nsresult status = NS_OK;
PRInt32 command = 0;
char commandString[5];
commandString[0] = '\0';
printf("Enter command number: ");
fgets(commandString, sizeof(commandString), stdin);
strip_nonprintable(commandString);
if (commandString && *commandString)
{
command = atoi(commandString);
}
// now switch on command to the appropriate
switch (command)
{
case 0:
status = ListCommands();
break;
case 1:
status = OnOpenFilterFile();
break;
case 2:
status = OnWriteFilterList();
break;
default:
status = OnExit();
break;
}
return status;
}
nsresult nsFilterTestDriver::ListCommands()
{
printf("Commands currently available: \n");
printf("0) List commands. \n");
printf("1) Open a filter file. \n");
printf("2) Output a filter file. \n");
printf("9) Exit the test application. \n");
return NS_OK;
}
nsresult nsFilterTestDriver::OnExit()
{
printf("Terminating Mailbox test harness....\n");
m_runTestHarness = PR_FALSE; // next time through the test driver loop, we'll kick out....
return NS_OK;
}
nsresult nsFilterTestDriver::OnOpenFilterFile()
{
nsCOMPtr <nsIMsgFilterList> filterList;
PromptForUserData("enter filter file name");
nsFileSpec filterFile(m_userData);
if (filterFile.Exists())
{
nsresult res = m_filterService->OpenFilterList(&filterFile, getter_AddRefs(filterList));
if (NS_SUCCEEDED(res))
{
}
}
return NS_OK;
}
nsresult nsFilterTestDriver::OnWriteFilterList()
{
return NS_OK;
}
int main()
{
nsresult rv;
NS_WITH_SERVICE(nsIRDFService, rdf, kRDFServiceCID, &rv);
NS_WITH_SERVICE(nsIMsgFilterService, filterService, kMsgFilterServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
// okay, everything is set up, now we just need to create a test driver and run it...
nsFilterTestDriver * driver = new nsFilterTestDriver(filterService);
if (driver)
{
driver->RunDriver();
// when it kicks out...it is done....so delete it...
delete driver;
}
// shut down:
return 0;
}

View File

@ -0,0 +1,61 @@
#!nmake
#
# The contents of this file are subject to the Netscape Public License
# Version 1.0 (the "NPL"); you may not use this file except in
# compliance with the NPL. You may obtain a copy of the NPL at
# http://www.mozilla.org/NPL/
#
# Software distributed under the NPL is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
# for the specific language governing rights and limitations under the
# NPL.
#
# The Initial Developer of this code under the NPL is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All Rights
# Reserved.
DEPTH=..\..\..\..
IGNORE_MANIFEST=1
MAKE_OBJ_TYPE = EXE
PROGRAM = .\$(OBJDIR)\filterTest.exe
OBJS = \
.\$(OBJDIR)\filterTest.obj \
$(NULL)
LINCS= \
-I$(PUBLIC)\raptor \
-I$(PUBLIC)\xpcom \
-I$(PUBLIC)\netlib \
-I$(PUBLIC)\mailnews \
-I$(PUBLIC)\security \
-I$(PUBLIC)\pref \
-I$(PUBLIC)\js \
-I$(PUBLIC)\rdf
MYLIBS= \
$(DIST)\lib\xpcom32.lib \
$(LIBNSPR) \
$(DIST)\lib\raptorbase.lib \
$(DIST)\lib\raptorgfxwin.lib \
$(DIST)\lib\raptorwidget.lib \
$(DIST)\lib\plc3.lib \
$(DIST)\lib\msgcore.lib \
$(DIST)\lib\xplib.lib \
$(NULL)
LLIBS= $(MYLIBS) \
-SUBSYSTEM:CONSOLE
include <$(DEPTH)\config\rules.mak>
install:: $(PROGRAM)
$(MAKE_INSTALL) $(PROGRAM) $(DIST)\bin
clobber::
rm -f $(DIST)\bin\filterTest.exe
$(PROGRAM):: $(OBJS) $(MYLIBS)