kexidbdrivermanager.cpp
1/***************************************************************************
2 * kexidbdrivermanager.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 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 "kexidbdrivermanager.h"
21#include "kexidbdriver.h"
22#include "kexidbconnectiondata.h"
23#include "kexidbfield.h"
24#include "kexidbschema.h"
25
26#include <api/exception.h>
27
28#include <tqguardedptr.h>
29#include <kdebug.h>
30#include <kmimetype.h>
31
32#include <kexidb/driver.h>
33#include <kexidb/connectiondata.h>
34#include <kexidb/field.h>
35#include <kexidb/tableschema.h>
36#include <kexidb/queryschema.h>
37
38using namespace Kross::KexiDB;
39
40KexiDBDriverManager::KexiDBDriverManager()
41 : Kross::Api::Class<KexiDBDriverManager>("DriverManager")
42{
43 //krossdebug( TQString("Kross::KexiDB::KexiDBDriverManager::KexiDBDriverManager()") );
44
45 this->addFunction0< Kross::Api::Variant >("driverNames", this, &KexiDBDriverManager::driverNames);
46
47 this->addFunction1< KexiDBDriver, Kross::Api::Variant >("driver", this, &KexiDBDriverManager::driver);
48 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("lookupByMime", this, &KexiDBDriverManager::lookupByMime);
49 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("mimeForFile", this, &KexiDBDriverManager::mimeForFile);
50
51 this->addFunction0< KexiDBConnectionData >("createConnectionData", this, &KexiDBDriverManager::createConnectionData);
52 this->addFunction1< KexiDBConnectionData, Kross::Api::Variant >("createConnectionDataByFile", this, &KexiDBDriverManager::createConnectionDataByFile);
53 this->addFunction0< KexiDBField >("field", this, &KexiDBDriverManager::field);
54 this->addFunction1< KexiDBTableSchema, Kross::Api::Variant >("tableSchema", this, &KexiDBDriverManager::tableSchema);
55 this->addFunction0< KexiDBQuerySchema>("querySchema", this, &KexiDBDriverManager::querySchema);
56}
57
58KexiDBDriverManager::~KexiDBDriverManager() {
59 //krossdebug( TQString("Kross::KexiDB::KexiDBDriverManager::~KexiDBDriverManager()") );
60}
61
62const TQString KexiDBDriverManager::getClassName() const {
63 return "Kross::KexiDB::KexiDBDriverManager";
64}
65
66KexiDB::DriverManager& KexiDBDriverManager::driverManager()
67{
68 if(m_drivermanager.error())
69 throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("KexiDB::DriverManager error: %1").arg(m_drivermanager.errorMsg())) );
70 return m_drivermanager;
71}
72
73const TQStringList KexiDBDriverManager::driverNames() {
74 return driverManager().driverNames();
75}
76
77KexiDBDriver* KexiDBDriverManager::driver(const TQString& drivername) {
78 TQGuardedPtr< ::KexiDB::Driver > driver = driverManager().driver(drivername); // caching is done by the DriverManager
79 if(! driver) return 0;
80 if(driver->error()) throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("KexiDB::Driver error for drivername '%1': %2").arg(drivername).arg(driver->errorMsg())) );
81 return new KexiDBDriver(driver);
82}
83
84const TQString KexiDBDriverManager::lookupByMime(const TQString& mimetype) {
85 return driverManager().lookupByMime(mimetype);
86}
87
88const TQString KexiDBDriverManager::mimeForFile(const TQString& filename) {
89 TQString mimename = KMimeType::findByFileContent( filename )->name();
90 if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain")
91 mimename = KMimeType::findByURL(filename)->name();
92 return mimename;
93}
94
95KexiDBConnectionData* KexiDBDriverManager::createConnectionData() {
96 return new KexiDBConnectionData( new ::KexiDB::ConnectionData() );
97}
98
99KexiDBConnectionData* KexiDBDriverManager::createConnectionDataByFile(const TQString& filename) {
101
102 TQString mimename = KMimeType::findByFileContent(filename)->name();
103 if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain")
104 mimename = KMimeType::findByURL(filename)->name();
105
106 if(mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") {
107 TDEConfig config(filename, true, false);
108 TQString groupkey;
109 TQStringList groups(config.groupList());
110 TQStringList::ConstIterator it, end( groups.constEnd() );
111 for( it = groups.constBegin(); it != end; ++it) {
112 if((*it).lower()!="file information") {
113 groupkey = *it;
114 break;
115 }
116 }
117 if(groupkey.isNull()) {
118 kdDebug() << "No groupkey in KexiDBDriverManager::createConnectionDataByFile filename=" << filename << endl;
119 return 0;
120 }
121
122 config.setGroup(groupkey);
123 //TQString type( config.readEntry("type", "database").lower() );
124 //bool isDatabaseShortcut = (type == "database");
125
126 ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData();
127 int version = config.readNumEntry("version", 2); //KexiDBShortcutFile_version
128 data->setFileName(TQString());
129 data->caption = config.readEntry("caption");
130 data->description = config.readEntry("comment");
131 TQString dbname = config.readEntry("name");
132 data->driverName = config.readEntry("engine");
133 data->hostName = config.readEntry("server");
134 data->port = config.readNumEntry("port", 0);
135 data->useLocalSocketFile = config.readBoolEntry("useLocalSocketFile", false);
136 data->localSocketFileName = config.readEntry("localSocketFile");
137
138 if(version >= 2 && config.hasKey("encryptedPassword")) {
139 data->password = config.readEntry("encryptedPassword");
140 uint len = data->password.length();
141 for (uint i=0; i<len; i++)
142 data->password[i] = TQChar( data->password[i].unicode() - 47 - i );
143 }
144 if(data->password.isEmpty())
145 data->password = config.readEntry("password");
146
147 data->savePassword = ! data->password.isEmpty();
148 data->userName = config.readEntry("user");
149
151 c->setDatabaseName(dbname);
152 return c;
153 }
154
155 TQString const drivername = driverManager().lookupByMime(mimename);
156 if(! drivername) {
157 kdDebug() << "No driver in KexiDBDriverManager::createConnectionDataByFile filename=" << filename << " mimename=" << mimename << endl;
158 return 0;
159 }
160
161 ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData();
162 data->setFileName(filename);
163 data->driverName = drivername;
164 return new KexiDBConnectionData(data);
165}
166
167KexiDBField* KexiDBDriverManager::field() {
168 return new KexiDBField( new ::KexiDB::Field() );
169}
170
171KexiDBTableSchema* KexiDBDriverManager::tableSchema(const TQString& tablename) {
172 return new KexiDBTableSchema( new ::KexiDB::TableSchema(tablename) );
173}
174
175KexiDBQuerySchema* KexiDBDriverManager::querySchema() {
176 return new KexiDBQuerySchema( new ::KexiDB::QuerySchema() );
177}
178
bool savePassword
True if password should be saved to a file for the connection.
TQString caption
The caption of the connection.
TQString localSocketFileName
Name of local (named) socket file.
TQString description
The additional description for the connection.
TQString userName
Username used for the connection.
TQString password
Password used for the connection.
TQString driverName
the name of the driver that should be used to create a connection
unsigned short int port
Port used for the remote connection.
TQString hostName
Host name used for the remote connection.
bool useLocalSocketFile
True if local socket file should be used instead of TCP/IP port.
Database specific connection data, e.g. host, port.
void setFileName(const TQString &fn)
Set the filename used by the connection.
Database driver management, e.g. finding and loading drivers.
const TQStringList driverNames()
Driver * driver(const TQString &name)
TQString lookupByMime(const TQString &mimeType)
bool error() const
Definition object.h:38
const TQString & errorMsg() const
Definition object.h:42
A KexiDBConnectionData is used to store the details needed for a connection with a database.
The drivermanager is the base class to access KexiDBDriver objects and provides common functionality ...
Drivers are the implementations Kexi uses to access the driver-backends.
A field in a record.
Definition kexidbfield.h:38
The KexiDBTableSchema object implements a KexiDBSchema for queries.
The KexiDBTableSchema object implements a KexiDBSchema for tables.
KrossKexiDB provides access to the KexiDB database functionality.