From: Piotr Kosko Date: Fri, 27 Nov 2015 09:24:21 +0000 (+0100) Subject: [Contact] Added handling of groupIds in Contact X-Git-Tag: submit/tizen/20160418.070226^2~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=eb0eda09e5a824736a85a6630404110dd268fb82;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Contact] Added handling of groupIds in Contact [Feature] Gathering and updating groupIds of Contact object is added. [Verification] Code compiles without errors. TCT passrate is 100%. // Code below works var addressbook, c; function getAll() { // Defines the error callback. function errorCB(err) { console.log( 'The following error occurred: ' + err.name); } function contactsFoundCB(contacts) { c = contacts; for (var i =0; i < contacts.length; ++i) { var message = "Found contact " + contacts[i].name.displayName + " is in groups: [ "; console.log(contacts[i].groupIds.length); for (var j = 0; j < contacts[i].groupIds.length; ++j) { message += addressbook.getGroup(contacts[i].groupIds[j]).name + " "; } message += " ]"; console.log(message); } } addressbook = tizen.contact.getDefaultAddressBook(); try { addressbook.find(contactsFoundCB, errorCB); } catch (err) { console.log( 'The following error occurred while finding: ' + err.name); } } function updateGroup(groups) { c[0].groupIds = groups; addressbook.update(c[0]) } //Step 1 // see contacts getAll(); //Step 2 // update group of first contact updateGroup([1]); // see if contact changed getAll(); //Step 3 // again update group of first contact updateGroup(['1', '2']); // see if contact changed getAll(); Change-Id: Ie17aa6c9d9c3b357074fef416afd71a0a3f04e8e Signed-off-by: Piotr Kosko --- diff --git a/src/contact/contact_util.cc b/src/contact/contact_util.cc index 6e91ef00..cdc51a53 100755 --- a/src/contact/contact_util.cc +++ b/src/contact/contact_util.cc @@ -2367,6 +2367,86 @@ PlatformResult ExportNotesToContactsRecord(contacts_record_h contacts_record, return PlatformResult(ErrorCode::NO_ERROR); } +PlatformResult ImportGroupIdsFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonValue* val) { + + LoggerD("Enter"); + // contacts_record is protected by unique_ptr and its ownership is not passed + // here + if (!contacts_record) { + LoggerE("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); + } + + contacts_record_h record = nullptr; + int err = contacts_record_get_child_record_at_p( + contacts_record, _contacts_contact.group_relation, index, &record); + if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) { + // ignoring this record + LoggerW("Skipping record with index %i. error code: %i", index, err); + return PlatformResult(ErrorCode::NO_ERROR); + } + + int group_id = -1; + PlatformResult status = + ContactUtil::GetIntFromRecord(record, _contacts_group_relation.id, &group_id); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + if (-1 == group_id) { + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Fetching groupId failed"); + } + *val = JsonValue{std::to_string(group_id)}; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult ExportGroupIdsToContactsRecord(contacts_record_h contacts_record, + int value) { + + LoggerD("Enter"); + contacts_record_h notes_record = nullptr; + // contacts_record is protected by unique_ptr and its ownership is not passed + // here + if (!contacts_record) { + LoggerE("Contacts record is null"); + return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null"); + } + + int err = contacts_record_create(_contacts_group_relation._uri, ¬es_record); + PlatformResult status = + ContactUtil::ErrorChecker(err, "Fail to create note record in database"); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + ContactsRecordHPtr record(¬es_record, ContactsDeleter); + + status = ContactUtil::SetIntInRecord(notes_record, _contacts_group_relation.group_id, + value); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + err = contacts_record_add_child_record(contacts_record, + _contacts_contact.group_relation, notes_record); + status = + ContactUtil::ErrorChecker(err, "Fail to save note record in database"); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + // Do not delete record, it is passed to the platform + record.release(); + + return PlatformResult(ErrorCode::NO_ERROR); +} + PlatformResult ImportContactFromContactsRecord( contacts_record_h contacts_record, JsonObject* out_ptr) { @@ -2593,6 +2673,29 @@ PlatformResult ImportContactFromContactsRecord( "vibrationURI", value ? JsonValue{ConvertPathToUri(value)} : JsonValue{})); value = nullptr; + + //### groupIds: ### + JsonArray& group_ids = out.insert(std::make_pair(std::string("groupIds"), + picojson::value(JsonArray()))).first->second.get(); + + status = ContactUtil::GetNumberOfChildRecord( + contacts_record, _contacts_contact.group_relation, &child_rec_count); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + for (int i = 0; i < child_rec_count; ++i) { + JsonValue val{JsonObject{}}; + + status = ImportGroupIdsFromContactsRecord(contacts_record, static_cast(i), &val); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + group_ids.push_back(val); + } } return PlatformResult(ErrorCode::NO_ERROR); @@ -2782,6 +2885,26 @@ PlatformResult ExportContactToContactsRecord(contacts_record_h contacts_record, } } + { + //### groupIds: ### + PlatformResult status = ContactUtil::ClearAllContactRecord( + contacts_record, _contacts_contact.group_relation); + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + const JsonArray& elements = FromJson(in, "groupIds"); + for (auto& element : elements) { + auto& str = JsonCast(element); + if (str.empty()) { + continue; + } + ExportGroupIdsToContactsRecord(contacts_record, atoi(str.c_str())); + } + } + + return PlatformResult(ErrorCode::NO_ERROR); } diff --git a/src/contact/contact_util.h b/src/contact/contact_util.h index 9e7b9854..471d1248 100644 --- a/src/contact/contact_util.h +++ b/src/contact/contact_util.h @@ -132,6 +132,10 @@ common::PlatformResult ImportContactNotesFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonValue *val); common::PlatformResult ExportNotesToContactsRecord( contacts_record_h contacts_record, const std::string &value); +common::PlatformResult ImportGroupIdsFromContactsRecord( + contacts_record_h contacts_record, unsigned int index, JsonValue *val); +common::PlatformResult ExportGroupIdsToContactsRecord( + contacts_record_h contacts_record, int value); common::PlatformResult ImportContactFromContactsRecord( contacts_record_h contacts_record, JsonObject *out); common::PlatformResult ExportPersonToContactsRecord(contacts_record_h record, diff --git a/src/contact/js/contact.js b/src/contact/js/contact.js index 1db49ffd..bb2f1685 100755 --- a/src/contact/js/contact.js +++ b/src/contact/js/contact.js @@ -70,9 +70,7 @@ var Contact = function(data) { } for (var i = 0; i < arr.length; ++i) { if (type_.isString(type)) { - if (!type_.isString(arr[i])) { - return previousValue; - } + arr[i] = converter_.toString(arr[i]); } else if (_editGuard.isEditEnabled()) { arr[i] = new type(arr[i]); } else if (!(arr[i] instanceof type)) {