// -----------------------------------------------------------------------------
// CLogPlayerManager::ReadOpenDbL
// -----------------------------------------------------------------------------
//
HBufC* CLogPlayerManager::ReadOpenDbL(RFileReadStream& aReadStream, TInt& aIndex, TBool& aIsSearcher)
    {
    aIndex = ReadNumberL(aReadStream);

    // Read SEARCHER / INDEXER
    ReadTextL(aReadStream, iTextBuffer);
    if (iTextBuffer.Compare(KIndexer) == 0)
        {
        aIsSearcher = EFalse;
        }
    else if (iTextBuffer.Compare(KSearcher) == 0)
        {
        aIsSearcher = ETrue;
        }
    else // Unknown type
        {
        User::Leave(KErrNotSupported);
        }

    // Read base app class
    ReadTextL(aReadStream, iTextBuffer, KLogPlayerFieldSeparatorEndLine);
    TBuf<KLogPlayerMaxTextLength> baseAppClass16;
    baseAppClass16.Copy(iTextBuffer);
    return baseAppClass16.AllocL();
    }
void CHelpContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();

	ReadTextL();

	iRtEd = new (ELeave) CEikRichTextEditor;

	Prepare();

	iRtEd->ConstructL(this, 0, 0, EEikEdwinAvkonDisableCursor|EEikEdwinReadOnly);
	iRtEd->SetFocus(ETrue);

	SetRect(aRect);

	TRgb color;
	AknsUtils::GetCachedColor(AknsUtils::SkinInstance(), color,
			KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG1/*EAknsCIQsnLineColorsCG1 */);

//	SetColor(color);
	const CFont* font = OKCUI()->GetFont();
	SetFontAndColor(font,color);
	IntroL();
	ActivateL();
	SetupL();

	iBgContext = CAknsBasicBackgroundControlContext::NewL(
			KAknsIIDQsnBgAreaMain, aRect, ETrue);
	}
// -----------------------------------------------------------------------------
// CLogPlayerManager::ReadSearchL
// -----------------------------------------------------------------------------
//
HBufC* CLogPlayerManager::ReadSearchL(RFileReadStream& aReadStream, TInt& aIndex)
    {
    aIndex = ReadNumberL(aReadStream);

    ReadTextL(aReadStream, iTextBuffer, KLogPlayerFieldSeparatorEndLine);
    TBuf<KLogPlayerMaxTextLength> searchQuery;
    searchQuery.Copy(iTextBuffer);
    return searchQuery.AllocL();
    }
// -----------------------------------------------------------------------------
// CLogPlayerManager::ReadDeleteL
// -----------------------------------------------------------------------------
//
HBufC* CLogPlayerManager::ReadDeleteL(RFileReadStream& aReadStream, TInt& aIndex)
    {
    aIndex = ReadNumberL(aReadStream);

    ReadTextL(aReadStream, iTextBuffer, KLogPlayerFieldSeparatorEndLine);
    TBuf<KLogPlayerMaxTextLength> docUid16;
    docUid16.Copy(iTextBuffer);
    return docUid16.AllocL();
    }
// -----------------------------------------------------------------------------
// CLogPlayerManager::ReadAddL
// -----------------------------------------------------------------------------
//
CSearchDocument* CLogPlayerManager::ReadAddL(RFileReadStream& aReadStream, TInt& aIndex)
    {
    aIndex = ReadNumberL(aReadStream);

    // Data is in serialized binary format preceded by size in bytes
    TInt serializedSize = ReadNumberL(aReadStream);
    HBufC8* data = HBufC8::NewLC(serializedSize);
    TPtr8 dataPtr = data->Des();
    ReadTextL(aReadStream, dataPtr, serializedSize);

    RDesReadStream desReadStream(*data);
    CleanupClosePushL(desReadStream);
    CSearchDocument* searchDocument = CSearchDocument::NewLC(
            desReadStream);

    CleanupStack::Pop(searchDocument);
    CleanupStack::PopAndDestroy(&desReadStream);
    CleanupStack::PopAndDestroy(data);

    // Remove new line
    ReadTextL(aReadStream, iTextBuffer, KLogPlayerFieldSeparatorEndLine);
    
    return searchDocument;
    }
// -----------------------------------------------------------------------------
// CLogPlayerManager::ReadHouseKeepingL
// -----------------------------------------------------------------------------
//
void CLogPlayerManager::ReadHouseKeepingL(RFileReadStream& aReadStream)
    {
    // Remove new line
    ReadTextL(aReadStream, iTextBuffer, KLogPlayerFieldSeparatorEndLine);
    }
// -----------------------------------------------------------------------------
// CLogPlayerManager::ContinueExecutionL()
// -----------------------------------------------------------------------------
//
void CLogPlayerManager::ContinueExecutionL()
    {
    _LIT8( KOpenDb, "OPENDB" );
    _LIT8( KDefineVolume, "DEFINEVOLUME" );
    _LIT8( KReset, "RESET" );
    _LIT8( KFlush, "FLUSH" );
    _LIT8( KAdd, "ADD" );
    _LIT8( KUpdate, "UPDATE" );
    _LIT8( KDelete, "DELETE" );
    _LIT8( KSearch, "SEARCH" );
    _LIT8( KGet, "GET" );
    _LIT8( KCancel, "CANCEL" );
    _LIT8( KHouseKeep, "HOUSEKEEP" );


    // Note: Huge descriptor from stack. Re-use this if possible.
	// TODO: Remove and use member variable iTextBuffer
	TBuf8<KLogPlayerMaxTextLength> textBuffer;

	// Read command
	ReadTextL(iReadStream, textBuffer);

	// Command: OPENDB
	if (textBuffer.Compare(KOpenDb) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("OPENDB\n"));
            }
		TInt index = 0;
		TBool isSearcher = EFalse;
		HBufC* baseAppClass = ReadOpenDbL(iReadStream, index, isSearcher);
		CleanupStack::PushL( baseAppClass );

		if ( index == iHandle )
		{
			if ( isSearcher )
			{
				TRAP_IGNORE( iSearcher->OpenDatabaseL(*baseAppClass) );
			}
			else
			{
				TRAP_IGNORE( iIndexer->OpenDatabaseL(*baseAppClass) );
			}
		}
		CleanupStack::PopAndDestroy( baseAppClass );

		// Check if handler for this index is created
		if ( iHandle == -1 && !IsHandlerCreated( index ) )
		{
			THandleInformation handleInformation;
			handleInformation.iLogFileName = iLogFileName;
			handleInformation.iFilePosition = iStartPosition.Offset();
			handleInformation.iHandleId = index;
			handleInformation.iStartTime = iStartTime;

			iHandleInformationArray.Append( handleInformation );

			// Create new thread for this new handler
			// Thread name: "LogManager_" + handle id
			TBuf<KMaxFileName> threadName;
			_LIT( KThreadNameBase, "LogManager_" );
			threadName.Append( KThreadNameBase );
			threadName.AppendNum( index );

			// Create thread
			RThread thread;
			thread.Create( threadName,
					ThreadFunction,
					1024*10,
					0,
					&iHandleInformationArray[iHandleInformationArray.Count()-1] );
			thread.Resume();
		}
	}
	// Command: DEFINEVOLUME
	else if (textBuffer.Compare(KDefineVolume) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("DEFINEVOLUME\n"));
            }

		// Get QualifiedBaseAppClass
		ReadTextL(iReadStream, textBuffer, KDefineVolumeSeparator);
		HBufC* qualifiedBaseAppClass = HBufC::NewLC(textBuffer.Length());
		TUint textBufferLen = textBuffer.Length() - 1; // Remove separator at end
		qualifiedBaseAppClass->Des().Copy( textBuffer.Left( textBufferLen ) );
		qualifiedBaseAppClass->Des().Trim();

		// Get Index path
		ReadTextL(iReadStream, textBuffer, KLogPlayerFieldSeparatorEndLine);
		HBufC* indexDbPath = HBufC::NewLC(textBuffer.Length());
		indexDbPath->Des().Copy(textBuffer);

		if ( iHandle == -1 )
		{
			iSession.DefineVolume(*qualifiedBaseAppClass, *indexDbPath);
		}

		CleanupStack::PopAndDestroy(indexDbPath);
		CleanupStack::PopAndDestroy(qualifiedBaseAppClass);
	}
	// Command: RESET
	else if (textBuffer.Compare(KReset) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("RESET\n"));
            }

		TInt index = 0;
		ReadResetL(iReadStream, index);
		if ( index == iHandle )
		{
			TRAP_IGNORE( iIndexer->ResetL() );
		}
	}
	// Command: FLUSH
	else if (textBuffer.Compare(KFlush) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("FLUSH\n"));
            }

		TInt index = 0;
		ReadFlushL(iReadStream, index);
		if ( index == iHandle )
		{
			TRAP_IGNORE( iIndexer->FlushL() );
		}
	}
	// Command: ADD
	else if (textBuffer.Compare(KAdd) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("ADD\n"));
            }

		TInt index = 0;
		CSearchDocument* doc = ReadAddL(iReadStream, index);
		CleanupStack::PushL(doc);

		if ( index == iHandle )
		{
			TRAP_IGNORE( iIndexer->AddL(*doc) );
		}

		CleanupStack::PopAndDestroy(doc);
	}
	// Command: UPDATE
	else if (textBuffer.Compare(KUpdate) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("UPDATE\n"));
            }

		TInt index = 0;
		CSearchDocument* doc = ReadAddL(iReadStream, index);
		CleanupStack::PushL(doc);

		if ( index == iHandle )
		{
			TRAP_IGNORE( iIndexer->UpdateL(*doc) );
		}

		CleanupStack::PopAndDestroy(doc);
	}
	// Command: DELETE
	else if (textBuffer.Compare(KDelete) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("DELETE\n"));
            }

		TInt index = 0;
		HBufC* docUid = ReadDeleteL(iReadStream, index);
		CleanupStack::PushL(docUid);

		if ( index == iHandle )
		{
			TRAP_IGNORE( iIndexer->DeleteL(*docUid) );
		}

		CleanupStack::PopAndDestroy(docUid);
	}
	// Command: SEARCH
	else if (textBuffer.Compare(KSearch) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("SEARCH\n"));
            }

		TInt index = 0;
		HBufC* searchQuery = ReadSearchL(iReadStream, index);
		CleanupStack::PushL(searchQuery);

		if ( index == iHandle )
		{
			TRAP_IGNORE( iSearcher->SearchL(*this, *searchQuery) );
		}

		CleanupStack::PopAndDestroy(searchQuery);
	}
	// Command: GET
	else if (textBuffer.Compare(KGet) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("GET\n"));
            }

		TInt index = 0;
		TInt docIndex = ReadGetL(iReadStream, index);

		if ( index == iHandle )
		{
			TRAP_IGNORE( iSearcher->GetDocumentL(docIndex, *this) );
		}
	}
	// Command: CANCEL
	else if (textBuffer.Compare(KCancel) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("CANCEL\n"));
            }

		TInt index = 0;
		ReadCancelL(iReadStream, index);

		if ( index == iHandle )
		{
			iSearcher->Cancel();
		}
	}
	// Command: HOUSEKEEP
	else if (textBuffer.Compare(KHouseKeep) == 0)
	{
        if ( iConsole )
            {
            iConsole->Printf(_L("HOUSEKEEP\n"));
            }

		ReadHouseKeepingL(iReadStream);
		if ( iHandle == -1 )
		{
			iSession.ForceHouseKeeping();
		}
	}
	else
	{
		iWait->AsyncStop();
		return;
	}
	
	ReadTimeAndWait();
}