/*
 *  Sample EMS plugin: Launch
 *
 *  Filename: lnchAPI.cpp
 *
 *  Last edited: April 1, 1997
 *
 *  Author: Julia Blumin
 *
 *  Copyright 1997 QUALCOMM Inc.
 Copyright (c) 2016, 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. 


 *
 *  Send comments and questions to <emsapi-info@qualcomm.com>
 */

#include "stdafx.h"


#ifdef _DEBUG
#include <ctype.h>
#endif // _DEBUG

#include <afxwin.h> // MFC & Windows master header
#include <afxdlgs.h>
#include <cderr.h> // Common dialog error returns
#include <windows.h>

#include <malloc.h> // malloc, free
#include <string.h> // strdup
#include <string>
#include <ctime>

#include "WabBook.h" 

#include "WabInterface.h"

/*
	Write the contents of the system address book to a file
	in Eudora nickname format
*/

//	Debugging
static int numGens = 0;

//	Prototype
HRESULT CreateAddrBook ( WabFile &book, const char *nickFolderPath );

enum {
    ieidPR_DISPLAY_NAME = 0,
    ieidPR_EMAIL_ADDRESS,
    ieidPR_ENTRYID,
	ieidPR_OBJECT_TYPE,
    ieidMax
};
static const SizedSPropTagArray(ieidMax, ptaEid)=
{
    ieidMax,
    {
        PR_DISPLAY_NAME,
        PR_EMAIL_ADDRESS,
        PR_ENTRYID,
		PR_OBJECT_TYPE,
    }
};


static FILE *nick = NULL;
static bool EachMail ( LPSRow lpRow ) {
	char nameRoot [ 100 ];
	char *foo = nameRoot;

	strcpy ( nameRoot, lpRow->lpProps [ ieidPR_EMAIL_ADDRESS ].Value.lpszA );
	while ( *foo != '@' && *foo != '\0' ) ++foo;
	*foo = '\0';
	fprintf ( nick, "alias %s %s \n", nameRoot, lpRow->lpProps [ ieidPR_EMAIL_ADDRESS ].Value.lpszA );

	return true;
}


HRESULT CreateAddrBook ( WabFile &book, const char *nickFolderPath ) {
	HRESULT	retVal = NOERROR;

//	TRACE ( nickFolderPath );
	clock_t startTime = clock ();
	::MessageBeep ( MB_ICONASTERISK );
	nick = fopen ( nickFolderPath, "w" );
	if ( nick == NULL )
		retVal = E_FAIL;
	else {
		fprintf ( nick, "## Nickname file created from Windows address book\n" );
		fprintf ( nick, "## Do not edit this file!\n" );
		fprintf ( nick, "\n\n" );

		retVal = book.ForEachMailAddressDo ((LPSPropTagArray) &ptaEid, EachMail );

		clock_t endTime = clock ();
		time_t now = time ( NULL );
		fprintf ( nick, "\n\n" );
		fprintf ( nick, "## File created on %s", ctime ( &now )); // because ctime adds a '\n'!!
		fprintf ( nick, "## File created in %f seconds\n", ((float) ( endTime - startTime )) / CLOCKS_PER_SEC );
		fprintf ( nick, "## File created for the %dth time\n", ++numGens );
		fclose ( nick );
		nick = NULL;

	}

	return retVal;
}
