| 2535 | } |
| 2536 | |
| 2537 | void BookmarkManager::MoveCategoryToPosition(kml::MarkGroupId categoryId, size_t targetPos) |
| 2538 | { |
| 2539 | CHECK_THREAD_CHECKER(m_threadChecker, ()); |
| 2540 | |
| 2541 | auto entryName = GetMetadataEntryName(categoryId); |
| 2542 | if (entryName.empty()) |
| 2543 | { |
| 2544 | auto * group = GetBmCategory(categoryId); |
| 2545 | std::string name = RemoveInvalidSymbols(group->GetName()); |
| 2546 | if (name.empty()) |
| 2547 | name = kDefaultBookmarksFileName; |
| 2548 | entryName = GenerateUniqueFileName(GetBookmarksDirectory(), std::move(name), kKmlExtension); |
| 2549 | group->SetFileName(entryName); |
| 2550 | } |
| 2551 | |
| 2552 | auto const entryBasename = base::FileNameFromFullPath(entryName); |
| 2553 | auto & catOrder = m_metadata.m_categoryOrder; |
| 2554 | if (catOrder.empty()) |
| 2555 | { |
| 2556 | auto const sorted = GetSortedBmGroupIdList(); |
| 2557 | for (auto const & id : sorted) |
| 2558 | { |
| 2559 | auto const fn = GetMetadataEntryName(id); |
| 2560 | if (!fn.empty()) |
| 2561 | catOrder.push_back(base::FileNameFromFullPath(fn)); |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | auto it = std::find(catOrder.begin(), catOrder.end(), entryBasename); |
| 2566 | if (it == catOrder.end()) |
| 2567 | { |
| 2568 | catOrder.push_back(entryBasename); |
| 2569 | it = std::prev(catOrder.end()); |
| 2570 | } |
| 2571 | |
| 2572 | auto const currentIdx = static_cast<size_t>(std::distance(catOrder.begin(), it)); |
| 2573 | if (currentIdx == targetPos || targetPos >= catOrder.size()) |
| 2574 | return; |
| 2575 | |
| 2576 | // Move by repeatedly swapping toward target position. |
| 2577 | if (targetPos > currentIdx) |
| 2578 | { |
| 2579 | for (size_t i = currentIdx; i < targetPos; ++i) |
| 2580 | std::swap(catOrder[i], catOrder[i + 1]); |
| 2581 | } |
| 2582 | else |
| 2583 | { |
| 2584 | for (size_t i = currentIdx; i > targetPos; --i) |
| 2585 | std::swap(catOrder[i], catOrder[i - 1]); |
| 2586 | } |
| 2587 | |
| 2588 | SaveMetadata(); |
| 2589 | NotifyBookmarksChanged(); |
| 2590 | NotifyCategoriesChanged(); |
| 2591 | } |
| 2592 | |
| 2593 | BookmarkManager::CategorySortType BookmarkManager::NormalizeCategorySortType(CategorySortType sortType) |
| 2594 | { |
no test coverage detected