protected void Page_Load(object sender, EventArgs e) { this.Page.Title = "vCalendar/iCalendar Browser"; lblMsg.Text = String.Empty; VCalendar vc = (VCalendar)Session["VCalendar"]; // Load a default calendar on first use and store it in the session if not already there and bind it // to the data grids. if (!Page.IsPostBack || vc == null) { if (vc == null) { if (Page.IsPostBack) { lblMsg.Text = "Session appears to have timed out. Default calendar loaded."; } vc = VCalendarParser.ParseFromFile(Server.MapPath("RFC2445.ics")); vc.Events.Sort(CalendarSorter); vc.ToDos.Sort(CalendarSorter); vc.Journals.Sort(CalendarSorter); vc.FreeBusys.Sort(CalendarSorter); Session["VCalendar"] = vc; } dgEvents.DataSource = vc.Events; dgToDos.DataSource = vc.ToDos; dgJournals.DataSource = vc.Journals; dgFreeBusys.DataSource = vc.FreeBusys; dgEvents.DataBind(); dgToDos.DataBind(); dgJournals.DataBind(); dgFreeBusys.DataBind(); if (vc.Version == SpecificationVersions.vCalendar10) { lblVersion.Text = "vCalendar 1.0"; dgJournals.Visible = dgFreeBusys.Visible = false; } else { lblVersion.Text = "iCalendar 2.0"; dgJournals.Visible = dgFreeBusys.Visible = true; } } }
/// <summary> /// Initialize the time zone and recurrence information at start up /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> protected void Application_Start(object sender, EventArgs e) { // Load the time zones if not already done. The collection is static so it only needs to be loaded // once. if (VCalendar.TimeZones.Count == 0) { // Since it is static, we will use the Lock property to synchronize access to it in the web app // as multiple sessions may try to access it at the same time. The parser will acquire a write // lock if it needs to merge a time zone component but since we are loading many time zones at // once, we'll lock it now. VCalendar.TimeZones.AcquireWriterLock(250); try { // If still zero, load the file if (VCalendar.TimeZones.Count == 0) { VCalendarParser.ParseFromFile(Server.MapPath("TimeZoneDB.ics")); VCalendar.TimeZones.Sort(true); } } finally { VCalendar.TimeZones.ReleaseWriterLock(); } } // Load a default set of holidays into the recurrence holiday collection. It too is static, but // since it probably won't change after being set, it uses a simple SyncRoot property to lock the // collection. if (Recurrence.Holidays.Count == 0) { lock (((ICollection)Recurrence.Holidays).SyncRoot) { if (Recurrence.Holidays.Count == 0) { Recurrence.Holidays.AddStandardHolidays(); } } } }
/// <summary> /// Open a vCalendar or iCalendar file /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void miOpen_Click(object sender, EventArgs e) { if (wasModified && MessageBox.Show("Do you want to discard your changes to the current calendar?", "Discard Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Load Calendar File"; dlg.Filter = "ICS files (*.ics)|*.ics|VCS files (*.vcs)|*.vcs|All files (*.*)|*.*"; if (vCal.Version == SpecificationVersions.vCalendar10) { dlg.DefaultExt = "vcs"; dlg.FilterIndex = 2; } else { dlg.DefaultExt = "ics"; dlg.FilterIndex = 1; } dlg.InitialDirectory = Path.GetFullPath(Path.Combine( Environment.CurrentDirectory, @"..\..\..\..\..\PDIFiles")); if (dlg.ShowDialog() == DialogResult.OK) { try { this.Cursor = Cursors.WaitCursor; // Parse the calendar information from the file and load the data grid with some basic // information about the items in it. vCal.Dispose(); vCal = VCalendarParser.ParseFromFile(dlg.FileName); LoadComponentList(); // Find the first collection with items if (vCal.Events.Count != 0) { cboComponents.SelectedIndex = 0; } else if (vCal.ToDos.Count != 0) { cboComponents.SelectedIndex = 1; } else if (vCal.Journals.Count != 0) { cboComponents.SelectedIndex = 2; } else if (vCal.FreeBusys.Count != 0) { cboComponents.SelectedIndex = 3; } else { cboComponents.SelectedIndex = 0; } LoadGridWithItems(true); lblFilename.Text = dlg.FileName; } catch (Exception ex) { string error = String.Format("Unable to load calendar:\n{0}", ex.Message); if (ex.InnerException != null) { error += ex.InnerException.Message + "\n"; if (ex.InnerException.InnerException != null) { error += ex.InnerException.InnerException.Message; } } System.Diagnostics.Debug.Write(ex); MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Cursor = Cursors.Default; } } } }