/******************************************************************************
 SyGFileTreeNode.cc

	Data structure for JXFileTree.

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

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

#include "SyGFileTreeNode.h"
#include "SyGFileTree.h"
#include "SyGFileTreeList.h"

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

#include <jGlobals.h>

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


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

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

SyGFileTreeNode::SyGFileTreeNode
(
	SyGFileTree* 		tree,
	JUNIXDirEntry* 	entry,
	JNamedTreeNode* parent,
	const char* 	name,
	const JBoolean 	isBranch,
	const JBoolean 	isTemporary
)
	:
	GSimpleFileTreeNode(tree, entry, name, isBranch)
{
}


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

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

SyGFileTreeNode::~SyGFileTreeNode()
{
}

/******************************************************************************
	IsExecutable

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

JBoolean 
SyGFileTreeNode::IsExecutable() 
	const
{
	return GetDirEntry()->IsExecutable();
}

/******************************************************************************
	IsWritable

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

JBoolean 
SyGFileTreeNode::IsWritable() 
	const
{
	return GetDirEntry()->IsWritable();
}

/******************************************************************************
	IsReadable

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

JBoolean 
SyGFileTreeNode::IsReadable() 
	const
{
	return GetDirEntry()->IsReadable();
}

/******************************************************************************
 OKToChange

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

JBoolean 
SyGFileTreeNode::OKToChange()
	const
{
	return IsWritable();
}


/******************************************************************************
 GoTo

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

JError
SyGFileTreeNode::GoTo
	(
	const JCharacter* path
	)
{
	JError err = GetDirInfo()->GoTo(path);
	if (err.OK())
		{
		DeleteAll();
		SetFullNameAndUpdate(path);
		if (GetName().GetLastCharacter() != '/')
			{
			SetName(GetName() + "/");
			}
		BuildChildList();
		JTree* tree			= GetTree();
		SyGFileTree* stree	= dynamic_cast(SyGFileTree*, tree);
		assert(stree != NULL);
		stree->BroadcastPathChanged();
		}
	return err;
}

/******************************************************************************
 TurnOnFilter

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

void
SyGFileTreeNode::TurnOnFilter
	(
	const JCharacter* filter
	)
{
	if (GetDirInfo() != NULL)
		{
		GetDirInfo()->SetWildcardFilter(filter);
		UpdateChildren();
		const JSize count = GetChildCount();
		for (JIndex i = 1; i <= count; i++)
			{
			SyGFileTreeNode* node = dynamic_cast(SyGFileTreeNode*, GetChild(i));
			assert (node != NULL);
			node->TurnOnFilter(filter);
			}
		}
}

/******************************************************************************
 ShowHidden

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

void
SyGFileTreeNode::ShowHidden
	(
	const JBoolean show
	)
{
	if (GetDirInfo() != NULL)
		{
		GetDirInfo()->ShowHidden(show);
		UpdateChildren();
		JSize count = GetChildCount();
		for (JSize i = 1; i <= count; i++)
			{
			SyGFileTreeNode* node = dynamic_cast(SyGFileTreeNode*, GetChild(i));
			assert (node != NULL);
			node->ShowHidden(show);
			}
		}
}

/******************************************************************************
 CreateChild (protected)

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

void
SyGFileTreeNode::CreateChild
	(
	JUNIXDirEntry* 			entry,
	GSimpleFileTreeNode** 	node,
	const JBoolean 			isBranch,
	const JBoolean			append
	)
{
	JTree* tree			= NULL;
	SyGFileTree* stree	= NULL;

	if (GetTree(&tree))
		{
		stree = dynamic_cast(SyGFileTree*, tree);
		assert(stree != NULL);
		}
	
	*node = new SyGFileTreeNode(stree, entry, 
				NULL, entry->GetName(), isBranch);
	assert( node != NULL);
	if (append)
		{
		InsertSorted(*node);
		}
}

/******************************************************************************
 OKToOpen (virtual protected)

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

JBoolean
SyGFileTreeNode::OKToOpen()
	const
{
	if (!GSimpleFileTreeNode::OKToOpen())
		{
		return kFalse;
		}

	const JTree* ctree	= NULL;
	SyGFileTree* stree	= NULL;

	if (GetTree(&ctree))
		{
		JTree* tree		= const_cast<JTree*>(ctree);
		stree = dynamic_cast(SyGFileTree*, tree);
		assert(stree != NULL);
		(const_cast<SyGFileTreeNode*>(this))->TurnOnFilter(stree->GetFilter());
		stree->ReApplySort();
		}

	return kTrue;
}

/******************************************************************************
 ChangeSort (public)

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

void
SyGFileTreeNode::ChangeSortByType
	(
	GFMColType type
	)
{
	if (type == kGFMUID)
		{
		ChangeSort(GSimpleFileTreeNode::CompareUser, JOrderedSetT::kSortAscending);
		}
	else if (type == kGFMGID)
		{
		ChangeSort(GSimpleFileTreeNode::CompareGroup, JOrderedSetT::kSortAscending);
		}
	else if (type == kGFMSize)
		{
		ChangeSort(GSimpleFileTreeNode::CompareSize, JOrderedSetT::kSortDescending);
		}
	else if (type == kGFMDate)
		{
		ChangeSort(GSimpleFileTreeNode::CompareDate, JOrderedSetT::kSortDescending);
		}
	else
		{
		ChangeSort(JNamedTreeNode::DynamicCastCompareNames, JOrderedSetT::kSortAscending);
		}
}

/******************************************************************************
 PathChanged (virtual protected)

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

void
SyGFileTreeNode::PathChanged()
{
	JTree* tree			= GetTree();
	SyGFileTree* stree	= dynamic_cast(SyGFileTree*, tree);
	assert(stree != NULL);
	stree->BroadcastPathChanged();
}
