examples/ForumNokia/ContactsModel/src/ContactsModelDocument.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include "ContactsModelDocument.h"
00007 #include "ContactsModelAppUi.h"
00008 
00009 #include <cntfilt.h>
00010 #include <cntitem.h>
00011 
00012 const TInt KBufferSize = 256;
00013 
00014 // ================= MEMBER FUNCTIONS =======================
00015 
00016 // constructor
00017 CContactsModelDocument::CContactsModelDocument(CEikApplication& aApp)
00018 : CAknDocument(aApp)
00019     {
00020     }
00021 
00022 // destructor
00023 CContactsModelDocument::~CContactsModelDocument()
00024     {
00025         delete iContacts;
00026         delete iTextDef;
00027     }
00028 
00029 // EPOC default constructor can leave.
00030 void CContactsModelDocument::ConstructL()
00031     {
00032         iContacts = NULL;
00033         UpdateContactsL();
00034         //create custom text definition to display contact item
00035         iTextDef = CContactTextDef::NewL();
00036         //add given name field and family name field
00037         iTextDef->AppendL(TContactTextDefItem(KUidContactFieldGivenName, _L(" ")));
00038         iTextDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
00039     }
00040 
00041 // Two-phased constructor.
00042 CContactsModelDocument* CContactsModelDocument::NewL(
00043         CEikApplication& aApp)     // CContactsModelApp reference
00044     {
00045     CContactsModelDocument* self = new (ELeave) CContactsModelDocument( aApp );
00046     CleanupStack::PushL( self );
00047     self->ConstructL();
00048     CleanupStack::Pop();
00049 
00050     return self;
00051     }
00052 
00053 // Get the contact information
00054 HBufC * CContactsModelDocument::GetItemL(TInt anIndex)
00055         {
00056         // Open the default contact database
00057         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00058         CleanupStack::PushL(contactsDb);
00059 
00060         TBuf<KBufferSize> buf;
00061         //read contact item with the default text definition
00062         if((*iContacts).Count() >= anIndex)
00063                 contactsDb->ReadContactTextDefL((*iContacts)[anIndex], buf, iTextDef);
00064 
00065         CleanupStack::PopAndDestroy(); // contactsDb
00066         return buf.AllocL();
00067         }
00068 
00069 // return contacts items number
00070 TInt CContactsModelDocument::ItemCount()
00071         {
00072         return iContacts->Count();
00073         }
00074 
00075 void CContactsModelDocument::UpdateContactsL()
00076         {
00077         //open database
00078         // Open the default contact database
00079         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00080         CleanupStack::PushL(contactsDb);
00081 
00082         CCntFilter *filter = CCntFilter::NewLC();
00083 
00084         //get all contact items (no groups, templates...)
00085         filter->SetContactFilterTypeALL(EFalse);
00086         filter->SetContactFilterTypeCard(ETrue);
00087 
00088         contactsDb->FilterDatabaseL(*filter);
00089 
00090         if(iContacts)
00091         {
00092                 delete iContacts;
00093                 iContacts = NULL;
00094         }
00095         iContacts = CContactIdArray::NewL(filter->iIds);
00096 
00097         CleanupStack::PopAndDestroy(2); //filter, contactsDb
00098         }
00099 
00100 TInt CContactsModelDocument::ImportL(RReadStream& aReadStream)
00101         {
00102         // Open the default contact database
00103         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00104         CleanupStack::PushL(contactsDb);
00105 
00106         TBool result = EFalse;
00107         TUid p1;
00108         p1.iUid = KVersitEntityUidVCard;
00109         CArrayPtr<CContactItem>* imported = contactsDb->ImportContactsL(p1,
00110                                                                 aReadStream,
00111                                                                 result,
00112                                                                 CContactDatabase::ETTFormat);
00113         imported->ResetAndDestroy();
00114         delete imported;
00115 
00116         CleanupStack::PopAndDestroy(); // contactsDb
00117 
00118         return result?KErrNone:KErrGeneral;
00119         }
00120 
00121 void CContactsModelDocument::ExportL(RWriteStream& aWriteStream, TInt aExportItem)
00122         {
00123         // Open the default contact database
00124         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00125         CleanupStack::PushL(contactsDb);
00126 
00127         CContactIdArray* exportContacts = CContactIdArray::NewL();
00128         CleanupStack::PushL(exportContacts);
00129 
00130         if((*iContacts).Count() >= aExportItem)
00131                 exportContacts->AddL( (*iContacts)[aExportItem] );
00132 
00133         TUid p1;
00134         p1.iUid = KVersitEntityUidVCard;
00135         contactsDb->ExportSelectedContactsL(p1,
00136                                                                 *exportContacts,
00137                                                                 aWriteStream,
00138                                                                 CContactDatabase::EExcludeUid);
00139 
00140         CleanupStack::PopAndDestroy(2); // exportContacts, contactsDb
00141         }
00142 
00143 // ----------------------------------------------------
00144 // CContactsModelDocument::CreateAppUiL()
00145 // constructs CContactsModelAppUi
00146 // ----------------------------------------------------
00147 //
00148 CEikAppUi* CContactsModelDocument::CreateAppUiL()
00149     {
00150     return new (ELeave) CContactsModelAppUi;
00151     }
00152 
00153 // End of File

Generated by  doxygen 1.6.2