1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-06-27
* Description : Database Engine element configuration loader
*
* SPDX-FileCopyrightText: 2009-2010 by Holger Foerster <hamsi2k at freenet dot de>
* SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dbengineconfigloader.h"
// Qt includes
#include <QDir>
#include <QDomDocument>
#include <QDomNode>
#include <QDomNodeList>
#include <QFile>
#include <QIODevice>
#include <QTextStream>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
namespace Digikam
{
DbEngineConfigSettingsLoader::DbEngineConfigSettingsLoader(const QString& filepath, int xmlVersion)
{
isValid = readConfig(filepath, xmlVersion);
if (!isValid)
{
qCDebug(DIGIKAM_DBENGINE_LOG) << errorMessage;
}
}
DbEngineConfigSettings DbEngineConfigSettingsLoader::readDatabase(QDomElement& databaseElement)<--- Parameter 'databaseElement' can be declared as reference to const
{
DbEngineConfigSettings configElement;
configElement.databaseID = QLatin1String("Unidentified");
(void)configElement.databaseID; // prevent cppcheck warning.
if (!databaseElement.hasAttribute(QLatin1String("name")))
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing statement attribute <name>.";
}
configElement.databaseID = databaseElement.attribute(QLatin1String("name"));
QDomElement element = databaseElement.namedItem(QLatin1String("databaseName")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <databaseName>.";
}
configElement.databaseName = element.text();
element = databaseElement.namedItem(QLatin1String("userName")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <userName>.";
}
configElement.userName = element.text();
element = databaseElement.namedItem(QLatin1String("password")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <password>.";
}
configElement.password = element.text();
element = databaseElement.namedItem(QLatin1String("hostName")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <hostName>.";
}
configElement.hostName = element.text();
element = databaseElement.namedItem(QLatin1String("port")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <port>.";
}
configElement.port = element.text();
element = databaseElement.namedItem(QLatin1String("connectoptions")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <connectoptions>.";
}
configElement.connectOptions = element.text();
element = databaseElement.namedItem(QLatin1String("dbactions")).toElement();
if (element.isNull())
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing element <dbactions>.";
}
readDBActions(element, configElement);
return configElement;
}
void DbEngineConfigSettingsLoader::readDBActions(const QDomElement& sqlStatementElements,
DbEngineConfigSettings& configElement)
{
QDomElement dbActionElement = sqlStatementElements.firstChildElement(QLatin1String("dbaction"));
for ( ; !dbActionElement.isNull(); dbActionElement=dbActionElement.nextSiblingElement(QLatin1String("dbaction")))
{
if (!dbActionElement.hasAttribute(QLatin1String("name")))
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing statement attribute <name>.";
}
DbEngineAction action;
action.name = dbActionElement.attribute(QLatin1String("name"));
/*
qCDebug(DIGIKAM_DBENGINE_LOG) << "Getting attribute " << dbActionElement.attribute("name");
*/
if (dbActionElement.hasAttribute(QLatin1String("mode")))
{
action.mode = dbActionElement.attribute(QLatin1String("mode"));
}
QDomElement databaseElement = dbActionElement.firstChildElement(QLatin1String("statement"));
for ( ; !databaseElement.isNull() ; databaseElement = databaseElement.nextSiblingElement(QLatin1String("statement")))
{
if (!databaseElement.hasAttribute(QLatin1String("mode")))
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Missing statement attribute <mode>.";
}
DbEngineActionElement actionElement;
actionElement.mode = databaseElement.attribute(QLatin1String("mode"));
actionElement.statement = databaseElement.text();
action.dbActionElements.append(actionElement);
}
configElement.sqlStatements.insert(action.name, action);
}
}
bool DbEngineConfigSettingsLoader::readConfig(const QString& filepath, int xmlVersion)
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "Loading SQL code from config file" << filepath;
QFile file(filepath);
if (!file.exists())
{
errorMessage = i18n("Could not open the configuration file <b>%1</b>. "
"This file is installed with the digikam application "
"and is absolutely required to run digikam. "
"Please check your installation.", filepath);
return false;
}
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
errorMessage = i18n("Could not open configuration file <b>%1</b>", filepath);
return false;
}
QDomDocument doc(QLatin1String("DBConfig"));
if (!doc.setContent(&file))
{
errorMessage = i18n("The XML in the configuration file <b>%1</b> is invalid and cannot be read.", filepath);
file.close();
return false;
}
file.close();
QDomElement element = doc.namedItem(QLatin1String("databaseconfig")).toElement();
if (element.isNull())
{
errorMessage = i18n("The XML in the configuration file <b>%1</b> "
"is missing the required element <icode>%2</icode>",
filepath, element.tagName());
return false;
}
QDomElement defaultDB = element.namedItem(QLatin1String("defaultDB")).toElement();
if (defaultDB.isNull())
{
errorMessage = i18n("The XML in the configuration file <b>%1</b> "
"is missing the required element <b>%2</b>",
filepath, element.tagName());
return false;
}
QDomElement versionElement = element.namedItem(QLatin1String("version")).toElement();
int version = 0;
qCDebug(DIGIKAM_DBENGINE_LOG) << "Checking XML version ID => expected: " << xmlVersion
<< " found: " << versionElement.text().toInt();
if (!versionElement.isNull())
{
version = versionElement.text().toInt();
}
if (version < xmlVersion)
{
errorMessage = i18n("An old version of the configuration file <b>%1</b> "
"is found. Please ensure that the version released "
"with the running version of digiKam is installed. ",
filepath);
return false;
}
/*
qCDebug(DIGIKAM_DBENGINE_LOG) << "Default DB Node contains: " << defaultDB.text();
*/
QDomElement databaseElement = element.firstChildElement(QLatin1String("database"));
for ( ; !databaseElement.isNull(); databaseElement=databaseElement.nextSiblingElement(QLatin1String("database")))
{
DbEngineConfigSettings l_DBCfgElement = readDatabase(databaseElement);
databaseConfigs.insert(l_DBCfgElement.databaseID, l_DBCfgElement);
}
/*
qCDebug(DIGIKAM_DBENGINE_LOG) << "Found entries: " << databaseConfigs.size();
Q_FOREACH (const DbEngineConfigSettings& configElement, databaseConfigs )
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "DatabaseID: " << configElement.databaseID;
qCDebug(DIGIKAM_DBENGINE_LOG) << "HostName: " << configElement.hostName;
qCDebug(DIGIKAM_DBENGINE_LOG) << "DatabaseName: " << configElement.databaseName;
qCDebug(DIGIKAM_DBENGINE_LOG) << "UserName: " << configElement.userName;
qCDebug(DIGIKAM_DBENGINE_LOG) << "Password: " << configElement.password;
qCDebug(DIGIKAM_DBENGINE_LOG) << "Port: " << configElement.port;
qCDebug(DIGIKAM_DBENGINE_LOG) << "ConnectOptions: " << configElement.connectOptions;
qCDebug(DIGIKAM_DBENGINE_LOG) << "Database Server CMD: " << configElement.dbServerCmd;
qCDebug(DIGIKAM_DBENGINE_LOG) << "Database Init CMD: " << configElement.dbInitCmd;
qCDebug(DIGIKAM_DBENGINE_LOG) << "Statements:";
Q_FOREACH (const QString& actionKey, configElement.sqlStatements.keys())
{
QList<databaseActionElement> l_DBActionElement = configElement.sqlStatements[actionKey].dBActionElements;
qCDebug(DIGIKAM_DBENGINE_LOG) << "DBAction [" << actionKey << "] has [" << l_DBActionElement.size() << "] actions";
Q_FOREACH (const databaseActionElement statement, l_DBActionElement)
{
qCDebug(DIGIKAM_DBENGINE_LOG) << "\tMode ["<< statement.mode <<"] Value ["<< statement.statement <<"]";
}
}
}
*/
return true;
}
} // namespace Digikam
|