Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 37
Registered: ‎07-13-2014
My Device: Z10

Converting QByteArray to Contact using Contact Service API

Hi All,

 

I am using Contact Service API to convert contacts to QByteArray and subsequently write it to file.

 

Code for write is as below:-

 

QFile myFile(QDir::currentPath() + "vCardContacts.dat");

if(myFile.open(QIODevice::WriteOnly))
{
  QDataStream ds(&myFile);
  //First a magic number to identify the file as your custom format (define it as a constant!)
  ds << (quint32)0xAAAAFFAA
  //Second the version number of your custom file (define it as a constant!)
  ds << (quint32)777
  //depending on the version number of your file you should select the 
  //apropiate datastream version
  ds.setVersion(QDataStream::Qt_4_6);
   foreach (const Contact &idContact, contactsList) 
   {
      // Fetch the complete details for this contact ID
      const Contact contact = m_contactService->contactDetails(idContact.id());
      QByteArray contactArray = m_contactService->contactToVCard(contact);
      ds << contactArray;
   }
  myFile.close()
}

 

 

For read operation i tried below mentioned code snippet. 

 

 

QFile myFile(WHERE_THE_FILE_WAS_SAVED);

	if(myFile.open(QIODevice::ReadOnly))
	{
	  QDataStream ds(&myFile);
	  //First a magic number to identify the file as your custom format (define it as a constant!)
          quint32 magic;
	  ds >> magic;
	  //MyMagicNumber is a constant defined outside the class
          if(magic != MyMagicNumber)
	  {
	    //File is incompatible, return whatever you want to check for error
            return;
          }
	  //Second the version number of your custom file (define it as a constant!)
          quint32 fileVersion
	  ds >> fileVersion;
	  //depending on the version number of your file you should select the
	  //apropiate datastream version 
	  switch(fileVersion)
	  {
	    case 100:
	    case 200:
	      ds.setVersion(QDataStream::Qt_4_0);
	      break;
	    case 777:
	      ds.setVersion(QDataStream::Qt_4_6);
	      break;
	  }

	   while(!ds.atEnd())
	   {
              QByteArray tmpByteArray;
	      ds >> tmpByteArray;
	      //Fill your contactList
	   }
	  myFile.close();

 

Problem which is happening is that how to convert QByteArray to COntact.

 

  {
              QByteArray tmpByteArray;
	      ds >> tmpByteArray;
	      //Fill your contactList
	   }

 

 

I can't find any method which will convert QByte Array to Contact using ContactService

 

http://developer.blackberry.com/native/reference/cascades/bb__pim__contacts__contactservice.html

 

Please tell how to convert the QByteArray to Contact or is there any other way possible.

 

Thanks & Regards,

Developer
Posts: 16,583
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Converting QByteArray to Contact using Contact Service API

https://developer.blackberry.com/native/reference/cascades/bb__pim__contacts__contactservice.html#fu...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 37
Registered: ‎07-13-2014
My Device: Z10

Re: Converting QByteArray to Contact using Contact Service API

Hi,

 

As per the signature of method given in API its signature is as:-

 

Contact contactFromVCard (

 

But as per the code stated above i need to convert QByteArray into Contact but this method requires argument as QString.

 

Please resolve 

Developer
Posts: 16,583
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Converting QByteArray to Contact using Contact Service API

QString has a constructor that takes a QByteArray as an argument.
http://qt-project.org/doc/qt-4.8/qstring.html#QString-9
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter