Commit 0cb7d7ac authored by Volker Krause's avatar Volker Krause
Browse files

First draft of a self-updating collection model. This is work in

progress and far from usable, but please review and let me know if it is
going at least into the right direction ;-)

Also included is a very ugly test program that generates a large
collection tree and changes it randomly.

CCMAIL: kde-pim@kde.org

svn path=/trunk/KDE/kdepim/libakonadi/; revision=503536
parent ec715e44
INCLUDES = -I$(top_srcdir)/ $(all_includes)
SUBDIRS = . tests
INCLUDES = -I$(top_srcdir)/ -I$(top_srcdir)/libakonadi $(all_includes)
noinst_LTLIBRARIES = libakonadi.la
libakonadi_la_SOURCES = datarequest.cpp job.cpp query.cpp
libakonadi_la_SOURCES = datarequest.cpp job.cpp query.cpp collection.cpp \
collectionlistjob.cpp collectionmodel.cpp collectionmonitorjob.cpp \
collectionfetchjob.cpp collectionview.cpp
libakonadi_la_LDFLAGS = $(all_libraries) -no-undefined
libakonadi_la_LIBADD = $(LIB_QT) $(LIB_KDECORE)
akonadiincludedir = $(includedir)/libakonadi
akonadiinclude_HEADERS = datarequest.h job.h query.h
akonadiinclude_HEADERS = datarequest.h job.h query.h collection.h \
collectionlistjob.h collectionmodel.h collectionmonitorjob.h \
collectionfetchjob.h collectionview.h
METASOURCES = AUTO
......
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 "collection.h"
#include <QString>
#include <QStringList>
using namespace PIM;
class Collection::CollectionPrivate
{
public:
DataReference ref;
DataReference parent;
QString name;
QString type;
QStringList contentTypes;
QString query;
};
PIM::Collection::Collection( const DataReference &ref ) :
d( new Collection::CollectionPrivate() )
{
d->ref = ref;
}
PIM::Collection::~ Collection( )
{
delete d;
d = 0;
}
DataReference PIM::Collection::reference( ) const
{
return d->ref;
}
QString PIM::Collection::name( ) const
{
return d->name;
}
void PIM::Collection::setName( const QString & name )
{
d->name = name;
}
QString PIM::Collection::type( ) const
{
return d->type;
}
void PIM::Collection::setType( const QString & type )
{
d->type = type;
}
QStringList PIM::Collection::contentTypes( ) const
{
return d->contentTypes;
}
void PIM::Collection::setContentTypes( const QStringList & types )
{
d->contentTypes = types;
}
DataReference PIM::Collection::parent( ) const
{
return d->parent;
}
void PIM::Collection::setParent( const DataReference & parent )
{
d->parent = parent;
}
QString PIM::Collection::query( ) const
{
return d->query;
}
void PIM::Collection::setQuery( const QString & query )
{
d->query = query;
}
void PIM::Collection::copy( Collection * col )
{
d->name = col->name();
d->ref = col->reference();
d->parent = col->parent();
d->type = col->type();
d->contentTypes = col->contentTypes();
d->query = col->query();
}
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 PIM_COLLECTION_H
#define PIM_COLLECTION_H
#include <libakonadi/job.h>
namespace PIM {
/**
This class presents a collection of PIM objects, such as a folder on a mail or
groupware server.
Collections are hierarchical, i.e. they may have a parent collection.
*/
class Collection
{
public:
/**
Create a new collection.
@param ref The data reference of this collection.
*/
Collection( const DataReference &ref );
/**
Destroys this collection.
*/
virtual ~Collection();
/**
Returns the DataReference of this object.
*/
DataReference reference() const;
/**
Returns the name of this collection.
*/
QString name() const;
/**
Sets the name of this collection.
*/
void setName( const QString &name );
/**
Returns the type of this collection (e.g. virtual folder, folder on an
IMAP server, etc.).
*/
QString type() const;
/**
Sets the type of this collection.
*/
void setType( const QString &type );
/**
Returns a list of possible content mimetypes,
e.g. message/rfc822, x-akonadi/collection for a mail folder that
supports sub-folders.
*/
QStringList contentTypes() const;
/**
Sets the list of possible content mimetypes.
*/
void setContentTypes( const QStringList &types );
/**
Returns a reference to the parent collection, might be an empty reference if this
collection is a top-level collection.
*/
DataReference parent() const;
/**
Sets the parent collections.
*/
void setParent( const DataReference &parent );
/**
Returns a query for the Akonadi backend to list the folder content.
*/
QString query() const;
/**
Set the content query.
*/
void setQuery( const QString &query );
/**
Copy the content of the given collection into this one.
Used in PIM::CollectionModel to handle updates without changing pointers.
Note: Subclasses should overwrite this method, copy their data and call
the implementation of the super class.
*/
virtual void copy( Collection *col );
private:
class CollectionPrivate;
CollectionPrivate *d;
};
}
#endif
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 "collection.h"
#include "collectionfetchjob.h"
#include <QTimer>
using namespace PIM;
// ### just for testing, remove as soon as we have a real backend
#define DUMMY_FETCH_JOB
#ifdef DUMMY_FETCH_JOB
#include <QHash>
static Collection* col = 0;
QHash<DataReference, Collection*> global_collection_map;
#endif
PIM::CollectionFetchJob::CollectionFetchJob( const DataReference & ref )
{
#ifdef DUMMY_FETCH_JOB
if ( global_collection_map.contains( ref ) ) {
col = new Collection( ref );
col->copy( global_collection_map.value( ref ) );
} else
col = 0;
#else
Q_UNUSED( ref );
#endif
}
PIM::CollectionFetchJob::~CollectionFetchJob()
{
}
Collection * PIM::CollectionFetchJob::collection() const
{
#ifdef DUMMY_FETCH_JOB
return col;
#else
return 0;
#endif
}
void PIM::CollectionFetchJob::emitDone()
{
emit done( this );
}
void PIM::CollectionFetchJob::doStart()
{
QTimer::singleShot( 0, this, SLOT( emitDone() ) );
}
#include "collectionfetchjob.moc"
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 PIM_COLLECTIONFETCHJOB_H
#define PIM_COLLECTIONFETCHJOB_H
#include <libakonadi/job.h>
namespace PIM {
class Collection;
/**
Fetches a Collection object for a given DataReference from the storage
service.
@todo Derive from DataRequest instead from Job and support fetching lists?
*/
class CollectionFetchJob : public Job
{
Q_OBJECT
public:
/**
Creates a new collection fetch job.
@param ref The collection to fetch.
*/
CollectionFetchJob( const DataReference &ref );
/**
Destroys this job.
*/
virtual ~CollectionFetchJob();
/**
Returns the fetched collection, 0 if the collection is not (yet) available
or if there has been an error.
It is your responsibility to delete this collection object.
*/
Collection* collection() const;
private slots:
// ### just for testing ###
void emitDone();
private:
void doStart();
};
}
#endif
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 "collection.h"
#include "collectionlistjob.h"
#include <QHash>
#include <QTimer>
using namespace PIM;
PIM::CollectionListJob::CollectionListJob()
{
}
PIM::CollectionListJob::~CollectionListJob()
{
}
QHash<DataReference, Collection*> PIM::CollectionListJob::collections() const
{
return QHash<DataReference, Collection*>();
}
void PIM::CollectionListJob::doStart()
{
QTimer::singleShot( 0, this, SLOT( emitDone() ) );
}
void PIM::CollectionListJob::emitDone()
{
emit done( this );
}
#include "collectionlistjob.moc"
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 PIM_COLLECTION_JOB
#define PIM_COLLECTION_JOB
#include <libakonadi/job.h>
namespace PIM {
class Collection;
/**
This class can be used to retrieve the complete or partial collection tree
of the PIM storage service.
It returns a QHash of references to PIM::Collection objects.
@todo Add partial collection retrieval (eg. only collections containing contacts).
*/
class CollectionListJob : public Job
{
Q_OBJECT
public:
/**
Create a new CollectionListJob.
*/
CollectionListJob();
/**
Destroys this job.
*/
virtual ~CollectionListJob();
/**
Returns a QHash that maps references to PIM::Collection objects.
*/
QHash<DataReference, Collection*> collections() const;
private:
virtual void doStart();
private slots:
// just for testing
void emitDone();
};
}
#endif
/*
Copyright (c) 2006 Volker Krause <volker.krause@rwth-aachen.de>
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 "collection.h"
#include "collectionfetchjob.h"
#include "collectionmodel.h"
#include "collectionmonitorjob.h"
#include <kdebug.h>
#include <kiconloader.h>
#include <kinstance.h>
#include <klocale.h>
#include <QHash>
#include <QPixmap>
#include <QQueue>
using namespace PIM;
class CollectionModel::Private
{
public:
QHash<DataReference, Collection*> collections;
QHash<DataReference, DataReference::List> childCollections;
CollectionMonitorJob *monitorJob;
CollectionFetchJob *fetchJob;
QQueue<DataReference> updateQueue;
};
PIM::CollectionModel::CollectionModel( const QHash< DataReference, Collection * > & collections,
QObject * parent ) :
QAbstractItemModel( parent ),
d( new Private() )
{
d->collections = collections;
d->fetchJob = 0;
// build child collection hash
foreach ( const Collection *col, d->collections )
d->childCollections[ col->parent() ].append( col->reference() );
// monitor collection changes
d->monitorJob = new CollectionMonitorJob();
connect( d->monitorJob, SIGNAL( collectionChanged( const DataReference& ) ), SLOT( collectionChanged( const DataReference& ) ) );
connect( d->monitorJob, SIGNAL( collectionRemoved( const DataReference& ) ), SLOT( collectionRemoved( const DataReference& ) ) );
d->monitorJob->start();
// ### Hack to get the kmail resource folder icons
KGlobal::instance()->iconLoader()->addAppDir( "kmail" );
}
PIM::CollectionModel::~CollectionModel()
{
d->childCollections.clear();
qDeleteAll( d->collections );
d->collections.clear();
delete d->monitorJob;
d->monitorJob = 0;
delete d->fetchJob;
d->fetchJob = 0;
delete d;
d = 0;
}
int PIM::CollectionModel::columnCount( const QModelIndex & parent ) const
{
Q_UNUSED( parent );
return 1;
}
QVariant PIM::CollectionModel::data( const QModelIndex & index, int role ) const
{
Collection *col = static_cast<Collection*>( index.internalPointer() );
if ( role == Qt::DisplayRole ) {
return col->name();
}
if ( role == Qt::DecorationRole ) {
if ( col->type() == "akonadi/resource" )
return SmallIcon( "server" );