/*
 *  ltFileSelectDialog.c
 *  lt
 *
 *  Created by T T on 7/12/06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 */

#include "ltFileSelectDialog.h"

int ltFileSelectDialog( void* dataRef )
{
	OSStatus status;
	
	chooseFile = 1;
	
	pData data = (pData) dataRef;

	NavDialogCreationOptions dialogOptions;
	status = NavGetDefaultDialogCreationOptions( &dialogOptions );
	NavEventUPP gNavEventProc = NewNavEventUPP( NavEventProc );
	
	//Create and run dialog with options
	status = NavCreatePutFileDialog( &dialogOptions,
										'TEXT',
										NULL,
										gNavEventProc,
										NULL,
										&dialogRef );
										
	status = NavDialogRun (dialogRef);
	//Create and run dialog with options
	
	//Discard dialog after closing if there is no error.  Should print warning in the case of an error.
	if( status )
		NavDialogDispose (dialogRef);
	//Discard dialog after closing if there is no error.  Should print warning in the case of an error.
	
	//Makes a FSRef from the FSSpec passed by the dialog.
	FSRef newRef;
	FSpMakeFSRef( &fileSpec, &newRef );
	//Makes a FSRef from the FSSpec passed by the dialog.
	
	if( chooseFile == 0 )
	{
		//Constructs a new FSRef object from FSSpec created by the dialog box.
		FSRefMakePath( &newRef, &path, maxPathSize );
		//Constructs a new FSRef object from FSSpec created by the dialog box.
	
		UInt8* pathPtr = &path;
		char *pathString = (char*)pathPtr;
	
		char tempStr[2] = "/";
		strcat( tempStr, name );
		strcat( pathString, tempStr );
		
		data->dFileData.dFilePath = pathString;
	}
	
	
	return chooseFile;
}


void NavEventProc (NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, void *callBackUserData )
{
	OSStatus status;   
          
	switch ( callBackSelector )
	{
		case kNavCBUserAction:
		{
			NavReplyRecord  reply;
			NavUserAction   userAction = 0;
			if ((status = NavDialogGetReply ( callBackParms->context, &reply )) == noErr )
			{
				OSStatus tempErr;
				userAction = NavDialogGetUserAction ( callBackParms->context );
				switch ( userAction )
				{
					case kNavUserActionSaveAs:
					{
						CFStringGetCString(reply.saveFileName, name, CFStringGetLength(reply.saveFileName)+1, kCFStringEncodingMacRoman);
						chooseFile = 0;
						long count = 0;
						short index;
						status = AECountItems( &reply.selection, &count );
						for ( index = 1; index <= count; index++ )
						{
							AEKeyword   keyWord;
							AEDesc    theDesc;
							if (( status = AEGetNthDesc( &reply.selection, index, typeWildCard, &keyWord, &theDesc )) == noErr )
							{         
								if (( status = GetFSSpecInfo( &theDesc, &fileSpec )) == noErr ){
								// decide if the doc we are opening is a 'PICT':
									//if (( status = FSpGetFInfo( &fileSpec, &fileInfo)) == noErr )
									//{
										//if ( fileInfo.fdType == kFileTypePICT )
										//(void)doTheFile( &fileSpec );
									//}
									//else
									//{
										// error:
										// if we got this far, the document is a type we can't open and
										// (most likely) built-in translation was turned off.
										// You can alert the user that this returned selection or file spec
										// needs translation.
									//}
									
								}
								AEDisposeDesc( &theDesc );
							}
						}
					}
					break;

					case kNavUserActionCancel:
					break;
				
					case kNavUserActionNewFolder:
					break;
				}        
				tempErr = NavDisposeReply ( &reply );
				if( !status )
					status = tempErr;
            }
            break;
		}
		break;
		case kNavCBTerminate:
		{
			if( dialogRef )
                NavDialogDispose( callBackParms->context );
            // re-enable open if needed
            EnableMenuCommand ( NULL, kHICommandOpen );
		break;
		}
	}
	return;
}

static OSStatus GetFSSpecInfo( AEDesc* fileObject, FSSpec* returnSpec )
{
    OSStatus   theErr = noErr;
    AEDesc    theDesc;
    
    if ((theErr = AECoerceDesc( fileObject, typeFSS, &theDesc )) == noErr)
    {
  theErr = AEGetDescData( &theDesc, returnSpec, sizeof ( FSSpec ) );
  AEDisposeDesc( &theDesc );
    }
    else
    {
  if ((theErr = AECoerceDesc( fileObject, typeFSRef, &theDesc )) == noErr)
  {
      FSRef ref;
      if ((theErr = AEGetDescData( &theDesc, &ref, sizeof( FSRef ) )) == noErr)
    theErr = FSGetCatalogInfo( &ref, kFSCatInfoGettableInfo, NULL, NULL, returnSpec, NULL );
      AEDisposeDesc( &theDesc );
  }
    }

    return theErr;
}

void* MemAlloc(unsigned long memsize)
{
    if(memsize <= 0)
  return NULL;
    else
  return (malloc(memsize));
}