00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 void CContactWrite::ConstructL()
00034 {
00035 TRAPD(error, iCntDb = CContactDatabase::OpenL());
00036 if (KErrNotFound == error)
00037 {
00038 iCntDb = CContactDatabase::CreateL();
00039 }
00040 else
00041 {
00042 User::LeaveIfError(error);
00043 }
00044 }
00045
00046 CContactDatabase& CContactWrite::CntDatabase()
00047 {
00048 return *iCntDb;
00049 }
00050
00051 void CContactWrite::PopulateContactL()
00052 {
00053 PopulateOwnCardL();
00054 _LIT(KFirstFirst, "first");
00055 _LIT(KSecondFirst, "second");
00056 _LIT(KThirdFirst, "Partner");
00057 _LIT(KLast, "last");
00058 TContactItemId first = AddCardL(KFirstFirst(), KLast());
00059 TContactItemId second = AddCardL(KSecondFirst(), KLast());
00060 AddCardL(KThirdFirst(), KLast());
00061 _LIT(KGroup1, "MyGroup1");
00062 _LIT(KGroup2, "MyGroup2");
00063 MoveContactL(first, KGroup1());
00064 MoveContactL(second, KGroup2());
00065 }
00066
00067
00068 void CContactWrite::PopulateOwnCardL()
00069 {
00070 _LIT(KOwnFirstName, "ownfirstname");
00071 TContactItemId ownId = iCntDb->OwnCardId();
00072 if (KNullContactId == ownId)
00073 {
00074 CContactItem* ownCard = iCntDb->CreateOwnCardLC();
00075 CleanupStack::PopAndDestroy(ownCard);
00076 ownCard = NULL;
00077 ownId = iCntDb->OwnCardId();
00078 }
00079 CContactItemViewDef* viewDef = CContactItemViewDef::NewLC(
00080 CContactItemViewDef::EIncludeFields,
00081 CContactItemViewDef::EMaskHiddenFields);
00082 viewDef->AddL(KUidContactFieldGivenName);
00083 viewDef->AddL(KUidContactFieldCompanyName);
00084 CContactItem* ownItem = iCntDb->OpenContactLX(ownId, *viewDef );
00085 CleanupStack::PushL(ownItem);
00086 CContactItemFieldSet& ownFieldSet = ownItem->CardFields();
00087 TInt companyIndex = KErrNotFound;
00088 while (0 <= (companyIndex = ownFieldSet.Find(KUidContactFieldCompanyName)))
00089 {
00090 ownFieldSet.Remove(companyIndex);
00091 }
00092 TBool nameFound = EFalse;
00093 for(TInt ii = 0 ; ii < ownFieldSet.Count() && !nameFound; ++ii)
00094 {
00095 CContactItemField& field = ownFieldSet[ii];
00096 if (KStorageTypeText == field.StorageType())
00097 {
00098 CContactTextField* storage = field.TextStorage();
00099 TPtrC text = storage->Text();
00100 nameFound = (field.ContentType().ContainsFieldType(KUidContactFieldGivenName) &&
00101 storage->IsFull() && text.Length() > 0);
00102 }
00103 }
00104 if (!nameFound)
00105 {
00106 CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText);
00107 firstName->AddFieldTypeL(KUidContactFieldGivenName);
00108 firstName->TextStorage()->SetTextL(KOwnFirstName());
00109 ownItem->AddFieldL(*firstName);
00110 CleanupStack::Pop(firstName);
00111 iCntDb->CommitContactL(*ownItem);
00112 }
00113 CleanupStack::PopAndDestroy(ownItem);
00114 CleanupStack::PopAndDestroy();
00115 CleanupStack::PopAndDestroy(viewDef);
00116 }
00117
00118 TContactItemId CContactWrite::AddCardL(const TDesC& aFirstName, const TDesC& aLastName)
00119 {
00120 CContactCard* contactCard = CContactCard::NewLC();
00121
00122 CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText);
00123 firstName->AddFieldTypeL(KUidContactFieldGivenName);
00124 firstName->TextStorage()->SetTextL(aFirstName);
00125 contactCard->AddFieldL(*firstName);
00126 CleanupStack::Pop(firstName);
00127
00128 CContactItemField* lastName = CContactItemField::NewLC(KStorageTypeText);
00129 lastName->AddFieldTypeL(KUidContactFieldFamilyName);
00130 lastName->TextStorage()->SetTextL(aLastName);
00131 contactCard->AddFieldL(*lastName);
00132 CleanupStack::Pop(lastName);
00133
00134 TContactItemId result = iCntDb->AddNewContactL( *contactCard );
00135
00136 CleanupStack::PopAndDestroy(contactCard);
00137 return result;
00138 }
00139
00140 void CContactWrite::MoveContactL(TContactItemId aCntId, const TDesC& aNewGroup)
00141 {
00142
00143 CContactItem* item = iCntDb->OpenContactLX(aCntId);
00144 CleanupStack::PushL(item);
00145
00146 TContactItemId groupId = KNullContactId;
00147 CContactItem* group = NULL;
00148 if (KUidContactCard == item->Type())
00149 {
00150 CContactCard* card = (CContactCard*)item;
00151 CContactIdArray* ids = card->GroupsJoinedLC();
00152 if (0 < ids->Count())
00153 {
00154 groupId = (*ids)[0];
00155 }
00156 CleanupStack::PopAndDestroy(ids);
00157 }
00158 if (KNullContactId != groupId)
00159 {
00160
00161 group = iCntDb->OpenContactLX(groupId);
00162 CleanupStack::PushL(group);
00163 iCntDb->RemoveContactFromGroupL(*item, *group);
00164
00165 CleanupStack::PopAndDestroy(group);
00166 CleanupStack::PopAndDestroy();
00167 group = NULL;
00168 }
00169 CleanupStack::PopAndDestroy(item);
00170 CleanupStack::PopAndDestroy();
00171
00172
00173
00174 groupId = KNullContactId;
00175 CContactIdArray* groupArrayId = iCntDb->GetGroupIdListL();
00176 if (NULL != groupArrayId)
00177 {
00178 CleanupStack::PushL(groupArrayId);
00179 for (TInt ii = 0 ; ii < groupArrayId->Count() ; ++ii)
00180 {
00181 group = iCntDb->ReadContactLC((*groupArrayId)[ii]);
00182 TPtrC label = ((CContactGroup*)group)->GetGroupLabelL();
00183 if (aNewGroup == label)
00184 {
00185 groupId = (*groupArrayId)[ii];
00186 ii = groupArrayId->Count();
00187 }
00188 CleanupStack::PopAndDestroy(group);
00189 group = NULL;
00190 }
00191 CleanupStack::PopAndDestroy(groupArrayId);
00192 }
00193 if (KNullContactId == groupId)
00194 {
00195 group = iCntDb->CreateContactGroupLC(aNewGroup);
00196 groupId = group->Id();
00197 CleanupStack::PopAndDestroy(group);
00198 }
00199
00200 iCntDb->AddContactToGroupL(aCntId, groupId);
00201 }
00202
00203
00204