/******************************************************************************
 GMailFileTreeNode.cc

	Data structure for JXFileTree.

	Copyright <A9> 1997 by Glenn W. Bach. All rights reserved.

 *****************************************************************************/

#include "GMailFileTreeNode.h"
#include "GSimpleFileTree.h"
#include "JTreeList.h"

#include <JString.h>
#include <JPtrArray.h>
#include <JUNIXDirInfo.h>
#include <JError.h>
#include <JColormap.h>

#include <jGlobals.h>
#include <jDirUtil.h>
#include <jFileUtil.h>
#include <jStreamUtil.h>
#include <GMailboxUtils.h>

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <sys/stat.h>
#include <unistd.h>
#include <jAssert.h>


/******************************************************************************
	Constructor

******************************************************************************/

GMailFileTreeNode::GMailFileTreeNode
	(
	GSimpleFileTree* tree, 
	JUNIXDirEntry* 	entry,
	const char* 	name,
	const JBoolean 	isBranch
	)
	:
	GSimpleFileTreeNode(tree, entry, name, isBranch)
{
}

GMailFileTreeNode::GMailFileTreeNode
	(
	GSimpleFileTree* tree, 
	JUNIXDirEntry* 	entry,
	JNamedTreeNode* parent,
	const char* 	name,
	const JBoolean 	isBranch
	)
	:
	GSimpleFileTreeNode(tree, entry, parent, name, isBranch)
{
}


/******************************************************************************
	Destructor

******************************************************************************/

GMailFileTreeNode::~GMailFileTreeNode()
{
}

/******************************************************************************
 OKToAppendChild

 ******************************************************************************/

JBoolean	
GMailFileTreeNode::OKToAppendChild
	(
	const JUNIXDirEntry& entry
	)
{
	if ((entry.GetName().EndsWith(".lock")) && (entry.GetSize() == 0))
		{
		return kFalse;
		}
	JString pattern("^From[[:space:]]+.*.{3}[[:space:]]+.{3}[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+:)+[[:digit:]]+[[:space:]]+(...[[:space:]])?[[:digit:]]{4}");
	if (MatchesCookie(pattern, entry))
		{
		return kTrue;
		}
	return kFalse;
}

/******************************************************************************
 CreateChild

 ******************************************************************************/

void	
GMailFileTreeNode::CreateChild
	(
	JUNIXDirEntry* 			entry,
	GSimpleFileTreeNode** 	node,
	const JBoolean 			isBranch,
	const JBoolean			append
	)
{
	if (append)
		{
		*node = new GMailFileTreeNode(GetFileTree(), entry, 
					this, entry->GetName(), isBranch);
		assert( node != NULL);
		}
	else
		{
		*node = new GMailFileTreeNode(GetFileTree(), entry, 
					entry->GetName(), isBranch);
		assert( node != NULL);
		}
}