summaryrefslogtreecommitdiff
path: root/src/nodemanager.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/nodemanager.cc')
-rw-r--r--src/nodemanager.cc297
1 files changed, 46 insertions, 251 deletions
diff --git a/src/nodemanager.cc b/src/nodemanager.cc
index e94c798..e10b64d 100644
--- a/src/nodemanager.cc
+++ b/src/nodemanager.cc
@@ -27,10 +27,10 @@
*/
#include "nodemanager.h"
-#include <stdio.h>
-
#include "hugin.hpp"
+#include "exceptions.h"
+
// Global NodeManager object.
NodeManager node_manager;
@@ -41,21 +41,15 @@ NodeManager node_manager;
#define PROJECTS_ID 4
#define FIRST_NODE_ID 10
-static bool isProtected(nodeid_t id)
-{
- return id < FIRST_NODE_ID;
-}
-
NodeManager::NodeManager()
{
- idCount = FIRST_NODE_ID;
}
NodeManager::~NodeManager()
{
}
-void NodeManager::init(std::string filename)
+void NodeManager::init(const std::string& filename)
{
DEBUG(nodemgr, "Reading nodes from file: %s\n", filename.c_str());
file = filename;
@@ -103,182 +97,93 @@ void NodeManager::init(std::string filename)
tree.toStdOut();
}
-node_t NodeManager::node(nodeid_t t)
-{
- return tree.data(t);
-}
-
-nodeid_t NodeManager::createId()
-{
- return tree.createId();
-}
-
-bool NodeManager::hasId(nodeid_t id)
+NodeIdList NodeManager::createNode(nodeid_t parentid, nodeid_t id,
+ nodeid_t insertbeforeid)
{
- return tree.hasId(id);
-}
-
-NodeIdListPair NodeManager::moveNode(nodeid_t id, nodeid_t to, nodeid_t beforeId)
- throw (std::exception)
-{
- if(isProtected(id))
- {
- return NodeIdListPair();
- }
-
- if(id == to)
- {
- throw std::exception(); // Node and new parent are the same node.
- }
-
- //node_t t = tree.data(id);
-
- // Make sure the new parent exists. This will throw an exception if it doesn't
- //node_t t_ = tree.data(to);
-
- //t.parentid = to;
-
- //NodeIdList tilremove = tree.remove(id);
- NodeIdList tilremove;
- tilremove.push_back(id);
- NodeIdList ancestors = tree.ancestorList(id);
- tilremove.insert(tilremove.end(), ancestors.begin(), ancestors.end());
-
- // NodeIdList tilcreate = tree.insertAsChild(to, id, t);
- NodeIdList tilcreate;
- tilcreate.push_back(to);
- ancestors = tree.ancestorList(to);
- tilcreate.insert(tilcreate.end(), ancestors.begin(), ancestors.end());
-
- tree.move(id, to, beforeId);
+ NodeIdList affectedNodes;
- NodeIdListPair tilpair;
- tilpair.first = tilremove;
- tilpair.second = tilcreate;
+ node_t t;
+ t.attributes["title"] = "";
+ t.id = id;
+ affectedNodes = tree.insertAsChild(parentid, id, t, insertbeforeid);
flushNodes();
- return tilpair;
+ return affectedNodes;
}
-NodeIdList NodeManager::removeNode(nodeid_t id)
- throw (std::exception)
+NodeIdList NodeManager::updateNode(nodeid_t id, const std::string &name,
+ const std::string &value)
{
if(isProtected(id))
{
- return NodeIdList();
+ throw Error::ProtectedNode();
}
NodeIdList affectedNodes;
- if(tree.bfs(id).size() > 1)
- {
- throw std::exception();
- }
-
- try
- {
- affectedNodes = tree.remove(id);
- }
- catch(std::exception& e)
- {
- throw e;
- }
+ affectedNodes = tree.updateData(id, name, value);
flushNodes();
return affectedNodes;
}
-NodeIdList NodeManager::updateNode(nodeid_t id, const std::string &name,
- const std::string &value)
- throw (std::exception)
+NodeIdList NodeManager::removeNode(nodeid_t id)
{
if(isProtected(id))
{
- return NodeIdList();
+ throw Error::ProtectedNode();
}
NodeIdList affectedNodes;
-
- try
- {
- affectedNodes = tree.updateData(id, name, value);
- }
- catch(std::exception& e)
- {
- throw e;
- }
-
+ affectedNodes = tree.remove(id);
flushNodes();
return affectedNodes;
}
-NodeIdList NodeManager::createNode(nodeid_t parentid, nodeid_t id, nodeid_t insertbeforeid)
- throw (std::exception)
+NodeIdListPair NodeManager::moveNode(nodeid_t id, nodeid_t to,
+ nodeid_t beforeId)
{
- if(hasId(id))
+ if(isProtected(id))
{
- // Id alerady in tree, ignore
- return {};
+ throw Error::ProtectedNode();
}
- NodeIdList affectedNodes;
+ NodeIdList tilremove;
+ tilremove.push_back(id);
+ NodeIdList ancestors = tree.ancestorList(id);
+ tilremove.insert(tilremove.end(), ancestors.begin(), ancestors.end());
- node_t t;
- t.attributes["title"] = "";
- t.id = id;
+ NodeIdList tilcreate;
+ tilcreate.push_back(to);
+ ancestors = tree.ancestorList(to);
+ tilcreate.insert(tilcreate.end(), ancestors.begin(), ancestors.end());
- try
- {
- affectedNodes = tree.insertAsChild(parentid, id, t, insertbeforeid);
- }
- catch(std::exception& e)
- {
- throw e;
- }
+ tree.move(id, to, beforeId); // TODO: Use node list pair from return value
flushNodes();
- return affectedNodes;
+ return {tilremove, tilcreate};
}
NodeIdList NodeManager::subNodes(nodeid_t t)
- throw (std::exception)
{
NodeIdList affectedNodes;
-
- try
- {
- affectedNodes = tree.bfs(t);
- }
- catch(std::exception& e)
- {
- throw e;
- }
-
+ affectedNodes = tree.bfs(t);
return affectedNodes;
}
-//NodeIdList NodeManager::ancestorList(nodeid_t id)
-// throw (std::exception)
-//{
-// NodeIdList ancestors;
-//
-// try
-// {
-// ancestors = tree.ancestorList(id);
-// goto finish;
-// }
-// catch(std::exception& e)
-// {
-// throw e;
-// }
-//
-//finish:
-// return ancestors;
-//}
+node_t NodeManager::node(nodeid_t t)
+{
+ return tree.data(t);
+}
+
+nodeid_t NodeManager::createId()
+{
+ return tree.createId();
+}
void NodeManager::flushNodes()
{
@@ -292,122 +197,12 @@ void NodeManager::flushNodes()
fclose(fp);
}
-#if 0
-NodeList nodelist;
-
-node_t create_node(std::string title, std::string desc)
-{
- node_t t;
- t.parent_id = current_id_count();
- t.attributes["title"] = title;
- t.desc = desc;
- t.id = id_count; id_count++;
-
- return t;
-}
-
-NodeList load_nodelist_from_file(std::string file)
-{
- NodeList list;
-
- // create MuniaDb class which handles nodes, db-flush and db-init.
-
- return list;
-}
-
-bool save_nodelist_to_file(NodeList list, std::string file)
-{
- FILE* fp;
-
- if(! (fp = fopen(file.c_str(), "w")))
- {
- return false;
- }
-
- if(!fprintf(fp, "<nodelist>\n"))
- {
- fclose(fp);
- return false;
- }
-
- NodeList::iterator it;
- for(it = nodelist.begin(); it != nodelist.end(); it++)
- {
- node_t t = *it;
- int r = 1;
-
- //printf("Flushing node %d\n", t.id);
- r |= fprintf(fp, " <node id=\"%d\" parent_id=\"%d\">\n", t.id, t.parent_id);
- r |= fprintf(fp, " <title>%s</title>\n", xml_encode(t.attributes["title"]).c_str());
- r |= fprintf(fp, " <desc>%s</desc>\n", xml_encode(t.attributes["description"]).c_str());
- r |= fprintf(fp, " </node>)\n");
-
- if(!r)
- {
- fclose(fp);
- return false;
- }
- }
-
- if(!fprintf(fp, "</nodelist>\n"))
- {
- fclose(fp);
- return false;
- }
-
- fclose(fp);
- return true;
-}
-
-static void delete_node(node_t t, NodeList& graveyard);
-
-static void delete_children(node_t t, NodeList& graveyard)
-{
- NodeList::iterator it;
- for(it = nodelist.begin(); it != nodelist.end(); it++)
- {
- if(it->parent_id == t.id) {
- delete_node(*it, graveyard);
- }
- }
-}
-
-static void delete_node(node_t t, NodeList& graveyard)
+bool NodeManager::hasId(nodeid_t id)
{
- NodeList::iterator it;
- for(it = nodelist.begin(); it != nodelist.end(); it++)
- {
- if(it->id == t.id)
- {
- break;
- }
- }
-
- if(it != nodelist.end())
- {
- graveyard.push_back(*it);
- delete_children(*it, graveyard);
- }
+ return tree.hasId(id);
}
-void delete_node(int id, NodeList& graveyard)
+bool NodeManager::isProtected(nodeid_t id) const
{
- node_t t;
- t.id = id;
- delete_node(t, graveyard);
-
- for(NodeList::iterator it = graveyard.begin();
- it != graveyard.end(); it++)
- {
- for(NodeList::iterator it_tl = nodelist.begin();
- it_tl != nodelist.end(); it_tl++)
- {
- if(it_tl->id == it->id)
- {
- nodelist.erase(it_tl);
- break;
- }
- }
- }
+ return id < FIRST_NODE_ID;
}
-#endif