Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Save and remove support added for contacts
  • Loading branch information
Denis Kormalev committed Mar 16, 2012
1 parent b8243aa commit d39b3484c3a333d42400a90f74cb8396522f7c92
Showing 4 changed files with 257 additions and 56 deletions.
@@ -2,7 +2,6 @@
#include "../pluginregistry.h"

#include <QMap>
#include <QContactGuid>
#include <QContactDisplayLabel>
#include <QContactName>
#include <QContactNickname>
@@ -19,7 +18,6 @@
#include <QContactManager>
#include <QContactUnionFilter>
#include <QContactDetailFilter>

#include <QDebug>

#ifdef QTM_USE_NAMESPACE
@@ -41,7 +39,6 @@ Contacts::Contacts() :
void Contacts::init()
{
m_fieldNamePairs.clear();
m_fieldNamePairs["id"] = (QLatin1String)QContactGuid::DefinitionName;
m_fieldNamePairs["displayName"] = (QLatin1String)QContactDisplayLabel::DefinitionName;
m_fieldNamePairs["name"] = (QLatin1String)QContactName::DefinitionName;
m_fieldNamePairs["nickname"] = (QLatin1String)QContactNickname::DefinitionName;
@@ -62,36 +59,199 @@ void Contacts::init()

void Contacts::saveContact(int scId, int ecId, const QVariantMap &params)
{
Q_UNUSED(scId);
Q_UNUSED(ecId);

QContact *result = new QContact();
QContact result;
QList<QContactDetail *> detailsToDelete;
foreach (const QString& paramName, params.keys()) {
QString mobilityDetailName = cordovaFieldNameToQtDefinitionName(paramName);
if (mobilityDetailName.isEmpty())
foreach (const QString& field, params.keys()) {
QString qtDefinitionName = cordovaFieldNameToQtDefinitionName(field);
if (qtDefinitionName.isEmpty())
continue;
QContactDetail *detail = new QContactDetail(mobilityDetailName);
// detail->setValue(params[paramName]);
detailsToDelete << detail;
result->saveDetail(detail);

if (field == "nickname") {
QContactNickname *detail = new QContactNickname;
detail->setNickname(params[field].toString());
detailsToDelete << detail;
result.saveDetail(detail);
} else if (field == "note") {
QContactNote *detail = new QContactNote;
detail->setNote(params[field].toString());
detailsToDelete << detail;
result.saveDetail(detail);
} else if (field == "phoneNumbers") {
if (params[field].type() != QVariant::List)
continue;
QVariantList phonesList = params[field].toList();
foreach (const QVariant &phoneDesc, phonesList) {
if (phoneDesc.type() != QVariant::Map)
continue;
QContactPhoneNumber *detail = new QContactPhoneNumber;
detail->setNumber(phoneDesc.toMap()["value"].toString());
if (!phoneDesc.toMap()["type"].toString().isEmpty() && phoneDesc.toMap()["type"].toString() != "phone")
detail->setSubTypes(phoneDesc.toMap()["type"].toString());
detailsToDelete << detail;
result.saveDetail(detail);
}
} else if (field == "emails") {
if (params[field].type() != QVariant::List)
continue;
QVariantList emailsList = params[field].toList();
foreach (const QVariant &emailDesc, emailsList) {
if (emailDesc.type() != QVariant::Map)
continue;
QContactEmailAddress *detail = new QContactEmailAddress;
detail->setEmailAddress(emailDesc.toMap()["value"].toString());
detailsToDelete << detail;
result.saveDetail(detail);
}
} else if (field == "ims") {
if (params[field].type() != QVariant::List)
continue;
QVariantList imsList = params[field].toList();
foreach (const QVariant &imDesc, imsList) {
if (imDesc.type() != QVariant::Map)
continue;
QContactOnlineAccount *detail = new QContactOnlineAccount;
detail->setAccountUri(imDesc.toMap()["value"].toString());
if (!imDesc.toMap()["type"].toString().isEmpty())
detail->setSubTypes(imDesc.toMap()["type"].toString());
detailsToDelete << detail;
result.saveDetail(detail);
}
} else if (field == "photos") {
if (params[field].type() != QVariant::List)
continue;
QVariantList photosList = params[field].toList();
foreach (const QVariant &photoDesc, photosList) {
if (photoDesc.type() != QVariant::Map)
continue;
//TODO: we need to decide should we support base64 images or not
if (photoDesc.toMap()["type"].toString() != "url")
continue;
QContactAvatar *detail = new QContactAvatar;
detail->setImageUrl(QUrl(photoDesc.toMap()["value"].toString()));
detailsToDelete << detail;
result.saveDetail(detail);
}
} else if (field == "urls") {
if (params[field].type() != QVariant::List)
continue;
QVariantList urlsList = params[field].toList();
foreach (const QVariant &urlDesc, urlsList) {
if (urlDesc.type() != QVariant::Map)
continue;
QContactUrl *detail = new QContactUrl;
detail->setUrl(urlDesc.toMap()["value"].toString());
if (!urlDesc.toMap()["type"].toString().isEmpty())
detail->setSubType(urlDesc.toMap()["type"].toString());
detailsToDelete << detail;
result.saveDetail(detail);
}
} else if (field == "birthday") {
QContactBirthday *detail = new QContactBirthday;
detail->setDateTime(QDateTime::fromString(params[field].toString(), "yyyy-MM-ddThh:mm:ss.zzzZ"));
detailsToDelete << detail;
result.saveDetail(detail);
} else if (field == "organizations") {

if (params[field].type() != QVariant::List)
continue;
QVariantList organizationsList = params[field].toList();
foreach (const QVariant &organizationDesc, organizationsList) {
if (organizationDesc.type() != QVariant::Map)
continue;
QContactOrganization *detail = new QContactOrganization;
detail->setName(organizationDesc.toMap()["name"].toString());
detail->setDepartment(QStringList() << organizationDesc.toMap()["department"].toString());
detail->setRole(organizationDesc.toMap()["title"].toString());
detailsToDelete << detail;
result.saveDetail(detail);
}

} else if (field == "name") {
QContactName *detail = new QContactName;
QVariantMap nameMap = params[field].toMap();
detail->setLastName(nameMap["familyName"].toString());
detail->setFirstName(nameMap["givenName"].toString());
detail->setMiddleName(nameMap["middleName"].toString());
detail->setPrefix(nameMap["honorificPrefix"].toString());
detail->setSuffix(nameMap["honorificSuffix"].toString());
detailsToDelete << detail;
result.saveDetail(detail);
}

}
result = m_manager->compatibleContact(result);
if (!m_manager->saveContact(&result)) {
switch (m_manager->error()) {
case QContactManager::DoesNotExistError:
case QContactManager::AlreadyExistsError:
case QContactManager::InvalidDetailError:
case QContactManager::InvalidRelationshipError:
case QContactManager::BadArgumentError:
case QContactManager::InvalidContactTypeError:
callback(ecId, "ContactError.INVALID_ARGUMENT_ERROR");
break;
case QContactManager::DetailAccessError:
case QContactManager::PermissionsError:
callback(ecId, "ContactError.PERMISSION_DENIED_ERROR");
break;
case QContactManager::NotSupportedError:
callback(ecId, "ContactError.NOT_SUPPORTED_ERROR");
break;
case QContactManager::TimeoutError:
callback(ecId, "ContactError.TIMEOUT_ERROR");
break;
case QContactManager::UnspecifiedError:
case QContactManager::LockedError:
case QContactManager::OutOfMemoryError:
case QContactManager::VersionMismatchError:
case QContactManager::LimitReachedError:
case QContactManager::NoError:
default:
callback(ecId, "ContactError.UNKNOWN_ERROR");
break;
}
} else {
callback(scId, "");
}
m_manager->saveContact(result);
delete result;
qDeleteAll(detailsToDelete);
}

void Contacts::removeContact(int scId, int ecId, const QString &guid)
void Contacts::removeContact(int scId, int ecId, const QString &localId)
{
Q_UNUSED(scId)
Q_UNUSED(ecId)
if (!m_manager->removeContact(localId.toUInt())) {
switch (m_manager->error()) {
case QContactManager::DoesNotExistError:
case QContactManager::AlreadyExistsError:
case QContactManager::InvalidDetailError:
case QContactManager::InvalidRelationshipError:
case QContactManager::BadArgumentError:
case QContactManager::InvalidContactTypeError:
callback(ecId, "ContactError.INVALID_ARGUMENT_ERROR");
break;
case QContactManager::DetailAccessError:
case QContactManager::PermissionsError:
callback(ecId, "ContactError.PERMISSION_DENIED_ERROR");
break;
case QContactManager::NotSupportedError:
callback(ecId, "ContactError.NOT_SUPPORTED_ERROR");
break;
case QContactManager::TimeoutError:
callback(ecId, "ContactError.TIMEOUT_ERROR");
break;
case QContactManager::UnspecifiedError:
case QContactManager::LockedError:
case QContactManager::OutOfMemoryError:
case QContactManager::VersionMismatchError:
case QContactManager::LimitReachedError:
case QContactManager::NoError:
default:
callback(ecId, "ContactError.UNKNOWN_ERROR");
break;
}

QContactDetailFilter idFilter;
idFilter.setDetailDefinitionName(QContactGuid::DefinitionName, QContactGuid::FieldGuid);
idFilter.setValue(guid);
idFilter.setMatchFlags(QContactFilter::MatchExactly);
QList<QContactLocalId> contacts = m_manager->contactIds(idFilter);
m_manager->removeContacts(contacts);
} else {
callback(scId, "");
}
}

void Contacts::findContacts(int scId, int ecId, const QStringList &fields, const QString &filter, bool multiple)
@@ -150,14 +310,15 @@ QString Contacts::jsonedContact(const QContact &contact, const QStringList &fiel
QStringList resultingFields = fields;
if (resultingFields.empty())
resultingFields.append(m_fieldNamePairs.keys());
if (!resultingFields.contains("id"))
resultingFields << "id";
QStringList fieldValuesList;
foreach (const QString &field, resultingFields) {
QString qtDefinitionName = cordovaFieldNameToQtDefinitionName(field);
if (field == "id") {
QContactGuid detail = contact.detail(qtDefinitionName);
fieldValuesList << QString("%1: \"%2\"")
.arg(field)
.arg(detail.guid());
.arg(contact.localId());
} else if (field == "displayName") {
QContactDisplayLabel detail = contact.detail(qtDefinitionName);
fieldValuesList << QString("%1: \"%2\"")
@@ -40,7 +40,7 @@ class Contacts : public CPlugin

public slots:
void saveContact(int scId, int ecId, const QVariantMap &params);
void removeContact(int scId, int ecId, const QString &guid);
void removeContact(int scId, int ecId, const QString &localId);
void findContacts(int scId, int ecId, const QStringList &fields, const QString &filter, bool multiple);

private:
@@ -2,6 +2,33 @@
* JavaScript file
*/


//TODO: remove later this method. Now it is needed for debugging
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];

if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ... :\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\";\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}

var vibration_length = 1000;

function get(id)
@@ -152,22 +179,39 @@ function fileError( p_fileError ) {
}


function displayGiven(contacts){
var result = ""
for (var contact in contacts) {
result += contacts[contact].name.formatted + ": " + contacts[contact].phoneNumbers[0].value + "\n"
contacts[contact].remove(function() {searchForGiven()})
}

console.log(result)
}

function searchForGiven() {
navigator.contacts.find(["name", "phoneNumbers", "nickname", "displayName", "emails", "ims", "addresses", "organizations", "birthday", "photos"], displayGiven, 0, {filter:"Given", multiple: true})
}

/*
* Register for the device ready event
*/
document.addEventListener( "deviceready", function() {
console.log("basicjs.deviceReady")
get( "debug_output" ).innerHTML = "Device Ready!<br/>";

navigator.contacts.find(["name", "phoneNumbers", "nickname", "displayName", "emails"],
function(contacts){
var result = ""
for (var contact in contacts) {
result += contacts[contact].name.formatted + ": " + contacts[contact].phoneNumbers[0].value + "\n"
}
var created = navigator.contacts.create({"name": {familyName: "Family", givenName: "Given"}, phoneNumbers: [{"value": "+123456789", pref: false, type: "work"}], emails: [{"value": "given.family@gmail.com", pref: false, type: "email"}, {"value": "given@family.com", pref: false, type: "email"}], birthday: new Date(1985, 4, 3, 0, 0, 0)})
created.save(searchForGiven, 0)

// navigator.contacts.find(["name", "phoneNumbers", "nickname", "displayName", "emails", "ims", "addresses", "organizations", "birthday", "photos"],
// function(contacts){
// var result = ""
// for (var contact in contacts) {
// result += contacts[contact].name.formatted + ": " + contacts[contact].phoneNumbers[0].value + "\n"
// }

console.log(result)
}, 0, {filter:"mar", multiple: true})
// console.log(result)
// }, 0, {filter:"mar", multiple: true})
}, false );

document.addEventListener( "resume", function() {

0 comments on commit d39b348

Please sign in to comment.