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 <QMessageBox>
47 using namespace Akonadi;
50 : mParent( parent ), mMonitor( 0 ), mReadOnly( false ), mGroupModel( 0 )
54 ContactGroupEditor::Private::~Private()
59 void ContactGroupEditor::Private::adaptHeaderSizes()
61 mGui.membersView->header()->setDefaultSectionSize( mGui.membersView->header()->width() / 2 );
62 mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
65 void 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 ) {
87 Akonadi::CollectionFetchJob *collectionFetchJob =
new Akonadi::CollectionFetchJob( mItem.parentCollection(),
88 Akonadi::CollectionFetchJob::Base );
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 );
101 void ContactGroupEditor::Private::parentCollectionFetchDone( KJob *job )
103 if ( job->error() ) {
107 Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
112 const Akonadi::Collection parentCollection = fetchJob->collections().first();
113 if ( parentCollection.isValid() ) {
114 mReadOnly = !( parentCollection.rights() & Collection::CanChangeItem );
117 const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
118 loadContactGroup( group );
120 setReadOnly( mReadOnly );
125 void 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() );
139 void 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 ) {
148 ItemFetchJob *job =
new ItemFetchJob( mItem );
149 job->fetchScope().fetchFullPayload();
150 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
152 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
157 void ContactGroupEditor::Private::loadContactGroup(
const KABC::ContactGroup &group )
159 mGui.groupName->setText( group.name() );
161 mGroupModel->loadContactGroup( group );
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 );
173 bool 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() );
190 void ContactGroupEditor::Private::setupMonitor()
193 mMonitor =
new Monitor;
194 mMonitor->ignoreSession( Session::defaultSession() );
200 void ContactGroupEditor::Private::setReadOnly(
bool readOnly )
202 mGui.groupName->setReadOnly( readOnly );
203 mGui.membersView->setEnabled( !readOnly );
206 ContactGroupEditor::ContactGroupEditor(
Mode mode,
QWidget *parent )
207 :
QWidget( parent ), d( new Private( 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 );
226 d->mGui.membersView->header()->setStretchLastSection(
true );
237 Q_ASSERT_X(
false,
"ContactGroupEditor::loadContactGroup",
"You are calling loadContactGroup in CreateMode!" );
240 ItemFetchJob *job =
new ItemFetchJob( item );
241 job->fetchScope().fetchFullPayload();
242 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
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 );
271 ItemModifyJob *job =
new ItemModifyJob( d->mItem );
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 );
279 dlg->setAccessRightsFilter( Collection::CanCreateItem );
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() );
299 ItemCreateJob *job =
new ItemCreateJob( item, d->mDefaultCollection );
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;
318 void ContactGroupEditor::groupNameIsValid(
bool isValid)
320 #ifndef QT_NO_STYLE_STYLESHEET
323 const KColorScheme::BackgroundRole bgColorScheme( KColorScheme::NegativeBackground );
324 KStatefulBrush bgBrush( KColorScheme::View, bgColorScheme );
326 arg( bgBrush.brush(
this ).color().name() );
328 d->mGui.groupName->setStyleSheet( styleSheet );
332 #include "moc_contactgroupeditor.cpp"
virtual int rowCount(const QModelIndex &parent) const =0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
void setInformativeText(const QString &text)
A QPointer which when destructed, deletes the object it points to.
QString fromLatin1(const char *str, int size)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)