gal/struct_calendar.go

148 lines
4.0 KiB
Go

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 {
// Check if this event is an all-day event and, if so, is it actually today?
if _, err := time.Parse(time.RFC3339, e.Start.DateTime); err != nil {
tz := time.Now().Local().Format("-07:00")
stDt := e.Start.Date + "T00:00:00" + tz
stTm, err := time.Parse(time.RFC3339, stDt)
if err != nil {
continue
}
endDt := e.End.Date + "T00:00:00" + tz
endTm, err := time.Parse(time.RFC3339, endDt)
if err != nil {
continue
}
if stTm.After(maxTime) {
continue
}
if stTm.After(maxTime) || endTm.Before(minTime) {
continue
}
}
ret = append(ret, *GoogleEventToLocalWithId(e, c.Id))
}
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 {
ret = append(ret, *GoogleEventToLocalWithId(e, c.Id))
}
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
}