//
//  quick and dirty:
//     dialing with the ZyXEL modem 
//     by: Olaf Mueller <olaf@orest.escape.de>
//

#import "Addresses.h"
#import "AM.h"


@interface Addresses (PrivateAddressesMethods)	

static BOOL insertIntoMatrix (id matrix,char *string) ;
static char *conv1 (char *old,char *new) ;
static char *conv2 (char *old,char *new) ;
static char *nameFromBuffer (char *buffer,int where) ;

- click: sender ;

@end



@implementation Addresses


- windowWillClose: sender
{
	fclose (fp) ;
	return  self ;
}


//	init

- initForAddressBook: (const char*)filename
{
	char		tablename [MAXPATHLEN+1] ;

	[super init] ;

	strcpy (addressname,filename) ;
	[NXApp loadNibSection:"Addresses.nib" owner: self] ;
	[meinWindow setTitle: strrchr(filename,'/')+1] ;
	sprintf (tablename,"%s/AddressBook.table",filename) ;
	fp = fopen (tablename,"r") ;
	[meinBrowser setAction:@selector(click:)] ;
	[meinWindow makeKeyAndOrderFront: self] ;
	return  self ;
}


//	service for friends
- orderFront: sender
{
	[meinWindow makeKeyAndOrderFront: self] ;
	return  self ;
}


- (const char*)name
{
	return  addressname ;
}


- (int) browser:aBrowser fillMatrix:aMatrix inColumn:(int)aColumn
{
	int		count = 0 ;
	char	buffer [256] , name [256] ;

	while (fgets(buffer,sizeof(buffer),fp) != NULL)
		if (strstr(buffer," = {\n") != NULL  &&  !strstr(buffer,"Contents"))
			if (insertIntoMatrix(aMatrix,conv1(nameFromBuffer(buffer,1),name)))
				count++ ;
	return  count ;
}


- privateNumber: sender ;
{
	[NXApp newDialNumber: [meineForm stringValueAt: 4]] ;
	return  self ;
}


- companyNumber: sender ;
{
	[NXApp newDialNumber: [meineForm stringValueAt: 3]] ;
	return  self ;
}


// ------------------- private methods -------------------------

static BOOL insertIntoMatrix (id matrix,char *string)
{
	int		ii , cmp ;
	id		t ;

	for (ii = 0 ; ii < [matrix cellCount] ; ii++)
		if ((cmp = strcmp([[matrix cellAt:ii:0] stringValue],string)) == 0)
			return  NO ;
		else if (cmp > 0)
			break ;
	[matrix insertRowAt: ii] ;
	t = [matrix cellAt:ii :0] ;
	[t setStringValue: string] ;
	[t setLoaded: YES] ;
	[t setLeaf: YES] ;
	return  YES ;
}


static char *conv1 (char *old,char *new)
{
	int		special = 0 ;
	char	*cp ;

	for (cp = new ; *old ; old++)
	{
		if (special)
		{
			if (*old >= '0'  &&  *old <= '7')
				*cp = *cp * 8 + (*old - '0') ;
			else
			{
				cp++ ;
				*cp++ = *old ;
				special = 0 ;
			}
		}
		else if (*old == '\\')
		{
			*cp = 0 ;
			special = 1 ;
		}
		else
			*cp++ = *old ;
	}
	*cp = '\0' ;
	return  new ;
}


static char *conv2 (char *old,char *new)
{
	char	*cp ;

	for (cp = new ; *old ; old++)
	{
		if (*old < 0x20)
		{
			sprintf (cp,"\\%03o",(unsigned char)*old) ;
			cp += 4 ;
		}
		else
			*cp++ = *old ;
	}
	*cp = '\0' ;
	return  new ;
}


static char *nameFromBuffer (char *buffer,int where)
{
	char	*cp ;

	cp = strstr (buffer," = ") ;
	if (where == 1)
	{
		*cp = '\0' ;
		while (*buffer == ' '  ||  *buffer == '\"')
			buffer++ ;
		cp = buffer + strlen(buffer) - 1 ;
	}
	else
	{
		buffer = cp + 3 ;
		if (*buffer == '\"')
			buffer++ ;
		cp = strchr (buffer,';') ;
		*(cp--) = '\0' ;
	}
	if (*cp == '\"')
		*cp = '\0' ;
	return  buffer ;
}


- click: sender
{
	const char	*clickname = [[meinBrowser selectedCell] stringValue] ;
	int			ii ;
	char		buffer [256] , name [256] ;
	BOOL		nameFound = NO ;


	for (ii = 0 ; ii < 5 ; ii++)
		[meineForm setStringValue: NULL at: ii] ;
	[privateButton setEnabled: NO] ;
	[companyButton setEnabled: NO] ;

	if (clickname)
	{
		rewind (fp) ;
		sprintf (name,"%s",conv2((char*)clickname,buffer)) ;
		while (fgets(buffer,sizeof(buffer),fp) != NULL)
		{
			if (!nameFound)
			{
				if (strstr(buffer,name))
					nameFound = YES ;
			}
			else
			{
				if (strstr(buffer,"};"))
					break ;
				else if (strstr(buffer,"\"Full Name\" = "))
					[meineForm setStringValue: conv1(nameFromBuffer(buffer,2),name) at: 0] ;
				else if (strstr(buffer,"EMail = "))
					[meineForm setStringValue: conv1(nameFromBuffer(buffer,2),name) at: 1] ;
				else if (strstr(buffer,"FAX = "))
					[meineForm setStringValue: conv1(nameFromBuffer(buffer,2),name) at: 2] ;
				else if (strstr(buffer,"\"Work Phone\" = "))
				{
					[meineForm setStringValue: conv1(nameFromBuffer(buffer,2),name) at: 3] ;
					[companyButton setEnabled: YES] ;
				}
				else if (strstr(buffer,"\"Home Phone\" = "))
				{
					[meineForm setStringValue: conv1(nameFromBuffer(buffer,2),name) at: 4] ;
					[privateButton setEnabled: YES] ;
				}
			}
		}
	}
	return  self ;
}

@end
