2017-02-16 15:59:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
calendar "google.golang.org/api/calendar/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Calendar struct {
|
|
|
|
AccessRole string
|
|
|
|
BackgroundColor string
|
|
|
|
ColorId string
|
|
|
|
DefaultReminders []*calendar.EventReminder // TODO: Local Reminder struct?
|
|
|
|
Deleted bool
|
|
|
|
Description string
|
|
|
|
Etag string
|
|
|
|
ForegroundColor string
|
|
|
|
Hidden bool
|
|
|
|
Id string
|
|
|
|
Kind string
|
|
|
|
Location string
|
|
|
|
NotificationSettings *calendar.CalendarListEntryNotificationSettings // TODO: Needed?
|
|
|
|
Primary bool
|
|
|
|
Selected bool
|
|
|
|
Summary string
|
|
|
|
SummaryOverride string
|
|
|
|
TimeZone string
|
|
|
|
ForceSendFields []string // TODO: Needed?
|
|
|
|
NullFields []string //TODO: Needed?
|
|
|
|
}
|
|
|
|
|
|
|
|
func GoogleCalendarToLocal(c *calendar.CalendarListEntry) *Calendar {
|
|
|
|
ret := Calendar{
|
|
|
|
AccessRole: c.AccessRole,
|
|
|
|
BackgroundColor: c.BackgroundColor,
|
|
|
|
ColorId: c.ColorId,
|
|
|
|
DefaultReminders: c.DefaultReminders,
|
|
|
|
Deleted: c.Deleted,
|
|
|
|
Description: c.Description,
|
|
|
|
Etag: c.Etag,
|
|
|
|
ForegroundColor: c.ForegroundColor,
|
|
|
|
Hidden: c.Hidden,
|
|
|
|
Id: c.Id,
|
|
|
|
Kind: c.Kind,
|
|
|
|
Location: c.Location,
|
|
|
|
Primary: c.Primary,
|
|
|
|
Selected: c.Selected,
|
|
|
|
Summary: c.Summary,
|
|
|
|
SummaryOverride: c.SummaryOverride,
|
|
|
|
TimeZone: c.TimeZone,
|
|
|
|
}
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calendar) GetTodaysEvents() []Event {
|
|
|
|
var ret []Event
|
|
|
|
|
|
|
|
minTime := time.Date(
|
|
|
|
time.Now().Year(),
|
|
|
|
time.Now().Month(),
|
|
|
|
time.Now().Day(),
|
|
|
|
0, 0, 0, 0, time.Local,
|
|
|
|
)
|
|
|
|
maxTime := time.Date(
|
|
|
|
time.Now().Year(),
|
|
|
|
time.Now().Month(),
|
|
|
|
time.Now().Day(),
|
|
|
|
23, 59, 59, 999999999, time.Local,
|
|
|
|
)
|
|
|
|
events, err := state.account.Service.Events.List(c.Id).
|
|
|
|
ShowDeleted(false).
|
|
|
|
SingleEvents(true).
|
|
|
|
TimeMin(minTime.Format(time.RFC3339)).
|
|
|
|
TimeMax(maxTime.Format(time.RFC3339)).
|
|
|
|
OrderBy("startTime").
|
|
|
|
Do()
|
|
|
|
if err != nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
for _, e := range events.Items {
|
2017-03-15 19:38:37 +00:00
|
|
|
ret = append(ret, *GoogleEventToLocalWithId(e, c.Id))
|
2017-02-16 15:59:33 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calendar) GetCalendarEvents() []Event {
|
|
|
|
var ret []Event
|
|
|
|
events, err := state.account.Service.Events.List(c.Id).ShowDeleted(false).
|
|
|
|
SingleEvents(true).OrderBy("startTime").Do()
|
|
|
|
if err != nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
for _, e := range events.Items {
|
2017-03-15 19:38:37 +00:00
|
|
|
ret = append(ret, *GoogleEventToLocalWithId(e, c.Id))
|
2017-02-16 15:59:33 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For Adding a private copy of an existing event
|
|
|
|
func (c *Calendar) ImportEvent(event *Event) error {
|
|
|
|
_, err := state.account.Service.Events.Import(c.Id, event).Do()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
func (c *Calendar) InsertEvent(event *Event) (*Event, error) {
|
|
|
|
e, err := state.account.Service.Events.Insert(c.Id, LocalEventToGoogle(event)).Do()
|
|
|
|
return GoogleEventToLocal(e), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calendar) DeleteEvent(eId string) error {
|
|
|
|
return state.account.Service.Events.Delete(c.Id, eId).Do()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calendar) GetEvent(eId string) (*Event, error) {
|
|
|
|
e, err := state.account.Service.Events.Get(c.Id, eId).Do()
|
|
|
|
return GoogleEventToLocal(e), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calendar) QuickAdd(text string) (*Event, error) {
|
|
|
|
e, err := state.account.Service.Events.QuickAdd(c.Id, text).Do()
|
|
|
|
return GoogleEventToLocal(e), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Calendar) Update(eId string, event *Event) (*Event, error) {
|
|
|
|
e, err := state.account.Service.Events.Update(c.Id, eId, LocalEventToGoogle(event)).Do()
|
|
|
|
return GoogleEventToLocal(e), err
|
|
|
|
}
|