-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackend.cpp
137 lines (111 loc) · 3.6 KB
/
backend.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 "backend.h"
#include <QSettings>
#include <QDebug>
#include <QtConcurrent>
#include <QMutex>
#include <QMutexLocker>
const QString APP_VERSION = "1.1.1.0";
QMutex gTaskListMutex;
Backend::Backend(QObject *parent) : QObject(parent)
{
loadTasks();
}
Backend::~Backend()
{
qDeleteAll(m_taskList);
m_taskList.clear();
}
model::TaskModel* Backend::newTaskModel()
{
return &m_newTaskModel;
}
model::TaskModel* Backend::doneTaskModel()
{
return &m_doneTaskModel;
}
void Backend::newTask(QString text, QString color)
{
QMutexLocker locker(&gTaskListMutex);
model::TaskItem* task = new model::TaskItem(text, false, color, 0);
connect(task, &model::TaskItem::doneChanged, this, &Backend::taskStateChanged);
connect(task, &model::TaskItem::titleChanged, this, &Backend::scheduleSaveTasks);
connect(task, &model::TaskItem::orderChanged, this, &Backend::scheduleSaveTasks);
m_taskList.push_front(task);
m_newTaskModel.addTask(task);
scheduleSaveTasks();
}
void Backend::deleteCompletedTask(model::TaskItem *task)
{
QMutexLocker locker(&gTaskListMutex);
if (!task)
return;
if (m_doneTaskModel.takeTask(task)) {
m_taskList.removeOne(task);
scheduleSaveTasks();
}
}
QString Backend::appVersion()
{
return APP_VERSION;
}
void Backend::loadTasks()
{
QMutexLocker locker(&gTaskListMutex);
QSettings settings("IProApps", "BestToDoList");
int size = settings.beginReadArray("todos");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
model::TaskItem* task = new model::TaskItem(settings.value("taskText").toString(), settings.value("isDone").toBool(),
settings.value("color").toString(), settings.value("order").toInt());
connect(task, &model::TaskItem::doneChanged, this, &Backend::taskStateChanged);
connect(task, &model::TaskItem::titleChanged, this, &Backend::scheduleSaveTasks);
connect(task, &model::TaskItem::orderChanged, this, &Backend::scheduleSaveTasks);
m_taskList.append(task);
}
settings.endArray();
qSort(m_taskList.begin(), m_taskList.end(),
[](model::TaskItem* a, model::TaskItem* b) -> bool { return a->order() < b->order(); });
foreach (model::TaskItem* task, m_taskList) {
if (task->done()) {
m_doneTaskModel.addTask(task);
} else {
m_newTaskModel.addTask(task);
}
}
qDebug() << "Loaded tasks: " << m_taskList.size();
}
void Backend::scheduleSaveTasks()
{
QtConcurrent::run(this, &Backend::saveTasks);
}
void Backend::saveTasks()
{
QMutexLocker locker(&gTaskListMutex);
QSettings settings("IProApps", "BestToDoList");
settings.beginWriteArray("todos");
for (int i = 0; i < m_taskList.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue("taskText", m_taskList.at(i)->title());
settings.setValue("isDone", m_taskList.at(i)->done());
settings.setValue("color", m_taskList.at(i)->color());
settings.setValue("order", m_taskList.at(i)->order());
}
settings.endArray();
qDebug() << "Saved tasks: " << m_taskList.size();
}
void Backend::taskStateChanged()
{
model::TaskItem* task = qobject_cast<model::TaskItem*>(sender());
if (!task)
return;
if (task->done()) {
m_newTaskModel.takeTask(task);
task->setOrder(m_doneTaskModel.size());
m_doneTaskModel.addTask(task);
} else {
m_doneTaskModel.takeTask(task);
task->setOrder(m_newTaskModel.size());
m_newTaskModel.addTask(task);
}
scheduleSaveTasks();
}