examples/SFExamples/PIM/ContactMove/src/ContactWrite.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 "ContactWrite.h"
00018 
00019 CContactWrite* CContactWrite::NewL()
00020         {
00021         CContactWrite* self = new (ELeave) CContactWrite();
00022         CleanupStack::PushL(self);
00023         self->ConstructL();
00024         CleanupStack::Pop(self);
00025         return self;
00026         }
00027 
00028 CContactWrite::~CContactWrite()
00029         {
00030         delete iCntDb;
00031         }
00032 
00033 CContactWrite::CContactWrite()
00034                 : iCallback(&CContactWrite:: ParseWord)
00035         {
00036         }
00037 
00038 void CContactWrite::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& CContactWrite::CntDatabase()
00052         {
00053         return *iCntDb;
00054         }
00055 
00056 void CContactWrite::MoveContactL(TContactItemId aCntId, const TDesC& aNewGroup)
00057         {
00058         // open the contact item for exclusive use
00059         CContactItem* item = iCntDb->OpenContactLX(aCntId);
00060         CleanupStack::PushL(item);
00061         // Let’s find out what group the contact already belongs to
00062         TContactItemId groupId = KNullContactId;
00063         CContactItem* group = NULL;
00064         if (KUidContactCard == item->Type())
00065                 {
00066                 CContactCard* card = (CContactCard*)item;
00067                 CContactIdArray* ids = card->GroupsJoinedLC();
00068                 if (0 < ids->Count())
00069                         {
00070                         groupId = (*ids)[0];
00071                         }
00072                 CleanupStack::PopAndDestroy(ids);
00073                 }
00074         if (KNullContactId != groupId)
00075                 {
00076                 // let’s remove the contact from the group
00077                 group = iCntDb->OpenContactLX(groupId);
00078                 CleanupStack::PushL(group);
00079                 iCntDb->RemoveContactFromGroupL(*item, *group);
00080 
00081                 CleanupStack::PopAndDestroy(group);
00082                 CleanupStack::PopAndDestroy(); // exclusive lock on group
00083                 group = NULL;
00084                 }
00085         CleanupStack::PopAndDestroy(item);
00086         CleanupStack::PopAndDestroy(); // exclusive lock on item
00087 
00088         // let’s find the new group in the database, or create it if needs be
00089 
00090         groupId = KNullContactId;
00091         CContactIdArray* groupArrayId = iCntDb->GetGroupIdListL();
00092         if (NULL != groupArrayId)
00093                 {
00094                 CleanupStack::PushL(groupArrayId);
00095                 for (TInt ii = 0 ; ii < groupArrayId->Count() ; ++ii)
00096                         {
00097                         group = iCntDb->ReadContactLC((*groupArrayId)[ii]);
00098                         TPtrC label = ((CContactGroup*)group)->GetGroupLabelL();
00099                         if (aNewGroup == label)
00100                                 {
00101                                 groupId = (*groupArrayId)[ii];
00102                                 ii = groupArrayId->Count(); // break
00103                                 }
00104                         CleanupStack::PopAndDestroy(group);
00105                         group = NULL;
00106                         }
00107                 CleanupStack::PopAndDestroy(groupArrayId);
00108                 }
00109         if (KNullContactId == groupId)
00110                 {
00111                 group = iCntDb->CreateContactGroupLC(aNewGroup);
00112                 groupId = group->Id();
00113                 CleanupStack::PopAndDestroy(group);
00114                 }
00115         // let’s add the contact to the group
00116         iCntDb->AddContactToGroupL(aCntId, groupId);
00117         }
00118 
00119 CContactIdArray* CContactWrite::FindWithCallbackL(TFieldType aField, const TDesC& aMatch)
00120         {
00121         TContactTextDefItem field(aField);
00122         CContactTextDef* textDef = CContactTextDef::NewL();
00123         CleanupStack::PushL(textDef);
00124         textDef->AppendL(field);
00125         
00126         CDesCArray* match = new (ELeave) CDesCArrayFlat(1);
00127         CleanupStack::PushL(match);
00128         match->AppendL(aMatch);
00129         
00130         CContactIdArray* result = iCntDb->FindInTextDefLC(*match, textDef, iCallback);
00131         CleanupStack::Pop(result);
00132         CleanupStack::PopAndDestroy(match);
00133         CleanupStack::PopAndDestroy(textDef);
00134         return result;
00135         }
00136 
00137 TInt CContactWrite::ParseWord(TAny* aParam)
00138         {
00139         SFindInTextDefWordParser* parserStruct = (SFindInTextDefWordParser*)aParam;
00140         TPtrC searchString(*(parserStruct->iSearchString));
00141         TInt index = KErrNotFound;
00142         TInt error = KErrNone;
00143         while(0 <= (index = searchString.Locate(' ')))
00144                 {
00145                 if (index > 0)
00146                         {
00147                         TRAP(error, parserStruct->iWordArray->AppendL(searchString.Left(index)));
00148                         if (error != KErrNone)
00149                                 return error;
00150                         if (searchString.Length() > index + 1)
00151                                 {
00152                                 searchString.Set(searchString.Mid(index  +1));
00153                                 }
00154                         else
00155                                 {
00156                                 searchString.Set(KNullDesC());
00157                                 break;
00158                                 }
00159                         }
00160                 else
00161                         {
00162                         // remove first character as it is a space
00163                         searchString.Set(searchString.Mid(1));
00164                         }
00165                 }
00166         if(searchString.Length() > 0) // the last word
00167                 {
00168                 TRAP(error, parserStruct->iWordArray->AppendL(searchString));
00169                 if (error != KErrNone)
00170                         return error;
00171                 }
00172         return KErrNone;
00173         }
00174 
00175 // End of File

Generated by  doxygen 1.6.2