22 #include "contactgroupeditor.h"
23 #include "contactgroupeditor_p.h"
25 #include "autoqpointer_p.h"
26 #include "contactgroupmodel_p.h"
27 #include "contactgroupeditordelegate_p.h"
28 #include "waitingoverlay_p.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/contactgroup.h>
39 #include <klocalizedstring.h>
40 #include <klineedit.h>
41 #include <kmessagebox.h>
42 #include <KColorScheme>
44 #include <QtCore/QTimer>
45 #include <QGridLayout>
46 #include <QMessageBox>
49 using namespace Akonadi;
52 : mParent( parent ), mMonitor( 0 ), mReadOnly( false )
56 ContactGroupEditor::Private::~Private()
61 void ContactGroupEditor::Private::adaptHeaderSizes()
63 mGui.membersView->header()->setDefaultSectionSize( mGui.membersView->header()->width() / 2 );
64 mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
67 void ContactGroupEditor::Private::itemFetchDone( KJob *job )
73 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
78 if ( fetchJob->items().isEmpty() ) {
82 mItem = fetchJob->items().first();
85 if ( mMode == ContactGroupEditor::EditMode ) {
89 Akonadi::CollectionFetchJob *collectionFetchJob =
new Akonadi::CollectionFetchJob( mItem.parentCollection(),
90 Akonadi::CollectionFetchJob::Base );
91 mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
92 SLOT(parentCollectionFetchDone(KJob*)) );
94 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
95 loadContactGroup( group );
97 setReadOnly( mReadOnly );
99 QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
103 void ContactGroupEditor::Private::parentCollectionFetchDone( KJob *job )
105 if ( job->error() ) {
109 Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
114 const Akonadi::Collection parentCollection = fetchJob->collections().first();
115 if ( parentCollection.isValid() ) {
116 mReadOnly = !( parentCollection.rights() & Collection::CanChangeItem );
119 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
120 loadContactGroup( group );
122 setReadOnly( mReadOnly );
124 QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
127 void ContactGroupEditor::Private::storeDone( KJob *job )
129 if ( job->error() ) {
130 emit mParent->error( job->errorString() );
134 if ( mMode == EditMode ) {
135 emit mParent->contactGroupStored( mItem );
136 }
else if ( mMode == CreateMode ) {
137 emit mParent->contactGroupStored( static_cast<ItemCreateJob*>( job )->item() );
141 void ContactGroupEditor::Private::itemChanged(
const Item&,
const QSet<QByteArray>& )
145 dlg->setInformativeText( i18n(
"The contact group has been changed by someone else.\nWhat should be done?" ) );
146 dlg->addButton( i18n(
"Take over changes" ), QMessageBox::AcceptRole );
147 dlg->addButton( i18n(
"Ignore and Overwrite changes" ), QMessageBox::RejectRole );
149 if ( dlg->exec() == QMessageBox::AcceptRole ) {
150 ItemFetchJob *job =
new ItemFetchJob( mItem );
151 job->fetchScope().fetchFullPayload();
152 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
154 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
159 void ContactGroupEditor::Private::loadContactGroup(
const KABC::ContactGroup &group )
161 mGui.groupName->setText( group.name() );
163 mGroupModel->loadContactGroup( group );
165 const QAbstractItemModel *model = mGui.membersView->model();
166 mGui.membersView->setCurrentIndex( model->index( model->rowCount() - 1, 0 ) );
168 if ( mMode == EditMode ) {
169 mGui.membersView->setFocus();
172 mGui.membersView->header()->resizeSections( QHeaderView::Stretch );
175 bool ContactGroupEditor::Private::storeContactGroup( KABC::ContactGroup &group )
177 if ( mGui.groupName->text().isEmpty() ) {
178 KMessageBox::error( mParent, i18n(
"The name of the contact group must not be empty." ) );
182 group.setName( mGui.groupName->text() );
184 if ( !mGroupModel->storeContactGroup( group ) ) {
185 KMessageBox::error( mParent, mGroupModel->lastErrorMessage() );
192 void ContactGroupEditor::Private::setupMonitor()
195 mMonitor =
new Monitor;
196 mMonitor->ignoreSession( Session::defaultSession() );
198 connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
199 mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
202 void ContactGroupEditor::Private::setReadOnly(
bool readOnly )
204 mGui.groupName->setReadOnly( readOnly );
205 mGui.membersView->setEnabled( !readOnly );
208 ContactGroupEditor::ContactGroupEditor(
Mode mode, QWidget *parent )
209 : QWidget( parent ), d( new Private( this ) )
212 d->mGui.setupUi(
this );
214 d->mGui.membersView->setEditTriggers( QAbstractItemView::AllEditTriggers );
216 d->mGroupModel =
new ContactGroupModel(
this );
217 d->mGui.membersView->setModel( d->mGroupModel );
218 d->mGui.membersView->setItemDelegate(
new ContactGroupEditorDelegate( d->mGui.membersView,
this ) );
221 KABC::ContactGroup dummyGroup;
222 d->mGroupModel->loadContactGroup( dummyGroup );
224 QTimer::singleShot( 0,
this, SLOT(adaptHeaderSizes()) );
225 QTimer::singleShot( 0, d->mGui.groupName, SLOT(setFocus()) );
228 d->mGui.membersView->header()->setStretchLastSection(
true );
239 Q_ASSERT_X(
false,
"ContactGroupEditor::loadContactGroup",
"You are calling loadContactGroup in CreateMode!" );
242 ItemFetchJob *job =
new ItemFetchJob( item );
243 job->fetchScope().fetchFullPayload();
244 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
246 connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
249 d->mMonitor->setItemMonitored( item );
257 if ( !d->mItem.isValid() ) {
261 if ( d->mReadOnly ) {
265 KABC::ContactGroup group = d->mItem.payload<KABC::ContactGroup>();
267 if ( !d->storeContactGroup( group ) ) {
271 d->mItem.setPayload<KABC::ContactGroup>( group );
273 ItemModifyJob *job =
new ItemModifyJob( d->mItem );
274 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
276 if ( !d->mDefaultCollection.isValid() ) {
277 const QStringList mimeTypeFilter( KABC::ContactGroup::mimeType() );
280 dlg->setMimeTypeFilter( mimeTypeFilter );
281 dlg->setAccessRightsFilter( Collection::CanCreateItem );
282 dlg->setCaption( i18n(
"Select Address Book" ) );
283 dlg->setDescription( i18n(
"Select the address book the new contact group shall be saved in:" ) );
285 if ( dlg->exec() == KDialog::Accepted ) {
292 KABC::ContactGroup group;
293 if ( !d->storeContactGroup( group ) ) {
298 item.setPayload<KABC::ContactGroup>( group );
299 item.setMimeType( KABC::ContactGroup::mimeType() );
301 ItemCreateJob *job =
new ItemCreateJob( item, d->mDefaultCollection );
302 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
310 d->mGroupModel->loadContactGroup( group );
311 d->mGui.membersView->header()->setDefaultSectionSize( d->mGui.membersView->header()->width() / 2 );
312 d->mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
317 d->mDefaultCollection = collection;
320 void ContactGroupEditor::groupNameIsValid(
bool isValid)
322 #ifndef QT_NO_STYLE_STYLESHEET
325 const KColorScheme::BackgroundRole bgColorScheme( KColorScheme::NegativeBackground );
326 KStatefulBrush bgBrush( KColorScheme::View, bgColorScheme );
327 styleSheet = QString::fromLatin1(
"QLineEdit{ background-color:%1 }" ).
328 arg( bgBrush.brush(
this ).color().name() );
330 d->mGui.groupName->setStyleSheet( styleSheet );
334 #include "moc_contactgroupeditor.cpp"
A QPointer which when destructed, deletes the object it points to.