]> vaikene.ee Git - evaf/blob - src/apps/PswGen/Storage/module.h
Warning fixes and copyright update.
[evaf] / src / apps / PswGen / Storage / module.h
1 /**
2 * @file PswGen/Storage/module.h
3 * @brief Implementation of the iStorage interface
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011-2019 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #ifndef __PSWGEN_STORAGE_MODULE_H
21 # define __PSWGEN_STORAGE_MODULE_H
22
23 #include "istorage.h"
24 #include "version.h"
25
26 #include <Plugins/iPlugin>
27
28 #include <QObject>
29 #include <QString>
30 #include <QAbstractListModel>
31 #include <QMap>
32
33 class QSqlDatabase;
34
35 namespace eVaf {
36 namespace PswGen {
37
38 /// Module that stores options for strong passwords
39 namespace Storage {
40
41 /// Internal implementation of the Storage module
42 namespace Internal {
43 class StorageImpl;
44 } // namespace eVaf::PswGen::Storage::Internal
45
46 /**
47 * Module implementing the iStorage interface.
48 */
49 class Module : public Plugins::iPlugin
50 {
51 Q_OBJECT
52 Q_INTERFACES(eVaf::Plugins::iPlugin)
53 Q_PLUGIN_METADATA(IID VER_MODULE_NAME_STR)
54
55 public:
56
57 Module();
58
59 virtual ~Module();
60
61 virtual bool init(QString const & args);
62
63 virtual void done();
64
65 virtual bool isReady() const { return mReady; }
66
67
68 private: // Members
69
70 /// Flag indicating that the module is ready
71 bool mReady;
72
73 /// iStorage interface instance
74 Internal::StorageImpl * mStorage;
75
76 };
77
78
79 namespace Internal {
80
81 /**
82 * iStorage interface implementation.
83 *
84 * Implements the iStorage interface using a simple file storage.
85 */
86
87 class StorageImpl : public QAbstractListModel, public iStorage
88 {
89 Q_OBJECT
90 Q_INTERFACES(eVaf::PswGen::iStorage)
91
92 public:
93
94 StorageImpl();
95
96 virtual ~StorageImpl();
97
98 bool init();
99
100 void done();
101
102 /*
103 iStorage interface
104 */
105
106 virtual bool save(QString const & name, QExplicitlySharedDataPointer<Storage::Data> data);
107
108 virtual QExplicitlySharedDataPointer<Storage::Data> query(QString const & name) const;
109
110 virtual QAbstractItemModel * autoCompletionModel() { return this; }
111
112 /*
113 QAbstractListModel methods
114 */
115
116 virtual int rowCount(QModelIndex const &) const { return mData.count(); }
117
118 virtual QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const;
119
120
121 private: // Members
122
123 /// Name of the database connection
124 static char const * const DbConnectionName;
125
126 /// Name of the database file without path
127 static char const * const DbName;
128
129 /// List of name/data pairs
130 QMap<QString, QExplicitlySharedDataPointer<Storage::Data> > mData;
131
132
133 private: // Methods
134
135 /**
136 * Creates database tables if necessary
137 * @return True if ok; false if failed
138 */
139 bool createTables(QSqlDatabase & db);
140
141 /**
142 * Upgrades database tables if necessary
143 * @return True if ok; false if failed
144 *
145 * This function checks if database tables need to upgraded and
146 * performs the upgrade without a loss of data if possible.
147 * Returns false if upgrade is not possible or fails.
148 */
149 bool upgradeTables(QSqlDatabase & db);
150
151 /**
152 * Loads data from the database
153 * @return True if ok; false if failed
154 */
155 bool loadData(QSqlDatabase & db);
156
157 };
158
159 } // namespace eVaf::PswGen::Storage::Internal
160
161
162 } // namespace eVaf::PswGen::Storage
163 } // namespace eVaf::PswGen
164 } // namespace eVaf
165
166 #endif // module.h