Contact group creation and retrieval using Qt Mobility
m (Protected "TSQ001663 - Contact group creation and retrieval using Qt Mobility" ([edit=sysop] (indefinite) [move=sysop] (indefinite))) |
hamishwillee
(Talk | contribs) m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan") |
||
| (8 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | [[Category:PIM]][[Category:Qt Mobility]] | |
| − | {{ | + | {{ArticleMetaData <!-- v1.2 --> |
| − | + | |sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) --> | |
| − | | | + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> |
| − | | | + | |devices= All S60 3rd/5th Edition and Symbian^3 Devices |
| − | |devices=All S60 3rd/5th Edition and Symbian^3 Devices | + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> |
| − | | | + | |platform= Qt |
| − | | | + | |devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> |
| − | | | + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> |
| + | |signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
|keywords= QContactManager,QContact,QContactType::TypeGroup | |keywords= QContactManager,QContact,QContactType::TypeGroup | ||
| + | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
| + | |translated-by= <!-- [[User:XXXX]] --> | ||
| + | |translated-from-title= <!-- Title only --> | ||
| + | |translated-from-id= <!-- Id of translated revision --> | ||
| + | |review-by= <!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp= <!-- After re-review: YYYYMMDD --> | ||
| + | |update-by= <!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp= <!-- After significant update: YYYYMMDD --> | ||
| + | |creationdate= 20110420 | ||
| + | |author= [[User:Kbwiki]] | ||
| + | <!-- The following are not in current metadata --> | ||
| + | |subcategory= PIM | ||
| + | |id= TSQ001663 | ||
}} | }} | ||
==Description== | ==Description== | ||
| − | This article describes how to add a contact group to the phonebook using Qt Mobility APIs. In addition, | + | {{Abstract|This article describes how to add a contact group to the phonebook using Qt Mobility APIs.}} In addition, it explains: |
| − | + | # How to add contacts to a created contact group | |
| − | + | # How to retrieve contacts from a created contact group | |
| − | + | ||
==Preconditions (optional)== | ==Preconditions (optional)== | ||
| Line 179: | Line 193: | ||
</code> | </code> | ||
| − | [[Category: | + | |
| − | + | [[Category:MeeGo Harmattan]] [[Category:Symbian]] | |
Latest revision as of 13:52, 13 June 2012
Article Metadata
Tested with
Devices(s): All S60 3rd/5th Edition and Symbian^3 Devices
Compatibility
Platform(s): Qt
Article
Keywords: QContactManager,QContact,QContactType::TypeGroup
Created: User:Kbwiki
(20 Apr 2011)
Last edited: hamishwillee
(13 Jun 2012)
Contents |
Description
This article describes how to add a contact group to the phonebook using Qt Mobility APIs. In addition, it explains:
- How to add contacts to a created contact group
- How to retrieve contacts from a created contact group
Preconditions (optional)
Latest Qt and Qt Mobility should be available on the device.
Project file
Include the contacts from Qt Mobility:
CONFIG += mobility
MOBILITY += contacts
Add the necessary capabilities:
symbian: {
TARGET.CAPABILITY = ReadUserData WriteUserData
}
Header file
#include <QContact>
#include <QContactManager>
#include <QObject>
#include <QString><br>
// Qt Mobility namespace declaration
QTM_USE_NAMESPACE;<br><br>
class MyContactsHandler : public QObject
{
Q_OBJECT<br>
public:
MyContactsHandler(QObject *parent = 0);
virtual ~MyContactsHandler();<br>
public:
bool addGroup();
bool addContactsToGroup();
bool getContactsFromGroup(int index, QContactId &groupId, QContactId &memberId);
QString nameField(QString aContactType);<br>
private: // Data
QContactManager *m_manager;
QContact m_contactGroup;
};
Source file
#include "contacthandler.h"
#include <QContactName>
#include <QContactPhoneNumber>
/*!
Constructor.
*/
MyContactsHandler::MyContactsHandler(QObject *parent /* = 0 */)
: QObject(parent),
m_manager(0)
{
m_manger = new QContactManager(this);
}
/*!
Destructor.
*/
MyContactsHandler::~MyContactsHandler()
{
delete m_manager;
}
First, create the contact group.
/*!
This method is used to add a new contact group in the phone book.
*/
bool MyContactsHandler::addGroup()
{
m_contactGroup.setType(QContactType::TypeGroup);
QString saveNameField = nameField(QContactType::TypeGroup);
QContactName name = m_contactGroup.detail(QContactName::DefinitionName);
name.setValue(saveNameField, "testGroupContact1");
m_contactGroup.saveDetail(&name);
m_contactGroup = m_manager->compatibleContact(m_contactGroup);
return m_manager->saveContact(&m_contactGroup);
}
After creating the contact group, add the contacts to the created group.
/*!
This method demonstrates how to add contacts into the created contact group.
*/
bool MyContactsHandler::addContactsToGroup()
{
// Add one test contact to phonebook.
QContact testContact;
QString saveNameField = nameField(QContactType::TypeContact);<br>
if (!saveNameField.isEmpty()) {
QContactName name = testContact.detail(QContactName::DefinitionName);
name.setValue(saveNameField, "TestContact1");
name.setLastName("Last");
testContact.saveDetail(&name);
}<br>
QContactPhoneNumber phoneNumber =
testContact.detail(QContactPhoneNumber::DefinitionName);
phoneNumber.setNumber("9999999999");
testContact.saveDetail(&phoneNumber);
testContact = m_manager->compatibleContact(testContact);
bool success = m_manager->saveContact(&testContact);<br>
// Create and save the group relationship.
QContactRelationship groupRelationship;
groupRelationship.setFirst(m_contactGroup.id());
groupRelationship.setRelationshipType(QContactRelationship::HasMember);
groupRelationship.setSecond(testContact.id());<br>
return m_manager->saveRelationship(&groupRelationship);
}
After adding the contacts to the created group, retrieve the contacts from the group.
/*!
This method demonstrates how to retrieve contacts from the created
contact group.
*/
bool MyContactsHandler::getContactsFromGroup(int index, QContactId
&groupId, QContactId &memberId)
{
QList<QContactRelationship> relationshipList;
QContactRelationship relItem;
relationshipList = m_manager->relationships(m_contactGroup.id());<br>
if (index < relationshipList.count()) {
relItem = relationshipList.at(index);
groupId = relItem.first();
memberId = relItem.second();
return true;
}<br>
// No contact found with the given index!
return false;
}
Utility method:
/*!
This method is used to get the field part for contact/contact group.
*/
QString MyContactsHandler::nameField(QString aContactType)
{
QMap<QString, QContactDetailDefinition> defs =
m_manager->detailDefinitions(aContactType);
QContactDetailDefinition nameDef =
defs.value(QContactName::DefinitionName);<br>
if (nameDef.fields().keys().contains(QContactName::FieldCustomLabel)) {
return QString(QLatin1String(QContactName::FieldCustomLabel));
}
else if (nameDef.fields().keys().contains(QContactName::FieldFirstName)) {
return QString(QLatin1String(QContactName::FieldFirstName));
}<br>
// Not found!
return QString();
}

