2013-03-29 17:00:07 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Outlook = Microsoft.Office.Interop.Outlook;
|
|
|
|
|
|
|
|
|
|
namespace Outlook2013TodoAddIn
|
|
|
|
|
{
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// User control to hold the calendar, etc...
|
|
|
|
|
/// </summary>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
public partial class AppointmentsControl : UserControl
|
|
|
|
|
{
|
2013-03-29 19:03:00 +01:00
|
|
|
|
#region "Variables"
|
2013-04-01 18:03:41 +02:00
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used to retrieve the email address of a contact
|
|
|
|
|
/// </summary>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
private const string PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
#endregion "Variables"
|
|
|
|
|
|
|
|
|
|
#region "Properties"
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Number of days (including today) to retrieve appointments from in the future
|
|
|
|
|
/// </summary>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
public decimal NumDays
|
|
|
|
|
{
|
2013-03-29 19:03:00 +01:00
|
|
|
|
get { return this.numRangeDays.Value; }
|
|
|
|
|
set { this.numRangeDays.Value = value; }
|
2013-03-29 17:00:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-01 22:22:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets/sets whether mail notifications are enabled or not
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool MailAlertsEnabled
|
|
|
|
|
{
|
|
|
|
|
get { return this.chkMailAlerts.Checked; }
|
|
|
|
|
set { this.chkMailAlerts.Checked = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 16:27:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets/sets the selected calendar date
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTime SelectedDate
|
|
|
|
|
{
|
|
|
|
|
get { return this.apptCalendar.SelectedDate; }
|
|
|
|
|
set { this.apptCalendar.SelectedDate = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
#endregion "Properties"
|
|
|
|
|
|
|
|
|
|
#region "Methods"
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Default constructor
|
|
|
|
|
/// </summary>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
public AppointmentsControl()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2013-05-02 23:04:25 +02:00
|
|
|
|
this.btnRefresh.FlatAppearance.MouseOverBackColor = this.apptCalendar.HoverBackColor;
|
2013-03-29 17:00:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Respond to calendar changes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">DateRangeEventArgs</param>
|
2013-04-26 17:32:14 +02:00
|
|
|
|
private void apptCalendar_SelectedDateChanged(object sender, EventArgs e)
|
2013-03-29 17:00:07 +01:00
|
|
|
|
{
|
|
|
|
|
this.RetrieveAppointments();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Change days to retrieve appointments in the future
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">EventArgs</param>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
private void numRangeDays_ValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.NumDays = this.numRangeDays.Value;
|
|
|
|
|
this.RetrieveAppointments();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manual refresh
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">EventArgs</param>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
private void btnRefresh_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.RetrieveAppointments();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-01 22:22:27 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enables/disables email notifications
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">EventArgs</param>
|
|
|
|
|
private void chkMailAlerts_CheckedChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.MailAlertsEnabled = this.chkMailAlerts.Checked;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieve all appointments for the current configurations
|
|
|
|
|
/// </summary>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
public void RetrieveAppointments()
|
|
|
|
|
{
|
|
|
|
|
// Get the Outlook folder for the calendar to retrieve the appointments
|
|
|
|
|
Outlook.Folder calFolder =
|
|
|
|
|
Globals.ThisAddIn.Application.Session.GetDefaultFolder(
|
|
|
|
|
Outlook.OlDefaultFolders.olFolderCalendar)
|
|
|
|
|
as Outlook.Folder;
|
|
|
|
|
|
2013-04-26 17:32:14 +02:00
|
|
|
|
int selectedMonth = this.apptCalendar.SelectedDate.Month;
|
|
|
|
|
int selectedYear = this.apptCalendar.SelectedDate.Year;
|
2013-03-29 17:00:07 +01:00
|
|
|
|
|
|
|
|
|
// To get all the appointments for the current month (so it displays nicely bolded even for past events)
|
|
|
|
|
DateTime start = new DateTime(selectedYear, selectedMonth, 1); // MM-01-YYYY
|
|
|
|
|
DateTime end = start.AddMonths(1).AddDays(-1); // Last day of the month
|
|
|
|
|
end = end.AddDays((int)this.numRangeDays.Value); // So we get appointments for the "possible" first days of the next month
|
|
|
|
|
|
|
|
|
|
// Get all the appointments
|
|
|
|
|
Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
|
|
|
|
|
|
|
|
|
|
// Get a more manageable list
|
|
|
|
|
List<Outlook.AppointmentItem> appts = new List<Outlook.AppointmentItem>();
|
|
|
|
|
if (rangeAppts != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (Outlook.AppointmentItem appt in rangeAppts)
|
|
|
|
|
{
|
|
|
|
|
appts.Add(appt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Highlight dates with appointments in the current calendar
|
2013-04-26 17:32:14 +02:00
|
|
|
|
this.apptCalendar.BoldedDates = appts.Select<Outlook.AppointmentItem, DateTime>(a => a.Start.Date).Distinct().ToArray();
|
2013-03-29 17:00:07 +01:00
|
|
|
|
|
|
|
|
|
// Now display the actual appointments below the calendar
|
2013-04-26 17:32:14 +02:00
|
|
|
|
DateTime startRange = this.apptCalendar.SelectedDate;
|
2013-03-29 17:00:07 +01:00
|
|
|
|
DateTime endRange = startRange.AddDays((int)this.numRangeDays.Value);
|
|
|
|
|
|
|
|
|
|
// Get items in range
|
|
|
|
|
var lstItems = appts.Where(a => a.Start >= startRange && a.Start <= endRange);
|
|
|
|
|
|
|
|
|
|
int sameDay = -1; // startRange.Day;
|
|
|
|
|
|
|
|
|
|
List<ListViewItem> lstCol = new List<ListViewItem>();
|
2013-04-30 23:00:02 +02:00
|
|
|
|
ListViewGroup grp = null;
|
2013-03-29 17:00:07 +01:00
|
|
|
|
lstItems.ToList().ForEach(i =>
|
|
|
|
|
{
|
|
|
|
|
if (i.Start.Day != sameDay)
|
|
|
|
|
{
|
2013-04-30 23:00:02 +02:00
|
|
|
|
grp = new ListViewGroup(i.Start.ToShortDateString(), HorizontalAlignment.Left);
|
|
|
|
|
this.listView1.Groups.Add(grp); // TODO: Style it?
|
2013-03-29 17:00:07 +01:00
|
|
|
|
sameDay = i.Start.Day;
|
|
|
|
|
};
|
2013-04-30 23:00:02 +02:00
|
|
|
|
string loc = "-"; // TODO: If no second line is specified, the tile is stretched to only one line
|
|
|
|
|
if (!String.IsNullOrEmpty(i.Location)) loc = i.Location;
|
|
|
|
|
ListViewItem current = new ListViewItem(new string[] { String.Format("{0} {1}", i.Start.ToShortTimeString(), i.Subject), loc });
|
2013-03-29 17:00:07 +01:00
|
|
|
|
current.SubItems.Add(i.Subject);
|
|
|
|
|
|
2013-04-30 23:00:02 +02:00
|
|
|
|
// current.Font = new Font(this.Font, FontStyle.Bold);
|
|
|
|
|
// current.UseItemStyleForSubItems = false;
|
|
|
|
|
|
2013-03-29 17:00:07 +01:00
|
|
|
|
current.ToolTipText = String.Format("{0} - {1} {2}", i.Start.ToShortTimeString(), i.End.ToShortTimeString(), i.Subject);
|
|
|
|
|
current.Tag = i;
|
2013-04-30 23:00:02 +02:00
|
|
|
|
current.Group = grp;
|
2013-03-29 17:00:07 +01:00
|
|
|
|
switch (i.BusyStatus)
|
|
|
|
|
{
|
|
|
|
|
case Outlook.OlBusyStatus.olBusy:
|
|
|
|
|
current.ForeColor = Color.Purple;
|
|
|
|
|
break;
|
|
|
|
|
case Outlook.OlBusyStatus.olFree:
|
|
|
|
|
break;
|
|
|
|
|
case Outlook.OlBusyStatus.olOutOfOffice:
|
|
|
|
|
current.ForeColor = Color.Brown;
|
|
|
|
|
break;
|
|
|
|
|
case Outlook.OlBusyStatus.olTentative:
|
|
|
|
|
break;
|
|
|
|
|
case Outlook.OlBusyStatus.olWorkingElsewhere:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lstCol.Add(current);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.listView1.Items.Clear();
|
|
|
|
|
this.listView1.Items.AddRange(lstCol.ToArray());
|
2013-04-26 17:32:14 +02:00
|
|
|
|
|
|
|
|
|
this.apptCalendar.UpdateCalendar();
|
2013-03-29 17:00:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// Get recurring appointments in a date range.
|
2013-03-29 17:00:07 +01:00
|
|
|
|
/// </summary>
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <param name="folder">Outlook folder</param>
|
|
|
|
|
/// <param name="startTime">Start time</param>
|
|
|
|
|
/// <param name="endTime">End time</param>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
/// <returns>Outlook.Items</returns>
|
|
|
|
|
private Outlook.Items GetAppointmentsInRange(Outlook.Folder folder, DateTime startTime, DateTime endTime)
|
|
|
|
|
{
|
|
|
|
|
string filter = "[Start] >= '"
|
|
|
|
|
+ startTime.ToString("g")
|
|
|
|
|
+ "' AND [End] <= '"
|
|
|
|
|
+ endTime.ToString("g") + "'";
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Outlook.Items calItems = folder.Items;
|
|
|
|
|
calItems.IncludeRecurrences = true;
|
|
|
|
|
calItems.Sort("[Start]", Type.Missing);
|
|
|
|
|
Outlook.Items restrictItems = calItems.Restrict(filter);
|
|
|
|
|
if (restrictItems.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return restrictItems;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { return null; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Open the appointment, having in mind it might be a recurring event
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">EventArgs</param>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
private void listView1_DoubleClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (this.listView1.SelectedIndices.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
Outlook.AppointmentItem appt = this.listView1.SelectedItems[0].Tag as Outlook.AppointmentItem;
|
|
|
|
|
if (appt != null)
|
|
|
|
|
{
|
|
|
|
|
if (appt.IsRecurring)
|
|
|
|
|
{
|
|
|
|
|
FormRecurringOpen f = new FormRecurringOpen();
|
|
|
|
|
if (f.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (f.OpenRecurring)
|
|
|
|
|
{
|
|
|
|
|
// Open up the master appointment in a new window
|
|
|
|
|
// If we open the current instance then there is an error: "This item is no longer valid because it has been closed"
|
|
|
|
|
// One workaround is to refresh the appointments list to get new instances...
|
|
|
|
|
Outlook.AppointmentItem masterAppt = appt.Parent; // Get the master appointment item
|
|
|
|
|
masterAppt.Display(true); // Will modify ALL instances
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Open up the appointment in a new window
|
|
|
|
|
appt.Display(true); // Modal yes/no
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Open up the appointment in a new window
|
|
|
|
|
appt.Display(true); // Modal yes/no
|
|
|
|
|
}
|
|
|
|
|
// At the end, synchronously "refresh" items in case they have changed
|
|
|
|
|
this.RetrieveAppointments();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new mail item to reply all recipients of an appointment (except the current user)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">EventArgs</param>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
private void mnuItemReplyAllEmail_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (this.listView1.SelectedIndices.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
Outlook.AppointmentItem appt = this.listView1.SelectedItems[0].Tag as Outlook.AppointmentItem;
|
|
|
|
|
if (appt != null)
|
|
|
|
|
{
|
|
|
|
|
Outlook.MailItem mail = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
|
|
|
|
|
string curUserAddress = GetEmailAddress(Globals.ThisAddIn.Application.Session.CurrentUser);
|
|
|
|
|
foreach (Outlook.Recipient rcpt in appt.Recipients)
|
|
|
|
|
{
|
|
|
|
|
string smtpAddress = GetEmailAddress(rcpt);
|
|
|
|
|
if (curUserAddress != smtpAddress)
|
|
|
|
|
{
|
|
|
|
|
mail.Recipients.Add(smtpAddress);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mail.Body = Environment.NewLine + Environment.NewLine + appt.Body;
|
|
|
|
|
mail.Subject = "RE: " + appt.Subject;
|
|
|
|
|
mail.Display();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-29 19:03:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves Outlook recipient email address
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rcpt">Recipient</param>
|
|
|
|
|
/// <returns>Email address of the contact</returns>
|
2013-03-29 17:00:07 +01:00
|
|
|
|
private string GetEmailAddress(Outlook.Recipient rcpt)
|
|
|
|
|
{
|
|
|
|
|
Outlook.PropertyAccessor pa = rcpt.PropertyAccessor;
|
|
|
|
|
return pa.GetProperty(PR_SMTP_ADDRESS).ToString();
|
|
|
|
|
}
|
2013-03-29 19:03:00 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Switch to the calendar view when double-clicking a date
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Sender</param>
|
|
|
|
|
/// <param name="e">EventArgs</param>
|
2013-04-26 17:32:14 +02:00
|
|
|
|
private void apptCalendar_CellDoubleClick(object sender, EventArgs e)
|
2013-03-29 19:03:00 +01:00
|
|
|
|
{
|
2013-03-30 00:31:49 +01:00
|
|
|
|
// Clicking in days outside of the current month will cause the calendar to refresh to that day
|
2013-03-29 23:52:47 +01:00
|
|
|
|
// reposition all days and select the wrong one
|
|
|
|
|
Outlook.Folder f = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
|
|
|
|
|
Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder = f;
|
|
|
|
|
Outlook.CalendarView cv = (Outlook.CalendarView)(Globals.ThisAddIn.Application.ActiveExplorer().CurrentView);
|
|
|
|
|
cv.CalendarViewMode = Outlook.OlCalendarViewMode.olCalendarViewDay;
|
2013-04-26 17:32:14 +02:00
|
|
|
|
cv.GoToDate(this.apptCalendar.SelectedDate);
|
2013-03-29 19:03:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion "Methods"
|
2013-03-29 17:00:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|