examples/ForumNokia/ContactsModel/src/ContactsModelDocument.cpp

00001 /*
00002  * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 // INCLUDE FILES
00031 #include "ContactsModelDocument.h"
00032 #include "ContactsModelAppUi.h"
00033 
00034 #include <cntfilt.h>
00035 #include <cntitem.h>
00036 
00037 const TInt KBufferSize = 256;
00038 
00039 // ================= MEMBER FUNCTIONS =======================
00040 
00041 // constructor
00042 CContactsModelDocument::CContactsModelDocument(CEikApplication& aApp)
00043 : CAknDocument(aApp)
00044     {
00045     }
00046 
00047 // destructor
00048 CContactsModelDocument::~CContactsModelDocument()
00049     {
00050         delete iContacts;
00051         delete iTextDef;
00052     }
00053 
00054 // EPOC default constructor can leave.
00055 void CContactsModelDocument::ConstructL()
00056     {
00057         iContacts = NULL;
00058         UpdateContactsL();
00059         //create custom text definition to display contact item
00060         iTextDef = CContactTextDef::NewL();
00061         //add given name field and family name field
00062         iTextDef->AppendL(TContactTextDefItem(KUidContactFieldGivenName, _L(" ")));
00063         iTextDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
00064     }
00065 
00066 // Two-phased constructor.
00067 CContactsModelDocument* CContactsModelDocument::NewL(
00068         CEikApplication& aApp)     // CContactsModelApp reference
00069     {
00070     CContactsModelDocument* self = new (ELeave) CContactsModelDocument( aApp );
00071     CleanupStack::PushL( self );
00072     self->ConstructL();
00073     CleanupStack::Pop();
00074 
00075     return self;
00076     }
00077 
00078 // Get the contact information
00079 HBufC * CContactsModelDocument::GetItemL(TInt anIndex)
00080         {
00081         // Open the default contact database
00082         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00083         CleanupStack::PushL(contactsDb);
00084 
00085         TBuf<KBufferSize> buf;
00086         //read contact item with the default text definition
00087         if((*iContacts).Count() >= anIndex)
00088                 contactsDb->ReadContactTextDefL((*iContacts)[anIndex], buf, iTextDef);
00089 
00090         CleanupStack::PopAndDestroy(); // contactsDb
00091         return buf.AllocL();
00092         }
00093 
00094 // return contacts items number
00095 TInt CContactsModelDocument::ItemCount()
00096         {
00097         return iContacts->Count();
00098         }
00099 
00100 void CContactsModelDocument::UpdateContactsL()
00101         {
00102         //open database
00103         // Open the default contact database
00104         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00105         CleanupStack::PushL(contactsDb);
00106 
00107         CCntFilter *filter = CCntFilter::NewLC();
00108 
00109         //get all contact items (no groups, templates...)
00110         filter->SetContactFilterTypeALL(EFalse);
00111         filter->SetContactFilterTypeCard(ETrue);
00112 
00113         contactsDb->FilterDatabaseL(*filter);
00114 
00115         if(iContacts)
00116         {
00117                 delete iContacts;
00118                 iContacts = NULL;
00119         }
00120         iContacts = CContactIdArray::NewL(filter->iIds);
00121 
00122         CleanupStack::PopAndDestroy(2); //filter, contactsDb
00123         }
00124 
00125 TInt CContactsModelDocument::ImportL(RReadStream& aReadStream)
00126         {
00127         // Open the default contact database
00128         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00129         CleanupStack::PushL(contactsDb);
00130 
00131         TBool result = EFalse;
00132         TUid p1;
00133         p1.iUid = KVersitEntityUidVCard;
00134         CArrayPtr<CContactItem>* imported = contactsDb->ImportContactsL(p1,
00135                                                                 aReadStream,
00136                                                                 result,
00137                                                                 CContactDatabase::ETTFormat);
00138         imported->ResetAndDestroy();
00139         delete imported;
00140 
00141         CleanupStack::PopAndDestroy(); // contactsDb
00142 
00143         return result?KErrNone:KErrGeneral;
00144         }
00145 
00146 void CContactsModelDocument::ExportL(RWriteStream& aWriteStream, TInt aExportItem)
00147         {
00148         // Open the default contact database
00149         CContactDatabase* contactsDb = CContactDatabase::OpenL();
00150         CleanupStack::PushL(contactsDb);
00151 
00152         CContactIdArray* exportContacts = CContactIdArray::NewL();
00153         CleanupStack::PushL(exportContacts);
00154 
00155         if((*iContacts).Count() >= aExportItem)
00156                 exportContacts->AddL( (*iContacts)[aExportItem] );
00157 
00158         TUid p1;
00159         p1.iUid = KVersitEntityUidVCard;
00160         contactsDb->ExportSelectedContactsL(p1,
00161                                                                 *exportContacts,
00162                                                                 aWriteStream,
00163                                                                 CContactDatabase::EExcludeUid);
00164 
00165         CleanupStack::PopAndDestroy(2); // exportContacts, contactsDb
00166         }
00167 
00168 // ----------------------------------------------------
00169 // CContactsModelDocument::CreateAppUiL()
00170 // constructs CContactsModelAppUi
00171 // ----------------------------------------------------
00172 //
00173 CEikAppUi* CContactsModelDocument::CreateAppUiL()
00174     {
00175     return new (ELeave) CContactsModelAppUi;
00176     }
00177 
00178 // End of File

Generated by  doxygen 1.6.2