/* Copyright 2021, Michele Santullo
* This file is part of remarkable_tool.
*
* remarkable_tool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Remarkable_tool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with remarkable_tool. If not, see .
*/
#include "notebook_tree.hpp"
#include
#include
#include
#include
#include
#include
namespace fs = std::filesystem;
namespace duck {
namespace {
typedef std::unordered_map NotebookMapType;
template
std::vector& operator+= (std::vector& dst, const std::vector& src) {
dst.insert(dst.end(), src.begin(), src.end());
return dst;
}
NotebookMapType build_notebook_infos (
const fs::path& from_path
) {
simdjson::dom::parser parser;
NotebookMapType retval;
for (const auto& entry : fs::directory_iterator(from_path)) {
NotebookInfo& curr_info = retval[entry.path().stem()];
if (entry.path().extension() == ".metadata") {
simdjson::dom::object json_doc = parser.load(entry.path());
curr_info.deleted = json_doc["deleted"];
curr_info.parent = json_doc["parent"];
curr_info.type = json_doc["type"];
curr_info.version = json_doc["version"];
curr_info.visible_name = json_doc["visibleName"];
curr_info.isolated = false;
}
else if (fs::is_directory(entry)) {
for (const auto& subentry : fs::recursive_directory_iterator(entry)) {
curr_info.files.push_back(fs::relative(subentry, from_path));
if (fs::is_regular_file(subentry))
curr_info.total_size += fs::file_size(subentry);
}
}
curr_info.files.push_back(fs::relative(entry, from_path));
if (fs::is_regular_file(entry))
curr_info.total_size += fs::file_size(entry);
}
return retval;
}
void assert_parents_correct (const Node* node) {
#if !defined(NDEBUG)
for (const auto& child : node->children) {
assert(child.parent == node);
assert_parents_correct(&child);
}
#else
static_cast(node);
#endif
}
std::vector to_tree (const NotebookMapType& nbs) {
std::unordered_map> parents;
for (const auto& nb : nbs) {
auto it_ins = parents.emplace(nb.second.parent, std::make_pair(0U, Node{nb.second.parent, nullptr, nullptr}));
it_ins.first->second.first++;
}
for (const auto& nb : nbs) {
auto it_parent = parents.find(nb.first);
if (parents.cend() != it_parent) {
Node& node = it_parent->second.second;
node.info = &nb.second;
}
}
for (auto& par : parents) {
Node& node = par.second.second;
const unsigned int children_count = par.second.first;
node.children.reserve(children_count);
}
for (const auto& nb : nbs) {
if (0 == parents.count(nb.first)) {
Node& parent = parents[nb.second.parent].second;
parent.children.emplace_back(nb.first, &nb.second, &parent);
}
}
std::vector roots;
std::unordered_map grouped_nodes;
for (auto it = parents.begin(), it_end = parents.end(); it != it_end; it = parents.erase(it)) {
auto& node = it->second.second;
if (node.info) {
auto it_parent = parents.find(node.info->parent);
Node* dst_node = (parents.end() == it_parent ? grouped_nodes[node.info->parent] : &it_parent->second.second);
node.parent = dst_node;
assert(dst_node->children.capacity() > dst_node->children.size());
dst_node->children.push_back(std::move(node));
for (auto& child : dst_node->children.back().children) {
child.parent = &dst_node->children.back();
}
grouped_nodes[dst_node->children.back().name] = &dst_node->children.back();
}
else {
node.parent = nullptr;
roots.push_back(std::move(node));
grouped_nodes[node.name] = &roots.back();
}
}
for (auto& root : roots) {
for (auto& child : root.children) {
child.parent = &root;
}
}
return roots;
}
std::vector to_flat_notebook_list (const Node& tree) {
std::vector retval;
for (const auto& child : tree.children) {
retval += to_flat_notebook_list(child);
}
if (tree.info)
retval.push_back(tree.info);
return retval;
}
std::vector make_dele_list (const Node& tree) {
if (tree.info and (tree.info->deleted or tree.info->isolated))
return to_flat_notebook_list(tree);
std::vector retval;
for (const auto& child : tree.children) {
retval += make_dele_list(child);
}
return retval;
}
} //unnamed namespace
struct Notebooks::LocalData {
NotebookMapType nb_map;
};
Notebooks::Notebooks (std::string_view base_path) :
m_local(std::make_unique())
{
m_local->nb_map = build_notebook_infos(base_path);
}
Notebooks::~Notebooks() noexcept = default;
std::vector Notebooks::build_tree() const {
auto tree = to_tree(m_local->nb_map);
for (const auto& root : tree) {
assert_parents_correct(&root);
}
return tree; //to_tree(m_local->nb_map);
}
std::vector make_list_of_deleted (const std::vector& roots) {
std::vector dele_list;
for (const auto& root : roots) {
dele_list += make_dele_list(root);
}
return dele_list;
}
} //namespace duck