This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathicalendar.cpp
More file actions
executable file
·360 lines (300 loc) · 9.88 KB
/
Copy pathicalendar.cpp
File metadata and controls
executable file
·360 lines (300 loc) · 9.88 KB
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
#include "icalendar.h"
#include <iostream>
void ICalendar::LoadFromFile() {
string Line, NextLine;
Component CurrentComponent = VCALENDAR, PrevComponent = VCALENDAR;
Event *NewEvent = NULL;
Alarm NewAlarm;
// for getting some UIDs for events without them
unsigned int NoUID = 0;
fstream File;
File.open(FileName, ios::in | ios::binary);
if (!File.is_open()) {
File.clear();
File.open(FileName, ios::out | ios::binary | ios::trunc);
File << "BEGIN:VCALENDAR\r\n";
File << "VERSION:2.0\r\n";
File << "PRODID:-//Juliusz Gonera//NONSGML remind.me//EN\r\n";
File << "END:VCALENDAR\r\n";
File.close();
return;
}
getline(File, NextLine);
while (!File.eof()) {
Line = NextLine;
// lines can be wrapped after 75 octets so we may have to unwrap them
for (;;) {
getline(File, NextLine);
if (NextLine[0] != '\t' && NextLine[0] != ' ')
break;
Line += NextLine.substr(1);
}
switch (CurrentComponent) {
case VCALENDAR:
if (Line.find("BEGIN:VEVENT") == 0) {
NewEvent = new Event;
CurrentComponent = VEVENT;
}
break;
case VEVENT:
if (Line.find("UID") == 0) {
NewEvent->UID = GetProperty(Line);
} else if (Line.find("SUMMARY") == 0) {
NewEvent->Summary = GetProperty(Line);
} else if (Line.find("DTSTAMP") == 0) {
NewEvent->DtStamp = GetProperty(Line);
} else if (Line.find("DTSTART") == 0) {
NewEvent->DtStart = GetProperty(Line);
} else if (Line.find("DTEND") == 0) {
NewEvent->DtEnd = GetProperty(Line);
} else if (Line.find("DESCRIPTION") == 0) {
NewEvent->Description = GetProperty(Line);
} else if (Line.find("CATEGORIES") == 0) {
NewEvent->Categories = GetProperty(Line);
} else if (Line.find("RRULE") == 0) {
NewEvent->RRule.Freq = ConvertFrequency(GetSubProperty(Line, "FREQ"));
NewEvent->RRule.Interval = atoi(GetSubProperty(Line, "INTERVAL").c_str());
if (NewEvent->RRule.Interval == 0)
NewEvent->RRule.Interval = 1;
NewEvent->RRule.Count = atoi(GetSubProperty(Line, "COUNT").c_str());
NewEvent->RRule.Until = GetSubProperty(Line, "UNTIL");
} else if (Line.find("BEGIN:VALARM") == 0) {
NewAlarm.Clear();
PrevComponent = CurrentComponent;
CurrentComponent = VALARM;
} else if (Line.find("END:VEVENT") == 0) {
if (NewEvent->UID.empty())
NewEvent->UID = NoUID++;
Events.push_back(NewEvent);
CurrentComponent = VCALENDAR;
}
break;
case VALARM:
if (Line.find("ACTION") == 0) {
NewAlarm.Action = ConvertAlarmAction(GetProperty(Line));
} else if (Line.find("TRIGGER") == 0) {
NewAlarm.Trigger = GetProperty(Line);
} else if (Line.find("DESCRIPTION") == 0) {
NewAlarm.Description = GetProperty(Line);
} else if (Line.find("END:VALARM") == 0) {
NewEvent->Alarms->push_back(NewAlarm);
CurrentComponent = PrevComponent;
}
break;
}
}
File.close();
}
/*Event* ICalendar::GetEventByUID(char *UID) {
for (list<Event *>::iterator Iterator = Events.begin(); Iterator != Events.end(); ++Iterator) {
if ((*Iterator)->UID.find(UID) == 0) {
return *Iterator;
}
}
return NULL;
}*/
void ICalendar::AddEvent(Event *NewEvent) {
char Temp[16];
string Line;
streamoff Offset;
NewEvent->DtStamp.SetToNow();
NewEvent->UID = NewEvent->DtStamp;
NewEvent->UID += '-';
sprintf(Temp, "%d", rand());
NewEvent->UID += Temp;
Events.push_back(NewEvent);
// for some reason tellg() modifies the get pointer under Windows if the file
// is not opened in the binary mode (possibly because of UTF-8?)
fstream File(FileName, ios::in | ios::out | ios::binary);
do {
Offset = File.tellg();
getline(File, Line);
} while (!File.eof() && Line.find("END:VCALENDAR") != 0);
File.seekp(Offset, ios::beg);
File << *NewEvent;
File << "END:VCALENDAR\r\n";
File.close();
}
void ICalendar::DeleteEvent(Event *DeletedEvent) {
fstream File;
string Data, Line, PartialData;
unsigned int Length;
bool Copy = true, Deleted = false;
File.open(FileName, ios::in | ios::binary);
File.seekg(0, ios::end);
Length = File.tellg();
File.seekg(0, ios::beg);
// to avoid reallocating memory
Data.reserve(Length);
while (!File.eof()) {
getline(File, Line);
Length = Line.length();
if (Length <= 1)
continue;
// getline() removes only '\n' from the end of the line (not '\r')
FixLineEnd(Line, Length);
if (Line.find("BEGIN:VEVENT") == 0) {
Copy = false;
Deleted = false;
PartialData = "";
} else if (Line.find("UID:") == 0 && Line.find(DeletedEvent->UID) == 4) {
Deleted = true;
}
if (Copy == true)
Data += Line;
else if (Deleted == false)
PartialData += Line;
if (Line.find("END:VEVENT") == 0) {
Copy = true;
if (Deleted == false)
Data += PartialData;
}
}
File.close();
File.clear();
// again problems in non-binary mode - "\r\n" changed to "\r\r\n"
File.open(FileName, ios::out | ios::binary | ios::trunc);
File << Data;
File.close();
for (list<Event *>::iterator Iterator = Events.begin(); Iterator != Events.end();) {
if (*Iterator == DeletedEvent) {
delete *Iterator;
Events.erase(Iterator++);
} else
++Iterator;
}
}
void ICalendar::ModifyEvent(Event *ModifiedEvent) {
fstream File;
string Data, Line, PartialData;
unsigned int Length;
bool Copy = true, Modified = false;
File.open(FileName, ios::in | ios::binary);
File.seekg(0, ios::end);
Length = File.tellg();
File.seekg(0, ios::beg);
// we will probably need at least such amount of memory
Data.reserve(Length);
while (!File.eof()) {
getline(File, Line);
Length = Line.length();
if (Length <= 1)
continue;
// getline() removes only '\n' from the end of the line (not '\r')
FixLineEnd(Line, Length);
if (Line.find("BEGIN:VEVENT") == 0) {
Copy = false;
Modified = false;
PartialData = "";
} else if (Line.find("UID:") == 0 && Line.find(ModifiedEvent->UID) == 4) {
Modified = true;
Data += *ModifiedEvent;
}
if (Copy == true)
Data += Line;
else if (Modified == false)
PartialData += Line;
if (Line.find("END:VEVENT") == 0) {
Copy = true;
if (Modified == false)
Data += PartialData;
}
}
File.close();
File.clear();
// again problems in non-binary mode - "\r\n" changed to "\r\r\n"
File.open(FileName, ios::out | ios::binary | ios::trunc);
File << Data;
File.close();
}
/// ICalendar::Query
Event* ICalendar::Query::GetNextEvent(bool WithAlarm) {
static Event *RecurrentEvent = NULL;
/* not all events have DtEnd, but we need some DtEnd for various checks,
so we will use this for temporary DtEnd derived from DtStart (following
RFC 2445, 4.6.1) */
Date DtEnd;
unsigned long Difference;
unsigned short Rest;
if (RecurrentEvent != NULL) {
RecurrentEvent->DtStart[RecurrentEvent->RRule.Freq] += RecurrentEvent->RRule.Interval;
if (!RecurrentEvent->DtEnd.IsEmpty())
RecurrentEvent->DtEnd[RecurrentEvent->RRule.Freq] += RecurrentEvent->RRule.Interval;
++RecurrentEvent->RecurrenceNo;
if (
(!WithAlarm &&
RecurrentEvent->DtStart <= Criteria.To &&
(RecurrentEvent->RRule.Until.IsEmpty() || RecurrentEvent->RRule.Until >= RecurrentEvent->DtStart) &&
(RecurrentEvent->RRule.Count == 0 || RecurrentEvent->RecurrenceNo < RecurrentEvent->RRule.Count)) ||
(WithAlarm && RecurrentEvent->HasAlarm(Criteria.From, Criteria.To))
) {
RecurrentEvents.push_back(new Event(*RecurrentEvent));
return RecurrentEvents.back();
}
delete RecurrentEvent;
RecurrentEvent = NULL;
}
if (RecurrentEvent == NULL) {
for (; EventsIterator != Calendar->Events.end(); ++EventsIterator) {
if ((*EventsIterator)->DtEnd.IsEmpty()) {
DtEnd = (*EventsIterator)->DtStart;
if ((*EventsIterator)->DtStart.WithTime == false)
++DtEnd[DAY];
} else {
DtEnd = (*EventsIterator)->DtEnd;
}
if (
Criteria.AllEvents == true || (
!WithAlarm &&
// DtEnd is non-inclusive (according to RFC 2445)
(DtEnd > Criteria.From || (*EventsIterator)->DtStart >= Criteria.From) &&
(*EventsIterator)->DtStart <= Criteria.To
) ||
(WithAlarm && (*EventsIterator)->HasAlarm(Criteria.From, Criteria.To))
) {
if (Criteria.AllEvents == false && Criteria.IncludeRecurrent == true && (*EventsIterator)->RRule.IsEmpty() == false)
RecurrentEvent = new Event(**EventsIterator);
return *(EventsIterator++);
} else if (
(*EventsIterator)->RRule.IsEmpty() == false &&
(*EventsIterator)->DtStart < Criteria.From &&
((*EventsIterator)->RRule.Until.IsEmpty() || (*EventsIterator)->RRule.Until >= Criteria.From) &&
Criteria.IncludeRecurrent == true
) {
RecurrentEvent = new Event(**EventsIterator);
Difference = Criteria.From.Difference(DtEnd, RecurrentEvent->RRule.Freq);
Rest = Difference%RecurrentEvent->RRule.Interval;
if (Rest != 0)
Difference += RecurrentEvent->RRule.Interval - Rest;
//cout << Criteria.From.Format() << endl;
//cout << DtEnd.Format() << endl;
//cout << Difference << endl;
RecurrentEvent->DtStart[RecurrentEvent->RRule.Freq] += Difference;
DtEnd[RecurrentEvent->RRule.Freq] += Difference;
RecurrentEvent->RecurrenceNo = Difference / RecurrentEvent->RRule.Interval;
// <= because DtEnd is non-inclusive (according to RFC 2445)
while (DtEnd <= Criteria.From) {
RecurrentEvent->DtStart[RecurrentEvent->RRule.Freq] += RecurrentEvent->RRule.Interval;
DtEnd[RecurrentEvent->RRule.Freq] += RecurrentEvent->RRule.Interval;
++RecurrentEvent->RecurrenceNo;
}
if (
(!WithAlarm &&
RecurrentEvent->DtStart <= Criteria.To &&
// < because DtStart counts as the first occurence
(RecurrentEvent->RRule.Count == 0 || RecurrentEvent->RecurrenceNo < RecurrentEvent->RRule.Count)) ||
(WithAlarm && RecurrentEvent->HasAlarm(Criteria.From, Criteria.To))
) {
++EventsIterator;
if (!RecurrentEvent->DtEnd.IsEmpty())
RecurrentEvent->DtEnd = DtEnd;
RecurrentEvents.push_back(new Event(*RecurrentEvent));
return RecurrentEvents.back();
}
delete RecurrentEvent;
RecurrentEvent = NULL;
}
}
}
return NULL;
}