From: Pawel Andruszkiewicz Date: Mon, 8 Jun 2015 07:42:34 +0000 (+0200) Subject: [Bookmark] Fixed implementation of BookmarkTitleExistsInParent method. X-Git-Tag: submit/tizen_mobile/20150612.133019^2~2^2~42 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4cc4d6a4d9c482950e5ce051557d6967e78496a5;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Bookmark] Fixed implementation of BookmarkTitleExistsInParent method. Prevent CID: 387890 [Verification] TCT pass rate: 36/36. Change-Id: Ifca74a28401d05ff69f733da64fd88dba4b6ac3d Signed-off-by: Pawel Andruszkiewicz --- diff --git a/src/bookmark/bookmark_instance.cc b/src/bookmark/bookmark_instance.cc index 2b335377..b8c603c1 100755 --- a/src/bookmark/bookmark_instance.cc +++ b/src/bookmark/bookmark_instance.cc @@ -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(); data.url = const_cast(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) { diff --git a/src/bookmark/bookmark_instance.h b/src/bookmark/bookmark_instance.h index eb958994..74de11b3 100755 --- a/src/bookmark/bookmark_instance.h +++ b/src/bookmark/bookmark_instance.h @@ -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);