手头上正在进行一个关于iCalendar的任务,从数据库中获取事件,创建ics文件并提供给用户订阅(Subscribe)。订阅后的iCalendar具有提醒功能。可订阅的客户端包括: MS Outlook、Google Calendar、iPhone (iPad/iTouch/Mac)。
大致的工作包括:
- 了解iCalendar、协议和相关内容
- 创建iCalendar类,用于设置和创建日历对象(以及事件(Event)和事件的提醒(Alarm)等
- 提供订约(Subscribe)服务
准备工作
首先了解iCalendar,可以查阅的资料包括
- Wikipedia http://en.wikipedia.org/wiki/ICalendar
- RFC 5545 Internet Calendaring and Scheduling Core Object Specification (iCalendar)
- RFC 5546 iCalendar Transport-Independent Interoperability Protocol (iTIP)
一个ics文件定义了一个Calendar所包含的信息,对象使用BEGIN:<对象类型> .... END:<对象类型>来定义,属性使用<属性名>:<属性值>定义,属性名可包括额外信息,使用分号(;)与属性名隔开,用等号(=)赋额外信息值。
- 1个VCALENDAR,内含
- 1个VTIMEZONE 定义日历的时区
- 1个或多个VEVENT 定义事件
- 1个VALARM 定义提醒
-
1个或多个VTODO 定义代办
准备工作
- 在VS2010/2008创建新项目->选择ASP.NET Web Application模板
- 命名为MyCalendar
- 创建新类iCalendar,复制以下代码
iCalendar.cs
- iCalendar类
- iCalEvent类
- iCalAlarm类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyApp.iCal
{
public enum CalendarMethod
{
Publish,
Request,
Reply,
Add,
Cancel,
Refresh,
Counter,
DeclineCounter
}
public enum AlarmAction
{
Display,
Email,
Audio,
Procedure
}
public enum EventStatus
{
Tentative,
Confirmed,
Cancelled
}
public class iCalendar
{
private string uuID = "7C9280DD-9EB2-41e9-8A29-A53A1BFB8DB2";
public iCalendar()
{
Events = new List<iCalEvent>();
ProductID = "-//sample.com.au//MyApp 1.0//EN";
DisplayName = "iCalendar Sample";
Description = "This is description.";
TimeZone = "Australia/Sydney";
UpdateInterval = 10;
}
public List<iCalEvent> Events;
public CalendarMethod Method;
public string ProductID;
public string DisplayName;
public string Description;
public string UUID;
public string TimeZone;
public int UpdateInterval;
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:2.0");
sb.AppendLine(string.Format("METHOD:{0}", Method.ToString().ToUpper()));
sb.AppendLine(string.Format("PRODID:{0}", ProductID));
sb.AppendLine(string.Format("X-WR-CALNAME:{0}", DisplayName));
sb.AppendLine(string.Format("X-WR-CALDESC:{0}", Description));
sb.AppendLine(string.Format("X-WR-RELCALID:{0}-{1}", uuID, UUID));