82 lines
3.3 KiB
C#
82 lines
3.3 KiB
C#
|
|
#nullable enable
|
||
|
|
using System;
|
||
|
|
using System.Globalization;
|
||
|
|
using System.Text;
|
||
|
|
using ReminderOrganizer.Models;
|
||
|
|
|
||
|
|
namespace ReminderOrganizer.Services
|
||
|
|
{
|
||
|
|
/// <summary>iCalendar (.ics) export for Outlook, Google Calendar, Apple Calendar.</summary>
|
||
|
|
public static class IcsExport
|
||
|
|
{
|
||
|
|
public static string Build(AppData data, DateTime utcNow)
|
||
|
|
{
|
||
|
|
var sb = new StringBuilder();
|
||
|
|
void Line(string s) => sb.Append(Fold(s)).Append("\r\n");
|
||
|
|
|
||
|
|
Line("BEGIN:VCALENDAR");
|
||
|
|
Line("VERSION:2.0");
|
||
|
|
Line("PRODID:-//Michael Media//Reminder Organizer//EN");
|
||
|
|
Line("CALSCALE:GREGORIAN");
|
||
|
|
var stamp = utcNow.ToUniversalTime().ToString("yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
|
||
|
|
|
||
|
|
foreach (var folder in data.Folders)
|
||
|
|
{
|
||
|
|
foreach (var t in folder.Tasks)
|
||
|
|
{
|
||
|
|
if (t.IsNotified) continue;
|
||
|
|
Line("BEGIN:VEVENT");
|
||
|
|
Line($"UID:{t.Id}@reminder-organizer");
|
||
|
|
Line($"DTSTAMP:{stamp}");
|
||
|
|
// floating local time: shows at the same clock time in every calendar
|
||
|
|
Line($"DTSTART:{t.ReminderDate.ToString("yyyyMMdd'T'HHmmss", CultureInfo.InvariantCulture)}");
|
||
|
|
Line("DURATION:PT30M");
|
||
|
|
var summary = t.UpcomingAge is int age ? $"{t.Name} ({age})" : t.Name;
|
||
|
|
Line($"SUMMARY:{Escape(summary)}");
|
||
|
|
if (!string.IsNullOrWhiteSpace(t.Description)) Line($"DESCRIPTION:{Escape(t.Description)}");
|
||
|
|
Line($"CATEGORIES:{Escape(folder.Name)}");
|
||
|
|
if (t.IsRecurring)
|
||
|
|
{
|
||
|
|
var freq = t.RecurrenceType switch
|
||
|
|
{
|
||
|
|
RecurrenceType.Daily => "DAILY",
|
||
|
|
RecurrenceType.Weekly => "WEEKLY",
|
||
|
|
RecurrenceType.Monthly => "MONTHLY",
|
||
|
|
_ => "YEARLY"
|
||
|
|
};
|
||
|
|
Line($"RRULE:FREQ={freq}");
|
||
|
|
}
|
||
|
|
Line("BEGIN:VALARM");
|
||
|
|
Line("ACTION:DISPLAY");
|
||
|
|
Line($"DESCRIPTION:{Escape(t.Name)}");
|
||
|
|
Line(t.LeadDays > 0 ? $"TRIGGER:-P{t.LeadDays}D" : "TRIGGER:PT0M");
|
||
|
|
Line("END:VALARM");
|
||
|
|
Line("END:VEVENT");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Line("END:VCALENDAR");
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string Escape(string s) =>
|
||
|
|
s.Replace("\\", "\\\\").Replace(";", "\\;").Replace(",", "\\,").Replace("\r\n", "\\n").Replace("\n", "\\n").Replace("\r", "");
|
||
|
|
|
||
|
|
/// <summary>RFC 5545: lines longer than 75 octets continue with CRLF + space.</summary>
|
||
|
|
public static string Fold(string line)
|
||
|
|
{
|
||
|
|
var bytes = Encoding.UTF8.GetByteCount(line);
|
||
|
|
if (bytes <= 75) return line;
|
||
|
|
var sb = new StringBuilder();
|
||
|
|
int count = 0;
|
||
|
|
foreach (var ch in line)
|
||
|
|
{
|
||
|
|
int n = Encoding.UTF8.GetByteCount(ch.ToString());
|
||
|
|
if (count + n > 75) { sb.Append("\r\n "); count = 1; }
|
||
|
|
sb.Append(ch);
|
||
|
|
count += n;
|
||
|
|
}
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|