Accounts.cpp Example File
accounts/src/Accounts.cpp
#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)
{
m_model->setGrouping(ItemGrouping::None);
bool ok = connect(m_accountService, SIGNAL(accountsChanged(bb::pim::account::AccountsChanged)), SLOT(filterAccounts()));
Q_ASSERT(ok);
Q_UNUSED(ok);
filterAccounts();
}
void Accounts::setCurrentAccount(const QVariantList &indexPath)
{
if (indexPath.isEmpty()) {
m_currentAccountId = -1;
} else {
const QVariantMap entry = m_model->data(indexPath).toMap();
m_currentAccountId = entry.value("accountId").toLongLong();
}
}
void Accounts::createAccount()
{
m_accountEditor->reset();
m_accountEditor->setMode(AccountEditor::CreateMode);
}
void Accounts::editAccount()
{
m_accountEditor->loadAccount(m_currentAccountId);
m_accountEditor->setMode(AccountEditor::EditMode);
}
void Accounts::viewAccount()
{
m_accountViewer->setAccountId(m_currentAccountId);
}
void Accounts::deleteAccount()
{
m_accountService->deleteAccount(m_currentAccountId);
}
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)
return;
m_filter = filter;
emit filterChanged();
filterAccounts();
}
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));
m_model->clear();
foreach (const Account &account, accounts) {
QVariantMap entry;
entry["accountId"] = account.id();
entry["provider"] = account.provider().name();
entry["displayName"] = account.displayName();
m_model->insert(entry);
}
}