Kmz name.

This commit is contained in:
Daria Volvenkova 2018-04-13 12:43:49 +03:00 committed by Aleksandr Zatsepin
parent 85c79adc01
commit aaf461d3c3
3 changed files with 13 additions and 10 deletions

View file

@ -46,6 +46,7 @@ std::string const kLastEditedBookmarkCategory = "LastBookmarkCategory";
// TODO(darina): Delete old setting.
std::string const kLastBookmarkType = "LastBookmarkType";
std::string const kLastEditedBookmarkColor = "LastBookmarkColor";
std::string const kDefaultBookmarksFileName = "Bookmarks";
std::string const kKmzExtension = ".kmz";
std::string const kKmlExtension = ".kml";
std::string const kKmbExtension = ".kmb";
@ -117,8 +118,11 @@ public:
BookmarkManager::SharingResult GetFileForSharing(BookmarkManager::KMLDataCollectionPtr collection)
{
auto const & kmlToShare = collection->front();
auto const filePath = my::JoinPath(
GetPlatform().TmpDir(), my::GetNameFromFullPathWithoutExt(kmlToShare.first) + kKmlExtension);
auto const categoryName = kml::GetDefaultStr(kmlToShare.second->m_categoryData.m_name);
std::string fileName = BookmarkManager::RemoveInvalidSymbols(categoryName, "");
if (fileName.empty())
fileName = my::GetNameFromFullPathWithoutExt(kmlToShare.first);
auto const filePath = my::JoinPath(GetPlatform().TmpDir(), fileName + kKmlExtension);
MY_SCOPE_GUARD(fileGuard, std::bind(&FileWriter::DeleteFileX, filePath));
auto const categoryId = kmlToShare.second->m_categoryData.m_id;
@ -129,7 +133,6 @@ BookmarkManager::SharingResult GetFileForSharing(BookmarkManager::KMLDataCollect
"Bookmarks file does not exist.");
}
auto const fileName = my::GetNameFromFullPathWithoutExt(filePath);
auto const tmpFilePath = my::JoinPath(GetPlatform().TmpDir(), fileName + kKmzExtension);
if (!CreateZipFromPathDeflatedAndDefaultCompression(filePath, tmpFilePath))
{
@ -1426,7 +1429,7 @@ BookmarkManager::KMLDataCollectionPtr BookmarkManager::PrepareToSaveBookmarks(
auto * group = GetBmCategory(groupId);
// Get valid file name from category name
std::string const name = RemoveInvalidSymbols(group->GetName());
std::string const name = RemoveInvalidSymbols(group->GetName(), kDefaultBookmarksFileName);
std::string file = group->GetFileName();
if (file.empty())
@ -1887,12 +1890,12 @@ void BookmarkManager::MarksChangesTracker::ResetChanges()
}
// static
std::string BookmarkManager::RemoveInvalidSymbols(std::string const & name)
std::string BookmarkManager::RemoveInvalidSymbols(std::string const & name, std::string const & defaultName)
{
// Remove not allowed symbols
strings::UniString uniName = strings::MakeUniString(name);
uniName.erase_if(&IsBadCharForPath);
return (uniName.empty() ? "Bookmarks" : strings::ToUtf8(uniName));
return (uniName.empty() ? defaultName : strings::ToUtf8(uniName));
}
// static
@ -1919,14 +1922,14 @@ std::string BookmarkManager::GenerateUniqueFileName(const std::string & path, st
// static
std::string BookmarkManager::GenerateValidAndUniqueFilePathForKML(std::string const & fileName)
{
std::string filePath = RemoveInvalidSymbols(fileName);
std::string filePath = RemoveInvalidSymbols(fileName, kDefaultBookmarksFileName);
return GenerateUniqueFileName(GetPlatform().SettingsDir(), filePath, kKmlExtension);
}
// static
std::string BookmarkManager::GenerateValidAndUniqueFilePathForKMB(std::string const & fileName)
{
std::string filePath = RemoveInvalidSymbols(fileName);
std::string filePath = RemoveInvalidSymbols(fileName, kDefaultBookmarksFileName);
return GenerateUniqueFileName(GetBookmarksDirectory(), filePath, kKmbExtension);
}

View file

@ -270,7 +270,7 @@ public:
bool SaveBookmarkCategory(kml::MarkGroupId groupId);
bool SaveBookmarkCategory(kml::MarkGroupId groupId, Writer & writer, bool useBinary) const;
void CreateCategories(KMLDataCollection && dataCollection, bool autoSave = true);
static std::string RemoveInvalidSymbols(std::string const & name);
static std::string RemoveInvalidSymbols(std::string const & name, std::string const & defaultName);
static std::string GenerateUniqueFileName(std::string const & path, std::string name, std::string const & fileExt);
static std::string GenerateValidAndUniqueFilePathForKML(std::string const & fileName);
static std::string GenerateValidAndUniqueFilePathForKMB(std::string const & fileName);

View file

@ -471,7 +471,7 @@ UNIT_TEST(Bookmarks_IllegalFileName)
for (size_t i = 0; i < arrIllegal.size(); ++i)
{
string const name = BookmarkManager::RemoveInvalidSymbols(arrIllegal[i]);
string const name = BookmarkManager::RemoveInvalidSymbols(arrIllegal[i], "Bookmarks");
if (arrLegal[i].empty())
{