|
3 | 3 | using System.Collections.Specialized; |
4 | 4 | using System.Drawing; |
5 | 5 | using System.Drawing.Imaging; |
| 6 | +using System.Globalization; |
6 | 7 | using System.IO; |
7 | 8 | using System.IO.Compression; |
8 | 9 | using System.Linq; |
9 | 10 | using System.Net; |
10 | 11 | using System.Runtime.InteropServices; |
| 12 | +using System.Runtime.Serialization; |
11 | 13 | using System.Text; |
12 | 14 | using System.Text.RegularExpressions; |
13 | 15 | using System.Windows.Forms; |
| 16 | +using Ical.Net.DataTypes; |
14 | 17 | using LINQtoCSV; |
15 | 18 | using PasteIntoFile.Properties; |
16 | 19 | using PdfSharp.Drawing; |
@@ -603,6 +606,39 @@ public class CalendarContent : TextLikeContent { |
603 | 606 |
|
604 | 607 | public CalendarContent(string text) : base(FORMATS, EXTENSIONS, text) { } |
605 | 608 |
|
| 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 | + |
606 | 642 | public override string Description => string.Format(Resources.str_preview_calendar, |
607 | 643 | Text.ToUpperInvariant().Split('\n').Count(l => l.Trim().StartsWith("BEGIN:VEVENT"))); |
608 | 644 |
|
|
0 commit comments