app.cpp Example File
persistentobjects/src/app.cpp
#include "app.hpp"
#include "FileStorage.hpp"
#include "Person.hpp"
#include "SettingsStorage.hpp"
#include "Storage.hpp"
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Application>
#include <bb/cascades/Page>
#include <bb/cascades/QmlDocument>
#include <bb/system/SystemDialog>
#include <QDebug>
using namespace bb::cascades;
using namespace bb::system;
App::App(QObject *parent)
: QObject(parent)
, m_lastCustomerID(0)
, m_storageLocation(StoreInQSettings)
, m_storage(new SettingsStorage)
{
initDataModel();
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_app", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);
}
App::~App()
{
delete m_storage;
}
bool App::addObject(const QString &firstName, const QString &lastName)
{
bool added = false;
m_lastCustomerID++;
const QString id = QString::number(m_lastCustomerID);
Person *person = new Person(id, firstName, lastName);
m_dataModel->insert(person);
added = m_storage->save(m_lastCustomerID, m_dataModel);
return added;
}
void App::refreshObjects()
{
const int objectsReadCount = load();
if (objectsReadCount == 0) {
alert(tr("The customer list is empty."));
} else {
alert(tr("%1 objects loaded.").arg(objectsReadCount));
}
}
void App::clearObjects()
{
m_lastCustomerID = 0;
m_dataModel->clear();
m_storage->clear();
}
bool App::updateObject(const QString &id, const QString &firstName, const QString &lastName)
{
bool updated = false;
bool saved = false;
if (!validateID(id))
return false;
Person *person = new Person(id, firstName, lastName);
const QVariantList updateIndexPath = m_dataModel->find(person);
if (updateIndexPath.isEmpty()) {
alert(tr("Object ID not found."));
updated = false;
} else {
updated = m_dataModel->updateItem(updateIndexPath, person);
}
if (updated) {
saved = m_storage->save(m_lastCustomerID, m_dataModel);
}
return (updated && saved);
}
bool App::deleteObject(const QString &customerID)
{
bool deleted = false;
bool saved = false;
if (!validateID(customerID))
return false;
Person *person = new Person(customerID, QString(), QString());
const QVariantList deleteIndexPath = m_dataModel->find(person);
if (deleteIndexPath.isEmpty()) {
alert(tr("Object ID not found."));
} else {
deleted = m_dataModel->removeAt(deleteIndexPath);
}
if (deleted) {
saved = m_storage->save(m_lastCustomerID, m_dataModel);
}
return (deleted && saved);
}
void App::setStorageLocation(StorageLocations location)
{
if (m_storageLocation != location) {
m_storageLocation = location;
delete m_storage;
if (m_storageLocation == StoreInQSettings) {
m_storage = new SettingsStorage();
} else {
m_storage = new FileStorage();
}
refreshObjects();
}
}
void App::initDataModel()
{
m_dataModel = new GroupDataModel(this);
m_dataModel->setSortingKeys(QStringList() << "customerID");
m_dataModel->setGrouping(ItemGrouping::None);
load();
}
void App::addToDataModel(const QString &id, const QString &firstName, const QString &lastName)
{
m_dataModel->insert(new Person(id, firstName, lastName));
}
int App::load()
{
m_dataModel->clear();
const int loaded = m_storage->load(m_lastCustomerID, m_dataModel);
return loaded;
}
bool App::validateID(const QString &customerID)
{
bool intConversionGood = false;
customerID.toInt(&intConversionGood);
if (!intConversionGood) {
alert(tr("You must provide an integer key."));
}
return intConversionGood;
}
GroupDataModel* App::dataModel() const
{
return m_dataModel;
}
void App::alert(const QString &message)
{
qDebug() << "alert: " << message;
SystemDialog *dialog;
dialog = new SystemDialog(tr("OK"), 0);
dialog->setTitle(tr("Alert"));
dialog->setBody(message);
dialog->setDismissAutomatically(true);
bool ok = connect(dialog, SIGNAL(finished(bb::system::SystemUiResult::Type)), dialog, SLOT(deleteLater()));
Q_ASSERT(ok);
Q_UNUSED(ok);
dialog->show();
}