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