Commit 61b5a90e authored by Tobias Koenig's avatar Tobias Koenig
Browse files

Allow to select all contacts from a given address book

or address book folder.

BUG: 206299

svn path=/trunk/KDE/kdepim/kaddressbook/; revision=1021561
parent 0ad16c9c
......@@ -45,6 +45,7 @@ set( kaddressbook_LIB_SRCS
mainwidget.cpp
modelcolumnmanager.cpp
quicksearchwidget.cpp
recursiveitemfetchjob.cpp
standardcontactactionmanager.cpp
xxport/csv/csv_xxport.cpp
xxport/csv/csvimportdialog.cpp
......
......@@ -55,10 +55,14 @@ void ContactSelectionDialog::slotSelectedContacts( const KABC::Addressee::List &
accept();
}
void ContactSelectionDialog::slotOkClicked()
void ContactSelectionDialog::slotButtonClicked( int button )
{
// this will trigger slotSelectedContacts via signal/slot
mSelectionWidget->requestSelectedContacts();
if ( button == KDialog::Ok ) {
// this will trigger slotSelectedContacts via signal/slot
mSelectionWidget->requestSelectedContacts();
} else {
KDialog::slotButtonClicked( button );
}
}
#include "contactselectiondialog.moc"
......@@ -57,9 +57,11 @@ class ContactSelectionDialog : public KDialog
*/
KABC::Addressee::List selectedContacts() const;
protected Q_SLOTS:
void slotButtonClicked( int button );
private Q_SLOTS:
void slotSelectedContacts( const KABC::Addressee::List& );
void slotOkClicked();
private:
ContactSelectionWidget *mSelectionWidget;
......
......@@ -19,8 +19,11 @@
#include "contactselectionwidget.h"
#include "recursiveitemfetchjob.h"
#include <akonadi/contact/addressbookcombobox.h>
#include <akonadi/entitytreemodel.h>
#include <akonadi/itemfetchscope.h>
#include <klocale.h>
#include <QtCore/QAbstractItemModel>
......@@ -40,9 +43,6 @@ ContactSelectionWidget::ContactSelectionWidget( QAbstractItemModel *model, QItem
mSelectedContactsButton->setEnabled( mSelectionModel->hasSelection() );
mAddressBookSelection->setEnabled( false );
// disable until we can fetch a collection recursivly
mAddressBookContactsButton->setEnabled( false );
connect( mAddressBookContactsButton, SIGNAL( toggled( bool ) ),
mAddressBookSelection, SLOT( setEnabled( bool ) ) );
......@@ -136,7 +136,35 @@ void ContactSelectionWidget::collectSelectedContacts()
void ContactSelectionWidget::collectAddressBookContacts()
{
const Akonadi::Collection collection = mAddressBookSelection->selectedAddressBook();
if ( !collection.isValid() ) {
emit selectedContacts( KABC::Addressee::List() );
return;
}
Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob( collection, this );
job->fetchScope().fetchFullPayload();
connect( job, SIGNAL( result( KJob* ) ), SLOT( addressBookContactsFetched( KJob* ) ) );
job->start();
}
void ContactSelectionWidget::addressBookContactsFetched( KJob *job )
{
KABC::Addressee::List contacts;
if ( !job->error() ) {
Akonadi::RecursiveItemFetchJob *fetchJob = qobject_cast<Akonadi::RecursiveItemFetchJob*>( job );
const Akonadi::Item::List items = fetchJob->items();
foreach ( const Akonadi::Item &item, items ) {
if ( item.hasPayload<KABC::Addressee>() ) {
contacts.append( item.payload<KABC::Addressee>() );
}
}
}
emit selectedContacts( contacts );
}
#include "contactselectionwidget.moc"
......@@ -29,6 +29,8 @@ class QItemSelectionModel;
class QLabel;
class QRadioButton;
class KJob;
namespace Akonadi
{
class AddressBookComboBox;
......@@ -71,6 +73,9 @@ class ContactSelectionWidget : public QWidget
*/
void selectedContacts( const KABC::Addressee::List &contacts );
private Q_SLOTS:
void addressBookContactsFetched( KJob* );
private:
void initGui();
......
/*
Copyright (c) 2009 Tobias Koenig <tokoe@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 "recursiveitemfetchjob.h"
#include <akonadi/collectionfetchjob.h>
#include <akonadi/itemfetchjob.h>
#include <akonadi/itemfetchscope.h>
#include <QtCore/QVariant>
using namespace Akonadi;
class RecursiveItemFetchJob::Private
{
public:
Private( const Collection &collection, RecursiveItemFetchJob *parent )
: mParent( parent ), mCollection( collection ), mFetchCount( 0 )
{
}
void collectionFetchResult( KJob *job )
{
if ( job->error() ) {
mParent->emitResult();
return;
}
const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob*>( job );
Collection::List collections = fetchJob->collections();
collections.prepend( mCollection );
foreach ( const Collection &collection, collections ) {
ItemFetchJob *itemFetchJob = new ItemFetchJob( collection, mParent );
itemFetchJob->setFetchScope( mFetchScope );
mParent->connect( itemFetchJob, SIGNAL( result( KJob* ) ),
mParent, SLOT( itemFetchResult( KJob* ) ) );
mFetchCount++;
}
}
void itemFetchResult( KJob *job )
{
if ( !job->error() ) {
const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
mItems << fetchJob->items();
}
mFetchCount--;
if ( mFetchCount == 0 )
mParent->emitResult();
}
RecursiveItemFetchJob *mParent;
Collection mCollection;
Item::List mItems;
ItemFetchScope mFetchScope;
int mFetchCount;
};
RecursiveItemFetchJob::RecursiveItemFetchJob( const Collection &collection, QObject * parent )
: KJob( parent ), d( new Private( collection, this ) )
{
}
RecursiveItemFetchJob::~RecursiveItemFetchJob()
{
delete d;
}
void RecursiveItemFetchJob::setFetchScope( const ItemFetchScope &fetchScope )
{
d->mFetchScope = fetchScope;
}
ItemFetchScope &RecursiveItemFetchJob::fetchScope()
{
return d->mFetchScope;
}
void RecursiveItemFetchJob::start()
{
CollectionFetchJob *job = new CollectionFetchJob( d->mCollection, CollectionFetchJob::Recursive, this );
connect( job, SIGNAL( result( KJob* ) ), this, SLOT( collectionFetchResult( KJob* ) ) );
}
Akonadi::Item::List RecursiveItemFetchJob::items() const
{
return d->mItems;
}
#include "recursiveitemfetchjob.moc"
/*
Copyright (c) 2009 Tobias Koenig <tokoe@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 AKONADI_RECURSIVEITEMFETCHJOB_H
#define AKONADI_RECURSIVEITEMFETCHJOB_H
#include <akonadi/item.h>
#include <kjob.h>
namespace Akonadi {
class Collection;
class ItemFetchScope;
/**
* @short Job that fetches all items of a collection recursive.
*
* This job takes a collection as argument and returns a list of
* all items that are part of the passed collection and its child
* collections. The parts of the items that shall be fetched can
* by specified via an ItemFetchScope.
*
* Example:
*
* @code
*
* // Assume the following Akonadi storage tree structure:
* //
* // Root Collection
* // |
* // +- Contacts
* // | |
* // | +- Private
* // | |
* // | `- Business
* // |
* // `- Events
* //
* // Collection 'Contacts' has the ID 15, then the following code
* // returns all items from 'Contacts', 'Private' and 'Business'.
*
* const Akonadi::Collection contactsCollection( 15 );
*
* Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob( contactsCollection );
* job->fetchScope().fetchFullPayload();
* connect( job, SIGNAL( result( KJob* ) ), this, SLOT( fetchResult( KJob* ) ) );
*
* job->start();
*
* ...
*
* MyClass::fetchResult( KJob *job )
* {
* Akonadi::RecursiveItemFetchJob *fetchJob = qobject_cast<Akonadi::RecursiveItemFetchJob*>( job );
* const Akonadi::Item::List items = fetchJob->items();
* // do something with the items
* }
*
* @endcode
*
* @author Tobias Koenig <tokoe@kde.org>
*/
class RecursiveItemFetchJob : public KJob
{
Q_OBJECT
public:
/**
* Creates a new recursive item fetch job.
*
* @param collection The collection that shall be fetched recursive.
* @param parent The parent object.
*/
explicit RecursiveItemFetchJob( const Akonadi::Collection &collection, QObject *parent = 0 );
/**
* Destroys the recursive item fetch job.
*/
~RecursiveItemFetchJob();
/**
* Sets the item fetch scope.
*
* The ItemFetchScope controls how much of an item's data is fetched
* from the server, e.g. whether to fetch the full item payload or
* only meta data.
*
* @param fetchScope The new scope for item fetch operations.
*
* @see fetchScope()
*/
void setFetchScope( const Akonadi::ItemFetchScope &fetchScope );
/**
* Returns the item fetch scope.
*
* Since this returns a reference it can be used to conveniently modify the
* current scope in-place, i.e. by calling a method on the returned reference
* without storing it in a local variable. See the ItemFetchScope documentation
* for an example.
*
* @return a reference to the current item fetch scope
*
* @see setFetchScope() for replacing the current item fetch scope
*/
Akonadi::ItemFetchScope &fetchScope();
/**
* Returns the list of fetched items.
*/
Akonadi::Item::List items() const;
/**
* Starts the recursive item fetch job.
*/
virtual void start();
private:
//@cond PRIVATE
class Private;
Private* const d;
Q_PRIVATE_SLOT( d, void collectionFetchResult( KJob* ) )
Q_PRIVATE_SLOT( d, void itemFetchResult( KJob* ) )
//@endcond
};
}
#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