kexi

kexidbdrivermanager.cpp

00001 /***************************************************************************
00002  * kexidbdrivermanager.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "kexidbdrivermanager.h"
00021 #include "kexidbdriver.h"
00022 #include "kexidbconnectiondata.h"
00023 #include "kexidbfield.h"
00024 #include "kexidbschema.h"
00025 
00026 #include <api/exception.h>
00027 
00028 #include <qguardedptr.h>
00029 #include <kdebug.h>
00030 #include <kmimetype.h>
00031 
00032 #include <kexidb/driver.h>
00033 #include <kexidb/connectiondata.h>
00034 #include <kexidb/field.h>
00035 #include <kexidb/tableschema.h>
00036 #include <kexidb/queryschema.h>
00037 
00038 using namespace Kross::KexiDB;
00039 
00040 KexiDBDriverManager::KexiDBDriverManager()
00041     : Kross::Api::Class<KexiDBDriverManager>("DriverManager")
00042 {
00043     //krossdebug( QString("Kross::KexiDB::KexiDBDriverManager::KexiDBDriverManager()") );
00044 
00045     this->addFunction0< Kross::Api::Variant >("driverNames", this, &KexiDBDriverManager::driverNames);
00046 
00047     this->addFunction1< KexiDBDriver, Kross::Api::Variant >("driver", this, &KexiDBDriverManager::driver);
00048     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("lookupByMime", this, &KexiDBDriverManager::lookupByMime);
00049     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("mimeForFile", this, &KexiDBDriverManager::mimeForFile);
00050 
00051     this->addFunction0< KexiDBConnectionData >("createConnectionData", this, &KexiDBDriverManager::createConnectionData);
00052     this->addFunction1< KexiDBConnectionData, Kross::Api::Variant >("createConnectionDataByFile", this, &KexiDBDriverManager::createConnectionDataByFile);
00053     this->addFunction0< KexiDBField >("field", this, &KexiDBDriverManager::field);
00054     this->addFunction1< KexiDBTableSchema, Kross::Api::Variant >("tableSchema", this, &KexiDBDriverManager::tableSchema);
00055     this->addFunction0< KexiDBQuerySchema>("querySchema", this, &KexiDBDriverManager::querySchema);
00056 }
00057 
00058 KexiDBDriverManager::~KexiDBDriverManager() {
00059     //krossdebug( QString("Kross::KexiDB::KexiDBDriverManager::~KexiDBDriverManager()") );
00060 }
00061 
00062 const QString KexiDBDriverManager::getClassName() const {
00063     return "Kross::KexiDB::KexiDBDriverManager";
00064 }
00065 
00066 KexiDB::DriverManager& KexiDBDriverManager::driverManager()
00067 {
00068     if(m_drivermanager.error())
00069         throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::DriverManager error: %1").arg(m_drivermanager.errorMsg())) );
00070     return m_drivermanager;
00071 }
00072 
00073 const QStringList KexiDBDriverManager::driverNames() {
00074     return driverManager().driverNames();
00075 }
00076 
00077 KexiDBDriver* KexiDBDriverManager::driver(const QString& drivername) {
00078     QGuardedPtr< ::KexiDB::Driver > driver = driverManager().driver(drivername); // caching is done by the DriverManager
00079     if(! driver) return 0;
00080     if(driver->error()) throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::Driver error for drivername '%1': %2").arg(drivername).arg(driver->errorMsg())) );
00081     return new KexiDBDriver(driver);
00082 }
00083 
00084 const QString KexiDBDriverManager::lookupByMime(const QString& mimetype) {
00085     return driverManager().lookupByMime(mimetype);
00086 }
00087 
00088 const QString KexiDBDriverManager::mimeForFile(const QString& filename) {
00089     QString mimename = KMimeType::findByFileContent( filename )->name();
00090     if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain")
00091         mimename = KMimeType::findByURL(filename)->name();
00092     return mimename;
00093 }
00094 
00095 KexiDBConnectionData* KexiDBDriverManager::createConnectionData() {
00096     return new KexiDBConnectionData( new ::KexiDB::ConnectionData() );
00097 }
00098 
00099 KexiDBConnectionData* KexiDBDriverManager::createConnectionDataByFile(const QString& filename) {
00101 
00102     QString mimename = KMimeType::findByFileContent(filename)->name();
00103     if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain")
00104         mimename = KMimeType::findByURL(filename)->name();
00105 
00106     if(mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") {
00107         KConfig config(filename, true, false);
00108         QString groupkey;
00109         QStringList groups(config.groupList());
00110         QStringList::ConstIterator it, end( groups.constEnd() );
00111         for( it = groups.constBegin(); it != end; ++it) {
00112             if((*it).lower()!="file information") {
00113                 groupkey = *it;
00114                 break;
00115             }
00116         }
00117         if(groupkey.isNull()) {
00118             kdDebug() << "No groupkey in KexiDBDriverManager::createConnectionDataByFile filename=" << filename << endl;
00119             return 0;
00120         }
00121 
00122         config.setGroup(groupkey);
00123         //QString type( config.readEntry("type", "database").lower() );
00124         //bool isDatabaseShortcut = (type == "database");
00125 
00126         ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData();
00127         int version = config.readNumEntry("version", 2); //KexiDBShortcutFile_version
00128         data->setFileName(QString::null);
00129         data->caption = config.readEntry("caption");
00130         data->description = config.readEntry("comment");
00131         QString dbname = config.readEntry("name");
00132         data->driverName = config.readEntry("engine");
00133         data->hostName = config.readEntry("server");
00134         data->port = config.readNumEntry("port", 0);
00135         data->useLocalSocketFile = config.readBoolEntry("useLocalSocketFile", false);
00136         data->localSocketFileName = config.readEntry("localSocketFile");
00137 
00138         if(version >= 2 && config.hasKey("encryptedPassword")) {
00139             data->password = config.readEntry("encryptedPassword");
00140             uint len = data->password.length();
00141             for (uint i=0; i<len; i++)
00142                 data->password[i] = QChar( data->password[i].unicode() - 47 - i );
00143         }
00144         if(data->password.isEmpty())
00145             data->password = config.readEntry("password");
00146 
00147         data->savePassword = ! data->password.isEmpty();
00148         data->userName = config.readEntry("user");
00149 
00150         KexiDBConnectionData* c = new KexiDBConnectionData(data);
00151         c->setDatabaseName(dbname);
00152         return c;
00153     }
00154 
00155     QString const drivername = driverManager().lookupByMime(mimename);
00156     if(! drivername) {
00157         kdDebug() << "No driver in KexiDBDriverManager::createConnectionDataByFile filename=" << filename << " mimename=" << mimename << endl;
00158         return 0;
00159     }
00160 
00161     ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData();
00162     data->setFileName(filename);
00163     data->driverName = drivername;
00164     return new KexiDBConnectionData(data);
00165 }
00166 
00167 KexiDBField* KexiDBDriverManager::field() {
00168     return new KexiDBField( new ::KexiDB::Field() );
00169 }
00170 
00171 KexiDBTableSchema* KexiDBDriverManager::tableSchema(const QString& tablename) {
00172     return new KexiDBTableSchema( new ::KexiDB::TableSchema(tablename) );
00173 }
00174 
00175 KexiDBQuerySchema* KexiDBDriverManager::querySchema() {
00176     return new KexiDBQuerySchema( new ::KexiDB::QuerySchema() );
00177 }
00178 
KDE Home | KDE Accessibility Home | Description of Access Keys