-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.cpp
191 lines (152 loc) · 3.92 KB
/
todo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <QFile>
#include <QDateTime>
#include <QStringList>
#include <QTextStream>
#include "todo.h"
#include "configuration.h"
#include "../Lib/supportfunctions.h"
Todo::Todo()
{
createNew("") ;
}
Todo::~Todo()
{
}
bool Todo::isdirty()
{
return dirty ;
}
bool Todo::isEmpty()
{
return todoText.isEmpty() ;
}
// Search for string, and return -1 on failure
int Todo::find(QString text, int startline)
{
Q_UNUSED(startline) ;
QString overview = getText().toLower() ;
QRegularExpression re(".*(" +text.toLower() +").*") ;
QRegularExpressionMatch rem = re.match(overview) ;
if (rem.hasMatch()) return 1;
else return -1 ;
}
bool Todo::createNew(QString idname)
{
cachedPath = "" ;
dirty=false ;
todoText="" ;
idName = idname ;
getOverviewResult="" ;
return true ;
}
// TODO: Make this save a "safe save", i.e. save, check, rename
bool Todo::save(QString path)
{
Encryption *enc = gConf->encryption() ;
if (!enc) {
dbg("ERROR - call without enc") ;
return false ;
}
if (dirty && idName.compare("")!=0) {
if (path.isEmpty()) {
path = cachedPath ;
}
if (path.isEmpty()) {
dbg("ERROR - save with empty path called") ;
return false ;
}
QByteArray data ;
QTextStream out(&data, QIODevice::WriteOnly);
out.setEncoding(QStringConverter::Utf8) ;
out << todoText ;
out.flush();
if (gConf->encryptedEnabled()) {
// Save Encrypted
if (!enc->save(path + idName + ".ztodolist", data)) {
return false ;
}
} else {
// Save Plaintext
QFile file(path + idName + ".todolist");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
file.write(data) ;
file.close() ;
}
dirty = false ;
}
return true ;
}
// TODO: Make this load a "safe load", i.e. check for failed save
bool Todo::load(QString path, QString idname)
{
Encryption *enc = gConf->encryption() ;
if (!enc) {
dbg("ERROR - call without enc") ;
return false ;
}
createNew(idname) ;
cachedPath = path ;
QByteArray data ;
if (gConf->encryptedEnabled()) {
// Load Encrypted File
if (!enc->load(path + idname + ".ztodolist", data)) {
dbg(QString("Loading %1.ztodolist FAILED").arg(idname)) ;
return false ;
}
} else {
// Load Plaintext File
QFile file(path + idname + ".todolist");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
data = file.readAll() ;
file.close() ;
}
QTextStream in(&data);
in.setEncoding(QStringConverter::Utf8) ;
while (!in.atEnd()) {
todoText = todoText + in.readLine() + "\n" ;
}
dirty = false ;
return true ;
}
const QString& Todo::getOverview(enum TodoOverviewType overviewtype)
{
getOverviewResult = "" ;
if (overviewtype==todoAsHTML) {
QStringList lines = getText().split("\n") ;
Q_FOREACH(QString line, lines) {
line = line.trimmed() ;
if (!line.isEmpty()) {
getOverviewResult += "<p>" + line + "</p>\n" ;
}
}
} else {
getOverviewResult = getText() ;
}
return getOverviewResult ;
}
const QString& Todo::getText()
{
return todoText ;
}
bool Todo::setText(QString &newtext)
{
dirty = (todoText.compare(newtext)!=0) ;
if (dirty) { todoText = newtext ; }
return dirty ;
}
Todo* Todo::getThis()
{
return this ;
}
Todo& Todo::operator=(const Todo &rhs)
{
if (this == &rhs) return *this;
this->idName = rhs.idName ;
this->todoText = rhs.todoText ;
this->dirty = rhs.dirty ;
this->getOverviewResult = rhs.getOverviewResult ;
this->cachedPath = rhs.cachedPath ;
return *this ;
}