添加/删除/更新联系人时获取通知

时间:2013-09-04 05:24:11

标签: c++ qt blackberry-10 blackberry-cascades qt-mobility

在BlackBerry 10上,如何在添加,删除或更新联系人时通知我的Qt应用程序?是否有联系API?

3 个答案:

答案 0 :(得分:2)

QContactManager有以下3个信号:

void QContactManager::contactsAdded ( const QList<QContactLocalId> & contactIds );
void QContactManager::contactsChanged ( const QList<QContactLocalId> & contactIds );
void QContactManager::contactsRemoved ( const QList<QContactLocalId> & contactIds );

将您的广告位连接到他们以接收通知。

答案 1 :(得分:0)

--- hpp文件------

API :bb :: pim :: contacts

BB / PIM /联系人/的ContactService

BB / PIM /联系人/联系

BB / PIM /联系人/ ContactAttributeBuilder

bb / pim / contacts / ContactBuilder ..

见下面的例子

#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文件-----

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;
}

答案 2 :(得分:0)

使用这样的提醒 - &gt;

警告(tr(“联系人保存”));

警告(tr(“已添加联系人”));

警告(tr(“联系人已删除”));

参考以下示例

----------- -------------- QML

按钮{             horizo​​ntalAlignment:Horizo​​ntalAlignment.Center

        text: qsTr("Update")

        onClicked: {
            _app.updateRecord(idUpdateTextField.text, firstNameUpdateTextField.text, lastNameUpdateTextField.text);
        }
    }

----------------- cpp文件-------------------

bool App :: updateRecord(const QString&amp; customerID,const QString&amp; firstName,const QString&amp; 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));
    }