kexi
drivermanager.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <kexidb/drivermanager.h>
00023 #include <kexidb/drivermanager_p.h>
00024
00025 #include <kexidb/driver.h>
00026 #include <kexidb/driver_p.h>
00027 #include <kexidb/error.h>
00028
00029 #include <klibloader.h>
00030 #include <kparts/componentfactory.h>
00031 #include <ktrader.h>
00032 #include <kdebug.h>
00033 #include <klocale.h>
00034 #include <kservice.h>
00035
00036 #include <assert.h>
00037
00038 #include <qapplication.h>
00039
00040
00041 #undef KexiDBDbg
00042 #define KexiDBDbg if (0) kdDebug()
00043
00044 using namespace KexiDB;
00045
00046 DriverManagerInternal* DriverManagerInternal::s_self = 0L;
00047
00048
00049 DriverManagerInternal::DriverManagerInternal()
00050 : QObject( 0, "KexiDB::DriverManager" )
00051 , Object()
00052 , m_drivers(17, false)
00053 , m_refCount(0)
00054 , lookupDriversNeeded(true)
00055 {
00056 m_drivers.setAutoDelete(true);
00057 m_serverResultNum=0;
00058
00059 }
00060
00061 DriverManagerInternal::~DriverManagerInternal()
00062 {
00063 KexiDBDbg << "DriverManagerInternal::~DriverManagerInternal()" << endl;
00064 m_drivers.clear();
00065 if ( s_self == this )
00066 s_self = 0;
00067 KexiDBDbg << "DriverManagerInternal::~DriverManagerInternal() ok" << endl;
00068 }
00069
00070 void DriverManagerInternal::slotAppQuits()
00071 {
00072 if (qApp->mainWidget() && qApp->mainWidget()->isVisible())
00073 return;
00074 KexiDBDbg << "DriverManagerInternal::slotAppQuits(): let's clear drivers..." << endl;
00075 m_drivers.clear();
00076 }
00077
00078 DriverManagerInternal *DriverManagerInternal::self()
00079 {
00080 if (!s_self)
00081 s_self = new DriverManagerInternal();
00082
00083 return s_self;
00084 }
00085
00086 bool DriverManagerInternal::lookupDrivers()
00087 {
00088 if (!lookupDriversNeeded)
00089 return true;
00090
00091 if (qApp) {
00092 connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(slotAppQuits()));
00093 }
00094
00095
00096
00097
00098 lookupDriversNeeded = false;
00099 clearError();
00100 KTrader::OfferList tlist = KTrader::self()->query("Kexi/DBDriver");
00101 KTrader::OfferList::ConstIterator it(tlist.constBegin());
00102 for(; it != tlist.constEnd(); ++it)
00103 {
00104 KService::Ptr ptr = (*it);
00105 if (!ptr->property("Library").toString().startsWith("kexidb_")) {
00106 KexiDBWarn << "DriverManagerInternal::lookupDrivers():"
00107 " X-KDE-Library == " << ptr->property("Library").toString()
00108 << ": no \"kexidb_\" prefix -- skipped to avoid potential conflicts!" << endl;
00109 continue;
00110 }
00111 QString srv_name = ptr->property("X-Kexi-DriverName").toString();
00112 if (srv_name.isEmpty()) {
00113 KexiDBWarn << "DriverManagerInternal::lookupDrivers():"
00114 " X-Kexi-DriverName must be set for KexiDB driver \""
00115 << ptr->property("Name").toString() << "\" service!\n -- skipped!" << endl;
00116 continue;
00117 }
00118 if (m_services_lcase.contains(srv_name.lower())) {
00119 KexiDBWarn << "DriverManagerInternal::lookupDrivers(): more than one driver named '"
00120 << srv_name.lower() << "'\n -- skipping this one!" << endl;
00121 continue;
00122 }
00123
00124 QString srv_ver_str = ptr->property("X-Kexi-KexiDBVersion").toString();
00125 QStringList lst( QStringList::split(".", srv_ver_str) );
00126 uint minor_ver, major_ver;
00127 bool ok = (lst.count() == 2);
00128 if (ok)
00129 major_ver = lst[0].toUInt(&ok);
00130 if (ok)
00131 minor_ver = lst[1].toUInt(&ok);
00132 if (!ok) {
00133 KexiDBWarn << "DriverManagerInternal::lookupDrivers(): problem with detecting '"
00134 << srv_name.lower() << "' driver's version -- skipping it!" << endl;
00135 continue;
00136 }
00137 if (major_ver != KexiDB::version().major || minor_ver != KexiDB::version().minor) {
00138 KexiDBWarn << QString("DriverManagerInternal::lookupDrivers(): '%1' driver"
00139 " has version '%2' but required KexiDB driver version is '%3.%4'\n"
00140 " -- skipping this driver!").arg(srv_name.lower()).arg(srv_ver_str)
00141 .arg(KexiDB::version().major).arg(KexiDB::version().minor) << endl;
00142 possibleProblems += QString("\"%1\" database driver has version \"%2\" "
00143 "but required driver version is \"%3.%4\"")
00144 .arg(srv_name.lower()).arg(srv_ver_str)
00145 .arg(KexiDB::version().major).arg(KexiDB::version().minor);
00146 continue;
00147 }
00148
00149 QString drvType = ptr->property("X-Kexi-DriverType").toString().lower();
00150 if (drvType=="file") {
00151
00152 QStringList mimes( ptr->property("X-Kexi-FileDBDriverMimeList").toStringList() );
00153
00154 {
00155 QString mime( ptr->property("X-Kexi-FileDBDriverMime").toString().lower() );
00156 if (!mime.isEmpty())
00157 mimes.append( mime );
00158 }
00159
00160
00161 for (QStringList::ConstIterator mime_it = mimes.constBegin(); mime_it!=mimes.constEnd(); ++mime_it) {
00162 QString mime( (*mime_it).lower() );
00163 if (!m_services_by_mimetype.contains(mime)) {
00164 m_services_by_mimetype.insert(mime, ptr);
00165 }
00166 else {
00167 KexiDBWarn << "DriverManagerInternal::lookupDrivers(): more than one driver for '"
00168 << mime << "' mime type!" << endl;
00169 }
00170 }
00171 }
00172 m_services.insert(srv_name, ptr);
00173 m_services_lcase.insert(srv_name.lower(), ptr);
00174 KexiDBDbg << "KexiDB::DriverManager::lookupDrivers(): registered driver: " << ptr->name() << "(" << ptr->library() << ")" << endl;
00175 }
00176
00177 if (tlist.isEmpty())
00178 {
00179 setError(ERR_DRIVERMANAGER, i18n("Could not find any database drivers.") );
00180 return false;
00181 }
00182 return true;
00183 }
00184
00185 KexiDB::Driver::Info DriverManagerInternal::driverInfo(const QString &name)
00186 {
00187 KexiDB::Driver::Info i = m_driversInfo[name.lower()];
00188 if (!error() && i.name.isEmpty())
00189 setError(ERR_DRIVERMANAGER, i18n("Could not find database driver \"%1\".").arg(name) );
00190 return i;
00191 }
00192
00193 Driver* DriverManagerInternal::driver(const QString& name)
00194 {
00195 if (!lookupDrivers())
00196 return 0;
00197
00198 clearError();
00199 KexiDBDbg << "DriverManager::driver(): loading " << name << endl;
00200
00201 Driver *drv = name.isEmpty() ? 0 : m_drivers.find(name.latin1());
00202 if (drv)
00203 return drv;
00204
00205 if (!m_services_lcase.contains(name.lower())) {
00206 setError(ERR_DRIVERMANAGER, i18n("Could not find database driver \"%1\".").arg(name) );
00207 return 0;
00208 }
00209
00210 KService::Ptr ptr= *(m_services_lcase.find(name.lower()));
00211 QString srv_name = ptr->property("X-Kexi-DriverName").toString();
00212
00213 KexiDBDbg << "KexiDBInterfaceManager::load(): library: "<<ptr->library()<<endl;
00214 drv = KParts::ComponentFactory::createInstanceFromService<KexiDB::Driver>(ptr,
00215 this, srv_name.latin1(), QStringList(),&m_serverResultNum);
00216
00217 if (!drv) {
00218 setError(ERR_DRIVERMANAGER, i18n("Could not load database driver \"%1\".")
00219 .arg(name) );
00220 if (m_componentLoadingErrors.isEmpty()) {
00221 m_componentLoadingErrors[KParts::ComponentFactory::ErrNoServiceFound]="ErrNoServiceFound";
00222 m_componentLoadingErrors[KParts::ComponentFactory::ErrServiceProvidesNoLibrary]="ErrServiceProvidesNoLibrary";
00223 m_componentLoadingErrors[KParts::ComponentFactory::ErrNoLibrary]="ErrNoLibrary";
00224 m_componentLoadingErrors[KParts::ComponentFactory::ErrNoFactory]="ErrNoFactory";
00225 m_componentLoadingErrors[KParts::ComponentFactory::ErrNoComponent]="ErrNoComponent";
00226 }
00227 m_serverResultName=m_componentLoadingErrors[m_serverResultNum];
00228 return 0;
00229 }
00230 KexiDBDbg << "KexiDBInterfaceManager::load(): loading succeed: " << name <<endl;
00231
00232
00233
00234 drv->d->service = ptr.data();
00235 drv->d->fileDBDriverMimeType = ptr->property("X-Kexi-FileDBDriverMime").toString();
00236 drv->d->initInternalProperties();
00237
00238 if (!drv->isValid()) {
00239 setError(drv);
00240 delete drv;
00241 return 0;
00242 }
00243 m_drivers.insert(name.latin1(), drv);
00244 return drv;
00245 }
00246
00247 void DriverManagerInternal::incRefCount()
00248 {
00249 m_refCount++;
00250 KexiDBDbg << "DriverManagerInternal::incRefCount(): " << m_refCount << endl;
00251 }
00252
00253 void DriverManagerInternal::decRefCount()
00254 {
00255 m_refCount--;
00256 KexiDBDbg << "DriverManagerInternal::decRefCount(): " << m_refCount << endl;
00257
00258
00259
00260
00261
00262 }
00263
00264 void DriverManagerInternal::aboutDelete( Driver* drv )
00265 {
00266 m_drivers.take(drv->name());
00267 }
00268
00269
00270
00271
00272
00273
00274
00275 DriverManager::DriverManager()
00276 : QObject( 0, "KexiDB::DriverManager" )
00277 , Object()
00278 , d_int( DriverManagerInternal::self() )
00279 {
00280 d_int->incRefCount();
00281
00282
00283
00284 }
00285
00286 DriverManager::~DriverManager()
00287 {
00288 KexiDBDbg << "DriverManager::~DriverManager()" << endl;
00289
00290
00291
00292
00293
00294
00295
00296
00297 d_int->decRefCount();
00298 if (d_int->m_refCount==0) {
00299
00300 delete d_int;
00301 }
00302
00303
00304 KexiDBDbg << "DriverManager::~DriverManager() ok" << endl;
00305 }
00306
00307 const KexiDB::Driver::InfoMap DriverManager::driversInfo()
00308 {
00309 if (!d_int->lookupDrivers())
00310 return KexiDB::Driver::InfoMap();
00311
00312 if (!d_int->m_driversInfo.isEmpty())
00313 return d_int->m_driversInfo;
00314 ServicesMap::ConstIterator it;
00315 for ( it=d_int->m_services.constBegin() ; it != d_int->m_services.constEnd(); ++it ) {
00316 Driver::Info info;
00317 KService::Ptr ptr = it.data();
00318 info.name = ptr->property("X-Kexi-DriverName").toString();
00319 info.caption = ptr->property("Name").toString();
00320 info.comment = ptr->property("Comment").toString();
00321 if (info.caption.isEmpty())
00322 info.caption = info.name;
00323 info.fileBased = (ptr->property("X-Kexi-DriverType").toString().lower()=="file");
00324 if (info.fileBased)
00325 info.fileDBMimeType = ptr->property("X-Kexi-FileDBDriverMime").toString().lower();
00326 QVariant v = ptr->property("X-Kexi-DoNotAllowProjectImportingTo");
00327 info.allowImportingTo = v.isNull() ? true : !v.toBool();
00328 d_int->m_driversInfo.insert(info.name.lower(), info);
00329 }
00330 return d_int->m_driversInfo;
00331 }
00332
00333 const QStringList DriverManager::driverNames()
00334 {
00335 if (!d_int->lookupDrivers())
00336 return QStringList();
00337
00338 if (d_int->m_services.isEmpty() && d_int->error())
00339 return QStringList();
00340 return d_int->m_services.keys();
00341 }
00342
00343 KexiDB::Driver::Info DriverManager::driverInfo(const QString &name)
00344 {
00345 driversInfo();
00346 KexiDB::Driver::Info i = d_int->driverInfo(name);
00347 if (d_int->error())
00348 setError(d_int);
00349 return i;
00350 }
00351
00352 KService::Ptr DriverManager::serviceInfo(const QString &name)
00353 {
00354 if (!d_int->lookupDrivers()) {
00355 setError(d_int);
00356 return KService::Ptr();
00357 }
00358
00359 clearError();
00360 if (d_int->m_services_lcase.contains(name.lower())) {
00361 return *d_int->m_services_lcase.find(name.lower());
00362 } else {
00363 setError(ERR_DRIVERMANAGER, i18n("No such driver service: \"%1\".").arg(name) );
00364 return KService::Ptr();
00365 }
00366 }
00367
00368 const DriverManager::ServicesMap& DriverManager::services()
00369 {
00370 d_int->lookupDrivers();
00371
00372 return d_int->m_services;
00373 }
00374
00375 QString DriverManager::lookupByMime(const QString &mimeType)
00376 {
00377 if (!d_int->lookupDrivers()) {
00378 setError(d_int);
00379 return 0;
00380 }
00381
00382 KService::Ptr ptr = d_int->m_services_by_mimetype[mimeType.lower()];
00383 if (!ptr)
00384 return QString::null;
00385 return ptr->property("X-Kexi-DriverName").toString();
00386 }
00387
00388 Driver* DriverManager::driver(const QString& name)
00389 {
00390 Driver *drv = d_int->driver(name);
00391 if (d_int->error())
00392 setError(d_int);
00393 return drv;
00394 }
00395
00396 QString DriverManager::serverErrorMsg()
00397 {
00398 return d_int->m_serverErrMsg;
00399 }
00400
00401 int DriverManager::serverResult()
00402 {
00403 return d_int->m_serverResultNum;
00404 }
00405
00406 QString DriverManager::serverResultName()
00407 {
00408 return d_int->m_serverResultName;
00409 }
00410
00411 void DriverManager::drv_clearServerResult()
00412 {
00413 d_int->m_serverErrMsg=QString::null;
00414 d_int->m_serverResultNum=0;
00415 d_int->m_serverResultName=QString::null;
00416 }
00417
00418 QString DriverManager::possibleProblemsInfoMsg() const
00419 {
00420 if (d_int->possibleProblems.isEmpty())
00421 return QString::null;
00422 QString str;
00423 str.reserve(1024);
00424 str = "<ul>";
00425 for (QStringList::ConstIterator it = d_int->possibleProblems.constBegin();
00426 it!=d_int->possibleProblems.constEnd(); ++it)
00427 {
00428 str += (QString::fromLatin1("<li>") + *it + QString::fromLatin1("</li>"));
00429 }
00430 str += "</ul>";
00431 return str;
00432 }
00433
00434 #include "drivermanager_p.moc"
00435
|