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     addFunction("driverNames", &KexiDBDriverManager::driverNames);
00044     addFunction("driver", &KexiDBDriverManager::driver,
00045         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00046     addFunction("lookupByMime", &KexiDBDriverManager::lookupByMime,
00047         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00048     addFunction("mimeForFile", &KexiDBDriverManager::mimeForFile,
00049         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00050 
00051     addFunction("createConnectionData", &KexiDBDriverManager::createConnectionData);
00052     addFunction("createConnectionDataByFile", &KexiDBDriverManager::createConnectionDataByFile,
00053         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00054     addFunction("field", &KexiDBDriverManager::field);
00055     addFunction("tableSchema", &KexiDBDriverManager::tableSchema,
00056         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00057     addFunction("querySchema", &KexiDBDriverManager::querySchema);
00058 }
00059 
00060 KexiDBDriverManager::~KexiDBDriverManager()
00061 {
00062 }
00063 
00064 const QString KexiDBDriverManager::getClassName() const
00065 {
00066     return "Kross::KexiDB::KexiDBDriverManager";
00067 }
00068 
00069 KexiDB::DriverManager& KexiDBDriverManager::driverManager()
00070 {
00071     if(m_drivermanager.error())
00072         throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::DriverManager error: %1").arg(m_drivermanager.errorMsg())) );
00073     return m_drivermanager;
00074 }
00075 
00076 Kross::Api::Object::Ptr KexiDBDriverManager::driverNames(Kross::Api::List::Ptr)
00077 {
00078     return new Kross::Api::Variant(driverManager().driverNames(), "Kross::KexiDB::DriverManager::driverNames::StringList");
00079 }
00080 
00081 Kross::Api::Object::Ptr KexiDBDriverManager::driver(Kross::Api::List::Ptr args)
00082 {
00083     QString drivername = Kross::Api::Variant::toString(args->item(0));
00084     QGuardedPtr< ::KexiDB::Driver > driver = driverManager().driver(drivername); // caching is done by the DriverManager
00085     if(! driver)
00086         throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("No such KexiDB::Driver object for the defined drivername '%1'.").arg(drivername)) );
00087     if(driver->error())
00088         throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::Driver error for drivername '%1': %2").arg(drivername).arg(driver->errorMsg())) );
00089     return new KexiDBDriver(driver);
00090 }
00091 
00092 Kross::Api::Object::Ptr KexiDBDriverManager::lookupByMime(Kross::Api::List::Ptr args)
00093 {
00094     return new Kross::Api::Variant(
00095         driverManager().lookupByMime( Kross::Api::Variant::toString(args->item(0)) ));
00096 }
00097 
00098 Kross::Api::Object::Ptr KexiDBDriverManager::mimeForFile(Kross::Api::List::Ptr args)
00099 {
00100     QString const file = Kross::Api::Variant::toString(args->item(0));
00101     QString mimename = KMimeType::findByFileContent(file)->name();
00102     if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain")
00103         mimename = KMimeType::findByURL(file)->name();
00104     return new Kross::Api::Variant(mimename);
00105 }
00106 
00107 Kross::Api::Object::Ptr KexiDBDriverManager::createConnectionData(Kross::Api::List::Ptr)
00108 {
00109     return new KexiDBConnectionData( new ::KexiDB::ConnectionData() );
00110 }
00111 
00112 Kross::Api::Object::Ptr KexiDBDriverManager::createConnectionDataByFile(Kross::Api::List::Ptr args)
00113 {
00115 
00116     QString const file = Kross::Api::Variant::toString(args->item(0));
00117 
00118     QString mimename = KMimeType::findByFileContent(file)->name();
00119     if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain")
00120         mimename = KMimeType::findByURL(file)->name();
00121 
00122     if(mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") {
00123         KConfig config(file, true, false);
00124         QString groupkey;
00125         QStringList groups(config.groupList());
00126         for(QStringList::Iterator it = groups.begin(); it != groups.end(); ++it) {
00127             if((*it).lower()!="file information") {
00128                 groupkey = *it;
00129                 break;
00130             }
00131         }
00132         if(groupkey.isNull())
00133             return 0;
00134 
00135         config.setGroup(groupkey);
00136         //QString type( config.readEntry("type", "database").lower() );
00137         //bool isDatabaseShortcut = (type == "database");
00138 
00139         ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData();
00140         int version = config.readNumEntry("version", 2); //KexiDBShortcutFile_version
00141         data->setFileName(QString::null);
00142         data->caption = config.readEntry("caption");
00143         data->description = config.readEntry("comment");
00144         QString dbname = config.readEntry("name");
00145         data->driverName = config.readEntry("engine");
00146         data->hostName = config.readEntry("server");
00147         data->port = config.readNumEntry("port", 0);
00148         data->useLocalSocketFile = config.readBoolEntry("useLocalSocketFile", false);
00149         data->localSocketFileName = config.readEntry("localSocketFile");
00150 
00151         if(version >= 2 && config.hasKey("encryptedPassword")) {
00152             data->password = config.readEntry("encryptedPassword");
00153             uint len = data->password.length();
00154             for (uint i=0; i<len; i++)
00155                 data->password[i] = QChar( data->password[i].unicode() - 47 - i );
00156         }
00157         if(data->password.isEmpty())
00158             data->password = config.readEntry("password");
00159 
00160         data->savePassword = ! data->password.isEmpty();
00161         data->userName = config.readEntry("user");
00162 
00163         KexiDBConnectionData* c = new KexiDBConnectionData(data);
00164         c->setDatabaseName(dbname);
00165         return c;
00166     }
00167 
00168     QString const drivername = driverManager().lookupByMime(mimename);
00169     if(! drivername)
00170         return 0;
00171 
00172     ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData();
00173     data->setFileName(file);
00174     data->driverName = drivername;
00175     return new KexiDBConnectionData(data);
00176 }
00177 
00178 Kross::Api::Object::Ptr KexiDBDriverManager::field(Kross::Api::List::Ptr)
00179 {
00180     return new KexiDBField( new ::KexiDB::Field() );
00181 }
00182 
00183 Kross::Api::Object::Ptr KexiDBDriverManager::tableSchema(Kross::Api::List::Ptr args)
00184 {
00185     return new KexiDBTableSchema(
00186                new ::KexiDB::TableSchema(Kross::Api::Variant::toString(args->item(0)))
00187            );
00188 }
00189 
00190 Kross::Api::Object::Ptr KexiDBDriverManager::querySchema(Kross::Api::List::Ptr)
00191 {
00192     return new KexiDBQuerySchema( new ::KexiDB::QuerySchema() );
00193 }
00194 
KDE Home | KDE Accessibility Home | Description of Access Keys