Commit 3549b9ce authored by Volker Krause's avatar Volker Krause
Browse files

Implement creating default Kolab folders.

BUG: 251590

svn path=/trunk/KDE/kdepim/runtime/; revision=1180665
parent ef4af495
......@@ -26,6 +26,8 @@ set( kolabproxyresource_shared_SRCS
journalhandler.cpp
notehandler.cpp
note.cpp
setupdefaultfoldersjob.cpp
kolabdefs.cpp
)
set( kolabproxyresource_SRCS
......
/*
Copyright (c) 2010 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 "kolabdefs.h"
#include <klocale.h>
#include <boost/static_assert.hpp>
#include <sys/stat.h>
using namespace Kolab;
static const struct {
const char *name;
const char *label;
} folderTypeData[] = {
{ KOLAB_FOLDER_TYPE_MAIL, "" },
{ KOLAB_FOLDER_TYPE_CONTACT, I18N_NOOP( "Contacts" ) },
{ KOLAB_FOLDER_TYPE_EVENT, I18N_NOOP( "Calendar" ) },
{ KOLAB_FOLDER_TYPE_TASK, I18N_NOOP( "Tasks" ) },
{ KOLAB_FOLDER_TYPE_JOURNAL, I18N_NOOP( "Journal" ) },
{ KOLAB_FOLDER_TYPE_NOTE, I18N_NOOP( "Notes" ) }
};
static const int numFolderTypeData = sizeof folderTypeData / sizeof *folderTypeData;
BOOST_STATIC_ASSERT( numFolderTypeData == Kolab::FolderTypeSize );
FolderType Kolab::folderTypeFromString(const QByteArray& folderTypeName)
{
if ( folderTypeName == KOLAB_FOLDER_TYPE_CONTACT || folderTypeName == KOLAB_FOLDER_TYPE_CONTACT KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX )
return Kolab::Contact;
if ( folderTypeName == KOLAB_FOLDER_TYPE_EVENT || folderTypeName == KOLAB_FOLDER_TYPE_EVENT KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX )
return Kolab::Event;
if ( folderTypeName == KOLAB_FOLDER_TYPE_TASK || folderTypeName == KOLAB_FOLDER_TYPE_TASK KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX )
return Kolab::Task;
if ( folderTypeName == KOLAB_FOLDER_TYPE_JOURNAL || folderTypeName == KOLAB_FOLDER_TYPE_JOURNAL KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX )
return Kolab::Journal;
if ( folderTypeName == KOLAB_FOLDER_TYPE_NOTE || folderTypeName == KOLAB_FOLDER_TYPE_NOTE KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX )
return Kolab::Note;
return Kolab::Mail;
}
QByteArray Kolab::folderTypeToString(FolderType type, bool isDefault )
{
Q_ASSERT( type >= 0 && type < FolderTypeSize );
QByteArray result = folderTypeData[ type ].name;
if ( isDefault )
result += KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX;
return result;
}
Kolab::FolderType Kolab::guessFolderTypeFromName(const QString& name)
{
for ( int i = 0; i < numFolderTypeData; ++i ) {
if ( name == i18n( folderTypeData[ i ].label ) || name == QString::fromLatin1( folderTypeData[ i ].label ) )
return static_cast<FolderType>( i );
}
return Kolab::Mail;
}
QString Kolab::nameForFolderType(FolderType type)
{
Q_ASSERT( type >= 0 && type < FolderTypeSize );
return i18n( folderTypeData[ type ].label );
}
/*
Copyright (c) 2010 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 KOLABDEFS_H
#define KOLABDEFS_H
#include <QtCore/QByteArray>
#define KOLAB_FOLDER_TYPE_MAIL "mail"
#define KOLAB_FOLDER_TYPE_CONTACT "contact"
#define KOLAB_FOLDER_TYPE_EVENT "event"
#define KOLAB_FOLDER_TYPE_TASK "task"
#define KOLAB_FOLDER_TYPE_JOURNAL "journal"
#define KOLAB_FOLDER_TYPE_NOTE "note"
#define KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX ".default"
#define KOLAB_FOLDER_TYPE_ANNOTATION "/vendor/kolab/folder-type"
namespace Kolab
{
enum FolderType {
Mail = 0,
Contact,
Event,
Task,
Journal,
Note,
FolderTypeSize = Note + 1,
};
FolderType folderTypeFromString( const QByteArray &folderTypeName );
QByteArray folderTypeToString( FolderType type, bool isDefault = false );
FolderType guessFolderTypeFromName( const QString &name );
QString nameForFolderType( FolderType type );
}
#endif
/*
Copyright (c) 2010 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 "setupdefaultfoldersjob.h"
#include "kolabdefs.h"
#include <akonadi/collectionfetchjob.h>
#include <akonadi/collectionfetchscope.h>
#include <akonadi/entitydisplayattribute.h>
#include <collectionannotationsattribute.h>
#include <akonadi/collectioncreatejob.h>
#include <akonadi/collectionmodifyjob.h>
using namespace Akonadi;
SetupDefaultFoldersJob::SetupDefaultFoldersJob(const Akonadi::AgentInstance& instance, QObject* parent): Job( parent ),
m_agentInstance( instance )
{
Q_ASSERT( instance.isValid() );
}
void SetupDefaultFoldersJob::doStart()
{
CollectionFetchJob *job = new CollectionFetchJob( Collection::root(), CollectionFetchJob::Recursive, this );
job->fetchScope().setResource( m_agentInstance.identifier() );
connect( job, SIGNAL(result(KJob*)), SLOT(collectionFetchResult(KJob*)) );
}
void SetupDefaultFoldersJob::collectionFetchResult(KJob* job)
{
if ( job->error() )
return; // Akonadi::Job propagates that automatically
// look for inbox
Collection defaultParent;
foreach ( const Collection &col, static_cast<CollectionFetchJob*>( job )->collections() ) {
EntityDisplayAttribute* attr = 0;
if ( (attr = col.attribute<EntityDisplayAttribute>()) ) {
if ( attr->iconName() == QLatin1String( "mail-folder-inbox" ) ) {
defaultParent = col;
}
}
}
if ( !defaultParent.isValid() ) {
// TODO: implement me
kWarning() << "No inbox found!";
}
// look for existing folders
QVector<Collection> existingDefaultFolders( Kolab::FolderTypeSize );
QVector<Collection> recoveryCandidates( Kolab::FolderTypeSize );
foreach ( const Collection &col, static_cast<CollectionFetchJob*>( job )->collections() ) {
if ( col.parentCollection() != defaultParent )
continue;
Kolab::FolderType folderType = Kolab::Mail;
CollectionAnnotationsAttribute *attr = 0;
if ( (attr = col.attribute<CollectionAnnotationsAttribute>()) ) {
folderType = Kolab::folderTypeFromString( attr->annotations().value( KOLAB_FOLDER_TYPE_ANNOTATION ) );
}
Kolab::FolderType guessedType = Kolab::guessFolderTypeFromName( col.name() );
if ( folderType != Kolab::Mail )
existingDefaultFolders[ folderType ] = col;
else if ( guessedType != Kolab::Mail )
recoveryCandidates[ guessedType ] = col;
}
// create/fix folders
for ( int i = Kolab::Contact; i < Kolab::FolderTypeSize; ++i ) {
if ( existingDefaultFolders[ i ].isValid() ) {
continue; // all good
} else if ( recoveryCandidates[ i ].isValid() ) {
Collection col = recoveryCandidates[ i ];
CollectionAnnotationsAttribute* attr = col.attribute<CollectionAnnotationsAttribute>( Entity::AddIfMissing );
QMap<QByteArray, QByteArray> annotations;
annotations.insert( KOLAB_FOLDER_TYPE_ANNOTATION, Kolab::folderTypeToString( static_cast<Kolab::FolderType>( i ), true ) );
attr->setAnnotations( annotations );
new CollectionModifyJob( col, 0 );
} else {
Collection col;
col.setName( Kolab::nameForFolderType( static_cast<Kolab::FolderType>( i ) ) );
col.setParentCollection( defaultParent );
CollectionAnnotationsAttribute* attr = col.attribute<CollectionAnnotationsAttribute>( Entity::AddIfMissing );
QMap<QByteArray, QByteArray> annotations;
annotations.insert( KOLAB_FOLDER_TYPE_ANNOTATION, Kolab::folderTypeToString( static_cast<Kolab::FolderType>( i ), true ) );
attr->setAnnotations( annotations );
new CollectionCreateJob( col, 0 );
}
}
emitResult();
}
#include "setupdefaultfoldersjob.moc"
/*
Copyright (c) 2010 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 SETUPDEFAULTFOLDERSJOB_H
#define SETUPDEFAULTFOLDERSJOB_H
#include <akonadi/job.h>
#include <akonadi/agentinstance.h>
class SetupDefaultFoldersJob : public Akonadi::Job
{
Q_OBJECT
public:
explicit SetupDefaultFoldersJob( const Akonadi::AgentInstance &instance, QObject *parent = 0 );
protected:
void doStart();
private slots:
void collectionFetchResult( KJob *job );
private:
Akonadi::AgentInstance m_agentInstance;
};
#endif
......@@ -28,12 +28,12 @@
#include <akonadi/collectioncreatejob.h>
#include <akonadi/entitydisplayattribute.h>
#include "collectionannotationsattribute.h"
#include "setupdefaultfoldersjob.h"
#define IMAP_RESOURCE_IDENTIFIER "akonadi_imap_resource"
#define KOLAB_FOLDERTYPE "/vendor/kolab/folder-type"
SetupKolab::SetupKolab( KolabProxyResource* parentResource,WId parent )
:KDialog(),
m_ui(new Ui::SetupKolabView),
......@@ -105,6 +105,7 @@ void SetupKolab::slotCreateDefaultKolabCollections()
{
const Akonadi::AgentInstance instanceSelected = m_agentList[m_ui->imapAccountComboBox->currentText()];
if ( instanceSelected.isValid() ) {
new SetupDefaultFoldersJob( instanceSelected, this );
#if 0
Akonadi::Collection collection;
collection.setName( "Calendar" );
......
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