~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/plugins/scripting/kexidb/kexidbmodule.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 * kexidbmodule.cpp
 
3
 * This file is part of the KDE project
 
4
 * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
 
5
 *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "kexidbmodule.h"
 
21
#include "kexidbconnection.h"
 
22
#include "kexidbdriver.h"
 
23
#include "kexidbconnectiondata.h"
 
24
#include "kexidbfield.h"
 
25
#include "kexidbschema.h"
 
26
 
 
27
#include <KDbDriver>
 
28
#include <KDbConnectionData>
 
29
#include <KDbField>
 
30
#include <KDbTableSchema>
 
31
#include <KDbQuerySchema>
 
32
 
 
33
#include <KConfigGroup>
 
34
 
 
35
#include <QDebug>
 
36
#include <QMimeDatabase>
 
37
 
 
38
// The as version() published versionnumber of this kross-module.
 
39
#define KROSS_KEXIDB_VERSION 1
 
40
 
 
41
extern "C"
 
42
{
 
43
    /**
 
44
     * Exported an loadable function as entry point to use
 
45
     * the \a KexiDBModule.
 
46
     */
 
47
    KDE_EXPORT QObject* krossmodule() {
 
48
        return new Scripting::KexiDBModule();
 
49
    }
 
50
}
 
51
 
 
52
using namespace Scripting;
 
53
 
 
54
KexiDBModule::KexiDBModule(QObject* parent)
 
55
        : QObject(parent)
 
56
{
 
57
    qDebug();
 
58
    setObjectName("KexiDB");
 
59
}
 
60
 
 
61
KexiDBModule::~KexiDBModule()
 
62
{
 
63
    qDebug();
 
64
}
 
65
 
 
66
int KexiDBModule::version()
 
67
{
 
68
    return KROSS_KEXIDB_VERSION;
 
69
}
 
70
 
 
71
const QStringList KexiDBModule::driverNames()
 
72
{
 
73
    return m_drivermanager.driverNames();
 
74
}
 
75
 
 
76
QObject* KexiDBModule::driver(const QString& drivername)
 
77
{
 
78
    QPointer< KDbDriver > driver = m_drivermanager.driver(drivername); // caching is done by the DriverManager
 
79
    if (! driver) {
 
80
        qWarning() << "No such driver '%1'" << drivername;
 
81
        return 0;
 
82
    }
 
83
    if (driver->error()) {
 
84
        qWarning() << "Error for drivername" << drivername << driver->errorMsg();
 
85
        return 0;
 
86
    }
 
87
    return new KexiDBDriver(this, driver);
 
88
}
 
89
 
 
90
const QString KexiDBModule::lookupByMime(const QString& mimetype)
 
91
{
 
92
    return m_drivermanager.lookupByMime(mimetype);
 
93
}
 
94
 
 
95
const QString KexiDBModule::mimeForFile(const QString& filename)
 
96
{
 
97
    QMimeDatabase db;
 
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();
 
101
    }
 
102
    return mimename;
 
103
}
 
104
 
 
105
QObject* KexiDBModule::createConnectionData()
 
106
{
 
107
    return new KexiDBConnectionData(this, new KDbConnectionData(), true);
 
108
}
 
109
 
 
110
QObject* KexiDBModule::createConnectionDataByFile(const QString& filename)
 
111
{
 
112
    //! @todo reuse the original code!
 
113
    QMimeDatabase db;
 
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();
 
117
    }
 
118
    if (mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") {
 
119
        KConfig _config(filename, KConfig::NoGlobals);
 
120
 
 
121
        QString groupkey;
 
122
        foreach(const QString &s, _config.groupList()) {
 
123
            if (s.toLower() != "file information") {
 
124
                groupkey = s;
 
125
                break;
 
126
            }
 
127
        }
 
128
        if (groupkey.isNull()) {
 
129
            qDebug() << "No groupkey, filename=" << filename;
 
130
            return 0;
 
131
        }
 
132
 
 
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"));
 
145
 
 
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);
 
151
            }
 
152
            data->setPassword(password);
 
153
        }
 
154
        if (data->password().isEmpty()) {
 
155
            data->setPassword(config.readEntry("password"));
 
156
        }
 
157
 
 
158
        data->setSavePassword(!data->password().isEmpty());
 
159
        data->setUserName(config.readEntry("user"));
 
160
 
 
161
        KexiDBConnectionData* c = new KexiDBConnectionData(this, data, true);
 
162
        c->setDatabaseName(dbname);
 
163
        return c;
 
164
    }
 
165
 
 
166
    const QStringList driverIds = m_drivermanager.driverIdsForMimeType(mimename);
 
167
    if (driverIds.isEmpty()) {
 
168
        qDebug() << "No driver, filename=" << filename << "mimename=" << mimename;
 
169
        return 0;
 
170
    }
 
171
 
 
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);
 
177
}
 
178
 
 
179
QObject* KexiDBModule::field()
 
180
{
 
181
    return new KexiDBField(this, new KDbField(), true);
 
182
}
 
183
 
 
184
QObject* KexiDBModule::tableSchema(const QString& tablename)
 
185
{
 
186
    return new KexiDBTableSchema(this, new KDbTableSchema(tablename), true);
 
187
}
 
188
 
 
189
QObject* KexiDBModule::querySchema()
 
190
{
 
191
    return new KexiDBQuerySchema(this, new KDbQuerySchema(), true);
 
192
}
 
193
 
 
194
QObject* KexiDBModule::connectionWrapper(QObject* connection)
 
195
{
 
196
    KDbConnection* c = dynamic_cast< KDbConnection* >(connection);
 
197
    return c ? new KexiDBConnection(c) : 0;
 
198
}
 
199