]> vaikene.ee Git - evaf/blob - src/apps/PswGen/Storage/module.h
ae784e96c17105fd08929878fe448e6b00b1d619
[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 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
52 public:
53
54 Module();
55
56 virtual ~Module();
57
58 virtual bool init(QString const & args);
59
60 virtual void done();
61
62 virtual bool isReady() const { return mReady; }
63
64
65 private: // Members
66
67 /// Flag indicating that the module is ready
68 bool mReady;
69
70 /// iStorage interface instance
71 Internal::StorageImpl * mStorage;
72
73 };
74
75
76 namespace Internal {
77
78 /**
79 * iStorage interface implementation.
80 *
81 * Implements the iStorage interface using a simple file storage.
82 */
83
84 class StorageImpl : public QAbstractListModel, public iStorage
85 {
86 Q_OBJECT
87 Q_INTERFACES(eVaf::PswGen::iStorage)
88
89 public:
90
91 StorageImpl();
92
93 virtual ~StorageImpl();
94
95 bool init();
96
97 void done();
98
99 /*
100 iStorage interface
101 */
102
103 virtual bool save(QString const & name, QExplicitlySharedDataPointer<Storage::Data> data);
104
105 virtual QExplicitlySharedDataPointer<Storage::Data> query(QString const & name) const;
106
107 virtual QAbstractItemModel * autoCompletionModel() { return this; }
108
109 /*
110 QAbstractListModel methods
111 */
112
113 virtual int rowCount(QModelIndex const & parent) const { return mData.count(); }
114
115 virtual QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const;
116
117
118 private: // Members
119
120 /// Name of the database connection
121 static char const * const DbConnectionName;
122
123 /// Name of the database file without path
124 static char const * const DbName;
125
126 /// Database connection
127 QSqlDatabase mDb;
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();
140
141 /**
142 * Loads data from the database
143 * @return True if ok; false if failed
144 */
145 bool loadData();
146
147 };
148
149 } // namespace eVaf::PswGen::Storage::Internal
150
151
152 } // namespace eVaf::PswGen::Storage
153 } // namespace eVaf::PswGen
154 } // namespace eVaf
155
156 #endif // module.h