Qt-based BB10 API Examples Documentation

Accounts.cpp Example File

    /* Copyright (c) 2012, 2013  BlackBerry Limited.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    * http://www.apache.org/licenses/LICENSE-2.0
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.

    #include "Accounts.hpp"

    #include "AccountEditor.hpp"
    #include "AccountViewer.hpp"

    #include <bb/pim/account/Provider>
    #include <bb/pim/account/Result>

    using namespace bb::cascades;
    using namespace bb::pim::account;

    Accounts::Accounts(QObject *parent)
        : QObject(parent)
        , m_accountService(new AccountService())
        , m_model(new GroupDataModel(this))
        , m_filter("Calendars")
        , m_accountEditor(new AccountEditor(m_accountService, this))
        , m_accountViewer(new AccountViewer(m_accountService, this))
        , m_currentAccountId(-1)
        // Disable grouping in data model

        // Ensure to invoke the filterAccounts() method whenever an account has been added, changed or removed
        bool ok = connect(m_accountService, SIGNAL(accountsChanged(bb::pim::account::AccountsChanged)), SLOT(filterAccounts()));

        // Fill the data model with accounts initially

    void Accounts::setCurrentAccount(const QVariantList &indexPath)
        // Extract the ID of the selected account from the model
        if (indexPath.isEmpty()) {
            m_currentAccountId = -1;
        } else {
            const QVariantMap entry = m_model->data(indexPath).toMap();
            m_currentAccountId = entry.value("accountId").toLongLong();

    void Accounts::createAccount()
        // Prepare the account editor for creating a new account

    void Accounts::editAccount()
        // Prepare the account editor for editing the current account

    void Accounts::viewAccount()
        // Prepare the account viewer for displaying the current account

    void Accounts::deleteAccount()

    bb::cascades::GroupDataModel* Accounts::model() const
        return m_model;

    QString Accounts::filter() const
        return m_filter;

    void Accounts::setFilter(const QString &filter)
        if (m_filter == filter)

        m_filter = filter;
        emit filterChanged();

        // Update the model now that the filter criterion has changed

    AccountEditor* Accounts::accountEditor() const
        return m_accountEditor;

    AccountViewer* Accounts::accountViewer() const
        return m_accountViewer;

    void Accounts::filterAccounts()
        static QHash<QString, Service::Type> serviceTypes;
        if (serviceTypes.isEmpty()) {
            serviceTypes.insert(QLatin1String("Calendars"), Service::Calendars);
            serviceTypes.insert(QLatin1String("Contacts"), Service::Contacts);
            serviceTypes.insert(QLatin1String("Notebook"), Service::Notebook);
            serviceTypes.insert(QLatin1String("Geolocations"), Service::Geolocations);
            serviceTypes.insert(QLatin1String("Linking"), Service::Linking);
            serviceTypes.insert(QLatin1String("Memos"), Service::Memos);
            serviceTypes.insert(QLatin1String("Messages"), Service::Messages);
            serviceTypes.insert(QLatin1String("Tags"), Service::Tags);
            serviceTypes.insert(QLatin1String("Tasks"), Service::Tasks);
            serviceTypes.insert(QLatin1String("Phone"), Service::Phone);

        const QList<Account> accounts = m_accountService->accounts(serviceTypes.value(m_filter));

        // Clear the old account information from the model

        // Iterate over the list of accounts
        foreach (const Account &account, accounts) {
            // Copy the data into a model entry
            QVariantMap entry;
            entry["accountId"] = account.id();
            entry["provider"] = account.provider().name();
            entry["displayName"] = account.displayName();

            // Add the entry to the model