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(
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) {
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) {
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);