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>
41#include <kmessagebox.h>
42#include <KColorScheme>
44#include <QtCore/QTimer>
50 : mParent( parent ), mMonitor( 0 ), mReadOnly( false ), mGroupModel( 0 )
54ContactGroupEditor::Private::~Private()
59void ContactGroupEditor::Private::adaptHeaderSizes()
61 mGui.membersView->header()->setDefaultSectionSize( mGui.membersView->header()->width() / 2 );
62 mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
65void ContactGroupEditor::Private::itemFetchDone( KJob *job )
71 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
76 if ( fetchJob->
items().isEmpty() ) {
80 mItem = fetchJob->
items().first();
83 if ( mMode == ContactGroupEditor::EditMode ) {
89 mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
90 SLOT(parentCollectionFetchDone(KJob*)) );
92 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
93 loadContactGroup( group );
95 setReadOnly( mReadOnly );
97 QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
101void ContactGroupEditor::Private::parentCollectionFetchDone( KJob *job )
103 if ( job->error() ) {
113 if ( parentCollection.
isValid() ) {
117 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
118 loadContactGroup( group );
120 setReadOnly( mReadOnly );
122 QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
125void ContactGroupEditor::Private::storeDone( KJob *job )
127 if ( job->error() ) {
128 emit mParent->error( job->errorString() );
132 if ( mMode == EditMode ) {
133 emit mParent->contactGroupStored( mItem );
134 }
else if ( mMode == CreateMode ) {
135 emit mParent->contactGroupStored(
static_cast<ItemCreateJob*
>( job )->item() );
139void ContactGroupEditor::Private::itemChanged(
const Item&,
const QSet<QByteArray>& )
143 dlg->setInformativeText( i18n(
"The contact group has been changed by someone else.\nWhat should be done?" ) );
144 dlg->addButton( i18n(
"Take over changes" ), QMessageBox::AcceptRole );
145 dlg->addButton( i18n(
"Ignore and Overwrite changes" ), QMessageBox::RejectRole );
147 if ( dlg->exec() == QMessageBox::AcceptRole ) {
152 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
157void ContactGroupEditor::Private::loadContactGroup(
const KABC::ContactGroup &group )
159 mGui.groupName->setText( group.name() );
161 mGroupModel->loadContactGroup( group );
163 const QAbstractItemModel *model = mGui.membersView->model();
164 mGui.membersView->setCurrentIndex( model->index( model->rowCount() - 1, 0 ) );
166 if ( mMode == EditMode ) {
167 mGui.membersView->setFocus();
170 mGui.membersView->header()->resizeSections( QHeaderView::Stretch );
173bool ContactGroupEditor::Private::storeContactGroup( KABC::ContactGroup &group )
175 if ( mGui.groupName->text().isEmpty() ) {
176 KMessageBox::error( mParent, i18n(
"The name of the contact group must not be empty." ) );
180 group.setName( mGui.groupName->text() );
182 if ( !mGroupModel->storeContactGroup( group ) ) {
183 KMessageBox::error( mParent, mGroupModel->lastErrorMessage() );
190void ContactGroupEditor::Private::setupMonitor()
196 connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
197 mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
200void ContactGroupEditor::Private::setReadOnly(
bool readOnly )
202 mGui.groupName->setReadOnly( readOnly );
203 mGui.membersView->setEnabled( !readOnly );
206ContactGroupEditor::ContactGroupEditor(
Mode mode, QWidget *parent )
207 : QWidget( parent ), d( new Private( this ) )
210 d->mGui.setupUi(
this );
212 d->mGui.membersView->setEditTriggers( QAbstractItemView::AllEditTriggers );
214 d->mGroupModel =
new ContactGroupModel(
this );
215 d->mGui.membersView->setModel( d->mGroupModel );
216 d->mGui.membersView->setItemDelegate(
new ContactGroupEditorDelegate( d->mGui.membersView,
this ) );
219 KABC::ContactGroup dummyGroup;
220 d->mGroupModel->loadContactGroup( dummyGroup );
222 QTimer::singleShot( 0,
this, SLOT(adaptHeaderSizes()) );
223 QTimer::singleShot( 0, d->mGui.groupName, SLOT(setFocus()) );
226 d->mGui.membersView->header()->setStretchLastSection(
true );
237 Q_ASSERT_X(
false,
"ContactGroupEditor::loadContactGroup",
"You are calling loadContactGroup in CreateMode!" );
244 connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
247 d->mMonitor->setItemMonitored( item );
255 if ( !d->mItem.isValid() ) {
259 if ( d->mReadOnly ) {
263 KABC::ContactGroup group = d->mItem.payload<KABC::ContactGroup>();
265 if ( !d->storeContactGroup( group ) ) {
269 d->mItem.setPayload<KABC::ContactGroup>( group );
272 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
274 if ( !d->mDefaultCollection.isValid() ) {
275 const QStringList mimeTypeFilter( KABC::ContactGroup::mimeType() );
278 dlg->setMimeTypeFilter( mimeTypeFilter );
280 dlg->setCaption( i18n(
"Select Address Book" ) );
281 dlg->setDescription( i18n(
"Select the address book the new contact group shall be saved in:" ) );
283 if ( dlg->exec() == KDialog::Accepted ) {
290 KABC::ContactGroup group;
291 if ( !d->storeContactGroup( group ) ) {
296 item.setPayload<KABC::ContactGroup>( group );
297 item.setMimeType( KABC::ContactGroup::mimeType() );
300 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
308 d->mGroupModel->loadContactGroup( group );
309 d->mGui.membersView->header()->setDefaultSectionSize( d->mGui.membersView->header()->width() / 2 );
310 d->mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
315 d->mDefaultCollection = collection;
318void ContactGroupEditor::groupNameIsValid(
bool isValid)
320#ifndef QT_NO_STYLE_STYLESHEET
323 const KColorScheme::BackgroundRole bgColorScheme( KColorScheme::NegativeBackground );
324 KStatefulBrush bgBrush( KColorScheme::View, bgColorScheme );
325 styleSheet = QString::fromLatin1(
"QLineEdit{ background-color:%1 }" ).
326 arg( bgBrush.brush(
this ).color().name() );
328 d->mGui.groupName->setStyleSheet( styleSheet );
332#include "moc_contactgroupeditor.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 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.