examples/SFExamples/PIM/ContactAsynchFind/src/ContactRead.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:
00015 // 
00016 
00017 #include "ContactRead.h"
00018 
00019 CContactRead* CContactRead::NewL()
00020         {
00021         CContactRead* self = new (ELeave) CContactRead();
00022         CleanupStack::PushL(self);
00023         self->ConstructL();
00024         CleanupStack::Pop(self);
00025         return self;
00026         }
00027 
00028 CContactRead::~CContactRead()
00029         {
00030         delete iMatch;
00031         delete iTextDef;
00032         delete iCntDb;
00033         }
00034 
00035 CContactRead::CContactRead()
00036                 : iCallback(&CContactRead:: ParseWord)
00037         {
00038         }
00039 
00040 void CContactRead::ConstructL()
00041         {
00042         TRAPD(error, iCntDb = CContactDatabase::OpenL());
00043         if (KErrNotFound == error)
00044                 {
00045                 iCntDb = CContactDatabase::CreateL();
00046                 }
00047         else
00048                 {
00049                 User::LeaveIfError(error);
00050                 }
00051         iTextDef = CContactTextDef::NewL();
00052         iMatch = new (ELeave) CDesCArrayFlat(1);
00053         }
00054 
00055 CContactDatabase& CContactRead::CntDatabase()
00056         {
00057         delete iMatch;
00058         delete iTextDef;
00059         return *iCntDb;
00060         }
00061 
00062 CIdleFinder* CContactRead::FindWithCallbackL(TFieldType aField, const TDesC& aMatch, MIdleFindObserver* aObserver)
00063         {
00064         iTextDef->Reset();
00065         TContactTextDefItem field(aField);
00066         iTextDef->AppendL(field);
00067         iMatch->Reset();
00068         iMatch->AppendL(aMatch);
00069         return iCntDb->FindInTextDefAsyncL(*iMatch, iTextDef, aObserver, iCallback);
00070         }
00071 
00072 TInt CContactRead::ParseWord(TAny* aParam)
00073         {
00074         SFindInTextDefWordParser* parserStruct = (SFindInTextDefWordParser*)aParam;
00075         TPtrC searchString(*(parserStruct->iSearchString));
00076         TInt index = KErrNotFound;
00077         TInt error = KErrNone;
00078         while(0 <= (index = searchString.Locate(' ')))
00079                 {
00080                 if (index > 0)
00081                         {
00082                         TRAP(error, parserStruct->iWordArray->AppendL(searchString.Left(index)));
00083                         if (error != KErrNone)
00084                                 return error;
00085                         if (searchString.Length() > index + 1)
00086                                 {
00087                                 searchString.Set(searchString.Mid(index  +1));
00088                                 }
00089                         else
00090                                 {
00091                                 searchString.Set(KNullDesC());
00092                                 break;
00093                                 }
00094                         }
00095                 else
00096                         {
00097                         // remove first character as it is a space
00098                         searchString.Set(searchString.Mid(1));
00099                         }
00100                 }
00101         if(searchString.Length() > 0) // the last word
00102                 {
00103                 TRAP(error, parserStruct->iWordArray->AppendL(searchString));
00104                 if (error != KErrNone)
00105                         return error;
00106                 }
00107         return KErrNone;
00108         }
00109 
00110 // End of File

Generated by  doxygen 1.6.2