-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTaskLogger.cpp
137 lines (101 loc) · 3.6 KB
/
TaskLogger.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QTemporaryFile>
#include "TaskItem.h"
#include "TaskLogger.h"
TaskLogger::TaskLogger()
: taskItems(NULL)
, taskHistoriesLoaded(false)
{
logDirectory = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/";
}
void TaskLogger::addTask(Task *task) {
QObject::connect(task, SIGNAL(toggled(bool)), this, SLOT(taskToggled(bool)));
}
void TaskLogger::deleteTaskHistory(Task *task) {
loadTaskHistories();
for(int i=0; i < taskSessions.size(); i++) {
if(taskSessions[i].taskId == task->getId()) {
taskSessions.removeAt(i);
i--;
}
}
saveTaskHistories();
}
// This will return all saved and active sessions
const QList<TaskSession> TaskLogger::getTaskSessions() {
loadTaskHistories();
QList<TaskSession> sessions = taskSessions;
if(taskItems != NULL) {
for(int i = 0; i < taskItems->size(); i++) {
Task *task = (*taskItems)[i]->task;
if(task->isActive()) {
TaskSession activeSession;
activeSession.taskId = task->getId();
activeSession.duration = task->getCurrentDuration();
activeSession.time = QDateTime::currentDateTime().toTime_t() - task->getCurrentDuration();
sessions.append(activeSession);
}
}
}
return sessions;
}
void TaskLogger::setTaskItems(const QList<TaskItem*>* taskItems) {
this->taskItems = taskItems;
}
void TaskLogger::taskToggled(bool active) {
Task *task = static_cast<Task*>(sender());
int taskId = task->getId();
if(active) {
initialTaskDuration[taskId] = task->getTotalTime();
taskActivationTime[taskId] = QDateTime::currentDateTime();
} else {
QDir dir;
QFile file(logDirectory + LOG_FILENAME);
TaskSession taskSession;
taskSession.taskId = taskId;
taskSession.duration = task->getTotalTime() - initialTaskDuration[taskId];
taskSession.time = taskActivationTime[taskId].toTime_t();
taskSessions.append(taskSession);
if(dir.mkpath(logDirectory) && file.open(QIODevice::Append | QIODevice::Text)) {
QTextStream stream(&file);
writeSessionToStream(taskSession, stream);
file.close();
}
}
}
void TaskLogger::writeSessionToStream(const TaskSession taskSession, QTextStream &stream) {
stream << taskSession.taskId << " " << taskSession.time << " " << taskSession.duration << "\n";
}
void TaskLogger::loadTaskHistories() {
if(taskHistoriesLoaded == false) {
QFile logFile(logDirectory + LOG_FILENAME);
if(!logFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
taskSessions.clear();
QTextStream logFileStream(&logFile);
QString line = logFileStream.readLine();
while(!line.isNull()) {
QTextStream lineStream(&line);
TaskSession taskSession;
lineStream >> taskSession.taskId >> taskSession.time >> taskSession.duration;
taskSessions.append(taskSession);
line = logFileStream.readLine();
}
taskHistoriesLoaded = true;
}
}
void TaskLogger::saveTaskHistories() {
QDir dir;
QFile file(logDirectory + LOG_FILENAME);
if(dir.mkpath(logDirectory) && file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
TaskSession taskSession;
foreach(taskSession, taskSessions) {
writeSessionToStream(taskSession, stream);
}
file.close();
}
}