int AbstractSyncSource::addItem(SyncItem& item) { QByteArray data((char *)item.getData()); QUniqueId id = m_model->addRecord(data, QPimSource(), m_type); item.setKey(id.toString().toAscii().data()); m_model->updateRecord(id, data, m_type); //qDebug() << "AbstractSyncSource::addItem() " << toString(item) << "Id:" << id.toString(); if (!id.isNull()) return 201; //ok, the requested item was added else return 500; //failed, the recipient encountered an error }
/*! Resets the SQL representation of the SIM Card for this storage. This includes removing any contacts that can be stored completely on the SIM, resetting the mapping of contact id to SIM card index, and in the case of the "SM" storage type, resetting the information on the available indexes to store contact phone numbers. A contact that can be stored completely on the sim is defined as one that is equal to a contact that has copied only the original contacts phone numbers, unique identity, and first name. \sa storage() */ void QContactSimSyncer::resetSqlState() { qLog(SimPhoneBook) << mSimType << "::resetSqlState()"; if (mSimType == "SM") { simValueSpace = new QValueSpaceObject("/SIM/Contacts"); simValueSpace->setAttribute("Loaded", false); } else simValueSpace = 0; QDateTime syncTime = QTimeZone::current().toUtc(QDateTime::currentDateTime()); syncTime = syncTime.addMSecs(-syncTime.time().msec()); if (mAccess->startTransaction(syncTime)) { QPreparedSqlQuery q(QPimSqlIO::database()); qLog(SimPhoneBook) << mSimType << "::resetSqlState() - get cardid"; q.prepare("SELECT cardid FROM currentsimcard WHERE storage = :simtype"); q.bindValue(":simtype", mSimType); q.exec(); if (!q.next()) { qLog(SimPhoneBook) << mSimType << "::resetSqlState() - no card found"; // already cleared the card. if (mAccess->commitTransaction()) return; } else { QString lastActiveCard = q.value(0).toString(); qLog(SimPhoneBook) << mSimType << "::resetSqlState() - clear" << lastActiveCard; q.prepare("SELECT recid, simlabelidmap.label FROM contacts JOIN simlabelidmap ON sqlid = recid WHERE cardid = :c AND storage = :s"); q.bindValue(":s", mSimType); q.bindValue(":c", lastActiveCard); q.exec(); QList<QUniqueId> removeTargets; while(q.next()) { QContact existing, compare; QUniqueId recid = QUniqueId::fromUInt(q.value(0).toUInt()); QString label = q.value(1).toString(); existing = mAccess->contact(recid); qLog(SimPhoneBook) << mSimType << "::resetSqlState() - check if" << recid.toString() << "should be removed"; if (QContactSimContext::simLabel(existing) != label) { qLog(SimPhoneBook) << mSimType << "::resetSqlState() - label mis-match" << QContactSimContext::simLabel(existing) << label; // truncated label, don't remove or will lose info. continue; } compare.setUid(existing.uid()); compare.setFirstName(existing.firstName()); compare.setLastName(existing.lastName()); compare.setPhoneNumbers(existing.phoneNumbers()); compare.setDefaultPhoneNumber(existing.defaultPhoneNumber()); if (compare == existing) { // e.g. is only data that fits on the sim. removeTargets.append(QUniqueId::fromUInt(q.value(0).toUInt())); qLog(SimPhoneBook) << mSimType << "::resetSqlState() - remove contact" << q.value(0).toUInt(); } else { qLog(SimPhoneBook) << mSimType << "::resetSqlState() - contact == mismatch"; } } if (mAccess->removeContacts(removeTargets)) { qLog(SimPhoneBook) << mSimType << "::resetSqlState() - remove current simcard for storage"; q.clearErrors(); q.prepare("DELETE FROM currentsimcard WHERE storage = :simtype"); q.bindValue(":simtype", mSimType); q.exec(); qLog(SimPhoneBook) << mSimType << "::resetSqlState() - remove idmap for storage"; q.prepare("DELETE FROM simcardidmap WHERE cardid = :c AND storage = :simtype"); q.bindValue(":simtype", mSimType); q.bindValue(":c", lastActiveCard); q.exec(); // only remove 'forgotten' records. qLog(SimPhoneBook) << mSimType << "::resetSqlState() - remove label idmap for storage"; q.prepare("DELETE FROM simlabelidmap WHERE sqlid = :r"); foreach (QUniqueId id, removeTargets) { q.bindValue(":r", id.toUInt()); q.exec(); } if (q.errorCount() == 0 && mAccess->commitTransaction()) return; }