//		Written by Todd Thomas Copyright (c) 1995 by Todd Thomas.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <misckit/MiscUser.h>
#import <misckit/MiscUserGroup.h>
#import "UIMgr.h"
#import "Controller.h"


@implementation Controller

- init
{
	[super init];
	displayList = nil;
	return self;
}


- free
{
	if (displayList != nil)
		[ [displayList freeObjects] free];
	return [super free];
}


- loadAllUsers
{
	// Load all the users into the display list which will be what
	// is displayed in the browser.
	if (displayList != nil)
		[ [displayList freeObjects] free];
	
	displayList = [MiscUser allUsers];
	return self;
}


- loadAllGroups
{
	// Load all the groups into the display list.
	if (displayList != nil)
		[ [displayList freeObjects] free];
	
	displayList = [MiscUserGroup allGroups];
	return self;
}


- (BOOL)lookupGid: (int)gid
{
	MiscUserGroup  *group = [ [MiscUserGroup alloc] initWithGroupId: gid];
	if (group == nil)
		return NO;
	
	[uiMgr setGroupName: [group groupName] ];
	if (displayList != nil)
		[ [displayList freeObjects] free];
	displayList = [group members];
	
	[group free];
	return YES;
}
	
	
- (BOOL)lookupGroupName: (const char *)gname
{
	MiscUserGroup  *group = [ [MiscUserGroup alloc] initWithGroupName: gname];
	if (group == nil)
		return NO;
		
	[uiMgr setGid: [group groupId] ];
	if (displayList != nil)
		[ [displayList freeObjects] free];
	displayList = [group members];

	[group free];
	return YES;
}


- (BOOL)lookupUid: (int)uid
{
	MiscUser  *user = [ [MiscUser alloc] initWithUserId: uid];
	if (user == nil)
		return NO;
		
	[uiMgr setUsername: [user username] ];
	[uiMgr setFullName: [user realname] ];
	[uiMgr setHomeDirectory: [user homeDirectory] ];
	[uiMgr setShell: [user shell] ];
	if (displayList != nil)
		[ [displayList freeObjects] free];
	displayList = [user groups];
	
	[user free];
	return YES;
}


- (BOOL)lookupUsername: (const char *)uname
{
	MiscUser  *user = [ [MiscUser alloc] initWithUsername: uname];
	if (user == nil)
		return NO;
	
	[uiMgr setUid: [user userId] ];
	[uiMgr setFullName: [user realname] ];
	[uiMgr setHomeDirectory: [user homeDirectory] ];
	[uiMgr setShell: [user shell] ];

	if (displayList != nil)
		[ [displayList freeObjects] free];
	displayList = [user groups];
	
	[user free];
	return YES;
}	

@end


@implementation Controller (BrowserDelegate)

- (int)browser:sender fillMatrix:matrix inColumn:(int)column
{
	id  anObject;
  	id  cell = nil;
  	int  i;
	
	for (i=0; i<[displayList count]; i++)
	{
		[matrix insertRowAt: i];
		cell = [matrix cellAt: i :0];
		anObject = [displayList objectAt: i];
		
		// The list can either be full of MiscUsers or MiscUserGroups,
		// so if it doesn't respond to one method, send it the other.
		if ([anObject respondsTo: @selector(username)])
        	[cell setStringValue: [anObject username] ];
		else
			[cell setStringValue: [anObject groupName] ];
		[cell setLoaded: YES];		
		[cell setLeaf: YES];
	 }

    return [displayList count];
}

@end

