-
Notifications
You must be signed in to change notification settings - Fork 2
/
icalendar.h
123 lines (106 loc) · 3.39 KB
/
icalendar.h
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
/*************************************************************************
** Copyright (C) 2024 Jan Pedersen <jp@jp-embedded.com>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#ifndef _ICALENDAR_H
#define _ICALENDAR_H
#include "types.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;
class ICalendar {
public:
ICalendar(const char *FileName) : FileName((char *)FileName) {
LoadFromFile();
}
~ICalendar() { for_each(Events.begin(), Events.end(), DeleteItem()); }
void LoadFromFile();
// Event* GetEventByUID(char *UID);
void AddEvent(Event *NewEvent);
void DeleteEvent(Event *DeletedEvent);
void ModifyEvent(Event *ModifiedEvent);
class Query;
private:
string GetProperty(const string &Line) const {
// if VALUE=DATE or VALUE=DATE-TIME used, then the date is not directly
// after the property name so we just search for the string after ':'
string Temp = Line.substr(Line.find_first_of(':') + 1);
unsigned int Length = Temp.length();
if (Length > 0 && Temp[Length - 1] == '\r')
Temp.resize(Length - 1);
return Temp;
}
string GetSubProperty(const string &Line, const char *SubProperty) const {
size_t Pos = Line.find(SubProperty);
if (Pos == string::npos)
return "";
Pos += strlen(SubProperty) + 1;
return Line.substr(Pos, Line.find_first_of(";:", Pos) - Pos);
}
void FixLineEnd(string &Line, unsigned int Length) {
if (Length > 0 && Line[Length - 1] == '\r')
Line.resize(Length - 1);
Line += "\r\n";
}
char *FileName;
list<Event *> Events;
};
class ICalendar::Query {
public:
Query(ICalendar *Calendar)
: Calendar(Calendar), EventsIterator(Calendar->Events.begin()) {}
~Query() {
for_each(RecurrentEvents.begin(), RecurrentEvents.end(), DeleteItem());
}
void ResetPosition() {
for_each(RecurrentEvents.begin(), RecurrentEvents.end(), DeleteItem());
RecurrentEvents.clear();
EventsIterator = Calendar->Events.begin();
}
Event *GetNextEvent(bool WithAlarm = false);
EventsCriteria Criteria;
private:
ICalendar *Calendar;
list<Event *> RecurrentEvents;
list<Event *>::iterator EventsIterator;
};
inline TimeUnit ConvertFrequency(string Name) {
if (Name == "SECONDLY")
return SECOND;
if (Name == "MINUTELY")
return MINUTE;
if (Name == "HOURLY")
return HOUR;
if (Name == "DAILY")
return DAY;
if (Name == "WEEKLY")
return WEEK;
if (Name == "MONTHLY")
return MONTH;
return YEAR;
}
inline AlarmAction ConvertAlarmAction(string Name) {
if (Name == "AUDIO")
return AUDIO;
if (Name == "PROCEDURE")
return PROCEDURE;
if (Name == "EMAIL")
return EMAIL;
return DISPLAY;
}
#endif // _ICALENDAR_H