Commit c140c9ea authored by Volker Krause's avatar Volker Krause
Browse files

Convert all resources we can't migrate directly to use the Akonadi ->

KResource bridge.
This requires a working config reload implementation in the bridge though,
I doubt my local version (kill(getpid(), 11)) is acceptable ;-)

svn path=/trunk/KDE/kdepim/akonadi/; revision=846571
parent 4833bddc
......@@ -2,6 +2,7 @@ set(kres-migrator_srcs
main.cpp
kabcmigrator.cpp
kcalmigrator.cpp
kresmigratorbase.cpp
)
kcfg_generate_dbus_interface(${CMAKE_SOURCE_DIR}/akonadi/resources/vcard/vcardresource.kcfg org.kde.Akonadi.VCard.Settings)
......
......@@ -31,12 +31,8 @@
using namespace Akonadi;
KABCMigrator::KABCMigrator()
{
migrateType( "contact" );
}
KABCMigrator::~KABCMigrator()
KABCMigrator::KABCMigrator() :
KResMigrator<KABC::Resource>( "contact" )
{
}
......@@ -45,6 +41,8 @@ void KABCMigrator::migrateResource( KABC::Resource* res)
kDebug() << res->identifier() << res->type();
if ( res->type() == "file" )
migrateFileResource( res );
else if ( migrationState( res ) == None )
migrateToBridge( res, "akonadi_kabc_resource" );
else
migrateNext();
}
......@@ -63,7 +61,7 @@ void KABCMigrator::migrateFileResource(KABC::Resource * res)
}
AgentInstanceCreateJob *job = new AgentInstanceCreateJob( type, this );
connect( job, SIGNAL(result(KJob*)), SLOT(fileResourceCreated(KJob*)) );
mJobMap.insert( job, res );
setResourceForJob( job, res );
job->start();
}
......@@ -73,8 +71,7 @@ void KABCMigrator::fileResourceCreated(KJob * job)
kDebug() << "Failed to create vcard resource!";
return;
}
Q_ASSERT( mJobMap.contains( job ) );
KABC::Resource *res = mJobMap.take( job );
KABC::Resource *res = resourceForJob( job );
AgentInstance instance = static_cast<AgentInstanceCreateJob*>( job )->instance();
const KConfigGroup kresCfg = kresConfig( res );
instance.setName( kresCfg.readEntry( "ResourceName", "Migrated Addressbook" ) );
......@@ -88,7 +85,7 @@ void KABCMigrator::fileResourceCreated(KJob * job)
iface->setPath( kresCfg.readPathEntry( "FileName", "" ) );
iface->setReadOnly( res->readOnly() );
instance.reconfigure();
resourceMigrated( res );
setMigrationState( res, Complete );
migrateNext();
}
......
......@@ -36,7 +36,6 @@ class KABCMigrator : public KResMigrator<KABC::Resource>
Q_OBJECT
public:
KABCMigrator();
~KABCMigrator();
void migrateResource( KABC::Resource *res );
......@@ -45,9 +44,6 @@ class KABCMigrator : public KResMigrator<KABC::Resource>
private:
void migrateFileResource( KABC::Resource *res );
private:
QHash<KJob*, KABC::Resource*> mJobMap;
};
#endif
......@@ -31,12 +31,8 @@
using namespace Akonadi;
KCalMigrator::KCalMigrator()
{
migrateType( "calendar" );
}
KCalMigrator::~KCalMigrator()
KCalMigrator::KCalMigrator() :
KResMigrator<KCal::ResourceCalendar>( "calendar" )
{
}
......@@ -45,6 +41,8 @@ void KCalMigrator::migrateResource( KCal::ResourceCalendar* res)
kDebug() << res->identifier() << res->type();
if ( res->type() == "file" )
migrateFileResource( res );
else if ( migrationState( res ) != None )
migrateToBridge( res, "akonadi_kcal_resource" );
else
migrateNext();
}
......@@ -63,7 +61,7 @@ void KCalMigrator::migrateFileResource(KCal::ResourceCalendar * res)
}
AgentInstanceCreateJob *job = new AgentInstanceCreateJob( type, this );
connect( job, SIGNAL(result(KJob*)), SLOT(fileResourceCreated(KJob*)) );
mJobMap.insert( job, res );
setResourceForJob( job, res );
job->start();
}
......@@ -73,8 +71,7 @@ void KCalMigrator::fileResourceCreated(KJob * job)
kDebug() << "Failed to create ical resource!";
return;
}
Q_ASSERT( mJobMap.contains( job ) );
KCal::ResourceCalendar *res = mJobMap.take( job );
KCal::ResourceCalendar *res = resourceForJob( job );
AgentInstance instance = static_cast<AgentInstanceCreateJob*>( job )->instance();
const KConfigGroup kresCfg = kresConfig( res );
instance.setName( kresCfg.readEntry( "ResourceName", "Migrated Calendar" ) );
......@@ -89,7 +86,7 @@ void KCalMigrator::fileResourceCreated(KJob * job)
iface->setPath( kresCfg.readPathEntry( "CalendarURL", "" ) );
iface->setReadOnly( res->readOnly() );
instance.reconfigure();
resourceMigrated( res );
setMigrationState( res, Complete );
migrateNext();
}
......
......@@ -36,7 +36,6 @@ class KCalMigrator : public KResMigrator<KCal::ResourceCalendar>
Q_OBJECT
public:
KCalMigrator();
~KCalMigrator();
void migrateResource( KCal::ResourceCalendar *res );
......
......@@ -20,6 +20,8 @@
#ifndef KRESMIGRATOR_H
#define KRESMIGRATOR_H
#include "kresmigratorbase.h"
#include <kresources/manager.h>
#include <kresources/resource.h>
......@@ -28,26 +30,26 @@
#include <KGlobal>
#include <KStandardDirs>
template <typename T> class KResMigrator : public QObject
template <typename T> class KResMigrator : public KResMigratorBase
{
public:
KResMigrator() : mConfig( 0 )
KResMigrator( const QString &type ) :
KResMigratorBase( type ),
mConfig( 0 )
{
KGlobal::ref();
}
virtual ~KResMigrator()
{
delete mConfig;
delete mManager;
KGlobal::deref();
}
void migrateType( const QString &type )
void migrate()
{
mManager = new KRES::Manager<T>( type );
mManager = new KRES::Manager<T>( mType );
mManager->readConfig();
const QString kresCfgFile = KStandardDirs::locateLocal( "config", QString( "kresources/%1/stdrc" ).arg( type ) );
const QString kresCfgFile = KStandardDirs::locateLocal( "config", QString( "kresources/%1/stdrc" ).arg( mType ) );
mConfig = new KConfig( kresCfgFile );
mIt = mManager->begin();
migrateNext();
......@@ -57,9 +59,10 @@ template <typename T> class KResMigrator : public QObject
{
while ( mIt != mManager->end() ) {
KConfigGroup cfg( KGlobal::config(), "Resource " + (*mIt)->identifier() );
if ( !cfg.readEntry( "Migrated", false ) ) {
migrateResource( *mIt );
if ( migrationState( *mIt ) != Complete ) {
T* res = *mIt;
++mIt;
migrateResource( res );
return;
}
++mIt;
......@@ -70,16 +73,17 @@ template <typename T> class KResMigrator : public QObject
virtual void migrateResource( T *res ) = 0;
void resourceMigrated( T *res ) const
KConfigGroup kresConfig( KRES::Resource* res ) const
{
KConfigGroup cfg( KGlobal::config(), "Resource " + res->identifier() );
cfg.writeEntry( "Migrated", true );
cfg.sync();
return KConfigGroup( mConfig, "Resource_" + res->identifier() );
}
KConfigGroup kresConfig( T* res ) const
T* resourceForJob( KJob *job )
{
return KConfigGroup( mConfig, "Resource_" + res->identifier() );
if ( mJobResMap.contains( job ) )
return static_cast<T*>( mJobResMap.value( job ) );
Q_ASSERT( false );
return 0;
}
private:
......
/*
Copyright (c) 2008 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "kresmigratorbase.h"
#include <akonadi/agentinstance.h>
#include <akonadi/agentinstancecreatejob.h>
#include <akonadi/agentmanager.h>
#include <akonadi/agenttype.h>
#include <kresources/manager.h>
#include <kresources/resource.h>
#include <KConfigGroup>
#include <KGlobal>
#include <KStandardDirs>
#include <QMetaEnum>
#include <QTimer>
using namespace Akonadi;
KResMigratorBase::KResMigratorBase(const QString & type) :
mType( type )
{
KGlobal::ref();
// load the vtable before we continue
QTimer::singleShot( 0, this, SLOT(migrate()) );
}
KResMigratorBase::~ KResMigratorBase()
{
KGlobal::deref();
}
KResMigratorBase::MigrationState KResMigratorBase::migrationState(KRES::Resource * res) const
{
KConfigGroup cfg( KGlobal::config(), "Resource " + res->identifier() );
QMetaEnum e = metaObject()->enumerator( metaObject()->indexOfEnumerator( "MigrationState" ) );
const QString s = cfg.readEntry( "MigrationState", e.valueToKey( None ) );
return (MigrationState)e.keyToValue( s.toLatin1() );
}
void KResMigratorBase::setMigrationState(KRES::Resource * res, MigrationState state)
{
KConfigGroup cfg( KGlobal::config(), "Resource " + res->identifier() );
QMetaEnum e = metaObject()->enumerator( metaObject()->indexOfEnumerator( "MigrationState" ) );
QString stateStr = e.valueToKey( state );
cfg.writeEntry( "MigrationState", stateStr );
cfg.sync();
}
void KResMigratorBase::setResourceForJob(KJob * job, KRES::Resource * res)
{
mJobResMap.insert( job, res );
}
void KResMigratorBase::migrateToBridge( KRES::Resource *res, const QString & typeId)
{
kDebug() << res->type() << res->identifier() << typeId;
if ( migrationState( res ) != None ) {
migrateNext();
return;
}
const AgentType type = AgentManager::self()->type( typeId );
if ( !type.isValid() ) {
kDebug() << "Unable to obtain kabc bridge resource type!" << typeId;
return;
}
AgentInstanceCreateJob *job = new AgentInstanceCreateJob( type, this );
connect( job, SIGNAL(result(KJob*)), SLOT(resourceBridgeCreated(KJob*)) );
setResourceForJob( job, res );
job->start();
}
void KResMigratorBase::resourceBridgeCreated(KJob * job)
{
kDebug();
if ( job->error() ) {
kWarning() << "Unable to create KResource bridge resource!";
migrateNext();
return;
}
KRES::Resource *res = mJobResMap.value( job );
Q_ASSERT( res );
AgentInstance instance = static_cast<AgentInstanceCreateJob*>( job )->instance();
const KConfigGroup kresCfg = kresConfig( res );
instance.setName( kresCfg.readEntry( "ResourceName", "Bridged KResource" ) );
const QString akoResCfgFile = KStandardDirs::locateLocal( "config", QString( "%1rc" ).arg( instance.identifier() ) );
KConfig *akoResConfig = new KConfig( akoResCfgFile );
KConfigGroup bridgeResCfg( akoResConfig, kresCfg.name() );
kresCfg.copyTo( &bridgeResCfg );
bridgeResCfg.sync();
KConfigGroup generalCfg( akoResConfig, "General" );
generalCfg.writeEntry( "ResourceKeys", res->identifier() );
generalCfg.writeEntry( "Standard", res->identifier() );
generalCfg.sync();
akoResConfig->sync();
delete akoResConfig;
instance.reconfigure();
setMigrationState( res, Bridged );
migrateNext();
}
#include "kresmigratorbase.moc"
/*
Copyright (c) 2008 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef KRESMIGRATORBASE_H
#define KRESMIGRATORBASE_H
#include <KConfigGroup>
#include <QHash>
#include <QObject>
namespace KRES {
class Resource;
}
class KJob;
/**
* Non-template QObject part of KResMigrator.
*/
class KResMigratorBase : public QObject
{
Q_OBJECT
public:
enum MigrationState
{
None,
Bridged,
Complete
};
Q_ENUMS( MigrationState )
KResMigratorBase( const QString &type );
~KResMigratorBase();
MigrationState migrationState( KRES::Resource *res ) const;
void setMigrationState( KRES::Resource *res, MigrationState state );
void setResourceForJob( KJob* job, KRES::Resource *res );
virtual void migrateNext() = 0;
void migrateToBridge( KRES::Resource* res, const QString &typeId );
virtual KConfigGroup kresConfig( KRES::Resource* res ) const = 0;
protected slots:
virtual void migrate() = 0;
protected:
QString mType;
QHash<KJob*, KRES::Resource*> mJobResMap;
private slots:
void resourceBridgeCreated( KJob *job );
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment