//---------------------------------------------------------------------------------------
//	MailAddressList.m created by erik on Tue 05-May-1998
//	This code is part of the Alexandra Newsreader Project. For copyright details see
//	GNU public license version 2 or above. No warranties implied. Use at own risk.
//	More information can be found at <http://www.object-factory.com/Alexandra>.
//	@(#)$Id: MailAddressList.m,v 1.1 1998/05/05 15:29:43 erik Exp $
//---------------------------------------------------------------------------------------

#import "Utilities.h"
#import "Message.h"


//-------------------------------------------------------------------------------------------
    @implementation MailAddressList
//-------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------
//	Factory
//-------------------------------------------------------------------------------------------

+ mailAddressListWithString:(NSString *)string;
{
    return [[[self alloc] initWithString:string] autorelease];
}


+ mailAddressListWithAddresses:(NSArray *)array;
{
    return [[[self alloc] initWithAddresses:array] autorelease];
}


//-------------------------------------------------------------------------------------------
//	Initialiser
//-------------------------------------------------------------------------------------------

- init
{
    [super init];
    addresses = [[NSMutableArray allocWithZone:[self zone]] init];
    return self;
}


- initWithString:(NSString *)stringList;
{
    NSEnumerator	*stringEnum;
    NSString		*string;
    MailAddress		*address;
    
    [self init];
    stringEnum = [[stringList componentsSeparatedByString:@","] objectEnumerator];
    while((string = [stringEnum nextObject]) != nil)
        {
        address = [[[MailAddress allocWithZone:[self zone]] initWithString:string] autorelease];
        [addresses addObject:address];
        }

    return self;
}


- initWithAddresses:(NSArray *)someAddresses;
{
    [super init];
    addresses = [[NSMutableArray allocWithZone:[self zone]] initWithArray:someAddresses];
    return self;
}


//-------------------------------------------------------------------------------------------
//	Representations
//-------------------------------------------------------------------------------------------

- (NSString *)stringRepresentation;
{
    unsigned int	i, n;
    NSMutableArray	*stringList;
    
    if(stringRep == nil)
        {
        stringList = [NSMutableArray array];
        for(i = 0, n = [addresses count]; i < n; i++)
            [stringList addObject:[[addresses objectAtIndex:i] stringRepresentation]];
        stringRep = [[stringList componentsJoinedByString:@", "] retain];
        }
    return stringRep;
}



- (NSData *)transferRepresentation;
{
    unsigned int	i, n;
    NSMutableData	*buffer;
    NSData			*delimiter;

    delimiter = [@", " dataUsingEncoding:NSASCIIStringEncoding];
    buffer = [NSMutableData data];
    for(i = 0, n = [addresses count]; i < n; i++)
        {
        [buffer appendData:[[addresses objectAtIndex:i] transferRepresentation]];
        if(i < n - 1)
            [buffer appendData:delimiter];
        }
    return buffer;
}


//-------------------------------------------------------------------------------------------
//	Attributes
//-------------------------------------------------------------------------------------------

- (void)addAddress:(MailAddress *)address;
{
    [addresses addObject:address];
}


- (NSArray *)addresses;
{
    return addresses;
}


//---------------------------------------------------------------------------------------
//	NSObject Stuff
//---------------------------------------------------------------------------------------

- (void)dealloc
{
    [addresses release];
    [super dealloc];
}


//-------------------------------------------------------------------------------------------
    @end
//-------------------------------------------------------------------------------------------
