[Contact] Added filtering in ContactManagerListenerCallback callback.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 19 May 2015 09:43:19 +0000 (11:43 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 19 May 2015 09:45:45 +0000 (11:45 +0200)
[Verification] PersonsChangeCallback_onpersonsadded passes, TCT pass rate: 441/442.

Change-Id: I290617bd2e5a9df944b98832e0d1403f0584c6b5
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/contact/contact_manager.cc

index 3e230b8b57d55faa184c4b3b1edd9c85bdbeef62..e48a641f697b06832e4aaf6bd49a4e392f2f44b7 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "contact/contact_manager.h"
 #include <memory>
+#include <set>
 
 #include "common/converter.h"
 #include "common/picojson.h"
@@ -810,6 +811,8 @@ bool IsNumeric(const char* s) {
 
 void ContactManagerListenerCallback(const char* view_uri, char* changes,
                                     void* user_data) {
+  LoggerD("ContactManagerListenerCallback");
+
   (void)view_uri;
 
   if (nullptr == changes) {
@@ -821,6 +824,9 @@ void ContactManagerListenerCallback(const char* view_uri, char* changes,
     return;
   }
 
+  SLoggerD("view_uri: %s", view_uri);
+  SLoggerD("changes: %s", changes);
+
   JsonValue result{JsonObject{}};
   JsonObject& result_obj = result.get<JsonObject>();
   result_obj.insert(std::make_pair(std::string("listenerId"),
@@ -835,6 +841,11 @@ void ContactManagerListenerCallback(const char* view_uri, char* changes,
   std::unique_ptr<char, void (*)(char*)> tmp(strdup(changes),
                                              [](char* p) { free(p); });
 
+  // 'changes' may contain repeated values, we need to filter it
+  std::set<int> added_ids;
+  std::set<int> updated_ids;
+  std::set<int> removed_ids;
+
   char* token = strtok(tmp.get(), kTokenDelimiter);
   while (token) {
     if (IsNumeric(token)) {
@@ -847,35 +858,45 @@ void ContactManagerListenerCallback(const char* view_uri, char* changes,
         int person_id = atoi(token);
         switch (type) {
           case CONTACTS_CHANGE_INSERTED: {
-            added.push_back(JsonValue{JsonObject{}});
-            PlatformResult status = ContactManagerGetInternal(
-                person_id, &added.back().get<JsonObject>());
-            if (status.IsError()) {
-              LoggerE("Caught exception in listener callback: %s",
-                      status.message().c_str());
-              return;
+            if (added_ids.find(person_id) == added_ids.end()) {
+              added.push_back(JsonValue{JsonObject{}});
+              PlatformResult status = ContactManagerGetInternal(
+                  person_id, &added.back().get<JsonObject>());
+              if (status.IsError()) {
+                LoggerE("Caught exception in listener callback: %s",
+                        status.message().c_str());
+                return;
+              }
+              added_ids.insert(person_id);
             }
 
             break;
           }
           case CONTACTS_CHANGE_UPDATED: {
-            updated.push_back(JsonValue{JsonObject{}});
-            PlatformResult status = ContactManagerGetInternal(
-                person_id, &updated.back().get<JsonObject>());
-            if (status.IsError()) {
-              LoggerE("Caught exception in listener callback: %s",
-                      status.message().c_str());
-              return;
+            if (updated_ids.find(person_id) == updated_ids.end()) {
+              updated.push_back(JsonValue{JsonObject{}});
+              PlatformResult status = ContactManagerGetInternal(
+                  person_id, &updated.back().get<JsonObject>());
+              if (status.IsError()) {
+                LoggerE("Caught exception in listener callback: %s",
+                        status.message().c_str());
+                return;
+              }
+              updated_ids.insert(person_id);
             }
 
             break;
           }
           case CONTACTS_CHANGE_DELETED: {
-            std::string id_str{std::to_string(person_id)};
-            removed.push_back(JsonValue{id_str.c_str()});
+            if (removed_ids.find(person_id) == removed_ids.end()) {
+              removed.push_back(JsonValue{std::to_string(person_id)});
+              removed_ids.insert(person_id);
+            }
+            break;
+          }
+          default: {
             break;
           }
-          default: {}
         }
       }
     }