email/imap4mtm/imapsession/src/cimapsearch.cpp
changeset 31 ebfee66fde93
parent 0 72b543305e3a
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "cimapsearch.h"
       
    17 #include "moutputstream.h"
       
    18 #include "cimapsessionconsts.h"
       
    19 #include "imappaniccodes.h"
       
    20 
       
    21 // IMAP SEARCH command
       
    22 _LIT8(KCommandSearch, "%d UID SEARCH %S\r\n");
       
    23 
       
    24 CImapSearch* CImapSearch::NewL(CImapFolderInfo* aSelectedFolderData, TInt aLogId, const TDesC8& aSearchCriteria, RArray<TUint>& aMatchingMessageIds)
       
    25 // static method first phase construction
       
    26 	{
       
    27 	CImapSearch* self = new (ELeave) CImapSearch(aSelectedFolderData, aLogId, aMatchingMessageIds);
       
    28 	CleanupStack::PushL(self);
       
    29 	self->ConstructL(aSearchCriteria);
       
    30 	CleanupStack::Pop();
       
    31 	return self;
       
    32 	}
       
    33 
       
    34 CImapSearch::CImapSearch(CImapFolderInfo* aSelectedFolderData, TInt aLogId, RArray<TUint>& aMatchingMessageIds)	
       
    35 	: CImapCommandEx(aSelectedFolderData, aLogId)
       
    36 	, iMatchingMessageIds(aMatchingMessageIds)
       
    37 	{
       
    38 	
       
    39 	}
       
    40 
       
    41 void CImapSearch::ConstructL(const TDesC8& aSearchCriteria)
       
    42 	{
       
    43 	iSearchCriteria = aSearchCriteria.AllocL();
       
    44 	}
       
    45 
       
    46 CImapSearch::~CImapSearch()
       
    47 	{
       
    48 	delete iSearchCriteria;
       
    49 	}
       
    50 
       
    51 /**
       
    52 Formats and sends the IMAP SEARCH command.
       
    53 @param aTagId Command sequence id which will be send along with the IMAP command.
       
    54 @param aStream A wrapper for the outbound stream of a connected socket, using which
       
    55 the command will be send to the server
       
    56 */
       
    57 void CImapSearch::SendMessageL(TInt aTagId, MOutputStream& aStream)
       
    58 	{
       
    59 	// From RFC3501
       
    60 	//	search = "SEARCH" [SP "CHARSET" SP astring] 1*(SP search-key)
       
    61 	//
       
    62 	// we have chosen not to send teh optional "CHARSET", which means we
       
    63 	// don't need to check for a BADCHARSET response text code in the resposne.
       
    64 	
       
    65 	iTagId = aTagId;
       
    66 
       
    67 	TInt bufLength = KCommandSearch().Length();
       
    68 	bufLength += TagLength(aTagId);
       
    69 	bufLength += iSearchCriteria->Length();
       
    70 	
       
    71 	__ASSERT_DEBUG(iOutputBuffer==NULL, TImapServerPanic::ImapPanic(TImapServerPanic::ECommandOutputBufferNotNull));
       
    72 	iOutputBuffer = HBufC8::NewL(bufLength);
       
    73 	iOutputBuffer->Des().Format(KCommandSearch, aTagId, iSearchCriteria);
       
    74 
       
    75 	//send the command to the server
       
    76 	aStream.SendDataReq(*iOutputBuffer);
       
    77 	}
       
    78 
       
    79 /**
       
    80 Checks if this is a SEARCH response.  If it is, then it is parsed.
       
    81 @return ECompleteUntagged if the line is an untagged SEARCH response.
       
    82 		ENotRecognised for any other response (i.e. one that is not known about by this command)
       
    83 */
       
    84 CImapCommand::TParseBlockResult CImapSearch::ParseUntaggedResponseL()
       
    85 	{	
       
    86 	TParseBlockResult result = ENotRecognised;
       
    87 	
       
    88 	// Is this a search response?
       
    89 	TPtrC8 part = GetNextPart(); // returns KNullDesC8 if there is no part available
       
    90 	if(part.CompareF(KImapTxtSearch) == 0)
       
    91 		{
       
    92 		ParseSearchResponseL();
       
    93 		result = ECompleteUntagged;
       
    94 		}
       
    95 	
       
    96 	return result;
       
    97 	}
       
    98 
       
    99 /**
       
   100 Parses the message uids from a previously identified untagged search response.
       
   101 */
       
   102 void CImapSearch::ParseSearchResponseL()
       
   103 	{
       
   104 	// From RFC3501 section 9 (edited)
       
   105 	//
       
   106 	// mailbox-data = "SEARCH" *(SP nz-number)
       
   107 	//
       
   108 	// "SEARCH" has already been found, so the remainder of response 
       
   109 	// should be space-separated numbers representing UIDS of messages.
       
   110 	
       
   111 	RDesParts messageUidList;
       
   112 	CleanupClosePushL(messageUidList);
       
   113 
       
   114 	GetDelimitedPartsL(' ', iUnparsedData, messageUidList);
       
   115 		
       
   116 	TUint messageUid = 0;
       
   117 	
       
   118 	TInt countMessageUids = messageUidList.Count(); 	 
       
   119 	for(TInt i = 0; i < countMessageUids; ++i)
       
   120 		{
       
   121 		TLex8 desToInt(messageUidList[i]);
       
   122 		
       
   123 		TInt err = desToInt.Val(messageUid);
       
   124 		if (err != KErrNone)
       
   125 			{
       
   126 			CorruptDataL();
       
   127 			}
       
   128 			
       
   129 		iMatchingMessageIds.Append(messageUid);
       
   130 		}
       
   131 				
       
   132 	CleanupStack::PopAndDestroy(&messageUidList);
       
   133 	}