Qt-based BB10 API Examples Documentation

SettingsStorage.cpp Example File

persistentobjects/src/SettingsStorage.cpp
    /* 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 "SettingsStorage.hpp"
    #include "Person.hpp"

    const QString SettingsStorage::m_author = "Example Inc."; // for creating settings
    const QString SettingsStorage::m_appName = "PersistentObjectApp"; // for creating settings

    //Keys for settings file:
    const QString SettingsStorage::m_personListSettingsKey = "PersonList";
    const QString SettingsStorage::m_lastCustomerIDKey = "PersonList_LastCustomerID";
    const QString SettingsStorage::m_customerIDKey = "customerID";
    const QString SettingsStorage::m_firstNameKey = "firstName";
    const QString SettingsStorage::m_lastNameKey = "lastName";

    SettingsStorage::SettingsStorage()
    {
    }

    SettingsStorage::~SettingsStorage()
    {
    }
    int SettingsStorage::load(int& lastID, GroupDataModel *model)
    {
        // number of persons loaded.
        int loadedCount = 0;

        // Get a handle on the settings object for this
        // author and application.
        QSettings settings(m_author, m_appName);

        // Get the last customer id first.
        // ID's will be generated by incrementing this number
        // Note values coming from settings should be cast to the
        // required type.
        lastID = settings.value(m_lastCustomerIDKey, 0).toInt();

        // This function opens an array to add values to, and returns the
        // Close it with endArray() so that subsequent reads are not
        // from this array.
        const int personCount = settings.beginReadArray(m_personListSettingsKey);

        // Load all the persons in the settings array.
        for (int i = 0; i < personCount; i++) {
            settings.setArrayIndex(i);

            Person *p;
            if (loadPerson(settings, p)) {
                model->insert(p);
                loadedCount++;
            } else {
                break;
            }
        }

        settings.endArray(); // Use the same function for read or write to close the array

        return loadedCount;
    }

    // Clear the objects stored in settings.
    bool SettingsStorage::clear()
    {
        QSettings settings(m_author, m_appName);
        settings.clear();
        return true;
    }
    bool SettingsStorage::save(int lastID, GroupDataModel *model)
    {
        QSettings settings(m_author, m_appName);

        settings.setValue(m_lastCustomerIDKey, lastID);

        settings.beginWriteArray(m_personListSettingsKey);
        for (int i = 0; i < model->size(); i++) {
            settings.setArrayIndex(i);

            Person *person = (Person *) model->children()[i];
            settings.setValue(m_customerIDKey, person->customerID());
            settings.setValue(m_firstNameKey, person->firstName());
            settings.setValue(m_lastNameKey, person->lastName());
        }
        settings.endArray();

        return true;
    }
    bool SettingsStorage::loadPerson(const QSettings& settings, Person*& person) const
    {
        bool loaded = false;

        // Make sure keys exist before loading into person object.
        if (settings.contains(m_customerIDKey) && settings.contains(m_firstNameKey)
                && settings.contains(m_lastNameKey)) {
            person = new Person(); // this will go in the datamodel and will be destroyed from there.
            person->setCustomerID(settings.value(m_customerIDKey).toString());
            person->setFirstName(settings.value(m_firstNameKey).toString());
            person->setLastName(settings.value(m_lastNameKey).toString());
            loaded = true;
        } else {
            //Invalid person data in settings.
            return false;
        }

        return (loaded && person); // Don't return true if person was not set.
    }