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>
42 #include <QVBoxLayout>
43 #include <QMessageBox>
45 using namespace Akonadi;
47 class ContactEditor::Private
51 : mParent( parent ), mMode( mode ), mMonitor( 0 ), mReadOnly( false )
54 mEditorWidget = editorWidget;
55 #ifndef DISABLE_EDITOR_WIDGETS
72 void itemFetchDone( KJob* );
73 void parentCollectionFetchDone( KJob* );
74 void storeDone( KJob* );
85 Akonadi::Monitor *mMonitor;
86 Akonadi::Collection mDefaultCollection;
87 AbstractContactEditorWidget *mEditorWidget;
91 void ContactEditor::Private::itemFetchDone( KJob *job )
93 if ( job->error() != KJob::NoError ) {
97 Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
102 if ( fetchJob->items().isEmpty() ) {
106 mItem = fetchJob->items().first();
113 Akonadi::CollectionFetchJob *collectionFetchJob =
new Akonadi::CollectionFetchJob( mItem.parentCollection(),
114 Akonadi::CollectionFetchJob::Base );
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 );
125 void ContactEditor::Private::parentCollectionFetchDone( KJob *job )
127 if ( job->error() ) {
131 Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
136 const Akonadi::Collection parentCollection = fetchJob->collections().first();
137 if ( parentCollection.isValid() ) {
138 mReadOnly = !( parentCollection.rights() & Collection::CanChangeItem );
141 mEditorWidget->setReadOnly( mReadOnly );
143 const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
144 mContactMetaData.load( mItem );
145 loadContact( addr, mContactMetaData );
148 void 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 ) {
159 emit mParent->contactStored( static_cast<Akonadi::ItemCreateJob*>( job )->item() );
161 emit mParent->finished();
164 void ContactEditor::Private::itemChanged(
const Akonadi::Item&,
const QSet<QByteArray>& )
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 ) {
173 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mItem );
174 job->fetchScope().fetchFullPayload();
176 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
178 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
184 void ContactEditor::Private::loadContact(
const KABC::Addressee &addr,
const ContactMetaData &metaData )
186 mEditorWidget->loadContact( addr, metaData );
189 void ContactEditor::Private::storeContact( KABC::Addressee &addr,
ContactMetaData &metaData )
191 mEditorWidget->storeContact( addr, metaData );
194 void ContactEditor::Private::setupMonitor()
197 mMonitor =
new Akonadi::Monitor;
198 mMonitor->ignoreSession( Akonadi::Session::defaultSession() );
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!" );
230 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( item );
231 job->fetchScope().fetchFullPayload();
233 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
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 );
264 Akonadi::ItemModifyJob *job =
new Akonadi::ItemModifyJob( d->mItem );
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 );
272 dlg->setAccessRightsFilter( Collection::CanCreateItem );
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 );
291 Akonadi::ItemCreateJob *job =
new Akonadi::ItemCreateJob( item, d->mDefaultCollection );
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 );
315 Akonadi::ItemModifyJob *job =
new Akonadi::ItemModifyJob( d->mItem );
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 );
323 dlg->setAccessRightsFilter( Collection::CanCreateItem );
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 );
342 Akonadi::ItemCreateJob *job =
new Akonadi::ItemCreateJob( item, d->mDefaultCollection );
343 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
351 d->loadContact( contact, d->mContactMetaData );
356 d->mDefaultCollection = collection;
359 #include "moc_contacteditor.cpp"
void setMargin(int margin)
A QPointer which when destructed, deletes the object it points to.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSpacing(int spacing)