/* Copyright (c) 2017, Computer History Museum 
All rights reserved. 
Redistribution and use in source and binary forms, with or without modification, are permitted (subject to 
the limitations in the disclaimer below) provided that the following conditions are met: 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
   disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of Computer History Museum nor the names of its contributors may be used to endorse or promote products 
   derived from this software without specific prior written permission. 
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE 
COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE. */

#include "palmconduitae.h"
/* Copyright (c) 2000 by QUALCOMM Incorporated */
#define FILE_NUM 142

/************************************************************************
 * ABConduitGetSettingsFile - get the settings file
 ************************************************************************/
OSErr ABConduitGetSettingsFile(AppleEvent *reply, uLong refCon)
{
	FSSpec settingsSpec;
	short err = errAENoSuchObject;

	err = GetFileByRef (SettingsRefN, &settingsSpec);
	if (err == noErr)
	{
		err = MyAEPutAlias(reply,keyAEResult,&settingsSpec);
	}

	return err;
}

/************************************************************************
 * ABConduitCanSyncAddressBooks - can we sync the address books now?
 ************************************************************************/
OSErr ABConduitCanSyncAddressBooks(AppleEvent *reply, void *data, long dataSize)
{
	OSErr err = noErr;
	Boolean ok = true;
	Boolean prep;
	
	// we must maintain changebit information from now on ...
	SetPref(PREF_CHANGE_BITS_FOR_CONDUIT, YesStr);
	
	// check to see if the address books should be prepped, too.
	BlockMove(data, &prep, sizeof(Boolean));
	
	// Is the address book window open?
	if (AliasWinIsOpen())
	{
		// are there any dirty nicknames?
		ok = !AnyNicknamesDirty(kAddressBookUndefined);
	}
	
	if (prep)
	{
		// if the user isn't modifying addresses, prep them and save them.
		if (ok) 
		{
			// add changebits and ids to all the nicknames that need them ...
			err = PrepAllAddressBooksForSync();
			
			// save the addresses
			if (err == noErr) ok = SaveAliases(true);
			else ok = false;
		}
	}
		
	// return true if there's a dirty nickname file ...
	err = AEPutBool(reply,keyAEResult,ok);
	
	return (err);
}

/************************************************************************
 * ABConduitBackupAddressBooks - backup the changed address book files
 ************************************************************************/
OSErr ABConduitBackupAddressBooks(AppleEvent *reply, FSSpecPtr cbSpecs, long ptrSize)
{
	OSErr err = noErr;
	FSSpecPtr spec = cbSpecs;
	long numSpecs = ptrSize/sizeof(FSSpec);
	long retErr = 0;
	
	while (numSpecs)
	{
		err = NickBackup(spec);
		if (err != noErr) retErr = err;
		numSpecs--;
		spec++;
	}
	
	// return the error generated by the backup action ...
	err = AEPutLong(reply,keyAEResult,retErr);
	
	return (err);
}

/************************************************************************
 * ABConduitReopenAddressBooks - reopen all the nickname files
 ************************************************************************/
OSErr ABConduitReopenAddressBooks(AppleEvent *reply)
{
	OSErr err = noErr;
	long retErr = 0;
	extern void GenAliasList(void);
	
	// clear the AB window selection ...
	if (AliasWinIsOpen()) ABUnselectCurrentNickname();
	
	// rebuild the list of nicknames.
	ZapAliases();
	ZapHandle(Aliases);
	GenAliasList();
	err = RegenerateAllAliases(true);
	
	// reset the change bits
	if ((err == noErr) && (ClearAllAddressBookChangeBits(AllButPrivate) == noErr))
		SaveAliases(false);
		
	// and redraw the address book window.
	ABTickleHardEnoughToMakeYouPuke();
			
	// return any error ...
	err = AEPutLong(reply,keyAEResult,retErr);
	
	return (err);
}

/************************************************************************
 * ABconduitGenerateUniqueID - generate a new unique ID
 ************************************************************************/
OSErr ABconduitGenerateUniqueID(AppleEvent *reply, void *data, long dataSize)
{
	OSErr err = noErr;
	long id;
	Str255 nickname, bookname;
	unsigned char *scan;
	unsigned char *end = (unsigned char *)(data) + dataSize;
	
	Zero(nickname);
	Zero(bookname);
	
	// nickname
	scan = data;
	nickname[0] = *scan;
	BMD(scan+1, nickname+1, nickname[0]);
	scan += nickname[0] + 1;
	
	// bookname
	if (scan < end) 
	{
		bookname[0] = *scan;
		BMD(scan+1, bookname+1, bookname[0]);
	}
	
	// generate the unique id
	id = NickGenerateUniqueID();
	
	// return the new ID to the conduit ...
	err = AEPutLong(reply,keyAEResult,id);

	return (err);
}