]> vaikene.ee Git - evaf/blob - src/apps/PswGen/Storage/module.h
Added 'suffix' option, which can be used to generate different password versions...
[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-2012 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
25 #include <Plugins/iPlugin>
26
27 #include <QObject>
28 #include <QString>
29 #include <QAbstractListModel>
30 #include <QMap>
31 #include <QtSql/QSqlDatabase>
32
33
34 namespace eVaf {
35 namespace PswGen {
36
37 /// Module that stores options for strong passwords
38 namespace Storage {
39
40 /// Internal implementation of the Storage module
41 namespace Internal {
42 class StorageImpl;
43 } // namespace eVaf::PswGen::Storage::Internal
44
45 /**
46 * Module implementing the iStorage interface.
47 */
48 class Module : public Plugins::iPlugin
49 {
50 Q_OBJECT
51 Q_INTERFACES(eVaf::Plugins::iPlugin)
52
53 public:
54
55 Module();
56
57 virtual ~Module();
58
59 virtual bool init(QString const & args);
60
61 virtual void done();
62
63 virtual bool isReady() const { return mReady; }
64
65
66 private: // Members
67
68 /// Flag indicating that the module is ready
69 bool mReady;
70
71 /// iStorage interface instance
72 Internal::StorageImpl * mStorage;
73
74 };
75
76
77 namespace Internal {
78
79 /**
80 * iStorage interface implementation.
81 *
82 * Implements the iStorage interface using a simple file storage.
83 */
84
85 class StorageImpl : public QAbstractListModel, public iStorage
86 {
87 Q_OBJECT
88 Q_INTERFACES(eVaf::PswGen::iStorage)
89
90 public:
91
92 StorageImpl();
93
94 virtual ~StorageImpl();
95
96 bool init();
97
98 void done();
99
100 /*
101 iStorage interface
102 */
103
104 virtual bool save(QString const & name, QExplicitlySharedDataPointer<Storage::Data> data);
105
106 virtual QExplicitlySharedDataPointer<Storage::Data> query(QString const & name) const;
107
108 virtual QAbstractItemModel * autoCompletionModel() { return this; }
109
110 /*
111 QAbstractListModel methods
112 */
113
114 virtual int rowCount(QModelIndex const & parent) const { return mData.count(); }
115
116 virtual QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const;
117
118
119 private: // Members
120
121 /// Name of the database connection
122 static char const * const DbConnectionName;
123
124 /// Name of the database file without path
125 static char const * const DbName;
126
127 /// Database connection
128 QSqlDatabase mDb;
129
130 /// List of name/data pairs
131 QMap<QString, QExplicitlySharedDataPointer<Storage::Data> > mData;
132
133
134 private: // Methods
135
136 /**
137 * Creates database tables if necessary
138 * @return True if ok; false if failed
139 */
140 bool createTables();
141
142 /**
143 * Upgrades database tables if necessary
144 * @return True if ok; false if failed
145 *
146 * This function checks if database tables need to upgraded and
147 * performs the upgrade without a loss of data if possible.
148 * Returns false if upgrade is not possible or fails.
149 */
150 bool upgradeTables();
151
152 /**
153 * Loads data from the database
154 * @return True if ok; false if failed
155 */
156 bool loadData();
157
158 };
159
160 } // namespace eVaf::PswGen::Storage::Internal
161
162
163 } // namespace eVaf::PswGen::Storage
164 } // namespace eVaf::PswGen
165 } // namespace eVaf
166
167 #endif // module.h