Example #1
0
        public static string GenerateIcs(Model.Schedule schedule)
        {
            int      dayCount = schedule.Count * 7;
            DateTime firstDay = RuntimeData.StartDate;
            Calendar calendar = new Calendar();

            for (int i = 0; i < dayCount; i++)
            {
                DateTime date = firstDay.AddDays(i);
                foreach (Model.ScheduleEntry e in schedule.GetDaySchedule(i))
                {
                    (var start, var end) = SessionTimeConverter.ConvertShort(e.SessionSpan, CampusSelector.IsCampusD(e.Room));
                    CalendarEvent calEvent = new CalendarEvent()
                    {
                        DtStart     = new CalDateTime(new DateTime(date.Year, date.Month, date.Day, start.Hour, start.Minute, 0)),
                        DtEnd       = new CalDateTime(new DateTime(date.Year, date.Month, date.Day, end.Hour, end.Minute, 0)),
                        Location    = e.Room,
                        Summary     = e.Name,
                        Description = $"教师: {e.Lecturer}",
                        Alarms      =
                        {
                            new Alarm()
                            {
                                Trigger = new Trigger(new TimeSpan(0, -15, 0))
                            }
                        }
                    };
                    calendar.Events.Add(calEvent);
                }
            }
            var serializer = new Ical.Net.Serialization.CalendarSerializer();

            return(serializer.SerializeToString(calendar));
        }
Example #2
0
        public async Task <IActionResult> ExportICal(string calendarItemId)
        {
            CalendarItem calendarItem = await repository.GetDocument(calendarItemId);

            if (calendarItem == null)
            {
                return(new NotFoundResult());
            }
            string description = calendarItem.PlainDescription ?? calendarItem.Summary ?? String.Empty;

            if (!String.IsNullOrEmpty(calendarItem.UrlTitle))
            {
                description += " https://robert-brands.com/termine/" + calendarItem.UrlTitle;
            }
            CalendarEvent e = new CalendarEvent
            {
                Start       = new CalDateTime(calendarItem.StartDate),
                End         = new CalDateTime(calendarItem.EndDate),
                Summary     = calendarItem.Title,
                Description = description,
                Location    = calendarItem.Place,
                IsAllDay    = calendarItem.WholeDay,
                Organizer   = new Organizer(calendarItem.Host)
            };

            Ical.Net.Calendar calendar = new Ical.Net.Calendar();
            calendar.Events.Add(e);

            var    serializer = new Ical.Net.Serialization.CalendarSerializer();
            string iCal       = serializer.SerializeToString(calendar);

            return(File(new System.Text.UTF8Encoding().GetBytes(iCal), "text/calendar", "iCal.ics"));
        }
        // Create a .ics file for calendar events from a date range.
        public ActionResult ExportEventsToIcal(DateTime start, DateTime end)
        {
            // Check if dates are null before doing anything.
            if (start == null || end == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> No date range provided.", ""));
            }

            // Get user's calendar entries within the date range.
            List <CalEntry> userEntries = _scheduleRepository.GetAllUserEntries(User.Identity.GetUserId(), start, end);

            // Check if there are any diary entries to sync.
            if (userEntries == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> No calendar events to sync.", ""));
            }

            // Create iCal.
            var iCal = new Ical.Net.Calendar()
            {
                ProductId = "ASP.Net Diary Scheduler",
                Version   = "2.0"
            };

            // Create a new event for each calendar entry.
            foreach (CalEntry entry in userEntries)
            {
                // Create event.
                var evt = iCal.Create <Ical.Net.CalendarComponents.CalendarEvent>();

                // Prepare ical event.
                evt.Uid         = entry.CalendarEntryId.ToString();
                evt.Start       = new Ical.Net.DataTypes.CalDateTime(entry.DateFrom);
                evt.End         = new Ical.Net.DataTypes.CalDateTime(entry.DateTo);
                evt.Description = entry.Description;
                evt.Summary     = entry.Title;
                evt.IsAllDay    = entry.AllDay;
            }

            // Build the .ics file.
            Ical.Net.Serialization.SerializationContext ctx = new Ical.Net.Serialization.SerializationContext();
            var serialiser = new Ical.Net.Serialization.CalendarSerializer(ctx);

            string output      = serialiser.SerializeToString(iCal);
            string contentType = "text/calendar";
            var    bytes       = System.Text.Encoding.UTF8.GetBytes(output);

            // Create a name.
            Guid   fileId   = Guid.NewGuid();
            string fileName = fileId.ToString() + ".ics";

            return(File(bytes, contentType, fileName));
        }
Example #4
0
 public static string SerializeCalendar(Ical.Net.Calendar calendar)
 {
     try
     {
         var serializer = new Ical.Net.Serialization.CalendarSerializer(calendar);
         return(serializer.SerializeToString(calendar));
     }
     catch (Exception)
     {
         return(null);
     }
 }
Example #5
0
 public static string SerializeCalendar(Ical.Net.Calendar calendar)
 {
     try
     {
         var serializer = new Ical.Net.Serialization.CalendarSerializer();
         return(serializer.SerializeToString(calendar));
     }
     catch (Exception ex)
     {
         LogManager.GetLogger("ASC.Calendar").Error(ex);
         return(null);
     }
 }
Example #6
0
        public static string SerializeCalendar(Ical.Net.Calendar calendar)
        {
            try
            {
                var serializer = new Ical.Net.Serialization.CalendarSerializer();

                return(StringUtils.NormalizeStringForMySql(serializer.SerializeToString(calendar)));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return(null);
            }
        }
        // Create a .ics file for a calendar event.
        public ActionResult ExportEventToIcal(Guid id)
        {
            // Check if an id was sent.
            if (id == Guid.Empty)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> Invalid calendar event id.", ""));
            }

            var entry = _scheduleRepository.GetCalendarEntry(id);

            if (entry == null)
            {
                return(SiteErrorHandler.GetBadRequestActionResult("<strong>Error:</strong> The calendar event could not be found.", ""));
            }

            // Create iCal.
            var iCal = new Ical.Net.Calendar()
            {
                ProductId = "ASP.Net Diary Scheduler",
                Version   = "2.0"
            };

            // Create event.
            var evt = iCal.Create <Ical.Net.CalendarComponents.CalendarEvent>();

            // Prepare ical event.
            evt.Uid         = entry.CalendarEntryId.ToString();
            evt.Start       = new Ical.Net.DataTypes.CalDateTime(entry.DateFrom);
            evt.End         = new Ical.Net.DataTypes.CalDateTime(entry.DateTo);
            evt.Description = entry.Description;
            evt.Summary     = entry.Title;
            evt.IsAllDay    = entry.AllDay;

            // Build the .ics file.
            var ctx        = new Ical.Net.Serialization.SerializationContext();
            var serialiser = new Ical.Net.Serialization.CalendarSerializer(ctx);

            string output      = serialiser.SerializeToString(iCal);
            string contentType = "text/calendar";
            var    bytes       = System.Text.Encoding.UTF8.GetBytes(output);

            // Create a name.
            Guid   fileId   = Guid.NewGuid();
            string fileName = fileId.ToString() + ".ics";

            return(File(bytes, contentType, fileName));
        }
Example #8
0
        /// <summary>
        /// Create the <see cref="CalendarIcalViewModel"/> using the <see cref="Ical.Net.Calendar"/>.
        /// </summary>
        /// <param name="iCal">The calendar to generate an ics from.</param>
        /// <returns>THe <see cref="CalendarIcalViewModel"/> holding the ics data.</returns>
        private CalendarIcalViewModel CreateCalendarIcalViewModelFromIcal(Ical.Net.Calendar iCal)
        {
            var fileData = new CalendarIcalViewModel();

            Ical.Net.Serialization.SerializationContext ctx = new Ical.Net.Serialization.SerializationContext();
            var serialiser = new Ical.Net.Serialization.CalendarSerializer(ctx);

            string output = serialiser.SerializeToString(iCal);

            fileData.ContentType = "text/calendar";
            fileData.Data        = System.Text.Encoding.UTF8.GetBytes(output);

            // Create a name.
            Guid fileId = Guid.NewGuid();

            fileData.FileName = fileId.ToString() + ".ics";
            return(fileData);
        }
Example #9
0
        private string generateIcal(List <AppointmentInfo> appointments)
        {
            var calendar = new Ical.Net.Calendar();

            calendar.AddTimeZone(new Ical.Net.CalendarComponents.VTimeZone("Europe/Berlin")); //bringt das was? Muss fast das ical checken...
            foreach (var appointment in appointments)
            {
                var newEvent = new Ical.Net.CalendarComponents.CalendarEvent();
                newEvent.Summary     = appointment.Name;
                newEvent.Location    = appointment.Location;
                newEvent.Start       = new Ical.Net.DataTypes.CalDateTime(appointment.DatStart);
                newEvent.End         = new Ical.Net.DataTypes.CalDateTime(appointment.DatEnd);
                newEvent.Description = appointment.Description;

                calendar.Events.Add(newEvent);
            }

            var calendarSerializer = new Ical.Net.Serialization.CalendarSerializer();
            var icalString         = calendarSerializer.SerializeToString(calendar);

            return(icalString);
        }