From 4873e6df45424d2132dc9685f76bcbe2d9dd447e Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 17 May 2012 11:43:16 +0200 Subject: Make persistent store (XML). --- src/taskmanager.cc | 68 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 17 deletions(-) (limited to 'src/taskmanager.cc') diff --git a/src/taskmanager.cc b/src/taskmanager.cc index 87cf2b0..6ebffd5 100644 --- a/src/taskmanager.cc +++ b/src/taskmanager.cc @@ -44,29 +44,45 @@ static bool isProtected(taskid_t id) return id < FIRST_TASK_ID; } -TaskManager::TaskManager() { +TaskManager::TaskManager(std::string file) { idCount = FIRST_TASK_ID; - task_t t; - t.title = "root"; - t.id = ROOT_ID; - tree.insertAsChild(0, ROOT_ID, t); + this->file = file; + + FILE *fp = fopen(file.c_str(), "r"); + if(fp) { + std::string xml; + while(!feof(fp)) { + char buf[64]; + size_t sz = fread(buf, 1, sizeof(buf), fp); + xml.append(buf, sz); + } + tree.fromXML(xml); + fclose(fp); + } else { - t.title = "Finished"; - t.id = FINISHED_ID; - tree.insertAsChild(ROOT_ID, FINISHED_ID, t); + // Create new basis config + task_t t; + t.title = "root"; + t.id = ROOT_ID; + tree.insertAsChild(0, ROOT_ID, t); - t.title = "Backlog"; - t.id = BACKLOG_ID; - tree.insertAsChild(ROOT_ID, BACKLOG_ID, t); + t.title = "Finished"; + t.id = FINISHED_ID; + tree.insertAsChild(ROOT_ID, FINISHED_ID, t); - t.title = "Lost+Found"; - t.id = LOSTFOUND_ID; - tree.insertAsChild(ROOT_ID, LOSTFOUND_ID, t); + t.title = "Backlog"; + t.id = BACKLOG_ID; + tree.insertAsChild(ROOT_ID, BACKLOG_ID, t); - t.title = "Projects"; - t.id = PROJECTS_ID; - tree.insertAsChild(ROOT_ID, PROJECTS_ID, t); + t.title = "Lost+Found"; + t.id = LOSTFOUND_ID; + tree.insertAsChild(ROOT_ID, LOSTFOUND_ID, t); + + t.title = "Projects"; + t.id = PROJECTS_ID; + tree.insertAsChild(ROOT_ID, PROJECTS_ID, t); + } tree.toStdOut(); } @@ -96,6 +112,8 @@ TaskIdList TaskManager::moveTask(taskid_t id, taskid_t to) throw e; } + flushTasks(); + return affectedTasks; } @@ -115,6 +133,8 @@ TaskIdList TaskManager::removeTask(taskid_t id) throw e; } + flushTasks(); + return affectedTasks; } @@ -132,6 +152,8 @@ TaskIdList TaskManager::updateTask(taskid_t id, task_t t) throw e; } + flushTasks(); + return affectedTasks; } @@ -152,6 +174,8 @@ TaskIdList TaskManager::createTask(taskid_t parentid, taskid_t *pid) throw e; } + flushTasks(); + return affectedTasks; } @@ -188,6 +212,16 @@ TaskIdList TaskManager::ancestorList(taskid_t id) } */ +void TaskManager::flushTasks() +{ + FILE *fp = fopen(file.c_str(), "w"); + if(!fp) return; + std::string xml = tree.toXML(); + fwrite(xml.c_str(), xml.size(), 1, fp); + fclose(fp); +} + + #if 0 TaskList tasklist; -- cgit v1.2.3