From 50caa0cc30196958e050ee5cf57ec5606777e8e1 Mon Sep 17 00:00:00 2001 From: Adam Banasiak Date: Thu, 11 Dec 2014 14:45:47 +0100 Subject: [PATCH] [Contact] Fix contacts (replace old-style json, prepare for new architecture) Change-Id: I2b5faafc042d4fd57a5d3f335d319479dcc831eb Signed-off-by: Adam Banasiak --- src/contact/addressbook.cc | 234 +++++++++++++----------------- src/contact/addressbook.h | 8 +- src/contact/contact.gyp | 0 src/contact/contact_manager.cc | 151 +++++++++---------- src/contact/contact_manager.h | 135 +---------------- src/contact/contact_util.cc | 321 +++++++++++++++++++++-------------------- src/contact/contact_util.h | 6 +- src/contact/person.cc | 24 ++- src/contact/person.h | 4 +- 9 files changed, 362 insertions(+), 521 deletions(-) create mode 100644 src/contact/contact.gyp diff --git a/src/contact/addressbook.cc b/src/contact/addressbook.cc index 9cd49ba..153a719 100644 --- a/src/contact/addressbook.cc +++ b/src/contact/addressbook.cc @@ -18,7 +18,7 @@ #include #include "common/converter.h" -#include "common/native-plugin.h" +#include "common/extension.h" #include "common/platform_exception.h" #include "common/logger.h" #include "common/task-queue.h" @@ -39,7 +39,7 @@ static const long kUnifiedAddressBookId = -1; const char* kContactListenerId = "ContactChangeListener"; inline long AddressBookId(const JsonObject& obj) { - return common::stol(FromJson(obj, "addressBook", "id")); + return common::stol(FromJson(obj, "addressBook", "id")); } inline bool IsUnified(const JsonObject& args) { @@ -51,9 +51,9 @@ inline bool IsUnified(long id) { return id == kUnifiedAddressBookId; } } // anonymous namespace void AddressBook_get(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); + ContactUtil::CheckDBConnection(); - int contact_id = common::stol(FromJson(args, "id")); + int contact_id = common::stol(FromJson(args, "id")); int err = CONTACTS_ERROR_NONE; contacts_record_h contacts_record = nullptr; @@ -66,20 +66,16 @@ void AddressBook_get(const JsonObject& args, JsonObject& out) { ContactUtil::ContactsRecordHPtr contacts_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); - JsonValue result{json::Object{}}; - auto& result_obj = result.get(); - result_obj.insert(std::make_pair("id", std::to_string(contact_id))); + out.insert(std::make_pair("id", std::to_string(contact_id))); ContactUtil::ImportContactFromContactsRecord(*contacts_record_ptr, - &result_obj); - ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &result_obj); - - NativePlugin::ReportSuccess(result, out); + &out); + ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); } void AddressBook_add(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); + ContactUtil::CheckDBConnection(); - const json::Object& contact = FromJson(args, "contact"); + const JsonObject& contact = FromJson(args, "contact"); if (!IsNull(contact, "id")) { LOGW("Contact already exists"); @@ -117,21 +113,17 @@ void AddressBook_add(const JsonObject& args, JsonObject& out) { contacts_record_ptr.reset(&reset_record); } - json::Value result{json::Object{}}; - json::Object& value_obj = result.get(); - value_obj.insert(std::make_pair("id", json::Value{std::to_string(id)})); - - ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &value_obj); + out.insert(std::make_pair("id", JsonValue{std::to_string(id)})); - NativePlugin::ReportSuccess(result, out); + ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); } void AddressBook_update(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); + ContactUtil::CheckDBConnection(); - const json::Object& contact = FromJson(args, "contact"); - const json::Object& addressbook = FromJson(args, "addressBook"); - long contactId = common::stol(FromJson(contact, "id")); + const JsonObject& contact = FromJson(args, "contact"); + const JsonObject& addressbook = FromJson(args, "addressBook"); + long contactId = common::stol(FromJson(contact, "id")); if (IsNull(contact, "id")) { LOGW("Contact doesn't exist"); @@ -169,18 +161,13 @@ void AddressBook_update(const JsonObject& args, JsonObject& out) { } } - json::Value result{json::Object{}}; - json::Object& value_obj = result.get(); - - ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &value_obj); - - NativePlugin::ReportSuccess(result, out); + ContactUtil::UpdateAdditionalInformation(contacts_record_ptr, &out); } -void AddressBook_remove(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); +void AddressBook_remove(const JsonObject& args) { + ContactUtil::CheckDBConnection(); - long contact_id = common::stol(FromJson(args, "id")); + long contact_id = common::stol(FromJson(args, "id")); if (contact_id < 0) { throw common::InvalidValuesException("Nagative contact id"); @@ -195,24 +182,23 @@ void AddressBook_remove(const JsonObject& args, JsonObject& out) { LOGW("Contacts record delete error, error code: %d", err); throw common::UnknownException("Contacts record delete error"); } - NativePlugin::ReportSuccess(out); } -void AddressBook_addBatch(const json::Object& args, json::Object& out) { +void AddressBook_addBatch(const JsonObject& args, JsonObject& out) { LoggerD("Enter"); + ContactUtil::CheckDBConnection(); long addressBookId = -1; - const json::Array& batch_args = FromJson(args, "batchArgs"); - addressBookId = common::stol(FromJson(args, "addressBookId")); + const JsonArray& batch_args = FromJson(args, "batchArgs"); + addressBookId = common::stol(FromJson(args, "addressBookId")); addressBookId = addressBookId == -1 ? 0 : addressBookId; auto batch_func = [ addressBookId, batch_args ](const shared_json_value & response)->void { NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); - json::Object& response_obj = response->get(); - json::Array& batch_result = - response_obj.insert( - std::make_pair("result", json::Value{json::Array{}})) - .first->second.get(); + JsonObject& response_obj = response->get(); + JsonArray& batch_result = + response_obj.insert(std::make_pair("result", JsonValue{JsonArray{}})) + .first->second.get(); try { unsigned length = batch_args.size(); @@ -236,8 +222,8 @@ void AddressBook_addBatch(const json::Object& args, json::Object& out) { } ContactUtil::ContactsRecordHPtr x(&contacts_record, ContactUtil::ContactsDeleter); - ContactUtil::ExportContactToContactsRecord( - contacts_record, JsonCast(item)); + ContactUtil::ExportContactToContactsRecord(contacts_record, + JsonCast(item)); ContactUtil::SetIntInRecord( contacts_record, _contacts_contact.address_book_id, addressBookId); error_code = contacts_list_add(*contacts_list_ptr, *(x.release())); @@ -263,7 +249,7 @@ void AddressBook_addBatch(const json::Object& args, json::Object& out) { } for (unsigned int i = 0; i < count; i++) { - json::Object out_object; + JsonObject out_object; contacts_record_h contact_record = nullptr; error_code = contacts_db_get_record(_contacts_contact._uri, ids[i], &contact_record); @@ -277,7 +263,7 @@ void AddressBook_addBatch(const json::Object& args, json::Object& out) { } ContactUtil::ImportContactFromContactsRecord(contact_record, &out_object); - batch_result.push_back(json::Value{out_object}); + batch_result.push_back(JsonValue{out_object}); } if (ids) { free(ids); @@ -297,35 +283,33 @@ void AddressBook_addBatch(const json::Object& args, json::Object& out) { callback_handle, response->serialize()); }; - TaskQueue::GetInstance().Queue( + TaskQueue::GetInstance().Queue( batch_func, after_work_func, - shared_json_value{new json::Value{json::Object{}}}); + shared_json_value{new JsonValue{JsonObject{}}}); NativePlugin::ReportSuccess(out); } void AddressBook_batchFunc(NativeFunction impl, const char* single_arg_name, - const json::Object& args, json::Object& out) { - const json::Array& batch_args = FromJson(args, "batchArgs"); - const json::Object& address_book = - FromJson(args, "addressBook"); + const JsonObject& args, JsonObject& out) { + const JsonArray& batch_args = FromJson(args, "batchArgs"); + const JsonObject& address_book = FromJson(args, "addressBook"); auto work_func = [=](const shared_json_value & response)->void { - json::Object& response_obj = response->get(); - json::Array& batch_result = - response_obj.insert( - std::make_pair("result", json::Value{json::Array{}})) - .first->second.get(); + JsonObject& response_obj = response->get(); + JsonArray& batch_result = + response_obj.insert(std::make_pair("result", JsonValue{JsonArray{}})) + .first->second.get(); try { for (auto& item : batch_args) { - json::Object single_args{}; + JsonObject single_args{}; single_args.insert(std::make_pair("addressBook", address_book)); single_args.insert(std::make_pair(single_arg_name, item)); - json::Object single_out; + JsonObject single_out; impl(single_args, single_out); - batch_result.push_back(json::Value{single_out}); + batch_result.push_back(JsonValue{single_out}); } NativePlugin::ReportSuccess(response_obj); } @@ -340,20 +324,19 @@ void AddressBook_batchFunc(NativeFunction impl, const char* single_arg_name, callback_handle, response->serialize()); }; - TaskQueue::GetInstance().Queue( + TaskQueue::GetInstance().Queue( work_func, after_work_func, - shared_json_value{new json::Value{json::Object{}}}); + shared_json_value{new JsonValue{JsonObject{}}}); NativePlugin::ReportSuccess(out); } -void AddressBook_find(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); +void AddressBook_find(const JsonObject& args) { + ContactUtil::CheckDBConnection(); // TODO implement contact filter and sorting. LoggerD("Entered"); - const json::Object& address_book = - FromJson(args, "addressBook"); + const JsonObject& address_book = FromJson(args, "addressBook"); int callback_handle = NativePlugin::GetAsyncCallbackHandle(args); @@ -361,7 +344,7 @@ void AddressBook_find(const JsonObject& args, JsonObject& out) { LoggerD("Entered"); try { long addressbook_id = - common::stol(FromJson(address_book, "id")); + common::stol(FromJson(address_book, "id")); // Read calendar by ID int error_code = 0; @@ -391,8 +374,8 @@ void AddressBook_find(const JsonObject& args, JsonObject& out) { unsigned int record_count = 0; error_code = contacts_list_get_count(list, &record_count); - json::Value result{json::Array{}}; - json::Array& contacts = result.get(); + JsonValue result{JsonArray{}}; + JsonArray& contacts = result.get(); contacts_list_first(list); for (unsigned int i = 0; i < record_count; i++) { contacts_record_h record; @@ -405,15 +388,15 @@ void AddressBook_find(const JsonObject& args, JsonObject& out) { contacts_record_get_int(record, _contacts_contact.id, &id_value); ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); - contacts.push_back(json::Value(static_cast(id_value))); + contacts.push_back(JsonValue(static_cast(id_value))); contacts_list_next(list); } - NativePlugin::ReportSuccess(result, response->get()); + NativePlugin::ReportSuccess(result, response->get()); } catch (const BasePlatformException& e) { LoggerE("error: %s: %s", e.name().c_str(), e.message().c_str()); - NativePlugin::ReportError(e, response->get()); + NativePlugin::ReportError(e, response->get()); } }; @@ -423,15 +406,13 @@ void AddressBook_find(const JsonObject& args, JsonObject& out) { callback_handle, response->serialize()); }; - TaskQueue::GetInstance().Queue( - get, get_response, shared_json_value(new json::Value(json::Object()))); - - NativePlugin::ReportSuccess(out); + TaskQueue::GetInstance().Queue( + get, get_response, shared_json_value(new JsonValue(JsonObject()))); } void AddressBook_addGroup(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); - const json::Object& group = FromJson(args, "group"); + ContactUtil::CheckDBConnection(); + const JsonObject& group = FromJson(args, "group"); if (!IsNull(group, "id") || !IsNull(group, "addressBookId")) { LOGE("Group object is previously added"); throw common::InvalidValuesException("Group object is previously added"); @@ -447,7 +428,7 @@ void AddressBook_addGroup(const JsonObject& args, JsonObject& out) { ContactUtil::ContactsDeleter); long addressbook_id = - common::stol(FromJson(args, "addressBookId")); + common::stol(FromJson(args, "addressBookId")); addressbook_id = (IsUnified(addressbook_id)) ? 0 : addressbook_id; ContactUtil::SetIntInRecord(contacts_record, _contacts_group.address_book_id, addressbook_id); @@ -457,18 +438,15 @@ void AddressBook_addGroup(const JsonObject& args, JsonObject& out) { err = contacts_db_insert_record(contacts_record, &groupId); ContactUtil::ErrorChecker(err, "Error during insert group record"); - json::Value result{json::Object{}}; - json::Object& result_obj = result.get(); - result_obj.insert(std::make_pair("id", std::to_string(groupId))); - result_obj.insert( + out.insert(std::make_pair("id", std::to_string(groupId))); + out.insert( std::make_pair("addressBookId", std::to_string(addressbook_id))); - NativePlugin::ReportSuccess(result, out); } void AddressBook_getGroup(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); + ContactUtil::CheckDBConnection(); - long id = common::stol(FromJson(args, "id")); + long id = common::stol(FromJson(args, "id")); if (id < 0) { throw common::InvalidValuesException("Incorrect group id"); @@ -485,7 +463,7 @@ void AddressBook_getGroup(const JsonObject& args, JsonObject& out) { ContactUtil::ContactsDeleter); long addressbook_id = - common::stol(FromJson(args, "addressBook", "id")); + common::stol(FromJson(args, "addressBook", "id")); if (IsUnified(addressbook_id)) { int address_book_id = 0; ContactUtil::GetIntFromRecord( @@ -495,17 +473,13 @@ void AddressBook_getGroup(const JsonObject& args, JsonObject& out) { } } - json::Value result{json::Object{}}; - ContactUtil::ImportContactGroupFromContactsRecord( - contacts_record, &result.get()); - - NativePlugin::ReportSuccess(result, out); + ContactUtil::ImportContactGroupFromContactsRecord(contacts_record, &out); } -void AddressBook_updateGroup(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); +void AddressBook_updateGroup(const JsonObject& args) { + ContactUtil::CheckDBConnection(); - const json::Object& group = FromJson(args, "group"); + const JsonObject& group = FromJson(args, "group"); if (IsNull(group, "id") || IsNull(group, "addressBookId")) { LOGE("Group object is not added"); @@ -513,9 +487,9 @@ void AddressBook_updateGroup(const JsonObject& args, JsonObject& out) { } long addressbook_id = - common::stol(FromJson(args, "addressBookId")); + common::stol(FromJson(args, "addressBookId")); long group_addressbook_id = - common::stol(FromJson(group, "addressBookId")); + common::stol(FromJson(group, "addressBookId")); if (IsUnified(addressbook_id) && (addressbook_id != group_addressbook_id)) { LOGE("Wrong address book"); throw common::InvalidValuesException("Wrong address book"); @@ -523,12 +497,10 @@ void AddressBook_updateGroup(const JsonObject& args, JsonObject& out) { if (FromJson(group, "readOnly")) { LOGW("Group is readonly - cancel update"); - // TODO should this be an error? - NativePlugin::ReportSuccess(out); - return; + throw common::UnknownException("Group is readonly - cancel update"); } - long group_id = common::stol(FromJson(group, "id")); + long group_id = common::stol(FromJson(group, "id")); if (group_id < 0) { throw common::InvalidValuesException("Incorrect group id"); } @@ -556,14 +528,12 @@ void AddressBook_updateGroup(const JsonObject& args, JsonObject& out) { throw common::NotFoundException("Problem during db_update_record"); } ContactUtil::ErrorChecker(err, "Problem during db_update_record"); - - NativePlugin::ReportSuccess(out); } -void AddressBook_removeGroup(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); +void AddressBook_removeGroup(const JsonObject& args) { + ContactUtil::CheckDBConnection(); - long id = common::stol(FromJson(args, "id")); + long id = common::stol(FromJson(args, "id")); if (id < 0) { throw common::InvalidValuesException("Incorrect group id"); } @@ -595,11 +565,10 @@ void AddressBook_removeGroup(const JsonObject& args, JsonObject& out) { throw common::NotFoundException("Problem during db_delete_record"); } ContactUtil::ErrorChecker(err, "Problem during db_delete_record"); - NativePlugin::ReportSuccess(out); } void AddressBook_getGroups(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); + ContactUtil::CheckDBConnection(); int err = CONTACTS_ERROR_NONE; contacts_list_h groups_list = nullptr; @@ -641,8 +610,8 @@ void AddressBook_getGroups(const JsonObject& args, JsonObject& out) { contacts_list_first(groups_list); - json::Value result{json::Array{}}; - json::Array& array = result.get(); + JsonValue result{JsonArray{}}; + JsonArray& array = result.get(); for (unsigned int i = 0; i < record_count; i++) { contacts_record_h contacts_record; @@ -652,7 +621,7 @@ void AddressBook_getGroups(const JsonObject& args, JsonObject& out) { throw common::UnknownException("Fail to get group record"); } - JsonValue group{json::Object{}}; + JsonValue group{JsonObject{}}; ContactUtil::ImportContactGroupFromContactsRecord(contacts_record, &group.get()); array.push_back(group); @@ -663,14 +632,14 @@ void AddressBook_getGroups(const JsonObject& args, JsonObject& out) { break; } } - - NativePlugin::ReportSuccess(result, out); + out.insert(std::make_pair("result", result)); } namespace { void AddressBook_listenerCallback(const char* view_uri, void* user_data) { (void)view_uri; (void)user_data; + ContactUtil::CheckDBConnection(); int* last_database_version = static_cast(user_data); @@ -701,17 +670,16 @@ void AddressBook_listenerCallback(const char* view_uri, void* user_data) { contacts_list_first(contacts_list); - json::Value result{json::Object{}}; - json::Object& result_obj = result.get(); - json::Array& added = - result_obj.insert(std::make_pair("added", json::Array{})) - .first->second.get(); - json::Array& updated = - result_obj.insert(std::make_pair("updated", json::Array{})) - .first->second.get(); - json::Array& removed = - result_obj.insert(std::make_pair("removed", json::Array{})) - .first->second.get(); + JsonValue result{JsonObject{}}; + JsonObject& result_obj = result.get(); + JsonArray& added = result_obj.insert(std::make_pair("added", JsonArray{})) + .first->second.get(); + JsonArray& updated = + result_obj.insert(std::make_pair("updated", JsonArray{})) + .first->second.get(); + JsonArray& removed = + result_obj.insert(std::make_pair("removed", JsonArray{})) + .first->second.get(); for (unsigned int i = 0; i < count; i++) { contacts_record_h contact_updated_record = nullptr; @@ -760,9 +728,9 @@ void AddressBook_listenerCallback(const char* view_uri, void* user_data) { ContactUtil::ContactsRecordHPtr contact_record_ptr( &contacts_record, ContactUtil::ContactsDeleter); - json::Value contact{json::Object{}}; + JsonValue contact{JsonObject{}}; ContactUtil::ImportContactFromContactsRecord( - *contact_record_ptr, &contact.get()); + *contact_record_ptr, &contact.get()); if (CONTACTS_CHANGE_INSERTED == changed_type) { added.push_back(std::move(contact)); @@ -771,8 +739,8 @@ void AddressBook_listenerCallback(const char* view_uri, void* user_data) { } } else if (CONTACTS_CHANGE_DELETED == changed_type) { // Need to send the addressbook id with the removed id - json::Value removed_data{json::Object{}}; - json::Object& removed_data_obj = removed_data.get(); + JsonValue removed_data{JsonObject{}}; + JsonObject& removed_data_obj = removed_data.get(); removed_data_obj.insert( std::make_pair("id", std::to_string(changed_id))); @@ -787,8 +755,8 @@ void AddressBook_listenerCallback(const char* view_uri, void* user_data) { } } -void AddressBook_startListening(int* current_state, const JsonObject&, - JsonObject& out) { +void AddressBook_startListening(int* current_state, const JsonObject&) { + ContactUtil::CheckDBConnection(); // Set the initial latest version before registering the callback. // The callback should only be registered once so no race can occur. int error_code = contacts_db_get_current_version(current_state); @@ -805,11 +773,11 @@ void AddressBook_startListening(int* current_state, const JsonObject&, throw UnknownException("Error while registering listener to contacts db"); } - NativePlugin::ReportSuccess(out); + // NativePlugin::ReportSuccess(out); } -void AddressBook_stopListening(int* current_state, const JsonObject&, - JsonObject& out) { +void AddressBook_stopListening(int* current_state, const JsonObject&) { + ContactUtil::CheckDBConnection(); int error_code = contacts_db_remove_changed_cb( _contacts_contact._uri, AddressBook_listenerCallback, current_state); @@ -818,7 +786,7 @@ void AddressBook_stopListening(int* current_state, const JsonObject&, throw UnknownException("Error while removing listener"); } - NativePlugin::ReportSuccess(out); + // NativePlugin::ReportSuccess(out); } } // AddressBook diff --git a/src/contact/addressbook.h b/src/contact/addressbook.h index 4ef3dc1..746b7c3 100644 --- a/src/contact/addressbook.h +++ b/src/contact/addressbook.h @@ -28,12 +28,12 @@ namespace AddressBook { void AddressBook_get(const JsonObject& args, JsonObject& out); void AddressBook_add(const JsonObject& args, JsonObject& out); void AddressBook_update(const JsonObject& args, JsonObject& out); -void AddressBook_remove(const JsonObject& args, JsonObject& out); -void AddressBook_find(const JsonObject& args, JsonObject& out); +void AddressBook_remove(const JsonObject& args); +void AddressBook_find(const JsonObject& args); void AddressBook_addGroup(const JsonObject& args, JsonObject& out); void AddressBook_getGroup(const JsonObject& args, JsonObject& out); -void AddressBook_updateGroup(const JsonObject& args, JsonObject& out); -void AddressBook_removeGroup(const JsonObject& args, JsonObject& out); +void AddressBook_updateGroup(const JsonObject& args); +void AddressBook_removeGroup(const JsonObject& args); void AddressBook_getGroups(const JsonObject& args, JsonObject& out); void AddressBook_startListening(int* current_state, const JsonObject& args, JsonObject& out); diff --git a/src/contact/contact.gyp b/src/contact/contact.gyp new file mode 100644 index 0000000..e69de29 diff --git a/src/contact/contact_manager.cc b/src/contact/contact_manager.cc index 51fc531..fc7a507 100644 --- a/src/contact/contact_manager.cc +++ b/src/contact/contact_manager.cc @@ -40,16 +40,16 @@ const char* kTokenDelimiter = " ,:"; using namespace extension::common; using namespace wrt::common; -void ContactManager_getAddressBooks(const json::Object& args, - json::Object& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); - typedef std::shared_ptr shared_json_value; +void ContactManager_getAddressBooks(const JsonObject& args, + JsonObject& out) { + ContactUtil::CheckDBConnection(); + typedef std::shared_ptr shared_json_value; auto work_func = [=](const shared_json_value & response)->void { - json::Object& response_obj = response->get(); - json::Array& batch_result = + JsonObject& response_obj = response->get(); + JsonArray& batch_result = response_obj.insert( - std::make_pair("result", json::Value{json::Array{}})) - .first->second.get(); + std::make_pair("result", JsonValue{JsonArray{}})) + .first->second.get(); try { contacts_list_h address_book_list = nullptr; @@ -104,12 +104,12 @@ void ContactManager_getAddressBooks(const json::Object& args, continue; } - json::Object single_out; + JsonObject single_out; single_out.insert(std::make_pair("id", std::to_string(id))); single_out.insert(std::make_pair("name", name)); single_out.insert(std::make_pair( "readOnly", CONTACTS_ADDRESS_BOOK_MODE_READONLY == mode)); - batch_result.push_back(json::Value{single_out}); + batch_result.push_back(JsonValue{single_out}); contacts_list_next(*contacts_list_ptr); } @@ -127,20 +127,18 @@ void ContactManager_getAddressBooks(const json::Object& args, callback_handle, response->serialize()); }; - TaskQueue::GetInstance().Queue( + TaskQueue::GetInstance().Queue( work_func, after_work_func, - std::shared_ptr{new json::Value{json::Object{}}}); - - NativePlugin::ReportSuccess(out); + std::shared_ptr{new JsonValue{JsonObject{}}}); } -void ContactManager_getAddressBook(const json::Object& args, - json::Object& out) { +void ContactManager_getAddressBook(const JsonObject& args, JsonObject& out) { NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); + ContactUtil::CheckDBConnection(); long address_book_id; try { address_book_id = - common::stol(FromJson(args, "addressBookID")); + common::stol(FromJson(args, "addressBookID")); } catch (const common::InvalidValuesException&) { throw common::NotFoundException("Invalid id"); @@ -165,13 +163,10 @@ void ContactManager_getAddressBook(const json::Object& args, ContactUtil::GetStrFromRecord(contacts_record, _contacts_address_book.name, &name); - json::Value arguments = json::Value(json::Object()); - json::Object& arguments_obj = arguments.get(); - arguments_obj.insert(std::make_pair("name", std::string(name))); - arguments_obj.insert(std::make_pair( + out.insert(std::make_pair("name", std::string(name))); + out.insert(std::make_pair( "readOnly", ((CONTACTS_ADDRESS_BOOK_MODE_READONLY == mode) ? "true" : "false"))); - NativePlugin::ReportSuccess(arguments, out); } namespace { @@ -193,20 +188,18 @@ void ContactManager_get_internal(int person_id, JsonObject* out) { } } -void ContactManager_get(const json::Object& args, json::Object& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); - long person_id = common::stol(FromJson(args, "personID")); - - json::Value val{json::Object{}}; - ContactManager_get_internal(person_id, &val.get()); +void ContactManager_get(const JsonObject& args, JsonObject& out) { + ContactUtil::CheckDBConnection(); + long person_id = common::stol(FromJson(args, "personID")); - NativePlugin::ReportSuccess(val, out); + JsonValue val{JsonObject{}}; + ContactManager_get_internal(person_id, &out); } -void ContactManager_update(const json::Object& args, json::Object& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); - const json::Object& person = FromJson(args, "person"); - long person_id = common::stol(FromJson(person, "id")); +void ContactManager_update(const JsonObject& args) { + ContactUtil::CheckDBConnection(); + const JsonObject& person = FromJson(args, "person"); + long person_id = common::stol(FromJson(person, "id")); int error_code = 0; contacts_record_h contacts_record = nullptr; @@ -230,13 +223,11 @@ void ContactManager_update(const json::Object& args, json::Object& out) { throw UnknownException( "Error during executing contacts_db_update_record()"); } - - NativePlugin::ReportSuccess(out); } -void ContactManager_remove(const json::Object& args, json::Object& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); - long person_id = common::stol(FromJson(args, "personId")); +void ContactManager_remove(const JsonObject& args) { + ContactUtil::CheckDBConnection(); + long person_id = common::stol(FromJson(args, "personId")); if (person_id < 0) { throw common::InvalidValuesException("Negative person id"); @@ -247,19 +238,17 @@ void ContactManager_remove(const json::Object& args, json::Object& out) { LoggerE("Error during removing contact, error: %d", error_code); throw NotFoundException("Error during removing contact"); } - - NativePlugin::ReportSuccess(out); } -void ContactManager_find(const json::Object& args, json::Object& out) { - NativePlugin::CheckAccess(ContactUtil::kContactReadPrivileges); +void ContactManager_find(const JsonObject& args, JsonObject& out) { + ContactUtil::CheckDBConnection(); // TODO implement contact sorting. LoggerD("Entered"); int callback_handle = NativePlugin::GetAsyncCallbackHandle(args); - auto get = [args](const std::shared_ptr & response)->void { + auto get = [args](const std::shared_ptr & response)->void { LoggerD("Entered"); try { int error_code = 0; @@ -278,7 +267,7 @@ void ContactManager_find(const json::Object& args, json::Object& out) { FilterVisitor visitor; visitor.SetOnAttributeFilter([&](const std::string& name, AttributeMatchFlag match_flag, - const json::Value& match_value) { + const JsonValue& match_value) { const Person::PersonProperty& property = Person::PersonProperty_fromString(name); @@ -357,8 +346,8 @@ void ContactManager_find(const json::Object& args, json::Object& out) { }); visitor.SetOnAttributeRangeFilter([&](const std::string& name, - const json::Value& initial_value, - const json::Value& end_value) { + const JsonValue& initial_value, + const JsonValue& end_value) { const Person::PersonProperty& property = Person::PersonProperty_fromString(name); @@ -586,7 +575,7 @@ void ContactManager_find(const json::Object& args, json::Object& out) { intermediate_filters.back().push_back(std::move(merged_filter_ptr)); }); - visitor.Visit(FromJson(args, "filter")); + visitor.Visit(FromJson(args, "filter")); // Should compute only one filter always. if ((intermediate_filters.size() != 1) || (intermediate_filters[0].size() != 1)) { @@ -615,8 +604,8 @@ void ContactManager_find(const json::Object& args, json::Object& out) { ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); contacts_list_first(person_list); - json::Value result{json::Array{}}; - json::Array& persons = result.get(); + JsonValue result{JsonArray{}}; + JsonArray& persons = result.get(); for (unsigned int i = 0; i < record_count; i++) { contacts_record_h contacts_record; @@ -633,36 +622,36 @@ void ContactManager_find(const json::Object& args, json::Object& out) { ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); - persons.push_back(json::Value(static_cast(id_value))); + persons.push_back(JsonValue(static_cast(id_value))); contacts_list_next(person_list); } - NativePlugin::ReportSuccess(result, response->get()); + NativePlugin::ReportSuccess(result, response->get()); } catch (const BasePlatformException& e) { LoggerE("error: %s: %s", e.name().c_str(), e.message().c_str()); - NativePlugin::ReportError(e, response->get()); + NativePlugin::ReportError(e, response->get()); } }; - auto get_response = [callback_handle](const std::shared_ptr & + auto get_response = [callback_handle](const std::shared_ptr & response)->void { wrt::common::NativeContext::GetInstance()->InvokeCallback( callback_handle, response->serialize()); }; - TaskQueue::GetInstance().Queue( + TaskQueue::GetInstance().Queue( get, get_response, - std::shared_ptr(new json::Value(json::Object()))); + std::shared_ptr(new JsonValue(JsonObject()))); NativePlugin::ReportSuccess(out); } -void ContactManager_importFromVCard(const json::Object& args, - json::Object& out) { +void ContactManager_importFromVCard(const JsonObject& args, JsonObject& out) { // I'm not sure how to call it. Should it be 'Contact', 'vCard' or what? - const char* vcard_char_ptr = FromJson(args, "contact").c_str(); + ContactUtil::CheckDBConnection(); + const char* vcard_char_ptr = FromJson(args, "contact").c_str(); contacts_list_h contacts_list = nullptr; @@ -693,10 +682,8 @@ void ContactManager_importFromVCard(const json::Object& args, throw UnknownException("Invalid vCard string."); } - json::Value result{json::Object{}}; - ContactUtil::ImportContactFromContactsRecord(contacts_record, - &result.get()); - NativePlugin::ReportSuccess(result, out); + JsonValue result{JsonObject{}}; + ContactUtil::ImportContactFromContactsRecord(contacts_record, &out); } namespace { @@ -723,16 +710,16 @@ void ContactManager_listenerCallback(const char* view_uri, char* changes, return; } - json::Value result{json::Object{}}; - json::Object& result_obj = result.get(); - json::Array& added = result_obj.insert(std::make_pair("added", json::Array{})) - .first->second.get(); - json::Array& updated = - result_obj.insert(std::make_pair("updated", json::Array{})) - .first->second.get(); - json::Array& removed = - result_obj.insert(std::make_pair("removed", json::Array{})) - .first->second.get(); + JsonValue result{JsonObject{}}; + JsonObject& result_obj = result.get(); + JsonArray& added = result_obj.insert(std::make_pair("added", JsonArray{})) + .first->second.get(); + JsonArray& updated = + result_obj.insert(std::make_pair("updated", JsonArray{})) + .first->second.get(); + JsonArray& removed = + result_obj.insert(std::make_pair("removed", JsonArray{})) + .first->second.get(); std::unique_ptr tmp(strdup(changes), [](char* p) { free(p); }); @@ -750,20 +737,20 @@ void ContactManager_listenerCallback(const char* view_uri, char* changes, int person_id = atoi(token); switch (type) { case CONTACTS_CHANGE_INSERTED: { - added.push_back(json::Value{json::Object{}}); + added.push_back(JsonValue{JsonObject{}}); ContactManager_get_internal(person_id, - &added.back().get()); + &added.back().get()); break; } case CONTACTS_CHANGE_UPDATED: { - updated.push_back(json::Value{json::Object{}}); + updated.push_back(JsonValue{JsonObject{}}); ContactManager_get_internal(person_id, - &updated.back().get()); + &updated.back().get()); break; } case CONTACTS_CHANGE_DELETED: { std::string id_str{std::to_string(person_id)}; - removed.push_back(json::Value{id_str.c_str()}); + removed.push_back(JsonValue{id_str.c_str()}); break; } default: {} @@ -784,7 +771,8 @@ void ContactManager_listenerCallback(const char* view_uri, char* changes, } } -void ContactManager_startListening(const JsonObject&, JsonObject& out) { +void ContactManager_startListening(/*const JsonObject&, JsonObject& out*/) { + ContactUtil::CheckDBConnection(); int error_code = contacts_db_add_changed_cb_with_info( _contacts_person._uri, ContactManager_listenerCallback, nullptr); @@ -794,10 +782,11 @@ void ContactManager_startListening(const JsonObject&, JsonObject& out) { throw UnknownException("Failed to start listening"); } - NativePlugin::ReportSuccess(out); + //NativePlugin::ReportSuccess(out); } -void ContactManager_stopListening(const JsonObject&, JsonObject& out) { +void ContactManager_stopListening(/*const JsonObject&, JsonObject& out*/) { + ContactUtil::CheckDBConnection(); int error_code = contacts_db_remove_changed_cb_with_info( _contacts_person._uri, ContactManager_listenerCallback, nullptr); @@ -807,7 +796,7 @@ void ContactManager_stopListening(const JsonObject&, JsonObject& out) { throw UnknownException("Failed to stop listening"); } - NativePlugin::ReportSuccess(out); + //NativePlugin::ReportSuccess(out); } } // namespace ContactManager diff --git a/src/contact/contact_manager.h b/src/contact/contact_manager.h index 6a6b923..00a32e3 100644 --- a/src/contact/contact_manager.h +++ b/src/contact/contact_manager.h @@ -24,148 +24,25 @@ namespace extension { namespace contact { namespace ContactManager { -/** - * Signature: @code void getAddressBooks(successCallback, errorCallback); - * @endcode - * JSON: @code data: {method: 'ContactManager_getAddressBook', args: {}} - * @endcode - * Invocation: @code native.call(request, result_callback) @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - * Result callback: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success', result: {addressbooks}} - * @endcode - */ -void ContactManager_getAddressBooks(const JsonObject& /*args*/, - JsonObject& /*out*/); +void ContactManager_getAddressBooks(const JsonObject& args, + JsonObject& out); -/** - * Signature: @code AddressBook getAddressBook(addressBookId); @endcode - * JSON: @code data: {method: 'ContactManager_getAddressBook', - * args: {addressBookID: addressBookId}} @endcode - * Invocation: @code native.callSync(request) @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - */ void ContactManager_getAddressBook(const JsonObject& args, JsonObject& out); -/** - * Signature: @code Person get(personId); @endcode - * JSON: @code data: {method: 'ContactManager_get', - * args: {personID: personId}} @endcode - * Invocation: @code native.callSync(request); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - */ void ContactManager_get(const JsonObject& args, JsonObject& out); -/** - * Signature: @code void update(person); @endcode - * JSON: @code data: {method: 'ContactManager_update', - * args: {person: person}} @endcode - * Invocation: @code native.callSync(request); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - */ -void ContactManager_update(const JsonObject& args, JsonObject& out); +void ContactManager_update(const JsonObject& args); -/** - * Signature: @code void updateBatch(persons, successCallback, errorCallback); - * @endcode - * JSON: @code data: {method: 'ContactManager_updateBatch', - * args: {persons: persons}} @endcode - * Invocation: @code native.call(request, result_callback); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - * Result callback: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success', result: {persons}} - * @endcode - */ void ContactManager_updateBatch(const JsonObject& /*args*/, JsonObject& /*out*/); -/** - * Signature: @code void remove(personId); @endcode - * JSON: @code data: {method: 'ContactManager_remove', - * args: {personID: personId}} @endcode - * Invocation: @code native.callSync(request); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - */ -void ContactManager_remove(const JsonObject& args, JsonObject& out); +void ContactManager_remove(const JsonObject& args); -/** - * Signature: @code void removeBatch(personIds, successCallback, errorCallback); - * @endcode - * JSON: @code data: {method: 'ContactManager_removeBatch', - * args: {personsIDs: personIds}} @endcode - * Invocation: @code native.call(request, result_callback); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - * Result callback: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - */ void ContactManager_removeBatch(const JsonObject& /*args*/, JsonObject& /*out*/); -/** - * Signature: @code void find(successCallback, errorCallback, filter, sortMode); - * @endcode - * JSON: @code data: {method: 'ContactManager_find', - * args: {filter: filter, sortMode: sortMode}} @endcode - * Invocation: @code native.call(request, result_callback); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success'} - * @endcode - * Result callback: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success', result: {persons}} - * @endcode - */ void ContactManager_find(const JsonObject& /*args*/, JsonObject& /*out*/); -/** - * Signature: @code void getAddressBook(contactString); @endcode - * JSON: @code data: {method: 'ContactManager_importFromVCard', - * args: {contact: contactString}} @endcode - * Invocation: @code native.callSync(request); @endcode - * Return: - * @code - * {status: 'error', error: {name, message}} - * {status: 'success', result: {contact}} - * @endcode - */ void ContactManager_importFromVCard(const JsonObject& args, JsonObject& out); /** @@ -179,7 +56,7 @@ void ContactManager_importFromVCard(const JsonObject& args, JsonObject& out); * {status: 'success'} * @endcode */ -void ContactManager_startListening(const JsonObject& args, JsonObject& out); +void ContactManager_startListening(/*const JsonObject& args, JsonObject& out*/); /** * Signature: @code void getAddressBook(contactString); @endcode @@ -192,7 +69,7 @@ void ContactManager_startListening(const JsonObject& args, JsonObject& out); * {status: 'success'} * @endcode */ -void ContactManager_stopListening(const JsonObject& args, JsonObject& out); +void ContactManager_stopListening(/*const JsonObject& args, JsonObject& out*/); } // namespace ContactManager } // namespace contact diff --git a/src/contact/contact_util.cc b/src/contact/contact_util.cc index 57b7529..5d94682 100644 --- a/src/contact/contact_util.cc +++ b/src/contact/contact_util.cc @@ -25,10 +25,6 @@ namespace extension { namespace contact { namespace ContactUtil { -const char* kContactReadPrivileges = "http://tizen.org/privilege/contact.read"; -const char* kContactWritePrivileges = - "http://tizen.org/privilege/contact.write"; - namespace { static const std::string kSchema("file://"); @@ -296,7 +292,7 @@ void ExportBirthdayToContactsRecord(contacts_record_h contacts_record, bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; if (!contacts_record) { LOGW("Contacts record is null"); throw common::UnknownException("Contacts record is null"); @@ -378,9 +374,9 @@ bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, contacts_record, _contacts_contact.nickname, &count); ContactUtil::ErrorChecker(err, "Contacts child record get count error"); - json::Array& nicknames = - out.insert(std::make_pair("nicknames", json::Array())) - .first->second.get(); + JsonArray& nicknames = + out.insert(std::make_pair("nicknames", JsonArray())) + .first->second.get(); for (unsigned int i = 0; i < count; ++i) { contacts_record_h nickname = nullptr; err = contacts_record_get_child_record_at_p( @@ -398,7 +394,7 @@ bool ImportContactNameFromContactsRecord(contacts_record_h contacts_record, } void ExportContactNameToContactsRecord(contacts_record_h contacts_record, - const json::Object& in) { + const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -421,40 +417,40 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, if (!IsNull(in, "prefix")) { ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.prefix, - FromJson(in, "prefix").c_str()); + FromJson(in, "prefix").c_str()); } if (!IsNull(in, "suffix")) { ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.suffix, - FromJson(in, "suffix").c_str()); + FromJson(in, "suffix").c_str()); } if (!IsNull(in, "firstName")) { ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.first, - FromJson(in, "firstName").c_str()); + FromJson(in, "firstName").c_str()); } if (!IsNull(in, "middleName")) { ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.addition, - FromJson(in, "middleName").c_str()); + FromJson(in, "middleName").c_str()); } if (!IsNull(in, "lastName")) { ContactUtil::SetStrInRecord(*contacts_name_ptr, _contacts_name.last, - FromJson(in, "lastName").c_str()); + FromJson(in, "lastName").c_str()); } if (!IsNull(in, "phoneticFirstName")) { ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.phonetic_first, - FromJson(in, "phoneticFirstName").c_str()); + FromJson(in, "phoneticFirstName").c_str()); } if (!IsNull(in, "phoneticMiddleName")) { ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.phonetic_middle, - FromJson(in, "phoneticMiddleName").c_str()); + FromJson(in, "phoneticMiddleName").c_str()); } if (!IsNull(in, "phoneticLastName")) { ContactUtil::SetStrInRecord( *contacts_name_ptr, _contacts_name.phonetic_last, - FromJson(in, "phoneticLastName").c_str()); + FromJson(in, "phoneticLastName").c_str()); } // contact_name is being added as a child to contacts_record @@ -468,7 +464,7 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, // by its parent (contacts_record) contacts_name_ptr.release(); - const json::Array& nicknames = FromJson(in, "nicknames"); + const JsonArray& nicknames = FromJson(in, "nicknames"); for (auto& nickname : nicknames) { contacts_record_h nickname_record = nullptr; @@ -482,7 +478,7 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, ContactsRecordHPtr nickname_ptr(&nickname_record, ContactsDeleter); ContactUtil::SetStrInRecord(*nickname_ptr, _contacts_nickname.name, - JsonCast(nickname).c_str()); + JsonCast(nickname).c_str()); if (!update) { err = contacts_record_add_child_record( contacts_record, _contacts_contact.nickname, *nickname_ptr); @@ -498,7 +494,7 @@ void ExportContactNameToContactsRecord(contacts_record_h contacts_record, void ImportContactEmailAddressFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -529,7 +525,7 @@ void ImportContactEmailAddressFromContactsRecord( char* label = nullptr; ContactUtil::GetStrFromRecord(child_record, _contacts_email.label, &label); out.insert( - std::make_pair("label", label ? json::Value{label} : json::Value{})); + std::make_pair("label", label ? JsonValue{label} : JsonValue{})); int type = 0; ContactUtil::GetIntFromRecord(child_record, _contacts_email.type, &type); @@ -556,7 +552,7 @@ void ImportContactEmailAddressFromContactsRecord( } void ExportContactEmailAddressToContactsRecord( - contacts_record_h contacts_record, const json::Object& in) { + contacts_record_h contacts_record, const JsonObject& in) { contacts_record_h c_email_record_h = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here @@ -571,19 +567,19 @@ void ExportContactEmailAddressToContactsRecord( ContactsRecordHPtr record(&c_email_record_h, ContactsDeleter); ContactUtil::SetStrInRecord(c_email_record_h, _contacts_email.email, - FromJson(in, "email").c_str()); + FromJson(in, "email").c_str()); ContactUtil::SetBoolInRecord(c_email_record_h, _contacts_email.is_default, FromJson(in, "isDefault")); if (!IsNull(in, "label")) { ContactUtil::SetStrInRecord(c_email_record_h, _contacts_email.label, - FromJson(in, "label").c_str()); + FromJson(in, "label").c_str()); } int type_to_set = 0; - auto& types = FromJson(in, "types"); + auto& types = FromJson(in, "types"); for (auto& type : types) { - auto type_str = JsonCast(type); + auto type_str = JsonCast(type); if (type_str == kContactEmailAddressTypeWork) { type_to_set |= CONTACTS_EMAIL_TYPE_WORK; } else if (type_str == kContactEmailAddressTypeOther) { @@ -607,7 +603,7 @@ void ExportContactEmailAddressToContactsRecord( void ImportContactPhoneNumberFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; int err = CONTACTS_ERROR_NONE; contacts_record_h child_record = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed @@ -695,7 +691,7 @@ void ImportContactPhoneNumberFromContactsRecord( } void ExportContactPhoneNumberToContactsRecord(contacts_record_h contacts_record, - const json::Object& in) { + const JsonObject& in) { contacts_record_h phone_record = nullptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here @@ -710,19 +706,19 @@ void ExportContactPhoneNumberToContactsRecord(contacts_record_h contacts_record, ContactsRecordHPtr record(&phone_record, ContactsDeleter); ContactUtil::SetStrInRecord(phone_record, _contacts_number.number, - FromJson(in, "number").c_str()); + FromJson(in, "number").c_str()); ContactUtil::SetBoolInRecord(phone_record, _contacts_number.is_default, FromJson(in, "isDefault")); if (!IsNull(in, "label")) { ContactUtil::SetStrInRecord(phone_record, _contacts_address.label, - FromJson(in, "label").c_str()); + FromJson(in, "label").c_str()); } int type_to_set = 0; - const json::Array& types = FromJson(in, "types"); + const JsonArray& types = FromJson(in, "types"); for (auto& type : types) { - auto& type_str = JsonCast(type); + auto& type_str = JsonCast(type); if (type_str == kContactPhoneTypeHome) { type_to_set |= CONTACTS_NUMBER_TYPE_HOME; } else if (type_str == kContactPhoneTypeWork) { @@ -772,7 +768,7 @@ void ExportContactPhoneNumberToContactsRecord(contacts_record_h contacts_record, void ImportContactOrganizationFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -816,7 +812,7 @@ void ImportContactOrganizationFromContactsRecord( } void ExportContactOrganizationToContactsRecord( - contacts_record_h contacts_record, const json::Object& in) { + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -833,25 +829,25 @@ void ExportContactOrganizationToContactsRecord( if (!IsNull(in, "name")) { ContactUtil::SetStrInRecord(organization_record, _contacts_company.name, - FromJson(in, "name").c_str()); + FromJson(in, "name").c_str()); } if (!IsNull(in, "department")) { ContactUtil::SetStrInRecord( organization_record, _contacts_company.department, - FromJson(in, "department").c_str()); + FromJson(in, "department").c_str()); } if (!IsNull(in, "title")) { ContactUtil::SetStrInRecord(organization_record, _contacts_company.job_title, - FromJson(in, "title").c_str()); + FromJson(in, "title").c_str()); } if (!IsNull(in, "role")) { ContactUtil::SetStrInRecord(organization_record, _contacts_company.role, - FromJson(in, "role").c_str()); + FromJson(in, "role").c_str()); } if (!IsNull(in, "logoURI")) { std::string path = - ContactUtil::ConvertUriToPath(FromJson(in, "logoURI")); + ContactUtil::ConvertUriToPath(FromJson(in, "logoURI")); ContactUtil::SetStrInRecord(organization_record, _contacts_company.logo, path.c_str()); } @@ -866,7 +862,7 @@ void ExportContactOrganizationToContactsRecord( void ImportContactWebSiteFromContactsRecord(contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -895,7 +891,7 @@ void ImportContactWebSiteFromContactsRecord(contacts_record_h contacts_record, } void ExportContactWebSiteToContactsRecord(contacts_record_h contacts_record, - const json::Object& in) { + const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -908,7 +904,7 @@ void ExportContactWebSiteToContactsRecord(contacts_record_h contacts_record, return; } - const std::string& url = FromJson(in, "url"); + const std::string& url = FromJson(in, "url"); if (url.empty()) { LOGD("WebSite urls are not set"); return; @@ -923,7 +919,7 @@ void ExportContactWebSiteToContactsRecord(contacts_record_h contacts_record, ContactUtil::SetStrInRecord(website_record_h, _contacts_url.url, url.c_str()); int type_to_set = - (FromJson(in, "type") == kContactWebSiteTypeHomePage) + (FromJson(in, "type") == kContactWebSiteTypeHomePage) ? CONTACTS_URL_TYPE_HOME : CONTACTS_URL_TYPE_WORK; @@ -941,7 +937,7 @@ void ExportContactWebSiteToContactsRecord(contacts_record_h contacts_record, bool ImportContactAnniversariesFromContactsRecord( contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -972,13 +968,13 @@ bool ImportContactAnniversariesFromContactsRecord( ContactUtil::GetStrFromRecord(child_record, _contacts_event.label, &label); out.insert( - std::make_pair("label", label ? json::Value{label} : json::Value{})); + std::make_pair("label", label ? JsonValue{label} : JsonValue{})); } return true; } void ExportContactAnniversariesToContactsRecord( - contacts_record_h contacts_record, const json::Object& in) { + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1005,7 +1001,7 @@ void ExportContactAnniversariesToContactsRecord( if (!IsNull(in, "label")) { ContactUtil::SetStrInRecord(anniversary_record, _contacts_event.label, - FromJson(in, "label").c_str()); + FromJson(in, "label").c_str()); } err = contacts_record_add_child_record( @@ -1017,8 +1013,8 @@ void ExportContactAnniversariesToContactsRecord( void ImportContactRelationshipFromContactsRecord( contacts_record_h contacts_record, unsigned int index, - json::Object* out_ptr) { - json::Object& out = *out_ptr; + JsonObject* out_ptr) { + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1041,7 +1037,7 @@ void ImportContactRelationshipFromContactsRecord( return; } - out.insert(std::make_pair("relativeName", json::String{relative})); + out.insert(std::make_pair("relativeName", JsonString{relative})); int type = 0; ContactUtil::GetIntFromRecord(child_record, _contacts_relationship.type, @@ -1051,68 +1047,68 @@ void ImportContactRelationshipFromContactsRecord( switch (type) { case CONTACTS_RELATIONSHIP_TYPE_CUSTOM: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeCustom})); + std::make_pair("type", JsonString{kContactRelationshipTypeCustom})); break; case CONTACTS_RELATIONSHIP_TYPE_ASSISTANT: out.insert(std::make_pair( - "type", json::String{kContactRelationshipTypeAssistant})); + "type", JsonString{kContactRelationshipTypeAssistant})); break; case CONTACTS_RELATIONSHIP_TYPE_BROTHER: out.insert(std::make_pair("type", - json::String{kContactRelationshipTypeBrother})); + JsonString{kContactRelationshipTypeBrother})); break; case CONTACTS_RELATIONSHIP_TYPE_CHILD: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeChild})); + std::make_pair("type", JsonString{kContactRelationshipTypeChild})); break; case CONTACTS_RELATIONSHIP_TYPE_DOMESTIC_PARTNER: out.insert(std::make_pair( - "type", json::String{kContactRelationshipTypeDomesticPartner})); + "type", JsonString{kContactRelationshipTypeDomesticPartner})); break; case CONTACTS_RELATIONSHIP_TYPE_FATHER: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeFather})); + std::make_pair("type", JsonString{kContactRelationshipTypeFather})); break; case CONTACTS_RELATIONSHIP_TYPE_FRIEND: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeFriend})); + std::make_pair("type", JsonString{kContactRelationshipTypeFriend})); break; case CONTACTS_RELATIONSHIP_TYPE_MANAGER: out.insert(std::make_pair("type", - json::String{kContactRelationshipTypeManager})); + JsonString{kContactRelationshipTypeManager})); break; case CONTACTS_RELATIONSHIP_TYPE_MOTHER: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeMother})); + std::make_pair("type", JsonString{kContactRelationshipTypeMother})); break; case CONTACTS_RELATIONSHIP_TYPE_PARENT: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeParent})); + std::make_pair("type", JsonString{kContactRelationshipTypeParent})); break; case CONTACTS_RELATIONSHIP_TYPE_PARTNER: out.insert(std::make_pair("type", - json::String{kContactRelationshipTypePartner})); + JsonString{kContactRelationshipTypePartner})); break; case CONTACTS_RELATIONSHIP_TYPE_REFERRED_BY: out.insert(std::make_pair( - "type", json::String{kContactRelationshipTypeReferredBy})); + "type", JsonString{kContactRelationshipTypeReferredBy})); break; case CONTACTS_RELATIONSHIP_TYPE_RELATIVE: out.insert(std::make_pair( - "type", json::String{kContactRelationshipTypeRelative})); + "type", JsonString{kContactRelationshipTypeRelative})); break; case CONTACTS_RELATIONSHIP_TYPE_SISTER: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeSister})); + std::make_pair("type", JsonString{kContactRelationshipTypeSister})); break; case CONTACTS_RELATIONSHIP_TYPE_SPOUSE: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeSpouse})); + std::make_pair("type", JsonString{kContactRelationshipTypeSpouse})); break; case CONTACTS_RELATIONSHIP_TYPE_OTHER: default: out.insert( - std::make_pair("type", json::String{kContactRelationshipTypeOther})); + std::make_pair("type", JsonString{kContactRelationshipTypeOther})); break; } @@ -1120,11 +1116,11 @@ void ImportContactRelationshipFromContactsRecord( ContactUtil::GetStrFromRecord(child_record, _contacts_relationship.label, &label); out.insert( - std::make_pair("label", label ? json::Value{label} : json::Value{})); + std::make_pair("label", label ? JsonValue{label} : JsonValue{})); } void ExportContactRelationshipToContactsRecord( - contacts_record_h contacts_record, const json::Object& in) { + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1140,9 +1136,9 @@ void ExportContactRelationshipToContactsRecord( ContactUtil::SetStrInRecord( child_record, _contacts_relationship.name, - FromJson(in, "relativeName").c_str()); + FromJson(in, "relativeName").c_str()); - const json::String& type = FromJson(in, "type"); + const JsonString& type = FromJson(in, "type"); int type_to_set; if (type == kContactRelationshipTypeAssistant) { type_to_set = CONTACTS_RELATIONSHIP_TYPE_ASSISTANT; @@ -1183,7 +1179,7 @@ void ExportContactRelationshipToContactsRecord( if (!IsNull(in, "label")) { ContactUtil::SetStrInRecord(child_record, _contacts_relationship.label, - FromJson(in, "label").c_str()); + FromJson(in, "label").c_str()); } err = contacts_record_add_child_record( @@ -1194,8 +1190,8 @@ void ExportContactRelationshipToContactsRecord( void ImportContactInstantMessengerFromContactsRecord( contacts_record_h contacts_record, unsigned int index, - json::Object* out_ptr) { - json::Object& out = *out_ptr; + JsonObject* out_ptr) { + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1220,7 +1216,7 @@ void ImportContactInstantMessengerFromContactsRecord( return; } - out.insert(std::make_pair("imAddress", json::Value{im_address})); + out.insert(std::make_pair("imAddress", JsonValue{im_address})); int type = 0; ContactUtil::GetIntFromRecord(child_record, _contacts_messenger.type, &type); @@ -1229,52 +1225,52 @@ void ImportContactInstantMessengerFromContactsRecord( switch (type) { case CONTACTS_MESSENGER_TYPE_CUSTOM: out.insert(std::make_pair("type", - json::Value{kContactInstantMessageTypeCustom})); + JsonValue{kContactInstantMessageTypeCustom})); break; case CONTACTS_MESSENGER_TYPE_GOOGLE: out.insert(std::make_pair("type", - json::Value{kContactInstantMessageTypeGoogle})); + JsonValue{kContactInstantMessageTypeGoogle})); break; case CONTACTS_MESSENGER_TYPE_WLM: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeWlm})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeWlm})); break; case CONTACTS_MESSENGER_TYPE_YAHOO: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeYahoo})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeYahoo})); break; case CONTACTS_MESSENGER_TYPE_FACEBOOK: out.insert(std::make_pair( - "type", json::Value{kContactInstantMessageTypeFacebook})); + "type", JsonValue{kContactInstantMessageTypeFacebook})); break; case CONTACTS_MESSENGER_TYPE_ICQ: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeIcq})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeIcq})); break; case CONTACTS_MESSENGER_TYPE_AIM: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeAim})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeAim})); break; case CONTACTS_MESSENGER_TYPE_QQ: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeQq})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeQq})); break; case CONTACTS_MESSENGER_TYPE_JABBER: out.insert(std::make_pair("type", - json::Value{kContactInstantMessageTypeJabber})); + JsonValue{kContactInstantMessageTypeJabber})); break; case CONTACTS_MESSENGER_TYPE_SKYPE: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeSkype})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeSkype})); break; case CONTACTS_MESSENGER_TYPE_IRC: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeIrc})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeIrc})); break; case CONTACTS_MESSENGER_TYPE_OTHER: default: out.insert( - std::make_pair("type", json::Value{kContactInstantMessageTypeOther})); + std::make_pair("type", JsonValue{kContactInstantMessageTypeOther})); break; } @@ -1282,11 +1278,11 @@ void ImportContactInstantMessengerFromContactsRecord( ContactUtil::GetStrFromRecord(child_record, _contacts_messenger.label, &label); out.insert( - std::make_pair("label", label ? json::Value{label} : json::Value{})); + std::make_pair("label", label ? JsonValue{label} : JsonValue{})); } void ExportContactInstantMessengerToContactsRecord( - contacts_record_h contacts_record, const json::Object& in) { + contacts_record_h contacts_record, const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1301,10 +1297,10 @@ void ExportContactInstantMessengerToContactsRecord( ContactsRecordHPtr record(&child_record, ContactsDeleter); ContactUtil::SetStrInRecord(child_record, _contacts_messenger.im_id, - FromJson(in, "imAddress").c_str()); + FromJson(in, "imAddress").c_str()); int type_to_set = 0; - const json::String& type = FromJson(in, "type"); + const JsonString& type = FromJson(in, "type"); if (type == kContactInstantMessageTypeGoogle) { type_to_set = CONTACTS_MESSENGER_TYPE_GOOGLE; } else if (type == kContactInstantMessageTypeWlm) { @@ -1336,7 +1332,7 @@ void ExportContactInstantMessengerToContactsRecord( if (!IsNull(in, "label")) { ContactUtil::SetStrInRecord(child_record, _contacts_messenger.label, - FromJson(in, "label").c_str()); + FromJson(in, "label").c_str()); } err = contacts_record_add_child_record( @@ -1348,7 +1344,7 @@ void ExportContactInstantMessengerToContactsRecord( void ImportContactAddressFromContactsRecord(contacts_record_h contacts_record, unsigned int index, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1417,7 +1413,7 @@ void ImportContactAddressFromContactsRecord(contacts_record_h contacts_record, } void ExportContactAddressToContactsRecord(contacts_record_h contacts_record, - const json::Object& in) { + const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1433,43 +1429,43 @@ void ExportContactAddressToContactsRecord(contacts_record_h contacts_record, if (!IsNull(in, "country")) { ContactUtil::SetStrInRecord(address_record, _contacts_address.country, - FromJson(in, "country").c_str()); + FromJson(in, "country").c_str()); } if (!IsNull(in, "region")) { ContactUtil::SetStrInRecord(address_record, _contacts_address.region, - FromJson(in, "region").c_str()); + FromJson(in, "region").c_str()); } if (!IsNull(in, "city")) { ContactUtil::SetStrInRecord(address_record, _contacts_address.locality, - FromJson(in, "city").c_str()); + FromJson(in, "city").c_str()); } if (!IsNull(in, "streetAddress")) { ContactUtil::SetStrInRecord( address_record, _contacts_address.street, - FromJson(in, "streetAddress").c_str()); + FromJson(in, "streetAddress").c_str()); } if (!IsNull(in, "additionalInformation")) { ContactUtil::SetStrInRecord( address_record, _contacts_address.extended, - FromJson(in, "additionalInformation").c_str()); + FromJson(in, "additionalInformation").c_str()); } if (!IsNull(in, "postalCode")) { ContactUtil::SetStrInRecord( address_record, _contacts_address.postal_code, - FromJson(in, "postalCode").c_str()); + FromJson(in, "postalCode").c_str()); } if (!IsNull(in, "label")) { ContactUtil::SetStrInRecord(address_record, _contacts_address.label, - FromJson(in, "label").c_str()); + FromJson(in, "label").c_str()); } ContactUtil::SetBoolInRecord(address_record, _contacts_address.is_default, FromJson(in, "isDefault")); int type_to_set = 0; - const json::Array& types = FromJson(in, "types"); + const JsonArray& types = FromJson(in, "types"); for (auto& type : types) { - auto& type_str = JsonCast(type); + auto& type_str = JsonCast(type); if (type_str == kContactAddressTypeWork) { type_to_set |= CONTACTS_ADDRESS_TYPE_WORK; } else if (type_str == kContactAddressTypeOther) { @@ -1544,7 +1540,7 @@ void ExportNotesToContactsRecord(contacts_record_h contacts_record, void ImportContactFromContactsRecord(contacts_record_h contacts_record, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1578,7 +1574,7 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, if (ImportContactNameFromContactsRecord(contacts_record, &name)) { out.insert(std::make_pair("name", name)); } else { - out.insert(std::make_pair("name", json::Value{})); + out.insert(std::make_pair("name", JsonValue{})); } typedef void (*ImportFunc)(contacts_record_h, unsigned int, JsonObject*); @@ -1603,26 +1599,26 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, ImportContactRelationshipFromContactsRecord}, }; for (auto& data : imports) { - json::Array& array = out.insert(std::make_pair(data.name, json::Array())) - .first->second.get(); + JsonArray& array = out.insert(std::make_pair(data.name, JsonArray())) + .first->second.get(); for (unsigned int i = 0, n = ContactUtil::GetNumberOfChildRecord( contacts_record, data.property_id); i < n; ++i) { - JsonValue val{json::Object{}}; + JsonValue val{JsonObject{}}; data.import_func(contacts_record, i, &val.get()); array.push_back(val); } } //### ContactAnniversary: ### - json::Array& anniversaries = - out.insert(std::make_pair("anniversaries", json::Array())) - .first->second.get(); + JsonArray& anniversaries = + out.insert(std::make_pair("anniversaries", JsonArray())) + .first->second.get(); for (unsigned int i = 0, n = ContactUtil::GetNumberOfChildRecord( contacts_record, _contacts_contact.event); i < n; ++i) { - JsonValue anniversary{json::Object{}}; + JsonValue anniversary{JsonObject{}}; if (ImportContactAnniversariesFromContactsRecord( contacts_record, i, &anniversary.get())) { anniversaries.push_back(anniversary); @@ -1632,8 +1628,8 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, } //### m_notes: ### - json::Array& notes = out.insert(std::make_pair("notes", json::Array())) - .first->second.get(); + JsonArray& notes = out.insert(std::make_pair("notes", JsonArray())) + .first->second.get(); for (unsigned int i = 0, n = ContactUtil::GetNumberOfChildRecord( contacts_record, _contacts_contact.note); i < n; ++i) { @@ -1657,18 +1653,18 @@ void ImportContactFromContactsRecord(contacts_record_h contacts_record, _contacts_contact.message_alert, &value); out.insert(std::make_pair( "messageAlertURI", - value ? json::Value{ConvertPathToUri(value)} : json::Value{})); + value ? JsonValue{ConvertPathToUri(value)} : JsonValue{})); value = nullptr; ContactUtil::GetStrFromRecord(contacts_record, _contacts_contact.vibration, &value); out.insert(std::make_pair( "vibrationURI", - value ? json::Value{ConvertPathToUri(value)} : json::Value{})); + value ? JsonValue{ConvertPathToUri(value)} : JsonValue{})); } } void ExportContactToContactsRecord(contacts_record_h contacts_record, - const json::Object& in) { + const JsonObject& in) { // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1679,10 +1675,10 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, //### ContactName: ### if (!IsNull(in, "name")) { ExportContactNameToContactsRecord(contacts_record, - FromJson(in, "name")); + FromJson(in, "name")); } - typedef void (*ExportFunc)(contacts_record_h, const json::Object&); + typedef void (*ExportFunc)(contacts_record_h, const JsonObject&); struct ExportDataHelper { unsigned int property_id; const char* name; @@ -1707,18 +1703,18 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, for (auto& data : exports) { ContactUtil::ClearAllContactRecord(contacts_record, data.property_id); - const json::Array& elements = FromJson(in, data.name); + const JsonArray& elements = FromJson(in, data.name); for (auto& element : elements) { - data.export_func(contacts_record, JsonCast(element)); + data.export_func(contacts_record, JsonCast(element)); } } { //### m_notes: ### ContactUtil::ClearAllContactRecord(contacts_record, _contacts_contact.note); - const json::Array& elements = FromJson(in, "notes"); + const JsonArray& elements = FromJson(in, "notes"); for (auto& element : elements) { - auto& str = JsonCast(element); + auto& str = JsonCast(element); if (str.empty()) { continue; } @@ -1758,7 +1754,7 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, std::string real_path; if (!IsNull(in, "photoURI")) { real_path = - ContactUtil::ConvertUriToPath(FromJson(in, "photoURI")); + ContactUtil::ConvertUriToPath(FromJson(in, "photoURI")); ContactUtil::SetStrInRecord(child_record, _contacts_image.path, real_path.c_str()); } @@ -1776,14 +1772,14 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, // Contact.ringtoneURI if (!IsNull(in, "ringtoneURI")) { real_path = ContactUtil::ConvertUriToPath( - FromJson(in, "ringtoneURI")); + FromJson(in, "ringtoneURI")); ContactUtil::SetStrInRecord( contacts_record, _contacts_contact.ringtone_path, real_path.c_str()); } // Contact.messageAlertURI if (!IsNull(in, "messageAlertURI")) { real_path = ContactUtil::ConvertUriToPath( - FromJson(in, "messageAlertURI")); + FromJson(in, "messageAlertURI")); ContactUtil::SetStrInRecord( contacts_record, _contacts_contact.message_alert, real_path.c_str()); } @@ -1791,15 +1787,15 @@ void ExportContactToContactsRecord(contacts_record_h contacts_record, // Contact.vibrationURI if (!IsNull(in, "vibrationURI")) { real_path = ContactUtil::ConvertUriToPath( - FromJson(in, "vibrationURI")); + FromJson(in, "vibrationURI")); ContactUtil::SetStrInRecord(contacts_record, _contacts_contact.vibration, real_path.c_str()); } } void ImportContactGroupFromContactsRecord(contacts_record_h contacts_record, - json::Object* out_ptr) { - json::Object& out = *out_ptr; + JsonObject* out_ptr) { + JsonObject& out = *out_ptr; // contacts_record is protected by unique_ptr and its ownership is not passed // here if (!contacts_record) { @@ -1810,13 +1806,13 @@ void ImportContactGroupFromContactsRecord(contacts_record_h contacts_record, // id int int_val = 0; ContactUtil::GetIntFromRecord(contacts_record, _contacts_group.id, &int_val); - out.insert(std::make_pair("id", json::Value{std::to_string(int_val)})); + out.insert(std::make_pair("id", JsonValue{std::to_string(int_val)})); // addressBookId ContactUtil::GetIntFromRecord(contacts_record, _contacts_group.address_book_id, &int_val); out.insert( - std::make_pair("addressBookId", json::Value{std::to_string(int_val)})); + std::make_pair("addressBookId", JsonValue{std::to_string(int_val)})); // name char* value = nullptr; @@ -1841,26 +1837,26 @@ void ImportContactGroupFromContactsRecord(contacts_record_h contacts_record, bool bool_value = false; ContactUtil::GetBoolFromRecord(contacts_record, _contacts_group.is_read_only, &bool_value); - out.insert(std::make_pair("readOnly", json::Value{bool_value})); + out.insert(std::make_pair("readOnly", JsonValue{bool_value})); } void ExportContactGroupToContactsRecord(contacts_record_h contacts_record, - const json::Object& in) { + const JsonObject& in) { // name ContactUtil::SetStrInRecord(contacts_record, _contacts_group.name, - FromJson(in, "name").c_str()); + FromJson(in, "name").c_str()); std::string real_path; // photoURI if (!IsNull(in, "photoURI")) { - real_path = ConvertUriToPath(FromJson(in, "photoURI")); + real_path = ConvertUriToPath(FromJson(in, "photoURI")); ContactUtil::SetStrInRecord(contacts_record, _contacts_group.image_path, real_path.c_str()); } // ringtoneURI if (!IsNull(in, "ringtoneURI")) { real_path = ContactUtil::ConvertUriToPath( - FromJson(in, "ringtoneURI")); + FromJson(in, "ringtoneURI")); // NOTE in the original code real path was not read ContactUtil::SetStrInRecord(contacts_record, _contacts_group.ringtone_path, real_path.c_str()); @@ -1872,61 +1868,61 @@ void ExportContactGroupToContactsRecord(contacts_record_h contacts_record, * @param[in] contacts_record_h Record which is used to fill Person */ void ImportPersonFromContactsRecord(contacts_record_h record, - json::Object* out_ptr) { + JsonObject* out_ptr) { if (nullptr == record) { LoggerW("Platform person record did not set"); throw InvalidValuesException("Platform person record did not set"); } - json::Object& arguments_obj = *out_ptr; + JsonObject& arguments_obj = *out_ptr; int int_value = 0; // id ContactUtil::GetIntFromRecord(record, _contacts_person.id, &int_value); arguments_obj.insert( - std::make_pair("id", json::Value(std::to_string(int_value)))); + std::make_pair("id", JsonValue(std::to_string(int_value)))); char* char_value = nullptr; // displayName ContactUtil::GetStrFromRecord(record, _contacts_person.display_name, &char_value); arguments_obj.insert(std::make_pair( - "displayName", char_value ? json::Value(char_value) : json::Value{})); + "displayName", char_value ? JsonValue(char_value) : JsonValue{})); // contactCount ContactUtil::GetIntFromRecord(record, _contacts_person.link_count, &int_value); arguments_obj.insert(std::make_pair( - "contactCount", json::Value(static_cast(int_value)))); + "contactCount", JsonValue(static_cast(int_value)))); bool bool_value = false; // hasPhoneNumber ContactUtil::GetBoolFromRecord(record, _contacts_person.has_phonenumber, &bool_value); arguments_obj.insert( - std::make_pair("hasPhoneNumber", json::Value(bool_value))); + std::make_pair("hasPhoneNumber", JsonValue(bool_value))); // hasEmail ContactUtil::GetBoolFromRecord(record, _contacts_person.has_email, &bool_value); - arguments_obj.insert(std::make_pair("hasEmail", json::Value(bool_value))); + arguments_obj.insert(std::make_pair("hasEmail", JsonValue(bool_value))); // isFavorite ContactUtil::GetBoolFromRecord(record, _contacts_person.is_favorite, &bool_value); - arguments_obj.insert(std::make_pair("isFavorite", json::Value(bool_value))); + arguments_obj.insert(std::make_pair("isFavorite", JsonValue(bool_value))); // photoURI ContactUtil::GetStrFromRecord(record, _contacts_person.image_thumbnail_path, &char_value); arguments_obj.insert(std::make_pair( - "photoURI", char_value ? json::Value(char_value) : json::Value{})); + "photoURI", char_value ? JsonValue(char_value) : JsonValue{})); // ringtoneURI ContactUtil::GetStrFromRecord(record, _contacts_person.ringtone_path, &char_value); arguments_obj.insert(std::make_pair( - "ringtoneURI", char_value ? json::Value(char_value) : json::Value{})); + "ringtoneURI", char_value ? JsonValue(char_value) : JsonValue{})); // displayContactId ContactUtil::GetIntFromRecord(record, _contacts_person.display_contact_id, @@ -1940,7 +1936,7 @@ void ImportPersonFromContactsRecord(contacts_record_h record, * @param[out] contacts_record_h Record which is updated */ void ExportPersonToContactsRecord(contacts_record_h record, - const json::Object& args) { + const JsonObject& args) { if (nullptr == record) { LoggerE("Platform person object did not set"); throw UnknownException("Platform person object did not set"); @@ -1950,10 +1946,10 @@ void ExportPersonToContactsRecord(contacts_record_h record, FromJson(args, "isFavorite")); try { if (!IsNull(args, "photoURI") && - !FromJson(args, "photoURI").empty()) { + !FromJson(args, "photoURI").empty()) { ContactUtil::SetStrInRecord( record, _contacts_person.image_thumbnail_path, - FromJson(args, "photoURI").c_str()); + FromJson(args, "photoURI").c_str()); } else { ContactUtil::SetStrInRecord(record, _contacts_person.image_thumbnail_path, ""); @@ -1965,37 +1961,52 @@ void ExportPersonToContactsRecord(contacts_record_h record, if (!IsNull(args, "ringtoneURI")) { ContactUtil::SetStrInRecord( record, _contacts_person.ringtone_path, - FromJson(args, "ringtoneURI").c_str()); + FromJson(args, "ringtoneURI").c_str()); } else { ContactUtil::SetStrInRecord(record, _contacts_person.ringtone_path, ""); } if (!IsNull(args, "displayContactId")) { ContactUtil::SetIntInRecord( record, _contacts_person.display_contact_id, - common::stol(FromJson(args, "displayContactId"))); + common::stol(FromJson(args, "displayContactId"))); } } void UpdateAdditionalInformation(const ContactsRecordHPtr& contacts_record_ptr, JsonObject* out_ptr) { - json::Object& out = *out_ptr; + JsonObject& out = *out_ptr; int int_value = -1; ContactUtil::GetIntFromRecord(*contacts_record_ptr, _contacts_contact.person_id, &int_value); out.insert( - std::make_pair("personId", json::Value{std::to_string(int_value)})); + std::make_pair("personId", JsonValue{std::to_string(int_value)})); ContactUtil::GetIntFromRecord(*contacts_record_ptr, _contacts_contact.address_book_id, &int_value); out.insert( - std::make_pair("addressBookId", json::Value{std::to_string(int_value)})); + std::make_pair("addressBookId", JsonValue{std::to_string(int_value)})); ContactUtil::GetIntFromRecord(*contacts_record_ptr, _contacts_contact.changed_time, &int_value); out.insert(std::make_pair("lastUpdated", - json::Value{static_cast(int_value)})); + JsonValue{static_cast(int_value)})); bool bool_value = false; ContactUtil::GetBoolFromRecord(*contacts_record_ptr, _contacts_contact.is_favorite, &bool_value); - out.insert(std::make_pair("isFavorite", json::Value{bool_value})); + out.insert(std::make_pair("isFavorite", JsonValue{bool_value})); +} + +void CheckDBConnection() +{ + static bool _connected = false; + if(_connected) + return; + + int err = contacts_connect(); + if (err == CONTACTS_ERROR_NONE) { + LoggerI("Connection established!"); + } else { + LoggerE("DB connection error occured: " << err); + throw UnknownException("DB connection error occured: " << err); + } } } // ContactUtil diff --git a/src/contact/contact_util.h b/src/contact/contact_util.h index ad4481f..c5b363e 100644 --- a/src/contact/contact_util.h +++ b/src/contact/contact_util.h @@ -30,12 +30,10 @@ namespace contact { typedef picojson::value JsonValue; typedef picojson::object JsonObject; typedef picojson::array JsonArray; +typedef std::string JsonString; namespace ContactUtil { -extern const char *kContactReadPrivileges; -extern const char *kContactWritePrivileges; - void ContactsDeleter(contacts_record_h *contacts_record); typedef std::unique_ptr ContactsRecordHPtr; @@ -145,6 +143,8 @@ void ExportContactGroupToContactsRecord(contacts_record_h contacts_record, void ImportPersonFromContactsRecord(contacts_record_h contacts_record, JsonObject *out); +void CheckDBConnection(); + } // ContactUtil } // contact } // extension diff --git a/src/contact/person.cc b/src/contact/person.cc index 2e477d7..0f7d2ed 100644 --- a/src/contact/person.cc +++ b/src/contact/person.cc @@ -38,11 +38,11 @@ static const PersonPropertyMap personPropertyMap = { {"displayContactId", { _contacts_person.display_contact_id, kPrimitiveTypeId } }, }; -void Person_link(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); +void Person_link(const JsonObject& args) { + ContactUtil::CheckDBConnection(); - long id = common::stol(FromJson(args, "id")); - long person_id = common::stol(FromJson(args, "person", "id")); + long id = common::stol(FromJson(args, "id")); + long person_id = common::stol(FromJson(args, "person", "id")); contacts_record_h contacts_record = nullptr; @@ -61,14 +61,12 @@ void Person_link(const JsonObject& args, JsonObject& out) { LOGW("person link fails, error code: %d", err); throw common::UnknownException("Error during executing person link()"); } - - NativePlugin::ReportSuccess(out); } -void Person_unlink(const JsonObject& args, JsonObject& out) { - NativePlugin::CheckAccess(ContactUtil::kContactWritePrivileges); +void Person_unlink(const JsonObject& args) { + ContactUtil::CheckDBConnection(); - long contact_id = common::stol(FromJson(args, "id")); + long contact_id = common::stol(FromJson(args, "id")); contacts_record_h contacts_record = nullptr; int error_code = contacts_db_get_record(_contacts_simple_contact._uri, @@ -92,7 +90,7 @@ void Person_unlink(const JsonObject& args, JsonObject& out) { throw common::UnknownException("Contact is not a member of person"); } - long person_id = common::stol(FromJson(args, "person", "id")); + long person_id = common::stol(FromJson(args, "person", "id")); if (contacts_person_id != person_id) { LOGW("Contact is not a member of person (wrong id's)"); throw common::InvalidValuesException("Contact is not a member of person"); @@ -116,14 +114,12 @@ void Person_unlink(const JsonObject& args, JsonObject& out) { throw common::UnknownException("Person not found"); } - json::Value person{json::Object{}}; + JsonValue person{JsonObject{}}; ContactUtil::ImportPersonFromContactsRecord(contacts_record, - &person.get()); + &person.get()); contacts_record_destroy(contacts_record, true); contacts_record = nullptr; - - NativePlugin::ReportSuccess(person, out); } const PersonProperty& PersonProperty_fromString(const std::string& name) { diff --git a/src/contact/person.h b/src/contact/person.h index e9fde46..85540ff 100644 --- a/src/contact/person.h +++ b/src/contact/person.h @@ -33,8 +33,8 @@ typedef std::map PersonPropertyMap; const PersonProperty& PersonProperty_fromString(const std::string& name); -void Person_link(const JsonObject& args, JsonObject& out); -void Person_unlink(const JsonObject& args, JsonObject& out); +void Person_link(const JsonObject& args); +void Person_unlink(const JsonObject& args); } // Person } // contact -- 2.7.4