// RECAPP.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include "recapp.h"
#include <apadef.h>
#include <apaid.h>
#include <apacmdln.h>
#include <e32svr.h>


//
// Class CApaAppRecognizer
//

CApaFileRecognizerType::TRecognizedType CApaAppRecognizer::DoRecognizeFileL(RFs& /*aFs*/,TUidType aType)
// Recognises App DLL files (.APP) and document files
	{
	if (aType[1]==KUidApp)
		iRecognizedType=EProgram;
	else if (aType[1]==KUidAppDllDoc8 || aType[1]==KUidAppDllDoc16)
		iRecognizedType=EDoc;
	return iRecognizedType;
	}


TThreadId CApaAppRecognizer::RunL(TApaCommand aCommand,const TDesC* aDocFileName,const TDesC8* aTailEnd) const
// Run the recognized file
// If it's an app, run the app with the given command line
// If it's a doc, find the app and run with the "Open" command and iFullFileName (aDocFileName will be NULL)
// If it's neither app nor doc, leaves KErrNotSupported
	{
	CApaCommandLine* commandLine=CApaCommandLine::NewLC();
	commandLine->SetCommandL(aCommand);
	if (iRecognizedType==EProgram)
		{
		commandLine->SetLibraryNameL(*iFullFileName);
		if (aDocFileName)
			commandLine->SetDocumentNameL(*aDocFileName);
		}
	else if (iRecognizedType==EDoc)
		{
		// we've recognised a doc so we need to scan for its associated app
		TApaAppEntry appEntry;
		User::LeaveIfError(iFileRecognizer->AppLocator()->GetAppEntryByUid(appEntry,iAppUid));
		commandLine->SetLibraryNameL(appEntry.iFullName);
		if (aDocFileName)
			commandLine->SetDocumentNameL(*iFullFileName);
		}
	else
		User::Leave(KErrNotSupported);
	if (aTailEnd)
		commandLine->SetTailEndL(*aTailEnd);
	TThreadId id=AppRunL(*commandLine);
	CleanupStack::PopAndDestroy(); // commandLine
	return id;
	}

//
// dll stuff...
//

EXPORT_C CApaFileRecognizerType* CreateRecognizer()
// The gate function - ordinal 1
//
	{
	CApaFileRecognizerType* thing=NULL;
	thing = new CApaAppRecognizer();
	return thing; // NULL if new failed
	}

/*
GLDEF_C void Panic(TAprAppPanic aPanic)
//
// Panic the process with RECAPP as the category.
//
	{
	User::Panic(_L("RECAPP"),aPanic);
	}
*/

GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
//
// DLL entry point
//
	{
	return KErrNone;
	}
