1
/***************************************************************************
3
* This file is part of the KDE project
4
* copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this program; see the file COPYING. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
18
***************************************************************************/
20
#include "kexidbmodule.h"
21
#include "kexidbconnection.h"
22
#include "kexidbdriver.h"
23
#include "kexidbconnectiondata.h"
24
#include "kexidbfield.h"
25
#include "kexidbschema.h"
28
#include <KDbConnectionData>
30
#include <KDbTableSchema>
31
#include <KDbQuerySchema>
33
#include <KConfigGroup>
36
#include <QMimeDatabase>
38
// The as version() published versionnumber of this kross-module.
39
#define KROSS_KEXIDB_VERSION 1
44
* Exported an loadable function as entry point to use
45
* the \a KexiDBModule.
47
KDE_EXPORT QObject* krossmodule() {
48
return new Scripting::KexiDBModule();
52
using namespace Scripting;
54
KexiDBModule::KexiDBModule(QObject* parent)
58
setObjectName("KexiDB");
61
KexiDBModule::~KexiDBModule()
66
int KexiDBModule::version()
68
return KROSS_KEXIDB_VERSION;
71
const QStringList KexiDBModule::driverNames()
73
return m_drivermanager.driverNames();
76
QObject* KexiDBModule::driver(const QString& drivername)
78
QPointer< KDbDriver > driver = m_drivermanager.driver(drivername); // caching is done by the DriverManager
80
qWarning() << "No such driver '%1'" << drivername;
83
if (driver->error()) {
84
qWarning() << "Error for drivername" << drivername << driver->errorMsg();
87
return new KexiDBDriver(this, driver);
90
const QString KexiDBModule::lookupByMime(const QString& mimetype)
92
return m_drivermanager.lookupByMime(mimetype);
95
const QString KexiDBModule::mimeForFile(const QString& filename)
98
QString mimename = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent).name();
99
if (mimename.isEmpty() || mimename == "application/octet-stream" || mimename == "text/plain") {
100
mimename = db.mimeTypeForUrl(filename).name();
105
QObject* KexiDBModule::createConnectionData()
107
return new KexiDBConnectionData(this, new KDbConnectionData(), true);
110
QObject* KexiDBModule::createConnectionDataByFile(const QString& filename)
112
//! @todo reuse the original code!
114
QString mimename = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent).name();
115
if (mimename.isEmpty() || mimename == "application/octet-stream" || mimename == "text/plain") {
116
mimename = db.mimeTypeForUrl(filename).name();
118
if (mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") {
119
KConfig _config(filename, KConfig::NoGlobals);
122
foreach(const QString &s, _config.groupList()) {
123
if (s.toLower() != "file information") {
128
if (groupkey.isNull()) {
129
qDebug() << "No groupkey, filename=" << filename;
133
KConfigGroup config(&_config, groupkey);
134
KDbConnectionData* data = new KDbConnectionData();
135
int version = config.readEntry("version", 2); //KexiDBShortcutFile_version
136
data->setDatabaseName(QString());
137
data->setCaption(config.readEntry("caption"));
138
data->setDescription(config.readEntry("comment"));
139
QString dbname = config.readEntry("name");
140
data->setDriverId(config.readEntry("engine"));
141
data->setHostName(config.readEntry("server"));
142
data->setPort(config.readEntry("port", 0));
143
data->setUseLocalSocketFile(config.readEntry("useLocalSocketFile", false));
144
data->setLocalSocketFileName(config.readEntry("localSocketFile"));
146
if (version >= 2 && config.hasKey("encryptedPassword")) {
147
QString password(config.readEntry("encryptedPassword"));
148
int len = password.length();
149
for (int i = 0; i < len; i++) {
150
password[i] = QChar(password[i].unicode() - 47 - i);
152
data->setPassword(password);
154
if (data->password().isEmpty()) {
155
data->setPassword(config.readEntry("password"));
158
data->setSavePassword(!data->password().isEmpty());
159
data->setUserName(config.readEntry("user"));
161
KexiDBConnectionData* c = new KexiDBConnectionData(this, data, true);
162
c->setDatabaseName(dbname);
166
const QStringList driverIds = m_drivermanager.driverIdsForMimeType(mimename);
167
if (driverIds.isEmpty()) {
168
qDebug() << "No driver, filename=" << filename << "mimename=" << mimename;
172
KDbConnectionData* data = new KDbConnectionData();
173
data->setDatabaseName(filename);
174
//! @todo there can be more than one driver
175
data->setDriverId(driverIds.first());
176
return new KexiDBConnectionData(this, data, true);
179
QObject* KexiDBModule::field()
181
return new KexiDBField(this, new KDbField(), true);
184
QObject* KexiDBModule::tableSchema(const QString& tablename)
186
return new KexiDBTableSchema(this, new KDbTableSchema(tablename), true);
189
QObject* KexiDBModule::querySchema()
191
return new KexiDBQuerySchema(this, new KDbQuerySchema(), true);
194
QObject* KexiDBModule::connectionWrapper(QObject* connection)
196
KDbConnection* c = dynamic_cast< KDbConnection* >(connection);
197
return c ? new KexiDBConnection(c) : 0;