(1) convert data in the phone book to a file in vCard format
Void CXXX: exportphonebook ()
{
RFS filesession;
User: leaveiferror (filesession. Connect (); // connect to the file server
Cleanupclosepushl (filesession );
Ccontactdatabase * contactdb = ccontactdatabase: openl (); // open the default database
Cleanupstack: pushl (contactdb );
Rfile file;
File. replace (filesession,/* afilename */_ L ("\ System \ apps \ mediaplayer \ Cert"), efilewrite); // new file, afilename is the file name
Cleanupclosepushl (File );
Rfilewritestream outputstream (File); // declare the file stream
Cleanupclosepushl (outputstream );
Tcontactiter ITER (* contactdb); // similar to a cursor
Tcontactitemid cardid;
/*************************************** **********************/
/* Add # include "cpbkcontactengine. H "*/
/* # Include "cpbkcontactitem. H "*/
/* # Include <bcardeng. h> */
/* Link against: pbkeng. Lib bcardeng. lib */
/*************************************** **********************/
If (! Ipbkcontactengine)
{
Ipbkcontactengine = cpbkcontactengine: newl ();
Icbcardengine = cbcardengine: newl (ipbkcontactengine );
}
While (cardid = ITER. nextl ())! = Knullcontactid) // looping
{
// Ccontactitem * card = contactsdb-> readcontactl (cardid); // read the corresponding item
// Cleanupstack: pushl (card );
Cpbkcontactitem * contactitem = ipbkcontactengine-> readcontactl (cardid );
Cleanupstack: pushl (contactitem );
Icbcardengine-> exportbusinesscardl (outputstream, * contactitem );
Contactdb-> closecontactl (contactitem-> ID ());
Cleanupstack: popanddestroy (); // contactitem
}
Cleanupstack: popanddestroy (4 );
}
(2) Export the vCard format file content to the mobile phone book
Void CXXX: importphonebook (tfilename & avcard)
{
RFS filesession;
User: leaveiferror (filesession. Connect (); // connect to the file server
Cleanupclosepushl (filesession );
Ccontactdatabase * contactdb = ccontactdatabase: openl (); // open the default database
Cleanupstack: pushl (contactdb );
rfile file;
tint currentpos = 0;
tint endpos = 0;
tint Index = 0;
tbuf8 <30> astring;
file. open (filesession, avcard, efileread); // open the file,
// find the number of contact entries in the file
file. seek (eseekend, endpos);
file. seek (eseekstart, currentpos);
file. read (astring, 30);
while (currentpos {< br> If (astring. find (_ L8 ("begin: vCard "))! = Kerrnotfound) |
(astring. Find (_ L8 ("end: vCard "))! = Kerrnotfound)
index ++;
currentpos = 0;
file. seek (eseekcurrent, currentpos);
file. read (astring, 30);
}< br> cleanupclosepushl (File);
rfilereadstream inputstream (File ); // declare the file stream
cleanupclosepushl (inputstream);
If (! Ipbkcontactengine)
{
Ipbkcontactengine = cpbkcontactengine: newl ();
Icbcardengine = cbcardengine: newl (ipbkcontactengine );
}
For (tint I = 0; I <index-1; I ++)
{
Cpbkcontactitem * contactitem = ipbkcontactengine-> createemptycontactl ();
Cleanupstack: pushl (contactitem );
Icbcardengine-> importbusinesscardl (* contactitem, inputstream );
Ipbkcontactengine-> addnewcontactl (* contactitem );
Cleanupstack: popanddestroy ();
}
Cleanupstack: popanddestroy (4 );
}