[bookmarks] Added file save/load methods

This commit is contained in:
Alex Zolotarev 2012-09-02 23:17:46 +03:00 committed by Alex Zolotarev
parent 113d7e655c
commit 3d8dd56e9d
3 changed files with 69 additions and 0 deletions

View file

@ -2,11 +2,15 @@
#include "../indexer/mercator.hpp"
#include "../platform/platform.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/parse_xml.hpp" // LoadFromKML
#include "../base/stl_add.hpp"
#include "../base/string_utils.hpp"
#include "../std/fstream.hpp"
#include "../std/algorithm.hpp"
@ -169,6 +173,23 @@ void BookmarkCategory::LoadFromKML(ReaderPtr<Reader> const & reader)
ParseXML(src, parser, true);
}
BookmarkCategory * BookmarkCategory::CreateFromKMLFile(string const & file)
{
BookmarkCategory * cat = new BookmarkCategory("");
try
{
cat->LoadFromKML(new FileReader(file));
cat->m_file = file;
}
catch (std::exception const & e)
{
LOG(LWARNING, ("Error while loading bookmarks from", file, e.what()));
delete cat;
cat = 0;
}
return cat;
}
namespace
{
char const * kmlHeader =
@ -265,3 +286,33 @@ void BookmarkCategory::SaveToKML(ostream & s)
s << kmlFooter;
}
bool BookmarkCategory::SaveToKMLFileAtPath(string const & path)
{
if (m_file.empty())
{
// Generate unique file name from category name
string newName = m_name + ".kml";
// Remove not allowed symbols
char const illegalChars[] = ":/";
for (size_t i = 0; i < ARRAY_SIZE(illegalChars); ++i)
newName.erase(std::remove(newName.begin(), newName.end(), illegalChars[i]), newName.end());
if (newName.empty())
newName = "Bookmarks";
while (Platform::IsFileExistsByFullPath(path + newName))
newName.append(strings::to_string(m_name.size()));
m_file = path + newName;
}
try
{
// @TODO On Windows UTF-8 file names are not supported.
ofstream fileToSave(m_file.c_str());
SaveToKML(fileToSave);
}
catch (std::exception const & e)
{
LOG(LWARNING, ("Can't save bookmarks catebory", m_name, "to file", m_file));
return false;
}
return true;
}

View file

@ -36,6 +36,8 @@ class BookmarkCategory : private noncopyable
string m_name;
vector<Bookmark *> m_bookmarks;
bool m_visible;
/// Stores file name from which category was loaded
string m_file;
public:
BookmarkCategory(string const & name) : m_name(name), m_visible(true) {}
@ -49,6 +51,7 @@ public:
bool IsVisible() const { return m_visible; }
void SetName(string const & name) { m_name = name; }
string GetName() const { return m_name; }
string GetFileName() const { return m_file; }
inline size_t GetBookmarksCount() const { return m_bookmarks.size(); }
Bookmark const * GetBookmark(size_t index) const;
@ -56,6 +59,12 @@ public:
void LoadFromKML(ReaderPtr<Reader> const & reader);
void SaveToKML(ostream & s);
/// @return 0 in the case of error
static BookmarkCategory * CreateFromKMLFile(string const & file);
/// Uses the same file name from which was loaded, or
/// creates unique file name on first save and uses it every time
/// @param[in] path directory name where to save
bool SaveToKMLFileAtPath(string const & path);
};
/// Non-const category is needed to "edit" bookmark (actually, re-add it)

View file

@ -149,6 +149,15 @@ UNIT_TEST(Bookmarks_ExportKML)
CheckBookmarks(cat);
TEST_EQUAL(cat.IsVisible(), true, ());
BookmarkCategory * cat2 = BookmarkCategory::CreateFromKMLFile(BOOKMARKS_FILE_NAME);
CheckBookmarks(*cat2);
FileWriter::DeleteFileX(BOOKMARKS_FILE_NAME);
cat2->SaveToKMLFileAtPath("./");
delete cat2;
cat2 = BookmarkCategory::CreateFromKMLFile(BOOKMARKS_FILE_NAME);
CheckBookmarks(*cat2);
FileWriter::DeleteFileX(BOOKMARKS_FILE_NAME);
}