void MessageModel::setContact(const QContact& contact) { clear(); // Locate messages whose sender is this contact QMailMessageKey msgsFrom; // Locate messages whose recipients list contains this contact QMailMessageKey msgsTo; // Match on any of contact's phone numbers foreach(const QString& number, contact.phoneNumbers().values()) { msgsFrom |= QMailMessageKey(QMailMessageKey::Sender, number); msgsTo |= QMailMessageKey(QMailMessageKey::Recipients, number, QMailMessageKey::Contains); } // Match on any of contact's email addresses foreach(const QString& address, contact.emailList()) { msgsFrom |= QMailMessageKey(QMailMessageKey::Sender, address); msgsTo |= QMailMessageKey(QMailMessageKey::Recipients, address, QMailMessageKey::Contains); } // Sort messages by timestamp, newest to oldest QMailMessageSortKey sort(QMailMessageSortKey::TimeStamp, Qt::DescendingOrder); // Fetch the messages matching either of our queries, and return them sorted QMailIdList matches(QMailStore::instance()->queryMessages(msgsFrom | msgsTo, sort)); // Add each returned message to our data model foreach (const QMailId& id, matches) appendRow(new MessageItem(id)); }
int GoogleSession::updateContacts(QList<QContact> &contacts, bool skip) { setState(UpdatingContacts); QContactModel filter; qDebug() << "PIM source" << filter.defaultSource().identity; for (int i = 0; i < contacts.size(); ++i) { QContact gContact = contacts.at(i); qDebug() << "process contact" << gContact.label(); // skip contacts with no phonenumbers if (skip && (! gContact.phoneNumbers().size() )) continue; /* filter.addContact(gContact, filter.phoneSource()); continue; */ filter.setFilter(gContact.label()); // single match. merging if (filter.count() == 1) { qDebug() << "Merging"; filter.updateContact( merge(filter.contact(0), gContact) ); } // no match. saving directly else if (filter.count() == 0) { qDebug() << "Adding"; filter.addContact(gContact); } else qDebug() << "WTF?! Multiple matches"; } setState(Authenticated); }
QContact GoogleSession::merge(QContact contact, GoogleContact gContact) { qDebug() << "=="; qDebug() << contact.label() << gContact.label(); // merge email lists QStringList gl = gContact.emailList(); QStringList el = contact.emailList(); for (int i=0; i<gl.size(); ++i) { if ( el.contains( gl.at(i) ) ) qDebug() << "already have email" << gl.at(i) ; else contact.insertEmail( gl.at(i) ); } // merge phone number lists QMap<QContact::PhoneType, QString> nums = contact.phoneNumbers(); QMap<QContact::PhoneType, QString> gNums = gContact.phoneNumbers(); // iterate google data QMapIterator<QContact::PhoneType, QString> gNumIt(gNums); while (gNumIt.hasNext()) { gNumIt.next(); QString phone = gNumIt.value(); QContact::PhoneType type = gNumIt.key(); if (! nums.values().contains( phone ) ) { gContact.setPhoneNumber(type, phone); qDebug () << "adding phone" << phone << "of type" << type; } else { // iterate qtopia data QMapIterator<QContact::PhoneType, QString> it(nums); while (it.hasNext()) { it.next(); qDebug() << "contacts has phone" << it.value() << "of type" << it.key(); qDebug() << "comparing to google" << phone << "of type" << type; if (it.value() == phone && it.key() != type ) { bool updatedef = (contact.defaultPhoneNumber() == phone); nums.remove(it.key() ); nums.remove(type); // FIXME nums.insert(type, phone); contact.setPhoneNumbers(nums); if (updatedef) contact.setDefaultPhoneNumber(type); qDebug() << "replaced phone of type" << type << phone << updatedef; break; } } qDebug () << "skipping phone" << phone << type ; } } QStringList googleGroupList = gContact.categories(); for (int i=0; i<googleGroupList.size(); ++i) { QString googleGroupId = googleGroupList.at(i); QString googleGroupName = groups[googleGroupId]; QString googleGroupQId = "category." + googleGroupName; qDebug() << "Group id" <<googleGroupId << "name" << googleGroupName ; if (googleGroupName.isEmpty()) continue; QList<QString> qGroupList = contact.categories(); if (! qGroupList.contains(googleGroupQId) ) { qGroupList << googleGroupQId; qDebug() << "Adding group" << googleGroupName ; contact.setCategories(qGroupList); } else { qDebug() << "Skipping group" << googleGroupName; } qDebug() << "group count" << qGroupList.count(); } qDebug() << "\n"; return contact; }