Adnan’s Blog

vCalendar

Posted on: November 18, 2008

I needed to integrate support for vCalendar in my current project. So i went through the vCalendar spec 1.0 at http://www.imc.org/pdi/vcal-10.txt and wrote this class. Although I have not implemented all the specification features, but this should prove sufficient for the usual requirements. If you have any comments / suggestions , let me know.

using System;

using System.Text;

using System.IO;

using System.Collections;

/// <summary>

/// Summary description for VCalendar

/// </summary>

public class VCalendar

{

private string _productID;

/// <summary>

/// A unique identifier identifying the originating program

/// </summary>

public string ProductIdentifier

{

get { return _productID; }

set { _productID = value; }

}

private ArrayList _categories;

/// <summary>

/// ArrayList of categories

/// </summary>

public ArrayList Categories

{

get { return _categories; }

set { _categories = value; }

}

private DateTime _createdOn = DateTime.MinValue;

/// <summary>

/// DateTime that the vEvent was created.

/// </summary>

public DateTime CreatedOn

{

get { return _createdOn; }

set { _createdOn = value; }

}

private DateTime _completedOn = DateTime.MinValue;

/// <summary>

/// DateTime the vEvent completed on

/// </summary>

public DateTime CompletedOn

{

get { return _completedOn; }

set { _completedOn = value; }

}

private DateTime _startOn = DateTime.MinValue;

/// <summary>

/// DateTime that the vEvent will start

/// </summary>

public DateTime StartOn

{

get { return _startOn; }

set { _startOn = value; }

}

private DateTime _endOn = DateTime.MinValue;

/// <summary>

/// DateTime that the vEvent will end

/// </summary>

public DateTime EndOn

{

get { return _endOn; }

set { _endOn = value; }

}

private ClassificationType _classification = ClassificationType.PUBLIC;

/// <summary>

/// Defines the access classification for the vEvent entity

/// </summary>

public ClassificationType Classification

{

get { return _classification; }

set { _classification = value; }

}

private int _priority = 0;

/// <summary>

/// 0 = undefined, 1 = High, -1 = Low. Defaults to 0

/// </summary>

public int Priority

{

get { return _priority; }

set { _priority = value; }

}

private string _subject;

/// <summary>

/// Subject of the vEvent

/// </summary>

public string Subject

{

get { return _subject; }

set { _subject = value; }

}

private string _description;

/// <summary>

/// Description of the vEvent

/// </summary>

public string Description

{

get { return _description; }

set { _description = value; }

}

private bool _enableAlarm;

/// <summary>

/// Flag to enable the reminder alarm

/// </summary>

public bool EnableAlarm

{

get { return _enableAlarm; }

set { _enableAlarm = value; }

}

public override string ToString()

{

StringBuilder sb = new StringBuilder();

sb.Append(“BEGIN:VCALENDAR\n”);

sb.Append(“VERSION:1.0\n”);

sb.Append(“BEGIN:VEVENT\n”);

// Product Identifier

if (!string.IsNullOrEmpty(_productID))

sb.AppendFormat(“PRODID:{0}\n”, _productID);

// Categories

if (_categories != null && _categories.Count != 0)

{

sb.Append(“CATEGORIES:”);

for (int i = 0; i < _categories.Count; i++)

{

if (i == 0)

sb.Append(_categories[0].ToString());

else

sb.AppendFormat(“;{0}”, _categories[i].ToString());

}

sb.Append(“\n”);

}

// Created On

if (_createdOn != DateTime.MinValue)

sb.AppendFormat(“DCREATED:{0}\n”, _createdOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

// Start Date Time

if (_startOn != DateTime.MinValue)

{

sb.AppendFormat(“DTSTART:{0}\n”, _startOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

// Display Reminder

if (_enableAlarm)

sb.AppendFormat(“DALARM:{0};PT5M;2;{1}\n”, _startOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”), _subject);

}

// End Date Time

if (_endOn != DateTime.MinValue)

sb.AppendFormat(“DTEND:{0}\n”, _endOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

// Completed On

if (_completedOn != DateTime.MinValue)

sb.AppendFormat(“COMPLETED:{0}\n”, _completedOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

// Priority

if (_priority != 0)

sb.AppendFormat(“PRIORITY:{0}\n”, _priority);

// Description

if (!string.IsNullOrEmpty(_description))

sb.AppendFormat(“DESCRIPTION:{0}\n”, _description);

// Subject

if (!string.IsNullOrEmpty(_subject))

sb.AppendFormat(“SUMMARY:{0}\n”, _subject);

if (_classification != ClassificationType.PUBLIC)

sb.AppendFormat(“CLASS:{0}\n”, Enum.GetName(typeof(ClassificationType), Classification));

sb.Append(“END:VEVENT\n”);

sb.Append(“END:vCalendar”);

return sb.ToString();

}

public void Generate(string filePath, FileMode mode)

{

FileStream fs = new FileStream(filePath, mode);

StreamWriter sw = new StreamWriter(fs);

using (sw)

{

sw.Write(this.ToString());

}

}

}

public class Attendee

{

private StatusType _status;

public StatusType Status

{

get { return _status; }

set { _status = value; }

}

private AttendeeType _role;

public AttendeeType Role

{

get { return _role; }

set { _role = value; }

}

public string _attendeeName;

public string AttendeeName

{

get { return _attendeeName; }

set { _attendeeName = value; }

}

public override string ToString()

{

StringBuilder sb = new StringBuilder();

if (!string.IsNullOrEmpty(_attendeeName))

{

sb.Append(“ATTENDEE”);

if (_role != AttendeeType.ATTENDEE)

sb.AppendFormat(“;ROLE={0}”, Enum.GetName(typeof(AttendeeType), Role));

if (_status != StatusType.NEEDS_ACTION)

sb.AppendFormat(“;STATUS={0}”, Enum.GetName(typeof(StatusType), Status));

sb.AppendFormat(“:{0}\n”, _attendeeName);

return sb.ToString();

}

else

return string.Empty;

}

}

public enum AttendeeType

{

ATTENDEE,

ORGANIZER,

OWNER,

DELEGATE,

}

public enum StatusType

{

ACCEPTED,

NEEDS_ACTION,

SENT,

TENTATIVE,

CONFIRMED,

DECLINED,

COMPLETED,

DELEGATED

}

public enum RSVPType

{

YES,

NO

}

public enum ExpectType

{

FYI,

REQUIRE,

REQUEST,

IMMEDIATE

}

public enum CategoryType

{

APPOINTMENT,

BUSINESS,

EDUCATION,

HOLIDAY,

MEETING,

MISCELLANEOUS,

PERSONAL,

TRAVEL,

VACATION

}

public enum ClassificationType

{

PUBLIC,

PRIVATE,

CONFIDENTIAL

}

Advertisements
Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: