-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.cpp
432 lines (350 loc) · 11 KB
/
calendar.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "calendar.h"
#include "appointment.h"
#include "configuration.h"
#include "../Lib/supportfunctions.h"
#include <QDateTime>
#include <QList>
#include <QFile>
#include <QTextStream>
#include <QtCore>
#include <QDir>
#include <QFileInfo>
#include <QStringList>
#include <QtAlgorithms>
Calendar::Calendar()
{
clear() ;
}
Calendar::~Calendar()
{
clear() ;
}
void Calendar::clear()
{
appointments.clear() ;
nullentry.setNull() ;
idName="" ;
isdirty=false ;
getOverviewResponse="" ;
selectedappointment="" ;
}
bool Calendar::load()
{
Encryption *enc = gConf->encryption() ;
if (!enc) return false;
QStringList nameFilter;
QFileInfoList list ;
QStringList files ;
clear() ;
// Login if not already done so
if (!enc->loggedIn()) {
enc->login() ;
}
if (!enc->loggedIn()) {
return false ;
}
// Load Calendar
QDir dir(gCalendarSavePath) ;
nameFilter << "*.appointment" ;
dir.setNameFilters(nameFilter) ;
list = dir.entryInfoList( nameFilter, QDir::Files );
files = dir.entryList() ;
for (int i=files.size()-1; i>=0; i--) {
QString filename ;
QString id ;
Appointment appt ;
filename = files.at(i) ;
id = filename ;
id.replace(".appointment","") ;
appt.load(gCalendarSavePath, id) ;
QString googleStatus = appt.getField(Appointment::GoogleStatus) ;
if (appt.isSet(Appointment::Deleted) && (googleStatus.isEmpty() || googleStatus.compare("cancelled")==0)) {
// TODO: Delete/move the file
backupFile(gCalendarSavePath, filename) ;
} else {
appointments.push_back(appt) ;
}
}
sort() ;
return true ;
}
bool Calendar::save()
{
Encryption *enc = gConf->encryption() ;
if (!enc) return false ;
bool success=true ;
if (size()>0) {
// Login if not already done so
if (!enc->loggedIn()) {
enc->login() ;
}
if (!enc->loggedIn()) {
return false ;
}
QDir cdir(gSavePath) ;
cdir.mkpath("calendar") ;
for (int i=0; i<size(); i++) {
success |= getAppointment(i).save(gCalendarSavePath) ;
}
}
return success ;
}
Appointment &Calendar::getAppointmentBy(enum Appointment::AppointmentRecord type, QString id)
{
for (int i=0, sz=size(); i<sz; i++) {
if (getAppointment(i).getField(type).compare(id)==0) {
return getAppointment(i) ;
}
}
return getNull() ;
}
bool Calendar::addAppointment(Appointment &appt, bool dosort)
{
// Get a pointer to the google record for the appoontment
Appointment& record = getAppointmentBy(Appointment::ID, appt.getField(Appointment::ID)) ;
if (record.isNull()) {
// Not found, so add it to the list
appointments.push_back(appt) ;
} else {
// Found, so update the entry
record = appt ;
}
// Sort the appointments list
if (dosort) sort() ;
return true ;
}
void Calendar::sort()
{
std::sort(appointments.begin(), appointments.end()) ;
}
int Calendar::size()
{
return appointments.size() ;
}
bool Calendar::appointmentIsInFuture(int n)
{
if (n<0 || n >= appointments.size()) {
return false ;
} else {
Appointment appt = appointments.at(n) ;
return appt.isInFuture() ;
}
}
bool Calendar::appointmentIsInRange(int n, qint64 beforedays, qint64 afterdays)
{
if (n<0 || n >= appointments.size()) {
return false ;
} else {
Appointment appt = appointments.at(n) ;
return appt.isInRange(beforedays, afterdays) ;
}
}
// Search for string, and return -1 on failure
int Calendar::find(QString text, bool futureonly)
{
QRegularExpression re(".*(" +text.toLower() +").*") ;
QRegularExpressionMatch rem ;
QString overview = getOverview(calendarAsText, "", futureonly).toLower() ;
rem = re.match(overview) ;
if (rem.hasMatch()) return 1;
else return -1 ;
}
Appointment &Calendar::getAppointment(enum CalendarSearchType method, int startindex)
{
Appointment *startappointment = &getNull() ;
Appointment *thisappointment = &getNull() ;
Appointment *nearestappointment = &getNull() ;
Appointment *lastappointment = &getNull() ;
Appointment *nextappointment = &getNull() ;
Appointment testappointment ;
if (startindex < 0 || startindex >= appointments.size()) {
return getNull() ;
}
QDateTime now = QDateTime::currentDateTimeUtc() ;
QDateTime ago = now.addSecs(-900) ;
testappointment.setDate(Appointment::From, ago) ;
testappointment.setDate(Appointment::To, now) ;
if (method == calendarSearchExact) {
startappointment = (Appointment *)&appointments.at(startindex) ;
} else {
int n = appointments.size() ;
if (n>0) {
for (int i=startindex; i<n; i++) {
Appointment *appt = (Appointment *)&appointments.at(i) ;
if (!appt->isSet(Appointment::Deleted)) {
// Last Appointment
if (testappointment.clashes(*appt)) {
if (lastappointment->isNull()) lastappointment = appt ;
else if (*appt < *lastappointment) lastappointment = appt ;
}
// This Appointment (and only this one)
if (appt->isCurrent()) {
if (thisappointment->isNull()) thisappointment = appt ;
else if (*appt < *thisappointment) thisappointment = appt ;
}
// Nearest appointment to now
if (appt->isCurrent() || appt->isInFuture()) {
if (nearestappointment->isNull()) nearestappointment = appt ;
else if (*appt < *nearestappointment) nearestappointment = appt ;
}
if (appt->isInFuture() && !appt->isCurrent()) {
if (nextappointment->isNull()) nextappointment = appt ;
else if (*appt < *nextappointment) nextappointment = appt ;
}
}
}
}
}
switch (method) {
case calendarSearchExact:
return *startappointment ;
case calendarSearchLast:
return *lastappointment ;
case calendarSearchThis:
return *thisappointment ;
case calendarSearchNearest:
return *nearestappointment ;
case calendarSearchNext:
return *nextappointment ;
}
return getNull() ;
}
Appointment &Calendar::getLastAppointment()
{
return getAppointment(calendarSearchLast, 0);
}
Appointment &Calendar::getCurrentAppointment()
{
return getAppointment(calendarSearchThis, 0);
}
Appointment &Calendar::getNearestAppointment()
{
return getAppointment(calendarSearchNearest, 0);
}
Appointment &Calendar::getNextAppointment()
{
return getAppointment(calendarSearchNext, 0);
}
Appointment& Calendar::getAppointment(int n)
{
return getAppointment(calendarSearchExact, n) ;
}
Appointment& Calendar::getNull()
{
return nullentry ;
}
QString& Calendar::getAppointmentAsText(int n)
{
return getAppointment(n).asText() ;
}
QString& Calendar::getAppointmentAsHTML(int n)
{
return getAppointment(n).asHTML() ;
}
// TODO: ...
// Append Calendar onto another one
// Sort Calendar
// Look for Conflicts with new appointment
QString Calendar::getOverview(enum CalendarOverviewType overviewtype, QString contactid, bool futureonly)
{
int ne=size() ;
getOverviewResponse = "" ;
for (int i=0; i<ne; i++) {
if ((contactid.isEmpty() || getAppointment(i).getField(Appointment::ContactId).compare(contactid)==0) &&
(appointmentIsInFuture(i) || !futureonly) &&
!getAppointment(i).isSet(Appointment::Deleted)) {
switch (overviewtype) {
case calendarAsHTML:
getOverviewResponse += "<p>" ;
getOverviewResponse += getAppointmentAsHTML(i) ;
getOverviewResponse += "</p>\n" ;
break ;
default:
getOverviewResponse += getAppointmentAsText(i) ;
getOverviewResponse += "\n" ;
break ;
}
}
}
return getOverviewResponse ;
}
Appointment& Calendar::getAppointmentClash(Appointment& appt)
{
for (int i=0; i<size(); i++) {
Appointment& test = getAppointment(i) ;
if ( !test.isSet(Appointment::Deleted) &&
appt.getField(Appointment::ID).compare(test.getField(Appointment::ID))!=0 &&
appt.clashes(test)) return test ;
}
return getNull() ;
}
//
// Searching and Selection
//
Appointment& Calendar::selectAppointment(QString id)
{
selectedappointment=id ;
return getSelected() ;
}
Appointment& Calendar::selectAppointment(Appointment &appointment)
{
return selectAppointment(appointment.getField(Appointment::ID)) ;
}
Appointment& Calendar::getSelected()
{
Appointment *appt = NULL ;
for (int i=0, ne=appointments.size(); i<ne && !appt; i++) {
QString& recordid = appointments[i].getField(Appointment::ID) ;
if (selectedappointment.compare(recordid)==0) appt = (Appointment*)&appointments[i] ;
}
if (!appt) {
appt=(Appointment *)&getNull() ;
selectedappointment = "" ;
}
return *appt ;
}
void Calendar::purgeBirthdays()
{
QList<Appointment>::iterator it = appointments.begin();
while (it != appointments.end()) {
if ((*it).isTemp()) {
it = appointments.erase(it);
} else {
++it;
}
}
}
void Calendar::addBirthday(QString who, QString whoid, QString when)
{
QStringList de = when.split("-") ;
int count = de.count() ;
if (count==3) {
int day = de.at(2).toInt() ;
int month = de[1].toInt() ;
QTime midnight(0,0,0) ;
QDateTime last, now, next ;
int year = last.currentDateTimeUtc().date().year() ;
last = QDateTime( QDate(year-1, month, day), midnight, Qt::UTC) ;
Appointment *appt1 = new Appointment() ;
appt1->setField(Appointment::Summary, QString("Birthday")) ;
appt1->setField(Appointment::For, who) ;
appt1->setField(Appointment::ContactId, whoid) ;
appt1->setDate(Appointment::From, last) ;
appt1->setDate(Appointment::To, last) ;
appt1->setId(QString("1-%1").arg(whoid)) ;
appt1->markAsTemp();
appointments.append(*appt1) ;
now = QDateTime( QDate(year, month, day), midnight, Qt::UTC) ;
Appointment *appt2 = new Appointment(*appt1) ;
appt2->setDate(Appointment::From, now) ;
appt2->setDate(Appointment::To, now) ;
appt1->setId(QString("2-%1").arg(whoid)) ;
appointments.append(*appt2) ;
next = QDateTime( QDate(year+1, month, day), midnight, Qt::UTC) ;
Appointment *appt3 = new Appointment(*appt1) ;
appt3->setDate(Appointment::From, next) ;
appt3->setDate(Appointment::To, next) ;
appt1->setId(QString("3-%1").arg(whoid)) ;
appointments.append(*appt3) ;
}
}