forked from laurent22/ical-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathical.go
374 lines (333 loc) · 9.86 KB
/
ical.go
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
package ical
import (
"strings"
"time"
"strconv"
"errors"
"log"
)
// ~/Library/Calendars/05C38275-CBA9-4321-8457-2DC326799AE8.calendar/Events
// BEGIN:VCALENDAR
// VERSION:2.0
// CALSCALE:GREGORIAN
// BEGIN:VTODO
// STATUS:COMPLETED
// CREATED:20130812T125803Z
// UID:4E00D9DC-5AF8-43DA-B543-EACB8182CA97
// SUMMARY:dghshdfjhf
// COMPLETED:20130812T125805Z
// DTSTAMP:20130812T125803Z
// PRIORITY:1
// PERCENT-COMPLETE:100
// SEQUENCE:0
// DESCRIPTION:erezrare\nrateztaz\ntrois
// END:VTODO
// END:VCALENDAR
// STATUS: NEEDS-ACTION / COMPLETE
// PERCENT-COMPLETE - enlever / 100
// DTSTART;TZID=Asia/Harbin:20130817T000000
// DUE;TZID=Asia/Harbin:20130817T000000
// BEGIN:VALARM
// X-WR-ALARMUID:FEEACF1F-D546-462C-A3E2-F9706F44BB33
// UID:FEEACF1F-D546-462C-A3E2-F9706F44BB33
// TRIGGER;VALUE=DATE-TIME:20130816T160000Z
// DESCRIPTION:Avertissement événement
// ACTION:DISPLAY
// END:VALARM
// TODO: TIME ZONE
func EncodeDateProperty(name string, t time.Time) string {
var output string
zone, _ := t.Zone()
if zone != "UTC" && zone != "" {
output = ";TZID=" + zone + ":" + t.Format("20060102T150405")
} else {
output = ":" + t.Format("20060102T150405") + "Z"
}
return name + output
}
func EscapeTextType(input string) string {
output := strings.Replace(input, "\\", "\\\\", -1)
output = strings.Replace(output, ";", "\\;", -1)
output = strings.Replace(output, ",", "\\,", -1)
output = strings.Replace(output, "\n", "\\n", -1)
return output
}
type Node struct {
Name string
Value string
Type int // 1 = Object, 0 = Name/Value
Parameters map[string]string
Children []*Node
}
func (this *Node) Parameter(name string, defaultValue string) string {
if len(this.Parameters) <= 0 { return defaultValue }
v, ok := this.Parameters[name]
if !ok { return defaultValue }
return v
}
func (this *Node) ChildrenByName(name string) []*Node {
var output []*Node
for _, child := range this.Children {
if child.Name == name {
output = append(output, child)
}
}
return output
}
func (this *Node) ChildByName(name string) *Node {
for _, child := range this.Children {
if child.Name == name {
return child
}
}
return nil
}
func (this *Node) PropString(name string, defaultValue string) string {
for _, child := range this.Children {
if child.Name == name {
return child.Value
}
}
return defaultValue
}
func (this *Node) PropDate(name string, defaultValue time.Time) time.Time {
// 20130816T160000Z
// Mon Jan 2 15:04:05 -0700 MST 2006
// DUE;TZID=Asia/Harbin:20130817T000000
node := this.ChildByName(name)
if node == nil { return defaultValue }
tzid := node.Parameter("TZID", "")
var output time.Time
var err error
if tzid != "" {
loc, err := time.LoadLocation(tzid)
if err != nil { panic(err) }
output, err = time.ParseInLocation("20060102T150405", node.Value, loc)
} else {
output, err = time.Parse("20060102T150405Z", node.Value)
}
if err != nil { panic(err) }
return output
}
func (this *Node) PropInt(name string, defaultValue int) int {
n := this.PropString(name, "")
if n == "" { return defaultValue }
output, err := strconv.Atoi(n)
if err != nil { panic(err) }
return output
}
func (this *Node) String() string {
s := ""
if this.Type == 1 {
s += "===== " + this.Name
s += "\n"
} else {
s += this.Name
s += ":" + this.Value
s += "\n"
}
for _, child := range this.Children {
s += child.String()
}
if this.Type == 1 {
s += "===== /" + this.Name
s += "\n"
}
return s
}
func UnescapeTextType(s string) string {
s = strings.Replace(s, "\\;", ";", -1)
s = strings.Replace(s, "\\,", ",", -1)
s = strings.Replace(s, "\\n", "\n", -1)
s = strings.Replace(s, "\\\\", "\\", -1)
return s
}
func ParseTextType(lines []string, lineIndex int) (string, int) {
line := lines[lineIndex]
colonIndex := strings.Index(line, ":")
output := strings.TrimSpace(line[colonIndex+1:len(line)])
lineIndex++
for {
line := lines[lineIndex]
if line == "" || line[0] != ' ' {
return UnescapeTextType(output), lineIndex
}
output += line[1:len(line)]
lineIndex++
}
return UnescapeTextType(output), lineIndex
}
func EncodeTextType(s string) string {
output := ""
s = EscapeTextType(s)
lineLength := 0
for _, c := range s {
if lineLength + len(string(c)) > 75 {
output += "\n "
lineLength = 1
}
output += string(c)
lineLength += len(string(c))
}
return output
}
// BEGIN:VCALENDAR
// VERSION:2.0
// CALSCALE:GREGORIAN
// BEGIN:VTODO
// STATUS:COMPLETED
// CREATED:20130812T125803Z
// UID:4E00D9DC-5AF8-43DA-B543-EACB8182CA97
// SUMMARY:dghshdfjhf
// COMPLETED:20130812T125805Z
// DTSTAMP:20130812T125803Z
// PRIORITY:1
// PERCENT-COMPLETE:100
// SEQUENCE:0
// DESCRIPTION:erezrare\nrateztaz\ntrois
// END:VTODO
// END:VCALENDAR
func TodoFromNode(node *Node) Todo {
if node.Name != "VTODO" { panic("Node is not a VTODO") }
var todo Todo
todo.SetId(node.PropString("UID", ""))
todo.SetSummary(node.PropString("SUMMARY", ""))
todo.SetDescription(node.PropString("DESCRIPTION", ""))
todo.SetDueDate(node.PropDate("DUE", time.Time{}))
//todo.SetAlarmDate(this.TimestampBytesToTime(reminderDate))
todo.SetCreatedDate(node.PropDate("CREATED", time.Time{}))
todo.SetModifiedDate(node.PropDate("DTSTAMP", time.Time{}))
todo.SetPriority(node.PropInt("PRIORITY", 0))
todo.SetPercentComplete(node.PropInt("PERCENT-COMPLETE", 0))
return todo
}
func ParseCalendarNode(lines []string, lineIndex int) (*Node, bool, error, int) {
line := strings.TrimSpace(lines[lineIndex])
_ = log.Println
colonIndex := strings.Index(line, ":")
if colonIndex <= 0 {
return nil, false, errors.New("Invalid value/pair: " + line), lineIndex + 1
}
name := line[0:colonIndex]
splitted := strings.Split(name, ";")
var parameters map[string]string
if len(splitted) >= 2 {
name = splitted[0]
parameters = make(map[string]string)
for i := 1; i < len(splitted); i++ {
p := strings.Split(splitted[i], "=")
if len(p) != 2 { panic("Invalid parameter format: " + name) }
parameters[p[0]] = p[1]
}
}
value := line[colonIndex+1:len(line)]
if name == "BEGIN" {
node := new(Node)
node.Name = value
node.Type = 1
lineIndex = lineIndex + 1
for {
child, finished, _, newLineIndex := ParseCalendarNode(lines, lineIndex)
if finished {
return node, false, nil, newLineIndex
} else {
if child != nil {
node.Children = append(node.Children, child)
}
lineIndex = newLineIndex
}
}
} else if name == "END" {
return nil, true, nil, lineIndex + 1
} else {
node := new(Node)
node.Name = name
if name == "DESCRIPTION" || name == "SUMMARY" {
text, newLineIndex := ParseTextType(lines, lineIndex)
node.Value = text
node.Parameters = parameters
return node, false, nil, newLineIndex
} else {
node.Value = value
node.Parameters = parameters
return node, false, nil, lineIndex + 1
}
}
panic("Unreachable")
return nil, false, nil, lineIndex + 1
}
type Calendar struct {
Items []CalendarItem
}
type CalendarItem struct {
id string
summary string
description string
priority int // 0..9 (O -> none, 1 -> highest, 9 -> lowest)
percentComplete int
createdDate time.Time
modifiedDate time.Time
completedDate time.Time
startDate time.Time
alarmDate time.Time
sequence int
}
func (this *CalendarItem) SetId(v string) { this.id = v }
func (this *CalendarItem) Id() string { return this.id }
func (this *CalendarItem) SetSummary(v string) { this.summary = v }
func (this *CalendarItem) Summary() string { return this.summary }
func (this *CalendarItem) SetDescription(v string) { this.description = v }
func (this *CalendarItem) Description() string { return this.description }
func (this *CalendarItem) SetPriority(v int) { this.priority = v }
func (this *CalendarItem) Priority() int { return this.priority }
func (this *CalendarItem) SetPercentComplete(v int) { this.percentComplete = v }
func (this *CalendarItem) PercentComplete() int { return this.percentComplete }
func (this *CalendarItem) SetCreatedDate(v time.Time) { this.createdDate = v }
func (this *CalendarItem) CreatedDate() time.Time { return this.createdDate }
func (this *CalendarItem) SetModifiedDate(v time.Time) { this.modifiedDate = v }
func (this *CalendarItem) ModifiedDate() time.Time { return this.modifiedDate }
func (this *CalendarItem) SetCompletedDate(v time.Time) { this.completedDate = v }
func (this *CalendarItem) CompletedDate() time.Time { return this.completedDate }
func (this *CalendarItem) SetStartDate(v time.Time) { this.startDate = v }
func (this *CalendarItem) StartDate() time.Time { return this.startDate }
func (this *CalendarItem) SetAlarmDate(v time.Time) { this.alarmDate = v }
func (this *CalendarItem) AlarmDate() time.Time { return this.alarmDate }
func (this *CalendarItem) SetSequence(v int) { this.sequence = v }
func (this *CalendarItem) Sequence() int { return this.sequence }
type Todo struct {
CalendarItem
dueDate time.Time
}
func (this *Todo) SetDueDate(v time.Time) { this.dueDate = v }
func (this *Todo) DueDate() time.Time { return this.dueDate }
func (this *Todo) ICalString(target string) string {
s := "BEGIN:VTODO\n"
if target == "macTodo" {
status := "NEEDS-ACTION"
if this.PercentComplete() == 100 {
status = "COMPLETED"
}
s += "STATUS:" + status + "\n"
}
s += EncodeDateProperty("CREATED", this.CreatedDate()) + "\n"
s += "UID:" + this.Id() + "\n"
s += "SUMMARY:" + EscapeTextType(this.Summary()) + "\n"
if this.PercentComplete() == 100 && !this.CompletedDate().IsZero() {
s += EncodeDateProperty("COMPLETED", this.CompletedDate()) + "\n"
}
s += EncodeDateProperty("DTSTAMP", this.ModifiedDate()) + "\n"
if this.Priority() != 0 {
s += "PRIORITY:" + strconv.Itoa(this.Priority()) + "\n"
}
if this.PercentComplete() != 0 {
s += "PERCENT-COMPLETE:" + strconv.Itoa(this.PercentComplete()) + "\n"
}
if target == "macTodo" {
s += "SEQUENCE:" + strconv.Itoa(this.Sequence()) + "\n"
}
if this.Description() != "" {
s += "DESCRIPTION:" + EncodeTextType(this.Description()) + "\n"
}
s += "END:VTODO\n"
return s
}