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>
45 #include <QtCore/QTimer>
46 #include <QGridLayout>
47 #include <QMessageBox>
50 using namespace Akonadi;
53 : mParent( parent ), mMonitor( 0 ), mReadOnly( false )
57 ContactGroupEditor::Private::~Private()
62 void ContactGroupEditor::Private::adaptHeaderSizes()
64 mGui.membersView->header()->setDefaultSectionSize( mGui.membersView->header()->width() / 2 );
65 mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
68 void ContactGroupEditor::Private::itemFetchDone( KJob *job )
79 if ( fetchJob->
items().isEmpty() ) {
83 mItem = fetchJob->
items().first();
86 if ( mMode == ContactGroupEditor::EditMode ) {
92 mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
93 SLOT(parentCollectionFetchDone(KJob*)) );
95 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
96 loadContactGroup( group );
98 setReadOnly( mReadOnly );
100 QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
104 void ContactGroupEditor::Private::parentCollectionFetchDone( KJob *job )
106 if ( job->error() ) {
116 if ( parentCollection.
isValid() ) {
117 mReadOnly = !( parentCollection.
rights() & Collection::CanChangeItem );
120 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
121 loadContactGroup( group );
123 setReadOnly( mReadOnly );
125 QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
128 void ContactGroupEditor::Private::storeDone( KJob *job )
130 if ( job->error() ) {
131 emit mParent->error( job->errorString() );
135 if ( mMode == EditMode ) {
136 emit mParent->contactGroupStored( mItem );
137 }
else if ( mMode == CreateMode ) {
138 emit mParent->contactGroupStored( static_cast<ItemCreateJob*>( job )->item() );
142 void ContactGroupEditor::Private::itemChanged(
const Item&,
const QSet<QByteArray>& )
146 dlg->setInformativeText( i18n(
"The contact group has been changed by someone else.\nWhat should be done?" ) );
147 dlg->addButton( i18n(
"Take over changes" ), QMessageBox::AcceptRole );
148 dlg->addButton( i18n(
"Ignore and Overwrite changes" ), QMessageBox::RejectRole );
150 if ( dlg->exec() == QMessageBox::AcceptRole ) {
155 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
160 void ContactGroupEditor::Private::loadContactGroup(
const KABC::ContactGroup &group )
162 mGui.groupName->setText( group.name() );
164 mGroupModel->loadContactGroup( group );
166 const QAbstractItemModel *model = mGui.membersView->model();
167 mGui.membersView->setCurrentIndex( model->index( model->rowCount() - 1, 0 ) );
169 if ( mMode == EditMode ) {
170 mGui.membersView->setFocus();
173 mGui.membersView->header()->resizeSections( QHeaderView::Stretch );
176 bool ContactGroupEditor::Private::storeContactGroup( KABC::ContactGroup &group )
178 if ( mGui.groupName->text().isEmpty() ) {
179 KMessageBox::error( mParent, i18n(
"The name of the contact group must not be empty." ) );
183 group.setName( mGui.groupName->text() );
185 if ( !mGroupModel->storeContactGroup( group ) ) {
186 KMessageBox::error( mParent, mGroupModel->lastErrorMessage() );
193 void ContactGroupEditor::Private::setupMonitor()
199 connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
200 mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
203 void ContactGroupEditor::Private::setReadOnly(
bool readOnly )
205 mGui.groupName->setReadOnly( readOnly );
206 mGui.membersView->setEnabled( !readOnly );
210 ContactGroupEditor::ContactGroupEditor(
Mode mode, QWidget *parent )
211 : QWidget( parent ), d( new Private( this ) )
214 d->mGui.setupUi(
this );
216 d->mGui.membersView->setEditTriggers( QAbstractItemView::AllEditTriggers );
218 d->mGroupModel =
new ContactGroupModel(
this );
219 d->mGui.membersView->setModel( d->mGroupModel );
220 d->mGui.membersView->setItemDelegate(
new ContactGroupEditorDelegate( d->mGui.membersView,
this ) );
223 KABC::ContactGroup dummyGroup;
224 d->mGroupModel->loadContactGroup( dummyGroup );
226 QTimer::singleShot( 0,
this, SLOT(adaptHeaderSizes()) );
227 QTimer::singleShot( 0, d->mGui.groupName, SLOT(setFocus()) );
230 d->mGui.membersView->header()->setStretchLastSection(
true );
241 Q_ASSERT_X(
false,
"ContactGroupEditor::loadContactGroup",
"You are calling loadContactGroup in CreateMode!" );
248 connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
251 d->mMonitor->setItemMonitored( item );
259 if ( !d->mItem.isValid() ) {
263 if ( d->mReadOnly ) {
267 KABC::ContactGroup group = d->mItem.payload<KABC::ContactGroup>();
269 if ( !d->storeContactGroup( group ) ) {
273 d->mItem.setPayload<KABC::ContactGroup>( group );
276 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
278 if ( !d->mDefaultCollection.isValid() ) {
279 const QStringList mimeTypeFilter( KABC::ContactGroup::mimeType() );
282 dlg->setMimeTypeFilter( mimeTypeFilter );
284 dlg->setCaption( i18n(
"Select Address Book" ) );
285 dlg->setDescription( i18n(
"Select the address book the new contact group shall be saved in:" ) );
287 if ( dlg->exec() == KDialog::Accepted ) {
294 KABC::ContactGroup group;
295 if ( !d->storeContactGroup( group ) ) {
300 item.setPayload<KABC::ContactGroup>( group );
301 item.setMimeType( KABC::ContactGroup::mimeType() );
304 connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
312 d->mGroupModel->loadContactGroup( group );
313 d->mGui.membersView->header()->setDefaultSectionSize( d->mGui.membersView->header()->width() / 2 );
314 d->mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
319 d->mDefaultCollection = collection;
322 void ContactGroupEditor::groupNameIsValid(
bool isValid)
324 #ifndef QT_NO_STYLE_STYLESHEET
327 const KColorScheme::BackgroundRole bgColorScheme( KColorScheme::NegativeBackground );
328 KStatefulBrush bgBrush( KColorScheme::View, bgColorScheme );
329 styleSheet = QString::fromLatin1(
"QLineEdit{ background-color:%1 }" ).
330 arg( bgBrush.brush(
this ).color().name() );
332 d->mGui.groupName->setStyleSheet( styleSheet );
336 #include "moc_contactgroupeditor.cpp"
Collection::List collections() const
Returns the list of fetched collection.
A collection selection dialog.
Represents a collection of PIM items.
Job that fetches collections from the Akonadi storage.
void ignoreSession(Session *session)
Ignores all change notifications caused by the given session.
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Item::List items() const
Returns the fetched item.
Only fetch the base collection.
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Only retrieve the immediate parent collection.
Can create new items in this collection.
Job that creates a new item in the Akonadi storage.
Rights rights() const
Returns the rights the user has on the collection.
A QPointer which when destructed, deletes the object it points to.
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Monitors an item or collection for changes.
Job that modifies an existing item in the Akonadi storage.
Job that fetches items from the Akonadi storage.
bool isValid() const
Returns whether the entity is valid.