zoukankan      html  css  js  c++  java
  • Import and Export vCard item(转)


    CS000900Creation dateApril 17, 2008
    PlatformS60 3rd Edition, FP1Tested on devicesNokia N93
    CategorySymbian C++SubcategoryPIM

    Keywords (APIs, classes, methods, functions): RFileReadStream, CContactDatabase, CContactItem, CContactDatabase::OpenL(), CContactDatabase::ImportContactsL()

    Overview

    This snippet shows a simple function implementation to import one or more vCards from a given file to the default contacts database.

    This snippet can be self-signed.

    MMP file

    The following capabilities and libraries are required:

    CAPABILITY WriteUserData
    LIBRARY  euser.lib
    LIBRARY estor.lib
    LIBRARY efsrv.lib
    LIBRARY cntmodel.lib

    Source file

    #include <e32cmn.h>   //TUid
    #include <e32std.h> //User
    #include <e32base.h> //CArrayPtr, CleanupStack
    #include <e32def.h> //TBool
    #include <s32file.h> //RFileReadStream
    #include <f32file.h> //RFs
    #include <cntdb.h> //CContactDatabase
    #include <cntitem.h> //CContactItem
    TBool ImportVCardL(const TDesC& aFileName)
    {
    RFs fileSession;
    RFile file;
    TBool result = EFalse;
     
    User::LeaveIfError(fileSession.Connect());
    CleanupClosePushL(fileSession);
     
    if (file.Open(fileSession, aFileName, EFileRead) != KErrNone)
    {
    //failed to open the file
    CleanupStack::PopAndDestroy(); //fileSession
    return EFalse;
    }
    CleanupClosePushL(file);
     
    //open a read stream to the file
    RFileReadStream inputFileStream(file);
    CleanupClosePushL(inputFileStream);
     
    //open the default contacts database
    CContactDatabase* contactsDb = CContactDatabase::OpenL();
    CleanupStack::PushL(contactsDb);
     
    //KVersitEntityUidVCard is used to identify a vCard
    TUid uid = TUid::Uid(KVersitEntityUidVCard);
     
    //import one or more vCards from the read stream
    CArrayPtr<CContactItem>* imported = contactsDb->ImportContactsL(uid,
    inputFileStream,
    result,
    CContactDatabase::ETTFormat);
     
    //caller has ownership of the array and frees allocated memory
    imported->ResetAndDestroy();
    delete imported;
     
    CleanupStack::PopAndDestroy(4); //contactsDb,inputFileStream,
    //file,fileSession
     
    return result;
    }

    Postconditions

    One or more vCards from the given file are imported to the default contacts database. ETrue is returned to the caller or in case of an error, EFalse is returned.

    IDCS000901Creation dateApril 17, 2008
    PlatformS60 3rd Edition, FP1Tested on devicesNokia N93
    CategorySymbian C++SubcategoryPIM

    Keywords (APIs, classes, methods, functions): RFileWriteStream, CContactDatabase, CContactIdArray, CCntFilter, CContactDatabase::OpenL(), CContactDatabase::ExportSelectedContactsL(), CContactDatabase::FilterDatabaseL(), CCntFilter::SetContactFilterTypeALL(), CCntFilter::SetContactFilterTypeCard()

    Overview

    This snippet shows a simple function implementation to export one contact from the default contacts database as a vCard to a given file.

    This snippet can be self-signed.

    MMP file

    The following capabilities and libraries are required:

    CAPABILITY ReadUserData
    LIBRARY  euser.lib
    LIBRARY estor.lib
    LIBRARY efsrv.lib
    LIBRARY cntmodel.lib

    Source file

    #include <e32cmn.h>   //TUid
    #include <e32std.h> //User
    #include <e32base.h> //CArrayPtr, CleanupStack
    #include <e32def.h> //TBool
    #include <s32file.h> //RFileReadStream
    #include <f32file.h> //RFs
    #include <cntdb.h> //CContactDatabase
    #include <cntdef.h> //CContactIdArray
    #include <cntfilt.h> //CCntFilter
    TBool ExportVCardL(const TDesC& aFileName, TInt aItemIndex)
    {
    RFs fileSession;
    RFile file;
     
    User::LeaveIfError(fileSession.Connect());
    CleanupClosePushL(fileSession);
     
    if (file.Replace(fileSession, aFileName, EFileWrite) != KErrNone)
    {
    //failed to create the file
    CleanupStack::PopAndDestroy(); //fileSession
    return EFalse;
    }
    CleanupClosePushL(file);
     
    //open a write stream to the file
    RFileWriteStream outputFileStream(file);
    CleanupClosePushL(outputFileStream);
     
    //open the default contacts database
    CContactDatabase* contactsDb = CContactDatabase::OpenL();
    CleanupStack::PushL(contactsDb);
     
    //create an array of contact IDs to export
    CContactIdArray* exportContacts = CContactIdArray::NewL();
    CleanupStack::PushL(exportContacts);
     
    //use a filter to get only contact items (e.g. templates are excluded)
    CCntFilter *filter = CCntFilter::NewLC();
    filter->SetContactFilterTypeALL(EFalse);
    filter->SetContactFilterTypeCard(ETrue);
    contactsDb->FilterDatabaseL(*filter);
     
    //create an array to hold all filtered contact items
    CContactIdArray* contactIds;
    contactIds = CContactIdArray::NewLC(filter->iIds);
     
    //add given contact(by index) to the array of contact IDs to export
    if((*contactIds).Count() >= aItemIndex)
    exportContacts->AddL((*contactIds)[aItemIndex] );
     
    CleanupStack::PopAndDestroy(2); //contactIds, filter
     
    //KVersitEntityUidVCard is used to identify a vCard
    TUid uid = TUid::Uid(KVersitEntityUidVCard);
    contactsDb->ExportSelectedContactsL(uid,
    *exportContacts,
    outputFileStream,
    //contact ID is no exported
    CContactDatabase::EExcludeUid);
     
    CleanupStack::PopAndDestroy(5); //exportContacts,contactsDb,
    //outputFileStream,file,fileSession
     
    return ETrue;
    }

    Postconditions

    A contact given by the index from the default contacts database is exported as a vCard to the given file.

    The function returns ETrue to the caller when the file creation succeeds or EFalse when the file creation fails.

  • 相关阅读:
    36.百马百担问题。有100匹马,驮100担货,大马驮3担,中马驮2担,两匹小马驮1担,问有大中小马多少匹,共有多少组解?
    35.鸡兔同笼问题:今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?
    34.设s=1+1/2+1/3+…+1/n,求与8最接近的s的值及与之对应的n值
    33.求1*2+2*3+3*4+……前n项的和
    32.求1+(1+2)+(1+2+3)+(1+2+3+4)+……的前n项的和
    31.假定2007年的一月一日是星期一,输入一个时间(包含年、月、日),求出它是星期几。
    vue-cli3 一直运行 /sockjs-node/info?t= 解决方案
    python pdf转word
    window django-https 证书
    Docker技术应用场景(转载)
  • 原文地址:https://www.cnblogs.com/yaoliang11/p/1858498.html
Copyright © 2011-2022 走看看