/// <summary> /// Load a calendar file uploaded by the user. It can be a vCalendar or an iCalendar file /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> protected void btnUpload_Click(object sender, EventArgs e) { VCalendar vc = (VCalendar)Session["VCalendar"]; if (hifUpload.Value == null || hifUpload.Value.Length == 0) { lblMsg.Text = "Specify a filename to upload"; return; } // Get the file data from the uploaded stream try { // Dispose of the old calendar vc.Dispose(); // Set the file and property encodings to use. Since we are opening the stream, we have to pass // the encoding to the StreamReader rather than using PDIParser.DefaultEncoding. Encoding fileEnc; switch (cboFileEncoding.SelectedIndex) { case 0: fileEnc = new UTF8Encoding(false, false); break; case 1: fileEnc = Encoding.GetEncoding("iso-8859-1"); break; default: fileEnc = new ASCIIEncoding(); break; } // This is only applicable for vCalendar 1.0 switch (cboPropEncoding.SelectedIndex) { case 0: BaseProperty.DefaultEncoding = new UTF8Encoding(false, false); break; case 1: BaseProperty.DefaultEncoding = Encoding.GetEncoding("iso-8859-1"); break; default: BaseProperty.DefaultEncoding = new ASCIIEncoding(); break; } using (var sr = new StreamReader(hifUpload.PostedFile.InputStream, fileEnc)) { vc = VCalendarParser.ParseFromStream(sr); 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; } } lblMsg.Text = "The file was loaded successfully"; } catch (PDIParserException pex) { System.Diagnostics.Debug.WriteLine(pex.ToString()); lblMsg.Text = "Unable to parse file: " + pex.Message; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); lblMsg.Text = "Unable to load file: " + ex.Message; } }
/// <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; } } } }
/// <summary> /// Generate instances for the specified component /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event parameters</param> protected void btnTest_Click(object sender, EventArgs e) { RecurringObject ro = null; DateTimeInstanceCollection instances; string calendar; int start; double elapsed; DateTime startDate, endDate; try { lblCount.Text = String.Empty; if (!DateTime.TryParse(txtStartDate.Text, CultureInfo.CurrentCulture, DateTimeStyles.None, out startDate) || !DateTime.TryParse(txtEndDate.Text, CultureInfo.CurrentCulture, DateTimeStyles.None, out endDate)) { lblCount.Text = "Invalid start or end date/time format"; return; } // Wrap it in VCALENDAR tags and parse it calendar = String.Format("BEGIN:VCALENDAR\nVERSION:2.0\n{0}\nEND:VCALENDAR", txtCalendar.Text); VCalendar cal = VCalendarParser.ParseFromString(calendar); // Get the first event, to-do, or journal item if (cal.Events.Count > 0) { ro = cal.Events[0]; } else if (cal.ToDos.Count > 0) { ro = cal.ToDos[0]; } else if (cal.Journals.Count > 0) { ro = cal.Journals[0]; } if (ro == null) { lblCount.Text = "No event, to-do, or journal item found"; return; } // Apply the time zone to the calendar. If "None" is selected, the time zone will be cleared on // all items. if (cboTimeZone.SelectedIndex < 1) { cal.ApplyTimeZone(null); } else { cal.ApplyTimeZone(VCalendar.TimeZones[cboTimeZone.SelectedIndex - 1]); } foreach (RRuleProperty rrule in ro.RecurrenceRules) { ApplyLimits(ro, rrule.Recurrence); } foreach (ExRuleProperty exrule in ro.ExceptionRules) { ApplyLimits(ro, exrule.Recurrence); } txtCalendar.Text = ro.ToString(); start = System.Environment.TickCount; instances = ro.InstancesBetween(startDate, endDate, chkInLocalTime.Checked); elapsed = (System.Environment.TickCount - start) / 1000.0; cal.Dispose(); // The date instance contains the start and end date/times, the duration, and time zone // information. The duration is based on the duration of the calendar component. The time zone // information is based on the "In Local Time" parameter of the InstancesBetween() method and // whether or not the component has a Time Zone ID specified. dlDates.DataSource = instances; dlDates.DataBind(); // If nothing was found remind the user that they may need to adjust the start and end date range // to find stuff within the item. if (instances.Count == 0) { lblCount.Text = "Nothing found. If this was unexpected, check the limiting date range " + "in the two date/time text boxes at the top of the form and the calendar item date/time " + "properties to make sure that they do overlap<br/><br/>"; } lblCount.Text += String.Format("Found {0:N0} instances in {1:N2} seconds ({2:N2} instances/second)", instances.Count, elapsed, instances.Count / elapsed); } catch (Exception ex) { lblCount.Text = ex.Message; } }
/// <summary> /// Test the recurrence within the specified iCalendar component /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void btnTest_Click(object sender, EventArgs e) { RecurringObject ro = null; DateTimeInstanceCollection instances; string calendar; int start; double elapsed; try { lblCount.Text = null; // Wrap it in VCALENDAR tags and parse it calendar = String.Format("BEGIN:VCALENDAR\nVERSION:2.0\n{0}\nEND:VCALENDAR", txtCalendar.Text); VCalendar cal = VCalendarParser.ParseFromString(calendar); // Get the first event, to-do, or journal item if (cal.Events.Count > 0) { ro = cal.Events[0]; } else if (cal.ToDos.Count > 0) { ro = cal.ToDos[0]; } else if (cal.Journals.Count > 0) { ro = cal.Journals[0]; } if (ro == null) { lblCount.Text = "No event, to-do, or journal item found"; return; } // Apply the time zone to the calendar. If "None" is selected, the time zone will be cleared on // all items. if (cboTimeZone.SelectedIndex < 1) { cal.ApplyTimeZone(null); } else { cal.ApplyTimeZone(VCalendar.TimeZones[cboTimeZone.SelectedIndex - 1]); } txtCalendar.Text = ro.ToString(); lbDates.Items.Clear(); this.Cursor = Cursors.WaitCursor; start = System.Environment.TickCount; instances = ro.InstancesBetween(dtpStartDate.Value, dtpEndDate.Value, chkInLocalTime.Checked); elapsed = (System.Environment.TickCount - start) / 1000.0; cal.Dispose(); // The date instance contains the start and end date/times, the duration, and time zone // information. The duration is based on the duration of the calendar component. The time // zone information is based on the "In Local Time" parameter of the InstancesBetween() method // and whether or not the component has a Time Zone ID specified. foreach (DateTimeInstance dti in instances) { lbDates.Items.Add(String.Format("{0:G} {1} to {2:G} {3} ({4})", dti.StartDateTime, dti.AbbreviatedStartTimeZoneName, dti.EndDateTime, dti.AbbreviatedEndTimeZoneName, dti.Duration.ToDescription())); if (lbDates.Items.Count > 5000) { lblCount.Text += "A large number of instances were returned. Only the first 5000 " + "have been loaded into the list box.\r\n"; break; } } // If nothing was found remind the user that they may need to adjust the start and end date range // to find stuff within the item. if (instances.Count == 0) { MessageBox.Show("Nothing found. If this was unexpected, check the limiting date range in " + "the two date/time text boxes at the top of the form and the calendar item date/time " + "properties to make sure that they do overlap"); } lblCount.Text += String.Format("Found {0:N0} instances in {1:N2} seconds ({2:N2} instances/second)", instances.Count, elapsed, instances.Count / elapsed); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { this.Cursor = Cursors.Default; } }