[Contact] Added filtering in ContactManagerListenerCallback callback.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 19 May 2015 09:43:19 +0000 (11:43 +0200)
committerHyunjin Park <hj.na.park@samsung.com>
Wed, 3 Jun 2015 04:25:20 +0000 (13:25 +0900)
[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 bf965c450bbeac340a8114f04dcb63577ec4dfcf..95452c20447a3162519d2a9894a492dbfab3d8a5 100755 (executable)
@@ -16,6 +16,7 @@
 
 #include "contact/contact_manager.h"
 #include <memory>
+#include <set>
 
 #include "common/converter.h"
 #include "common/picojson.h"
@@ -833,6 +834,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"),
@@ -847,6 +851,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)) {
@@ -859,35 +868,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: {}
         }
       }
     }