On BlackBerry 10, how can my Qt application be notified when a contact is added, deleted, or updated? Is there a contact API?
Get notification when contacts are added/deleted/updated
254 views Asked by Dhiraj Nangare At
3
There are 3 answers
3
On
---hpp file------
API : bb::pim::contacts
bb/pim/contacts/ContactService
bb/pim/contacts/Contact
bb/pim/contacts/ContactAttributeBuilder
bb/pim/contacts/ContactBuilder..
see folwing example
#include <bb/pim/contacts/ContactService>
#include <QtCore/QObject>
class ContactEditor : public QObject
{
Q_OBJECT
// The data properties of the contact that is created or updated
Q_PROPERTY(QString firstName READ firstName WRITE setFirstName NOTIFY firstNameChanged)
Q_PROPERTY(QString lastName READ lastName WRITE setLastName NOTIFY lastNameChanged)
Q_PROPERTY(QDateTime birthday READ birthday WRITE setBirthday NOTIFY birthdayChanged)
Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
// Defines whether the editor is in 'create' or 'edit' mode
Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
Q_ENUMS(Mode)
public:
/**
* Describes the mode of the contact editor.
* The mode information are used to adapt the behavior of the editor and
* provide hints to the UI.
*/
enum Mode {
CreateMode,
EditMode
};
ContactEditor(bb::pim::contacts::ContactService *service, QObject *parent = 0);
void setMode(Mode mode);
Mode mode() const;
public Q_SLOTS:
/**
* Loads the contact with the given ID.
*/
void loadContact(bb::pim::contacts::ContactId contactId);
/**
* Save the currently loaded contact if in 'edit' mode or creates a new one
* if in 'create' mode.
*/
void saveContact();
/**
* Resets all fields of the contact editor.
*/
void reset();
Q_SIGNALS:
// The change notification signals of the properties
void firstNameChanged();
void lastNameChanged();
void birthdayChanged();
void emailChanged();
void modeChanged();
private:
// The accessor methods of the properties
void setFirstName(const QString &firstName);
QString firstName() const;
void setLastName(const QString &lastName);
QString lastName() const;
void setBirthday(const QDateTime &birthday);
QDateTime birthday() const;
void setEmail(const QString &email);
QString email() const;
// The central object to access the contact service
bb::pim::contacts::ContactService *m_contactService;
// The ID of the currently loaded contact (if in 'edit' mode)
bb::pim::contacts::ContactId m_contactId;
// The property values
QString m_firstName;
QString m_lastName;
QDateTime m_birthday;
QString m_email;
Mode m_mode;
};
//! [0]
#endif
-----cpp file -----
using namespace bb::pim::contacts;
//! [0]
/**
* A helper method to update a single attribute on a Contact object.
* It first deletes the old attribute (if it exists) and adds the attribute with the
* new value afterwards.
*/
template <typename T>
static void updateContactAttribute(ContactBuilder &builder, const Contact &contact,
AttributeKind::Type kind, AttributeSubKind::Type subKind,
const T &value)
{
// Delete previous instance of the attribute
QList<ContactAttribute> attributes = contact.filteredAttributes(kind);
foreach (const ContactAttribute &attribute, attributes) {
if (attribute.subKind() == subKind)
builder.deleteAttribute(attribute);
}
// Add new instance of the attribute with new value
builder.addAttribute(ContactAttributeBuilder()
.setKind(kind)
.setSubKind(subKind)
.setValue(value));
}
//! [0]
//! [1]
ContactEditor::ContactEditor(ContactService *service, QObject *parent)
: QObject(parent)
, m_contactService(service)
, m_contactId(-1)
, m_birthday(QDateTime::currentDateTime())
, m_mode(CreateMode)
{
}
//! [1]
//! [2]
void ContactEditor::loadContact(ContactId contactId)
{
m_contactId = contactId;
// Load the contact from the persistent storage
const Contact contact = m_contactService->contactDetails(m_contactId);
// Update the properties with the data from the contact
m_firstName = contact.firstName();
m_lastName = contact.lastName();
m_birthday = QDateTime::currentDateTime();
const QList<ContactAttribute> dateAttributes = contact.filteredAttributes(AttributeKind::Date);
foreach (const ContactAttribute &dateAttribute, dateAttributes) {
if (dateAttribute.subKind() == AttributeSubKind::DateBirthday)
m_birthday = dateAttribute.valueAsDateTime();
}
m_email.clear();
const QList<ContactAttribute> emails = contact.emails();
if (!emails.isEmpty())
m_email = emails.first().value();
// Emit the change notifications
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
}
//! [2]
//! [3]
void ContactEditor::saveContact()
{
if (m_mode == CreateMode) {
// Create a builder to assemble the new contact
ContactBuilder builder;
// Set the first name
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameGiven)
.setValue(m_firstName));
// Set the last name
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameSurname)
.setValue(m_lastName));
// Set the birthday
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Date)
.setSubKind(AttributeSubKind::DateBirthday)
.setValue(m_birthday));
// Set the email address
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Email)
.setSubKind(AttributeSubKind::Other)
.setValue(m_email));
// Save the contact to persistent storage
m_contactService->createContact(builder, false);
} else if (m_mode == EditMode) {
// Load the contact from persistent storage
Contact contact = m_contactService->contactDetails(m_contactId);
if (contact.id()) {
// Create a builder to modify the contact
ContactBuilder builder = contact.edit();
// Update the single attributes
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameGiven, m_firstName);
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameSurname, m_lastName);
updateContactAttribute<QDateTime>(builder, contact, AttributeKind::Date, AttributeSubKind::DateBirthday, m_birthday);
updateContactAttribute<QString>(builder, contact, AttributeKind::Email, AttributeSubKind::Other, m_email);
// Save the updated contact back to persistent storage
m_contactService->updateContact(builder);
}
}
}
//! [3]
//! [4]
void ContactEditor::reset()
{
// Reset all properties
m_firstName.clear();
m_lastName.clear();
m_birthday = QDateTime::currentDateTime();
m_email.clear();
// Emit the change notifications
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
}
//! [4]
void ContactEditor::setFirstName(const QString &firstName)
{
if (m_firstName == firstName)
return;
m_firstName = firstName;
emit firstNameChanged();
}
QString ContactEditor::firstName() const
{
return m_firstName;
}
void ContactEditor::setLastName(const QString &lastName)
{
if (m_lastName == lastName)
return;
m_lastName = lastName;
emit lastNameChanged();
}
QString ContactEditor::lastName() const
{
return m_lastName;
}
void ContactEditor::setBirthday(const QDateTime &birthday)
{
if (m_birthday.date() == birthday.date())
return;
m_birthday = birthday;
emit birthdayChanged();
}
QDateTime ContactEditor::birthday() const
{
return m_birthday;
}
void ContactEditor::setEmail(const QString &email)
{
if (m_email == email)
return;
m_email = email;
emit emailChanged();
}
QString ContactEditor::email() const
{
return m_email;
}
void ContactEditor::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
ContactEditor::Mode ContactEditor::mode() const
{
return m_mode;
}
0
On
use alert like this -->
alert(tr("contact saved"));
alert(tr("contact added"));
alert(tr("contact deleted"));
refer following sample
-----------qml--------------
Button { horizontalAlignment: HorizontalAlignment.Center
text: qsTr("Update")
onClicked: {
_app.updateRecord(idUpdateTextField.text, firstNameUpdateTextField.text, lastNameUpdateTextField.text);
}
}
-----------------cpp file-------------------
bool App::updateRecord(const QString &customerID, const QString &firstName, const QString &lastName) {
bool intConversionGood = false;
const int customerIDKey = customerID.toInt(&intConversionGood);
if (!intConversionGood) {
alert(tr("You must provide valid integer key."));
return false;
}
QSqlDatabase database = QSqlDatabase::database();
QSqlQuery query(database);
const QString sqlCommand = "UPDATE customers "
" SET firstName = :firstName, lastName = :lastName"
" WHERE customerID = :customerID";
query.prepare(sqlCommand);
query.bindValue(":firstName", firstName);
query.bindValue(":lastName", lastName);
query.bindValue(":customerID", customerIDKey);
bool updated = false;
if (query.exec()) {
if (query.numRowsAffected() > 0) {
alert(tr("Customer with id=%1 was updated.").arg(customerID));
updated = true;
} else {
alert(tr("Customer with id=%1 was not found.").arg(customerID));
}
QContactManager has these 3 signals:
Connect your slots to them in order to receive the notifications.