[Bookmark] Fixed implementation of BookmarkTitleExistsInParent method.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 8 Jun 2015 07:42:34 +0000 (09:42 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 8 Jun 2015 07:42:34 +0000 (09:42 +0200)
Prevent CID: 387890

[Verification] TCT pass rate: 36/36.

Change-Id: Ifca74a28401d05ff69f733da64fd88dba4b6ac3d
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/bookmark/bookmark_instance.cc
src/bookmark/bookmark_instance.h

index 2b3353774115baf15c61ff2dd1ae8c37fb739cba..b8c603c16a7d83ac2b0321f63d0c5c636d2c4664 100755 (executable)
@@ -127,40 +127,55 @@ PlatformResult BookmarkInstance::BookmarkUrlExists(const char* url,
   return result;
 }
 
-bool BookmarkInstance::bookmark_title_exists_in_parent(
-    const char* title, int parent) {
+PlatformResult BookmarkInstance::BookmarkTitleExistsInParent(const char* title,
+                                                             int parent,
+                                                             bool* exists) {
 
   LoggerD("Enter");
   int ids_count = 0;
   int compare_parent = -1;
-  int* ids = NULL;
-  char* compare_title = NULL;
+  int* ids = nullptr;
+  char* compare_title = nullptr;
 
-  if (bp_bookmark_adaptor_get_ids_p(&ids, &ids_count, -1, 0, -1, -1, -1, -1,
-                                    BP_BOOKMARK_O_DATE_CREATED, 0) < 0)
-    return true;
-  if (ids_count > 0) {
-    for (int i = 0; i < ids_count; i++) {
-      if (bp_bookmark_adaptor_get_title(ids[i], &compare_title) < 0) {
-        free(ids);
-        return true;
-      }
-      if (bp_bookmark_adaptor_get_parent_id(ids[i], &compare_parent) < 0) {
-        free(compare_title);
-        free(ids);
-        return true;
-      }
-      if (strcmp(title, compare_title) == 0
-        && (parent == compare_parent)) {
-        free(compare_title);
-        free(ids);
-        return true;
-      }
+  if (bp_bookmark_adaptor_get_ids_p(&ids,  // ids
+                                    &ids_count,  // count
+                                    -1,  //limit
+                                    0,  // offset
+                                    -1,  //parent
+                                    -1,  //type
+                                    -1,  // is_operator
+                                    -1,  // is_editable
+                                    BP_BOOKMARK_O_DATE_CREATED,  // order_offset
+                                    0  // ordering ASC
+                                    ) < 0) {
+    LoggerE("Failed to obtain bookmarks");
+    return PlatformResult{ErrorCode::UNKNOWN_ERR, "Failed to obtain bookmarks"};
+  }
+
+  PlatformResult result{ErrorCode::NO_ERROR};
+  bool title_found = false;
+  for (int i = 0; (i < ids_count) && result && !title_found; ++i) {
+    if (bp_bookmark_adaptor_get_parent_id(ids[i], &compare_parent) < 0) {
+      LoggerE("Failed to obtain parent ID");
+      result = PlatformResult{ErrorCode::UNKNOWN_ERR, "Failed to obtain parent ID"};
+    } else if (bp_bookmark_adaptor_get_title(ids[i], &compare_title) < 0) {
+      LoggerE("Failed to obtain title");
+      result = PlatformResult{ErrorCode::UNKNOWN_ERR, "Failed to obtain title"};
+    } else {
+      title_found = (parent == compare_parent) && (0 == strcmp(title, compare_title));
+      free(compare_title);
+      compare_title = nullptr;
+      compare_parent = -1;
     }
   }
-  free(compare_title);
+
+  if (result) {
+    *exists = title_found;
+  }
+
   free(ids);
-  return false;
+
+  return result;
 }
 
 void BookmarkInstance::BookmarkGet(
@@ -209,7 +224,7 @@ void BookmarkInstance::BookmarkAdd(
   data.type   = arg.get(kType).get<double>();
   data.url    = const_cast<char*>(arg.get(kUrl).to_str().c_str());
 
-  if (!data.type) { // bookmark
+  if (!data.type) {  // bookmark
     bool exists = false;
     auto result = BookmarkUrlExists(data.url, &exists);
     if (!result) {
@@ -220,9 +235,17 @@ void BookmarkInstance::BookmarkAdd(
       return;
     }
   }
-  if (data.type && bookmark_title_exists_in_parent(data.title, data.parent)) {
-    ReportError(o);
-    return;
+
+  if (data.type) {  // folder
+    bool exists = false;
+    auto result = BookmarkTitleExistsInParent(data.title, data.parent, &exists);
+    if (!result) {
+      ReportError(result, &o);
+      return;
+    } else if (exists) {
+      ReportError(PlatformResult{ErrorCode::UNKNOWN_ERR, "Bookmark already exists"}, &o);
+      return;
+    }
   }
 
   if (bp_bookmark_adaptor_create(&saved_id) < 0) {
index eb958994095aa51f9b2688947e8f53b3f3258c9e..74de11b3371b6417e942821e62f57860783653e7 100755 (executable)
@@ -46,7 +46,8 @@ class BookmarkInstance : public common::ParsedInstance {
  private:
   bool bookmark_foreach(Context& ctx, bp_bookmark_info_fmt& info);
   common::PlatformResult BookmarkUrlExists(const char* url, bool* exists);
-  bool bookmark_title_exists_in_parent(const char* title, int parent);
+  common::PlatformResult BookmarkTitleExistsInParent(const char* title,
+                                                     int parent, bool* exists);
   void BookmarkGet(const picojson::value& arg, picojson::object& o);
   void BookmarkAdd(const picojson::value& arg, picojson::object& o);
   void BookmarkRemove(const picojson::value& arg, picojson::object& o);