]> vaikene.ee Git - evaf/blob - src/apps/PswGen/Storage/module.cpp
e867a0e7d21f6d138e841c5268185a5157a7ce9b
[evaf] / src / apps / PswGen / Storage / module.cpp
1 /**
2 * @file PswGen/Storage/module.cpp
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 #include "module.h"
21 #include "version.h"
22
23 #include <Common/Globals>
24 #include <Common/iLogger>
25 #include <Common/iRegistry>
26 #include <Common/iApp>
27
28 #include <QtCore>
29 #include <QtSql/QtSql>
30
31 VER_EXPORT_VERSION_INFO()
32 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, eVaf::PswGen::Storage::Module)
33
34 using namespace eVaf;
35 using namespace eVaf::PswGen;
36 using namespace eVaf::PswGen::Storage;
37
38 //-------------------------------------------------------------------
39
40 Module::Module()
41 : Plugins::iPlugin()
42 , mReady(false)
43 {
44 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
45
46 mStorage = new Internal::StorageImpl;
47
48 EVAF_INFO("%s created", qPrintable(objectName()));
49 }
50
51 Module::~Module()
52 {
53 delete mStorage;
54
55 EVAF_INFO("%s destroyed", qPrintable(objectName()));
56 }
57
58 bool Module::init(QString const & args)
59 {
60 Q_UNUSED(args);
61
62 if (!mStorage->init())
63 return false;
64
65 mReady = true;
66
67 EVAF_INFO("%s initialized", qPrintable(objectName()));
68
69 return true;
70 }
71
72 void Module::done()
73 {
74 mReady = false;
75
76 mStorage->done();
77
78 EVAF_INFO("%s finalized", qPrintable(objectName()));
79 }
80
81
82 //-------------------------------------------------------------------
83
84 using namespace eVaf::PswGen::Storage::Internal;
85
86 char const * const StorageImpl::DbConnectionName = "PswGenDB";
87
88 char const * const StorageImpl::DbName = "PswGen.sqlite";
89
90 StorageImpl::StorageImpl()
91 : QAbstractListModel()
92 {
93 setObjectName(QString("%1.iGenerator").arg(VER_MODULE_NAME_STR));
94
95 EVAF_INFO("%s created", qPrintable(objectName()));
96 }
97
98 StorageImpl::~StorageImpl()
99 {
100 EVAF_INFO("%s destroyed", qPrintable(objectName()));
101 }
102
103 bool StorageImpl::init()
104 {
105 EVAF_INFO("%s initialized", qPrintable(objectName()));
106
107 // Open the database
108 if (!QSqlDatabase::contains(DbConnectionName)) {
109 // No database connection yet
110 mDb = QSqlDatabase::addDatabase("QSQLITE", DbConnectionName);
111 mDb.setDatabaseName(Common::iApp::instance()->dataRootDir() + DbName);
112 if (!mDb.open()) {
113 QSqlError err = mDb.lastError();
114 EVAF_ERROR("Failed to open database : %s", qPrintable(err.text()));
115 return false;
116 }
117 }
118 else {
119 // Database connection already exists
120 mDb = QSqlDatabase::database(DbConnectionName);
121 }
122
123 // Create tables if necessary
124 if (!createTables())
125 return false;
126
127 // Load data
128 if (!loadData())
129 return false;
130
131 /// Register our interface
132 Common::iRegistry::instance()->registerInterface("iStorage", this);
133
134 return true;
135 }
136
137 void StorageImpl::done()
138 {
139 mData.clear();
140 EVAF_INFO("%s finalized", qPrintable(objectName()));
141 }
142
143 bool StorageImpl::save(QString const & name, QExplicitlySharedDataPointer<Storage::Data> data)
144 {
145 EVAF_TEST_X(data, "Data cannot be null");
146 EVAF_TEST_X(!name.isEmpty(), "Name cannot be empty");
147
148 // Is it an update or a new data record?
149 if (mData.constFind(name) != mData.constEnd()) {
150 // This is an update
151 if (data->modified()) {
152 QSqlQuery q(mDb);
153 if (!q.exec(QString("UPDATE data SET length = \'%1\', flags = \'%2\' WHERE name = \'%3\';").arg(data->length()).arg(data->flags()).arg(name))) {
154 QSqlError err = mDb.lastError();
155 EVAF_ERROR("Failed to update \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
156 return false;
157 }
158 }
159 }
160 else {
161 // Store to the database
162 QSqlQuery q(mDb);
163 if (!q.exec(QString("INSERT INTO data (name, length, flags) VALUES (\'%1\', %2, %3);").arg(name).arg(data->length()).arg(int(data->flags())))) {
164 QSqlError err = mDb.lastError();
165 EVAF_ERROR("Failed to insert \'%s\' : %s", qPrintable(name), qPrintable(err.text()));
166 return false;
167 }
168
169 // Store also into the local hash
170 mData.insert(name, data);
171
172 // Reset the model
173 reset();
174 }
175
176 data->reset();
177
178 return true;
179 }
180
181 QExplicitlySharedDataPointer<Storage::Data> StorageImpl::query(QString const & name) const
182 {
183 QMap<QString, QExplicitlySharedDataPointer<Storage::Data> >::const_iterator it = mData.constFind(name);
184 if (it != mData.constEnd())
185 return it.value();
186 else
187 return QExplicitlySharedDataPointer<Storage::Data>();
188 }
189
190 QVariant StorageImpl::data(QModelIndex const & index, int role) const
191 {
192 if (!index.isValid() || index.row() < 0 || index.row() >= mData.size() || index.column() != 0)
193 return QVariant();
194
195 if (role == Qt::EditRole || role == Qt::DisplayRole)
196 return mData.keys().at(index.row());
197
198 return QVariant();
199 }
200
201 bool StorageImpl::createTables()
202 {
203 QSqlQuery q(mDb);
204 if (!q.exec("SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'data\';")) {
205 QSqlError err = mDb.lastError();
206 EVAF_ERROR("Failed to query database : %s", qPrintable(err.text()));
207 return false;
208 }
209
210 if (q.isActive() && q.isSelect() && q.first())
211 return true; // We already have a table called 'data'
212
213 // Create the 'data' table
214 if (!q.exec("CREATE TABLE data (name text primary key not null, length integer, flags integer);")) {
215 QSqlError err = mDb.lastError();
216 EVAF_ERROR("Failed to create table \'data\' : %s", qPrintable(err.text()));
217 return false;
218 }
219
220 return true;
221 }
222
223 bool StorageImpl::loadData()
224 {
225 QSqlQuery q(mDb);
226 if (!q.exec("SELECT name, length, flags FROM data;")) {
227 QSqlError err = mDb.lastError();
228 EVAF_ERROR("Failed to query database : %s", qPrintable(err.text()));
229 return false;
230 }
231
232 while (q.next()) {
233 QString name = q.value(0).toString();
234 QExplicitlySharedDataPointer<Storage::Data> data(new Storage::Data(q.value(1).toInt(), uint(q.value(2).toInt())));
235 mData.insert(name, data);
236 }
237
238 reset();
239
240 return true;
241 }