examples/SFExamples/PIM/ContactFind/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 iCntDb;
00031         }
00032 
00033 CContactRead::CContactRead()
00034                 : iCallback(&CContactRead:: ParseWord)
00035         {
00036         }
00037 
00038 void CContactRead::ConstructL()
00039         {
00040         TRAPD(error, iCntDb = CContactDatabase::OpenL());
00041         if (KErrNotFound == error)
00042                 {
00043                 iCntDb = CContactDatabase::CreateL();
00044                 }
00045         else
00046                 {
00047                 User::LeaveIfError(error);
00048                 }
00049         }
00050 
00051 CContactDatabase& CContactRead::CntDatabase()
00052         {
00053         return *iCntDb;
00054         }
00055 
00056 CContactIdArray* CContactRead::FindWithCallbackL(TFieldType aField, const TDesC& aMatch)
00057         {
00058         TContactTextDefItem field(aField);
00059         CContactTextDef* textDef = CContactTextDef::NewL();
00060         CleanupStack::PushL(textDef);
00061         textDef->AppendL(field);
00062 
00063         CDesCArray* match = new (ELeave) CDesCArrayFlat(1);
00064         CleanupStack::PushL(match);
00065         match->AppendL(aMatch);
00066 
00067         CContactIdArray* result = iCntDb->FindInTextDefLC(*match, textDef, iCallback);
00068         CleanupStack::Pop(result);
00069         CleanupStack::PopAndDestroy(match);
00070         CleanupStack::PopAndDestroy(textDef);
00071         return result;
00072         }
00073 
00074 
00075 // the whole purpose of the callback at this point is to take the
00076 // content of the aMatch parameter in CContactRead::FindByCallbackL
00077 // and split it into words, presumably using punctuation and spaces.
00078 // note that ParseWord can’t leave.
00079 
00080 TInt CContactRead::ParseWord(TAny* aParam)
00081         {
00082         SFindInTextDefWordParser* parserStruct = (SFindInTextDefWordParser*)aParam;
00083         TPtrC searchString(*(parserStruct->iSearchString));
00084         TInt index = KErrNotFound;
00085         TInt error = KErrNone;
00086         while(0 <= (index = searchString.Locate(' ')))
00087                 {
00088                 if (index > 0)
00089                         {
00090                         TRAP(error, parserStruct->iWordArray->AppendL(searchString.Left(index)));
00091                         if (error != KErrNone)
00092                                 return error;
00093                         if (searchString.Length() > index + 1)
00094                                 {
00095                                 searchString.Set(searchString.Mid(index  +1));
00096                                 }
00097                         else
00098                                 {
00099                                 searchString.Set(KNullDesC());
00100                                 break;
00101                                 }
00102                         }
00103                 else
00104                         {
00105                         // remove first character as it is a space
00106                         searchString.Set(searchString.Mid(1));
00107                         }
00108                 }
00109         if(searchString.Length() > 0) // the last word
00110                 {
00111                 TRAP(error, parserStruct->iWordArray->AppendL(searchString));
00112                 if (error != KErrNone)
00113                         return error;
00114                 }
00115         return KErrNone;
00116         }
00117 
00118 
00119 
00120 // End of File

Generated by  doxygen 1.6.2