22 #include "contactgroupmodel_p.h"
24 #include <akonadi/itemfetchjob.h>
25 #include <akonadi/itemfetchscope.h>
26 #include <kabc/addressee.h>
28 #include <kiconloader.h>
29 #include <klocalizedstring.h>
31 using namespace Akonadi;
36 : isReference(false), loadingError( false )
40 KABC::ContactGroup::ContactReference reference;
41 KABC::ContactGroup::Data data;
42 KABC::Addressee referencedContact;
47 class ContactGroupModel::Private
50 Private( ContactGroupModel *parent )
55 void resolveContactReference(
const KABC::ContactGroup::ContactReference &reference,
int row )
58 if ( !reference.gid().isEmpty() ) {
59 item.setGid( reference.gid() );
61 item.setId( reference.uid().toLongLong() );
63 ItemFetchJob *job =
new ItemFetchJob( item, mParent );
64 job->setProperty(
"row", row );
65 job->fetchScope().fetchFullPayload();
67 mParent->connect( job, SIGNAL(result(KJob*)), SLOT(itemFetched(KJob*)) );
70 void itemFetched( KJob *job )
72 const int row = job->property(
"row" ).toInt();
75 mMembers[ row ].loadingError =
true;
76 emit mParent->dataChanged( mParent->index( row, 0,
QModelIndex() ), mParent->index( row, 1,
QModelIndex() ) );
80 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
82 if ( fetchJob->items().count() != 1 ) {
83 mMembers[ row ].loadingError =
true;
84 emit mParent->dataChanged( mParent->index( row, 0,
QModelIndex() ), mParent->index( row, 1,
QModelIndex() ) );
88 const Item item = fetchJob->items().first();
89 const KABC::Addressee contact = item.payload<KABC::Addressee>();
91 GroupMember &member = mMembers[ row ];
92 member.referencedContact = contact;
93 emit mParent->dataChanged( mParent->index( row, 0,
QModelIndex() ), mParent->index( row, 1,
QModelIndex() ) );
96 void normalizeMemberList()
99 bool needsNormalization =
false;
100 if ( mMembers.isEmpty() ) {
101 needsNormalization =
true;
103 for (
int i = 0; i < mMembers.count(); ++i ) {
104 const GroupMember &member = mMembers[ i ];
105 if ( !member.isReference && !( i == mMembers.count() - 1 ) ) {
106 if ( member.data.name().isEmpty() && member.data.email().isEmpty() ) {
107 needsNormalization =
true;
113 const GroupMember &member = mMembers.last();
114 if ( member.isReference || !( member.data.name().isEmpty() && member.data.email().isEmpty() ) ) {
115 needsNormalization =
true;
120 if ( !needsNormalization ) {
124 bool foundEmpty =
false;
127 mParent->beginInsertRows(
QModelIndex(), mMembers.count(), mMembers.count() );
129 member.isReference =
false;
130 mMembers.append( member );
131 mParent->endInsertRows();
136 for (
int i = 0; i < mMembers.count(); ++i ) {
137 const GroupMember &member = mMembers[ i ];
138 if ( !member.isReference && !( i == mMembers.count() - 1 ) ) {
139 if ( member.data.name().isEmpty() && member.data.email().isEmpty() ) {
141 mMembers.remove( i );
142 mParent->endRemoveRows();
148 }
while ( foundEmpty );
151 ContactGroupModel *mParent;
153 KABC::ContactGroup mGroup;
157 ContactGroupModel::ContactGroupModel(
QObject *parent )
162 ContactGroupModel::~ContactGroupModel()
167 void ContactGroupModel::loadContactGroup(
const KABC::ContactGroup &contactGroup )
169 emit layoutAboutToBeChanged();
172 d->mGroup = contactGroup;
174 for ( uint i = 0; i < d->mGroup.dataCount(); ++i ) {
175 const KABC::ContactGroup::Data data = d->mGroup.data( i );
177 member.isReference =
false;
180 d->mMembers.append( member );
183 for ( uint i = 0; i < d->mGroup.contactReferenceCount(); ++i ) {
184 const KABC::ContactGroup::ContactReference reference = d->mGroup.contactReference( i );
186 member.isReference =
true;
187 member.reference = reference;
189 d->mMembers.append( member );
191 d->resolveContactReference( reference, d->mMembers.count() - 1 );
194 d->normalizeMemberList();
196 emit layoutChanged();
199 bool ContactGroupModel::storeContactGroup( KABC::ContactGroup &group )
const
201 group.removeAllContactReferences();
202 group.removeAllContactData();
204 for (
int i = 0; i < d->mMembers.count(); ++i ) {
205 const GroupMember &member = d->mMembers[ i ];
206 if ( member.isReference ) {
207 group.append( member.reference );
209 if ( i != ( d->mMembers.count() - 1 ) ) {
210 if ( member.data.email().isEmpty() ) {
211 d->mLastErrorMessage =
212 i18n(
"The member with name <b>%1</b> is missing an email address",
213 member.data.name() );
216 group.append( member.data );
224 QString ContactGroupModel::lastErrorMessage()
const
226 return d->mLastErrorMessage;
231 return createIndex( row, col);
245 if ( index.
row() < 0 || index.
row() >= d->mMembers.count() ) {
253 const GroupMember &member = d->mMembers[ index.
row() ];
255 if ( role == Qt::DisplayRole ) {
256 if ( member.loadingError ) {
257 if ( index.
column() == 0 ) {
258 return i18n(
"Contact does not exist any more" );
264 if ( member.isReference ) {
265 if ( index.
column() == 0 ) {
266 return member.referencedContact.realName();
268 if ( !member.reference.preferredEmail().isEmpty() ) {
269 return member.reference.preferredEmail();
271 return member.referencedContact.preferredEmail();
275 if ( index.
column() == 0 ) {
276 return member.data.name();
278 return member.data.email();
283 if ( role == Qt::DecorationRole ) {
284 if ( index.
column() == 1 ) {
288 if ( member.loadingError ) {
292 if ( index.
row() == ( d->mMembers.count() - 1 ) ) {
296 if ( member.isReference ) {
297 return KIcon(
QLatin1String(
"x-office-contact" ), KIconLoader::global(),
304 if ( role == Qt::EditRole ) {
305 if ( member.isReference ) {
306 if ( index.
column() == 0 ) {
307 return member.referencedContact.realName();
309 if ( !member.reference.preferredEmail().isEmpty() ) {
310 return member.reference.preferredEmail();
312 return member.referencedContact.preferredEmail();
316 if ( index.
column() == 0 ) {
317 return member.data.name();
319 return member.data.email();
324 if ( role == IsReferenceRole ) {
325 return member.isReference;
328 if ( role == AllEmailsRole ) {
329 if ( member.isReference ) {
330 return member.referencedContact.emails();
339 bool ContactGroupModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
345 if ( index.
row() < 0 || index.
row() >= d->mMembers.count() ) {
353 GroupMember &member = d->mMembers[ index.
row() ];
355 if ( role == Qt::EditRole ) {
356 if ( member.isReference ) {
357 if ( index.
column() == 0 ) {
359 d->resolveContactReference( member.reference, index.
row() );
361 if ( index.
column() == 1 ) {
363 if ( email != member.referencedContact.preferredEmail() ) {
364 member.reference.setPreferredEmail( email );
366 member.reference.setPreferredEmail(
QString() );
370 if ( index.
column() == 0 ) {
371 member.data.setName( value.
toString() );
373 member.data.setEmail( value.
toString() );
377 d->normalizeMemberList();
382 if ( role == IsReferenceRole ) {
383 if ( ( value.
toBool() == true ) && !member.isReference ) {
384 member.isReference =
true;
386 if ( ( value.
toBool() == false ) && member.isReference ) {
387 member.isReference =
false;
388 member.data.setName( member.referencedContact.realName() );
389 member.data.setEmail( member.referencedContact.preferredEmail() );
398 QVariant ContactGroupModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
400 if ( section < 0 || section > 1 ) {
404 if ( orientation != Qt::Horizontal ) {
408 if ( role != Qt::DisplayRole ) {
412 if ( section == 0 ) {
413 return i18nc(
"contact's name",
"Name" );
415 return i18nc(
"contact's email address",
"EMail" );
421 if ( !index.
isValid() || index.
row() < 0 || index.
row() >= d->mMembers.count() ) {
422 return Qt::ItemIsEnabled;
425 if ( d->mMembers[ index.
row() ].loadingError ) {
430 return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable );
433 int ContactGroupModel::columnCount(
const QModelIndex &parent )
const
442 int ContactGroupModel::rowCount(
const QModelIndex &parent )
const
445 return d->mMembers.count();
451 bool ContactGroupModel::removeRows(
int row,
int count,
const QModelIndex &parent )
457 beginRemoveRows(
QModelIndex(), row, row + count - 1 );
458 for (
int i = 0; i < count; ++i ) {
459 d->mMembers.remove( row );
466 #include "moc_contactgroupmodel_p.cpp"
qlonglong toLongLong(bool *ok) const
QString number(int n, int base)
virtual Qt::ItemFlags flags(const QModelIndex &index) const