/** * @file PswGen/Storage/module.h * @brief Implementation of the iStorage interface * @author Enar Vaikene * * Copyright (c) 2011-2019 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * * This file can be used under the terms of the GNU General Public License * version 3.0 as published by the Free Software Foundation and appearing in * the file LICENSE included in the packaging of this file. Please review the * the following information to ensure the GNU General Public License version * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. * * Alternatively, this file may be used in accordance with the Commercial License * Agreement provided with the Software. */ #ifndef __PSWGEN_STORAGE_MODULE_H # define __PSWGEN_STORAGE_MODULE_H #include "istorage.h" #include "version.h" #include #include #include #include #include class QSqlDatabase; namespace eVaf { namespace PswGen { /// Module that stores options for strong passwords namespace Storage { /// Internal implementation of the Storage module namespace Internal { class StorageImpl; } // namespace eVaf::PswGen::Storage::Internal /** * Module implementing the iStorage interface. */ class Module : public Plugins::iPlugin { Q_OBJECT Q_INTERFACES(eVaf::Plugins::iPlugin) Q_PLUGIN_METADATA(IID VER_MODULE_NAME_STR) public: Module(); virtual ~Module(); virtual bool init(QString const & args); virtual void done(); virtual bool isReady() const { return mReady; } private: // Members /// Flag indicating that the module is ready bool mReady; /// iStorage interface instance Internal::StorageImpl * mStorage; }; namespace Internal { /** * iStorage interface implementation. * * Implements the iStorage interface using a simple file storage. */ class StorageImpl : public QAbstractListModel, public iStorage { Q_OBJECT Q_INTERFACES(eVaf::PswGen::iStorage) public: StorageImpl(); virtual ~StorageImpl(); bool init(); void done(); /* iStorage interface */ virtual bool save(QString const & name, QExplicitlySharedDataPointer data); virtual QExplicitlySharedDataPointer query(QString const & name) const; virtual QAbstractItemModel * autoCompletionModel() { return this; } /* QAbstractListModel methods */ virtual int rowCount(QModelIndex const &) const { return mData.count(); } virtual QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const; private: // Members /// Name of the database connection static char const * const DbConnectionName; /// Name of the database file without path static char const * const DbName; /// List of name/data pairs QMap > mData; private: // Methods /** * Creates database tables if necessary * @return True if ok; false if failed */ bool createTables(QSqlDatabase & db); /** * Upgrades database tables if necessary * @return True if ok; false if failed * * This function checks if database tables need to upgraded and * performs the upgrade without a loss of data if possible. * Returns false if upgrade is not possible or fails. */ bool upgradeTables(QSqlDatabase & db); /** * Loads data from the database * @return True if ok; false if failed */ bool loadData(QSqlDatabase & db); }; } // namespace eVaf::PswGen::Storage::Internal } // namespace eVaf::PswGen::Storage } // namespace eVaf::PswGen } // namespace eVaf #endif // module.h