Skip to content

Commit 5221f1e

Browse files
committed
Nice calendar preview
1 parent 7e9ffbe commit 5221f1e

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

PasteIntoFile/ClipboardContents.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
using System.Collections.Specialized;
44
using System.Drawing;
55
using System.Drawing.Imaging;
6+
using System.Globalization;
67
using System.IO;
78
using System.IO.Compression;
89
using System.Linq;
910
using System.Net;
1011
using System.Runtime.InteropServices;
12+
using System.Runtime.Serialization;
1113
using System.Text;
1214
using System.Text.RegularExpressions;
1315
using System.Windows.Forms;
16+
using Ical.Net.DataTypes;
1417
using LINQtoCSV;
1518
using PasteIntoFile.Properties;
1619
using PdfSharp.Drawing;
@@ -603,6 +606,39 @@ public class CalendarContent : TextLikeContent {
603606

604607
public CalendarContent(string text) : base(FORMATS, EXTENSIONS, text) { }
605608

609+
public Ical.Net.Calendar Calendar => Ical.Net.Calendar.Load(Text);
610+
611+
public override PreviewHolder Preview(string extension) {
612+
var description = string.Format(Resources.str_preview_calendar,
613+
Text.ToUpperInvariant().Split('\n').Count(l => l.Trim().StartsWith("BEGIN:VEVENT")));
614+
if (EXTENSIONS.Contains(extension)) {
615+
try {
616+
return PreviewHolder.ForHtml(
617+
"<!DOCTYPE html>\n<html>\n<head>\n<style>\n"
618+
+ "* { font-family: Sans-serif; font-size: small; }\n"
619+
+ "strong { font-size: medium; }\nbody { margin: 0; }\n"
620+
+ "p { background: aliceblue; border: solid silver 1pt; padding: 0.5em; margin: 0.5em; }\n"
621+
+ "</style>\n</head>\n<body>\n"
622+
+ string.Join("\n", Calendar.Events.Select(e
623+
=> {
624+
var occurences = e.Evaluator.Evaluate(e.Start != null ? e.Start : new CalDateTime(DateTime.UtcNow), null, null).Take(6).ToArray();
625+
return "<p>"
626+
+ string.Join("<br/>", occurences.Take(occurences.Length > 5 ? 4 : 5).Select(p
627+
=> p.StartTime.ToString(p.StartTime.HasTime ? "g" : "d", CultureInfo.CurrentCulture)
628+
+ $" ({p.EffectiveDuration?.ToString()?.TrimStart('P')?.TrimStart('T')})"))
629+
+ (occurences.Length > 5 ? "<br/>..." : "")
630+
+ $"<br/><strong>{e.Summary}</strong><br/>{e.Location}</p>";
631+
}))
632+
+ "\n</body>\n</html>\n",
633+
description
634+
);
635+
} catch (SerializationException e) {
636+
// ignored (default to text preview below)
637+
}
638+
}
639+
return PreviewHolder.ForText(Text, description);
640+
}
641+
606642
public override string Description => string.Format(Resources.str_preview_calendar,
607643
Text.ToUpperInvariant().Split('\n').Count(l => l.Trim().StartsWith("BEGIN:VEVENT")));
608644

PasteIntoFile/PasteIntoFile.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<ItemGroup>
4949
<PackageReference Include="BetterFolderBrowser" Version="1.2.0" />
5050
<PackageReference Include="CommandLineParser" Version="2.9.1" />
51+
<PackageReference Include="Ical.Net" Version="5.1.0" />
5152
<PackageReference Include="LINQtoCSV" Version="1.5.0" />
5253
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
5354
<PackageReference Include="PDFsharp" Version="1.50.5147" />

0 commit comments

Comments
 (0)