22#include "contacteditor.h"
24#include "abstractcontacteditorwidget_p.h"
25#include "autoqpointer_p.h"
26#include "contactmetadata_p.h"
27#include "contactmetadataattribute_p.h"
28#include "editor/contacteditorwidget.h"
30#include <akonadi/collectiondialog.h>
31#include <akonadi/collectionfetchjob.h>
32#include <akonadi/itemcreatejob.h>
33#include <akonadi/itemfetchjob.h>
34#include <akonadi/itemfetchscope.h>
35#include <akonadi/itemmodifyjob.h>
36#include <akonadi/monitor.h>
37#include <akonadi/session.h>
38#include <kabc/addressee.h>
39#include <klocalizedstring.h>
41#include <QtCore/QPointer>
47class ContactEditor::Private
51 : mParent( parent ), mMode( mode ), mMonitor( 0 ), mReadOnly( false )
54 mEditorWidget = editorWidget;
55#ifndef DISABLE_EDITOR_WIDGETS
61 QVBoxLayout *layout =
new QVBoxLayout( mParent );
62 layout->setMargin( 0 );
63 layout->setSpacing( 0 );
64 layout->addWidget( mEditorWidget );
72 void itemFetchDone( KJob* );
73 void parentCollectionFetchDone( KJob* );
74 void storeDone( KJob* );
75 void itemChanged(
const Akonadi::Item &item,
const QSet<QByteArray>& );
77 void loadContact(
const KABC::Addressee &addr,
const ContactMetaData &metaData );
87 AbstractContactEditorWidget *mEditorWidget;
91void ContactEditor::Private::itemFetchDone( KJob *job )
93 if ( job->error() != KJob::NoError ) {
102 if ( fetchJob->
items().isEmpty() ) {
106 mItem = fetchJob->
items().first();
115 mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
116 SLOT(parentCollectionFetchDone(KJob*)) );
118 const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
119 mContactMetaData.
load( mItem );
120 loadContact( addr, mContactMetaData );
121 mEditorWidget->setReadOnly( mReadOnly );
125void ContactEditor::Private::parentCollectionFetchDone( KJob *job )
127 if ( job->error() ) {
137 if ( parentCollection.
isValid() ) {
141 mEditorWidget->setReadOnly( mReadOnly );
143 const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
144 mContactMetaData.load( mItem );
145 loadContact( addr, mContactMetaData );
148void ContactEditor::Private::storeDone( KJob *job )
150 if ( job->error() != KJob::NoError ) {
151 emit mParent->error( job->errorString() );
152 emit mParent->finished();
156 if ( mMode == EditMode ) {
157 emit mParent->contactStored( mItem );
158 }
else if ( mMode == CreateMode ) {
161 emit mParent->finished();
164void ContactEditor::Private::itemChanged(
const Akonadi::Item&,
const QSet<QByteArray>& )
166 QPointer<QMessageBox> dlg =
new QMessageBox( mParent );
168 dlg->setInformativeText( i18n(
"The contact has been changed by someone else.\nWhat should be done?" ) );
169 dlg->addButton( i18n(
"Take over changes" ), QMessageBox::AcceptRole );
170 dlg->addButton( i18n(
"Ignore and Overwrite changes" ), QMessageBox::RejectRole );
172 if ( dlg->exec() == QMessageBox::AcceptRole ) {
178 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
184void ContactEditor::Private::loadContact(
const KABC::Addressee &addr,
const ContactMetaData &metaData )
186 mEditorWidget->loadContact( addr, metaData );
189void ContactEditor::Private::storeContact( KABC::Addressee &addr,
ContactMetaData &metaData )
191 mEditorWidget->storeContact( addr, metaData );
194void ContactEditor::Private::setupMonitor()
200 connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
201 mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
205 : QWidget( parent ), d( new Private( mode, FullMode, 0, this ) )
210 : QWidget( parent ), d( new Private( mode, FullMode, editorWidget, this ) )
215 : QWidget( parent ), d( new Private( mode, displayMode, 0, this ) )
227 Q_ASSERT_X(
false,
"ContactEditor::loadContact",
"You are calling loadContact in CreateMode!" );
235 connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
238 d->mMonitor->setItemMonitored( item );
243 KABC::Addressee addr;
244 d->storeContact( addr, d->mContactMetaData );
251 if ( !d->mItem.isValid() || d->mReadOnly ) {
256 KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
258 d->storeContact( addr, d->mContactMetaData );
260 d->mContactMetaData.store( d->mItem );
262 d->mItem.setPayload<KABC::Addressee>( addr );
265 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
267 if ( !d->mDefaultCollection.isValid() ) {
268 const QStringList mimeTypeFilter( KABC::Addressee::mimeType() );
271 dlg->setMimeTypeFilter( mimeTypeFilter );
273 dlg->setCaption( i18n(
"Select Address Book" ) );
274 dlg->setDescription( i18n(
"Select the address book the new contact shall be saved in:" ) );
275 if ( dlg->exec() == KDialog::Accepted ) {
282 KABC::Addressee addr;
283 d->storeContact( addr, d->mContactMetaData );
286 item.setPayload<KABC::Addressee>( addr );
287 item.setMimeType( KABC::Addressee::mimeType() );
289 d->mContactMetaData.store( item );
292 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
299 if ( !d->mItem.isValid() ) {
303 if ( d->mReadOnly ) {
307 KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
309 d->storeContact( addr, d->mContactMetaData );
311 d->mContactMetaData.store( d->mItem );
313 d->mItem.setPayload<KABC::Addressee>( addr );
316 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
318 if ( !d->mDefaultCollection.isValid() ) {
319 const QStringList mimeTypeFilter( KABC::Addressee::mimeType() );
322 dlg->setMimeTypeFilter( mimeTypeFilter );
324 dlg->setCaption( i18n(
"Select Address Book" ) );
325 dlg->setDescription( i18n(
"Select the address book the new contact shall be saved in:" ) );
326 if ( dlg->exec() == KDialog::Accepted ) {
333 KABC::Addressee addr;
334 d->storeContact( addr, d->mContactMetaData );
337 item.setPayload<KABC::Addressee>( addr );
338 item.setMimeType( KABC::Addressee::mimeType() );
340 d->mContactMetaData.store( item );
343 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
351 d->loadContact(
contact, d->mContactMetaData );
356 d->mDefaultCollection = collection;
359#include "moc_contacteditor.cpp"
A collection selection dialog.
Job that fetches collections from the Akonadi storage.
@ Base
Only fetch the base collection.
Collection::List collections() const
Returns the list of fetched collection.
Represents a collection of PIM items.
Rights rights() const
Returns the rights the user has on the collection.
@ CanCreateItem
Can create new items in this collection.
@ CanChangeItem
Can change items in this collection.
bool isValid() const
Returns whether the entity is valid.
Job that creates a new item in the Akonadi storage.
Job that fetches items from the Akonadi storage.
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Item::List items() const
Returns the fetched items.
void fetchAttribute(const QByteArray &type, bool fetch=true)
Sets whether the attribute of the given type should be fetched.
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval.
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
@ Parent
Only retrieve the immediate parent collection.
Job that modifies an existing item in the Akonadi storage.
Monitors an item or collection for changes.
void ignoreSession(Session *session)
Ignores all change notifications caused by the given session.
static Session * defaultSession()
Returns the default session for this thread.
A QPointer which when destructed, deletes the object it points to.
FreeBusyManager::Singleton.