AccountEditor.cpp Example File
accounts/src/AccountEditor.cpp
#include "AccountEditor.hpp"
#include <bb/cascades/QmlDocument>
#include <bb/pim/account/Account>
#include <bb/pim/account/Provider>
#include <bb/pim/account/Result>
using namespace bb::cascades;
using namespace bb::pim::account;
AccountEditor::AccountEditor(AccountService *service, QObject *parent)
: QObject(parent)
, m_accountService(service)
, m_accountId(-1)
, m_providerId("caldav")
, m_mode(CreateMode)
{
}
void AccountEditor::loadAccount(AccountKey accountId)
{
m_accountId = accountId;
updateFields();
loadAccount();
}
void AccountEditor::saveAccount()
{
if (m_mode == CreateMode) {
const Provider provider = m_accountService->provider(m_providerId);
Account account(provider);
foreach (const QString &key, provider.settingsKeys()) {
Control *field = m_fields.value(key);
if (field) {
const QVariant value = field->property("value");
if (value.isValid())
account.setSettingsValue(key, value);
}
}
m_accountService->createAccount(provider.id(), account);
} else if (m_mode == EditMode) {
Account account = m_accountService->account(m_accountId);
foreach (const QString &key, account.provider().settingsKeys()) {
Control *field = m_fields.value(key);
if (field)
account.setSettingsValue(key, field->property("value"));
else
account.setSettingsValue(key, QVariant());
}
m_accountService->updateAccount(account.id(), account);
}
}
void AccountEditor::reset()
{
m_accountId = -1;
m_providerId = "caldav";
if (m_form)
m_form->removeAll();
m_fields.clear();
}
QString AccountEditor::providerId() const
{
return m_providerId;
}
void AccountEditor::setProviderId(const QString &providerId)
{
if (m_providerId == providerId)
return;
m_providerId = providerId;
emit providerIdChanged();
updateFields();
}
bb::cascades::Container* AccountEditor::form() const
{
return m_form;
}
void AccountEditor::setForm(Container *form)
{
if (m_form == form)
return;
m_form = form;
emit formChanged();
updateFields();
loadAccount();
}
void AccountEditor::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
AccountEditor::Mode AccountEditor::mode() const
{
return m_mode;
}
void AccountEditor::loadAccount()
{
if (m_accountId == -1)
return;
const Account account = m_accountService->account(m_accountId);
foreach (const QString &key, account.provider().settingsKeys()) {
const QVariant value = account.settingsProperty(key);
Control *field = m_fields.value(key);
if (field)
field->setProperty("value", value);
}
}
void AccountEditor::updateFields()
{
if (m_form.isNull())
return;
m_form->removeAll();
m_fields.clear();
const Provider provider = (m_accountId == -1 ? m_accountService->provider(m_providerId)
: m_accountService->account(m_accountId).provider());
foreach (const QString &key, provider.settingsKeys()) {
QmlDocument *qml = QmlDocument::create("asset:///EditorField.qml").parent(this);
Control *field = qml->createRootObject<Control>();
field->setProperty("type", provider.settingsProperty(key, Property::Type));
field->setProperty("title", key);
m_form->add(field);
m_fields.insert(key, field);
}
}