Ejemplo n.º 1
0
        // ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
        public ResultCode ToPosixTime(ServiceCtx context)
        {
            long inBufferPosition = context.Request.SendBuff[0].Position;
            long inBufferSize     = context.Request.SendBuff[0].Size;

            CalendarTime calendarTime = context.RequestData.ReadStruct <CalendarTime>();

            if (inBufferSize != 0x4000)
            {
                // TODO: find error code here
                Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{inBufferSize:x} (expected 0x4000)");

                throw new InvalidOperationException();
            }

            TimeZoneRule rules = MemoryHelper.Read <TimeZoneRule>(context.Memory, inBufferPosition);

            ResultCode resultCode = _timeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);

            if (resultCode == 0)
            {
                long outBufferPosition = context.Request.RecvListBuff[0].Position;
                long outBufferSize     = context.Request.RecvListBuff[0].Size;

                context.Memory.Write((ulong)outBufferPosition, posixTime);
                context.ResponseData.Write(1);
            }

            return(resultCode);
        }
        public static DateTime ToDateTime(this CalendarTime calendarTime)
        {
            DateTime result;

            try
            {
                result = new DateTime(calendarTime.year, (int)(calendarTime.month + 1u), (int)(calendarTime.monthDay + 1u), calendarTime.hour, calendarTime.minute, 0);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Debug.LogException(ex);
                result = DateTime.MaxValue;
            }
            return(result);
        }
Ejemplo n.º 3
0
        // List events.
        private List <MetroCalendarEvent> listEvents()
        {
            List <MetroCalendarEvent> eventsList = new List <MetroCalendarEvent>();
            Events events = request.Execute();

            foreach (Event Event in events.Items)
            {
                CalendarTime time = new CalendarTime();
                time.Date     = Event.Start.Date;
                time.DateTime = Event.Start.DateTime.Value;
                time.TimeZone = Event.Start.TimeZone;
                MetroCalendarEvent metroEvent = new MetroCalendarEvent();
                metroEvent.Start   = time;
                metroEvent.Summary = Event.Summary;
                eventsList.Add(metroEvent);
            }
            return(eventsList);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets/replaces the event reminder for the specified calendar event
        /// </summary>
        /// <param name="calendarEvent">Event to add the reminder to</param>
        /// <param name="reminder">The reminder</param>
        /// <returns>If successful</returns>
        /// <exception cref="ArgumentException">Calendar event is not created or not valid</exception>
        /// <exception cref="System.InvalidOperationException">Editing recurring events is not supported</exception>
        /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public async Task <bool> AddEventReminderAsync(CalendarEvent calendarEvent, CalendarEventReminder reminder)
        {
            if (string.IsNullOrEmpty(calendarEvent.ExternalID))
            {
                throw new ArgumentException("Missing calendar event identifier", "calendarEvent");
            }

            var existingEvt = await GetEventByIdAsync(calendarEvent.ExternalID).ConfigureAwait(false);

            if (existingEvt == null)
            {
                throw new ArgumentException("Specified calendar event not found on device");
            }

            CalendarManager calManager = null;
            CalendarRecord  calRecord = null, calAlarm = null;
            CalendarTime    startTime = null, calTime = null;

            try
            {
                calManager = new CalendarManager();
                calRecord  = calManager.Database.Get(Event.Uri, Convert.ToInt32(calendarEvent.ExternalID));
                startTime  = calRecord.Get <CalendarTime>(Event.Start);
                calTime    = new CalendarTime(startTime.UtcTime.AddMinutes(reminder?.TimeBefore.TotalMinutes ?? 15).Ticks);
                calAlarm   = new CalendarRecord(Alarm.Uri);

                calAlarm.Set <CalendarTime>(Alarm.AlarmTime, calTime);
                calAlarm.Set <int>(Alarm.TickUnit, (int)CalendarTypes.TickUnit.Specific);
                calRecord.AddChildRecord(Event.Alarm, calAlarm);
                calManager.Database.Update(calRecord);
            }
            finally
            {
                calAlarm?.Dispose();
                calAlarm = null;

                calRecord?.Dispose();
                calRecord = null;

                calManager?.Dispose();
                calManager = null;
            }
            return(true);
        }
Ejemplo n.º 5
0
        // ToPosixTimeWithMyRule(nn::time::CalendarTime calendarTime) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
        public ResultCode ToPosixTimeWithMyRule(ServiceCtx context)
        {
            CalendarTime calendarTime = context.RequestData.ReadStruct <CalendarTime>();

            ResultCode resultCode = _timeZoneManager.ToPosixTimeWithMyRules(calendarTime, out long posixTime);

            if (resultCode == 0)
            {
                long outBufferPosition = context.Request.RecvListBuff[0].Position;
                long outBufferSize     = context.Request.RecvListBuff[0].Size;

                context.Memory.Write((ulong)outBufferPosition, posixTime);

                // There could be only one result on one calendar as leap seconds aren't supported.
                context.ResponseData.Write(1);
            }

            return(resultCode);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets a single calendar event by platform-specific ID.
        /// </summary>
        /// <param name="externalId">Platform-specific calendar event identifier</param>
        /// <returns>The corresponding calendar event, or null if not found</returns>
        /// <exception cref="System.UnauthorizedAccessException">Calendar access denied</exception>
        /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public Task <CalendarEvent> GetEventByIdAsync(string externalId)
        {
            CalendarEvent   calEvent   = null;
            CalendarManager calManager = null;
            CalendarRecord  calRecord  = null;

            try
            {
                calManager = new CalendarManager();
                calRecord  = calManager.Database.Get(Event.Uri, Convert.ToInt32(externalId));

                string       summary     = calRecord.Get <string>(Event.Summary);
                CalendarTime startTime   = calRecord.Get <CalendarTime>(Event.Start);
                CalendarTime endTime     = calRecord.Get <CalendarTime>(Event.End);
                string       location    = calRecord.Get <string>(Event.Location);
                string       description = calRecord.Get <string>(Event.Description);
                int          IsAllday    = calRecord.Get <int>(Event.IsAllday);
                int          Id          = calRecord.Get <int>(Event.Id);

                calEvent = new CalendarEvent
                {
                    Name        = summary,
                    Start       = startTime.UtcTime,
                    End         = endTime.UtcTime,
                    Location    = location,
                    Description = description,
                    AllDay      = Convert.ToBoolean(IsAllday),
                    ExternalID  = Id.ToString(),
                };
            }
            finally
            {
                calRecord?.Dispose();
                calRecord = null;

                calManager?.Dispose();
                calManager = null;
            }
            return(Task.FromResult(calEvent));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets all events for a calendar within the specified time range.
        /// </summary>
        /// <param name="calendar">Calendar containing events</param>
        /// <param name="start">Start of event range</param>
        /// <param name="end">End of event range</param>
        /// <returns>Calendar events</returns>
        /// <exception cref="System.ArgumentException">Calendar does not exist on device</exception>
        /// <exception cref="System.UnauthorizedAccessException">Calendar access denied</exception>
        /// <exception cref="Plugin.Calendars.Abstractions.PlatformException">Unexpected platform-specific error</exception>
        public Task <IList <CalendarEvent> > GetEventsAsync(Calendar calendar, DateTime start, DateTime end)
        {
            var             calEvent   = new List <CalendarEvent>();
            CalendarManager calManager = null;
            CalendarQuery   calQuery   = null;
            CalendarList    calList    = null;
            CalendarRecord  calRecord  = null;
            CalendarFilter  calFilter  = null;

            try
            {
                calManager = new CalendarManager();
                calFilter.AddCondition(CalendarFilter.LogicalOperator.And, Event.Start, CalendarFilter.IntegerMatchType.GreaterThanOrEqual, ConvertIntPtrToCalendarTime(start));
                calFilter.AddCondition(CalendarFilter.LogicalOperator.And, Event.End, CalendarFilter.IntegerMatchType.LessThanOrEqual, ConvertIntPtrToCalendarTime(end));
                calQuery = new CalendarQuery(Event.Uri);
                calQuery.SetFilter(calFilter);
                calList = calManager.Database.GetRecordsWithQuery(calQuery, 0, 0);

                if (calList.Count == 0)
                {
                    return(Task.FromResult <IList <CalendarEvent> >(calEvent));
                }

                calList.MoveFirst();

                do
                {
                    calRecord = calList.GetCurrentRecord();
                    string       summary     = calRecord.Get <string>(Event.Summary);
                    CalendarTime startTime   = calRecord.Get <CalendarTime>(Event.Start);
                    CalendarTime endTime     = calRecord.Get <CalendarTime>(Event.End);
                    string       location    = calRecord.Get <string>(Event.Location);
                    string       description = calRecord.Get <string>(Event.Description);
                    int          IsAllday    = calRecord.Get <int>(Event.IsAllday);
                    int          Id          = calRecord.Get <int>(Event.Id);

                    calEvent.Add(
                        new CalendarEvent
                    {
                        Name        = summary,
                        Start       = startTime.UtcTime,
                        End         = endTime.UtcTime,
                        Location    = location,
                        Description = description,
                        AllDay      = Convert.ToBoolean(IsAllday),
                        ExternalID  = Id.ToString(),
                    }
                        );
                } while (calList.MoveNext());
            }
            finally
            {
                calRecord?.Dispose();
                calRecord = null;

                calFilter?.Dispose();
                calFilter = null;

                calQuery?.Dispose();
                calQuery = null;

                calList?.Dispose();
                calList = null;

                calManager?.Dispose();
                calManager = null;
            }
            return(Task.FromResult <IList <CalendarEvent> >(calEvent));
        }