-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.cpp
122 lines (107 loc) · 4.02 KB
/
Scheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Scheduler.cpp
* Ryan Burgoyne
* 05 Dec 2011
* TimeField Scheduler
* This file provides the implementation for the Scheduler class which manages
* the tasks for TimeField.
*/
#include <string>
#include <vector>
#include <exception>
#include <boost/date_time/posix_time/posix_time.hpp>
// for loading tasks.xml
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include "Scheduler.h"
Scheduler::Scheduler(std::string tasksFilename) {
Scheduler::tasksFilename = tasksFilename;
// Set working interval to the current day
workingInterval = new boost::posix_time::
time_period(boost::posix_time::ptime(boost::gregorian::day_clock::
local_day()),
boost::posix_time::hours(24));
// Read persistent task data from file
try {
boost::property_tree::ptree pt;
read_xml(tasksFilename, pt);
BOOST_FOREACH(boost::property_tree::ptree::value_type &v,
pt.get_child("tasks"))
{
std::string title, notes, releaseDateString, dueDateString,
durationString;
title = v.second.get<std::string>("title");
notes = v.second.get<std::string>("notes");
releaseDateString = v.second.get<std::string>("release-date");
dueDateString = v.second.get<std::string>("due-date");
durationString = v.second.get<std::string>("duration");
boost::posix_time::ptime releaseDate =
boost::posix_time::time_from_string(releaseDateString);
boost::posix_time::ptime dueDate =
boost::posix_time::time_from_string(dueDateString);
boost::posix_time::time_period *interval =
new boost::posix_time::time_period(releaseDate, dueDate);
boost::posix_time::time_duration *duration =
new boost::posix_time::
time_duration(boost::posix_time::
duration_from_string(durationString));
addTask(new Task(title, notes, interval, duration, NULL));
}
}
catch (...) {
// Failed to read from file.
taskList.clear();
}
}
Scheduler::~Scheduler() {
// Write persistent task data to file
boost::property_tree::ptree pt;
BOOST_FOREACH(Task *task, taskList)
{
std::string title, notes, releaseDateString, dueDateString,
durationString;
title = task->getTitle();
notes = task->getNotes();
boost::posix_time::time_period *interval = task->getInterval();
releaseDateString =
boost::posix_time::to_simple_string(interval->begin());
dueDateString =
boost::posix_time::to_simple_string(interval->end());
durationString =
boost::posix_time::to_simple_string(*task->getDuration());
boost::property_tree::ptree subtree;
subtree.put("title", title);
subtree.put("notes", notes);
subtree.put("release-date", releaseDateString);
subtree.put("due-date", dueDateString);
subtree.put("duration", durationString);
pt.add_child("tasks.task", subtree);
delete task;
}
write_xml(tasksFilename, pt);
delete workingInterval;
}
boost::posix_time::time_period *Scheduler::getWorkingInterval() {
return workingInterval;
}
void Scheduler::setWorkingInterval(boost::posix_time::time_period *interval) {
delete workingInterval; // Delete the current object pointed to by
// workingInterval
workingInterval = interval; // Point it to the new interval
}
void Scheduler::addTask(Task *task) {
taskList.push_back(task);
}
void Scheduler::deleteTask(int i) {
if (i - 1 >= taskList.size()) {
throw std::exception();
}
taskList.erase(taskList.begin() + i - 1);
}
Task *Scheduler::getTask(int i) {
return taskList[i - 1];
}
std::vector<Task *> Scheduler::getTaskList() {
return taskList;
}