Outlook2013CalendarAddIn/Outlook2013TodoAddIn/ThisAddIn.cs

175 lines
7.6 KiB
C#
Raw Normal View History

2013-03-29 17:00:07 +01:00
using Microsoft.Win32;
using Outlook2013TodoAddIn.Forms;
2013-03-29 17:00:07 +01:00
using System;
using System.Drawing;
2013-03-29 17:00:07 +01:00
using System.Linq;
using System.Windows.Forms;
2013-03-29 17:00:07 +01:00
using Office = Microsoft.Office.Core;
namespace Outlook2013TodoAddIn
{
/// <summary>
/// Class for the add-in
/// </summary>
2013-03-29 17:00:07 +01:00
public partial class ThisAddIn
{
#region "Properties"
2013-03-29 17:00:07 +01:00
/// <summary>
/// Control with calendar, etc...
/// </summary>
public AppointmentsControl AppControl { get; set; }
/// <summary>
/// Custom task pane
/// </summary>
2013-03-29 17:00:07 +01:00
public Microsoft.Office.Tools.CustomTaskPane ToDoTaskPane { get; set; }
#endregion "Properties"
#region "Methods"
2013-03-29 17:00:07 +01:00
/// <summary>
/// Initialize settings upon add-in startup
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">EventArgs</param>
2013-03-29 17:00:07 +01:00
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.NewMailEx += Application_NewMailEx;
this.AddRegistryNotification();
this.AppControl = new AppointmentsControl();
this.AppControl.MailAlertsEnabled = Properties.Settings.Default.MailAlertsEnabled;
this.AppControl.ShowPastAppointments = Properties.Settings.Default.ShowPastAppointments;
this.AppControl.Accounts = Properties.Settings.Default.Accounts;
this.AppControl.ShowFriendlyGroupHeaders = Properties.Settings.Default.ShowFriendlyGroupHeaders;
2013-09-11 16:52:43 +02:00
this.AppControl.ShowDayNames = Properties.Settings.Default.ShowDayNames;
this.AppControl.ShowWeekNumbers = Properties.Settings.Default.ShowWeekNumbers;
2013-07-22 22:12:10 +02:00
this.AppControl.ShowTasks = Properties.Settings.Default.ShowTasks;
this.AppControl.FirstDayOfWeek = Properties.Settings.Default.FirstDayOfWeek;
this.AppControl.NumDays = Properties.Settings.Default.NumDays; // Setting the value will load the appointments
ToDoTaskPane = this.CustomTaskPanes.Add(this.AppControl, "Appointments");
ToDoTaskPane.Visible = Properties.Settings.Default.Visible;
2013-03-29 17:00:07 +01:00
ToDoTaskPane.Width = Properties.Settings.Default.Width;
ToDoTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
ToDoTaskPane.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal;
ToDoTaskPane.VisibleChanged += ToDoTaskPane_VisibleChanged;
this.AppControl.SizeChanged += appControl_SizeChanged;
// Selecting the date will retrieve the appointments
// Otherwise it'll take the one used when the designer changed!
this.AppControl.SelectedDate = DateTime.Today;
2013-07-22 22:12:10 +02:00
// this.AppControl.RetrieveData();
Globals.ThisAddIn.Application.ActiveExplorer().Deactivate += ThisAddIn_Deactivate;
// TODO: Make sure there are no memory leaks (dispose COM objects)
2013-03-29 17:00:07 +01:00
}
2013-04-01 23:04:34 +02:00
/// <summary>
/// Process new email
/// </summary>
/// <param name="EntryIDCollection">ID of the email</param>
private void Application_NewMailEx(string EntryIDCollection)
{
if (Properties.Settings.Default.MailAlertsEnabled)
{
Microsoft.Office.Interop.Outlook.MailItem newMail = Globals.ThisAddIn.Application.Session.GetItemFromID(EntryIDCollection) as Microsoft.Office.Interop.Outlook.MailItem;
if (newMail != null)
{
NewMailAlert nm = new NewMailAlert(newMail, Properties.Settings.Default.DisplayTimeOut);
// Show the popup without stealing focus
SoundHelper.sndPlaySoundW(SoundHelper.MailBeep, SoundHelper.SND_NODEFAULT);
nm.ShowPopup();
}
}
}
/// <summary>
/// Store the new size setting upon resizing
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">EventArgs</param>
2013-03-29 17:00:07 +01:00
private void appControl_SizeChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Width = ToDoTaskPane.Width;
}
/// <summary>
/// Toggle ribbon button's status
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">EventArgs</param>
2013-03-29 17:00:07 +01:00
private void ToDoTaskPane_VisibleChanged(object sender, EventArgs e)
{
// Can't save visibility here because this fires when closing Outlook, and by that time the pane is ALWAYS not visible
2013-03-29 17:00:07 +01:00
// Properties.Settings.Default.Visible = ToDoTaskPane.Visible;
TodoRibbonAddIn rbn = Globals.Ribbons.FirstOrDefault(r => r is TodoRibbonAddIn) as TodoRibbonAddIn;
if (rbn != null)
{
rbn.toggleButton1.Checked = ToDoTaskPane.Visible;
}
}
/// <summary>
/// This is the alternative to capture the visibility of the pane when shutting down Outlook
/// </summary>
private void ThisAddIn_Deactivate()
{
Properties.Settings.Default.Visible = ToDoTaskPane.Visible;
}
2013-03-29 17:00:07 +01:00
/// <summary>
/// This is not executed by default
2013-03-29 17:00:07 +01:00
/// http://msdn.microsoft.com/en-us/library/office/ee720183.aspx#OL2010AdditionalShutdownChanges_AddinShutdownChangesinOL2010Beta
/// We MANUALLY add notification to the registry of each user below
2013-03-29 17:00:07 +01:00
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">EventArgs</param>
2013-03-29 17:00:07 +01:00
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Can't call property setters such as: Properties.Settings.Default.NumDays = XXX because the pane is already disposed.
// Settings will be set while the app is running and saved here.
2013-03-29 17:00:07 +01:00
Properties.Settings.Default.Save();
}
/// <summary>
/// Implement shutdown notification for this particular add-in
/// http://msdn.microsoft.com/en-us/library/office/ee720183.aspx#OL2010AdditionalShutdownChanges_AddinShutdownChangesinOL2010Beta
/// HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\<ProgID>\[RequireShutdownNotification]=dword:0x1
/// </summary>
2013-03-29 17:00:07 +01:00
private void AddRegistryNotification()
{
// If the entry is not there when Outlook loads, it will NOT notify the add-in, so the first time won't save the results
2013-03-29 17:00:07 +01:00
string subKey = @"Software\Microsoft\Office\Outlook\Addins\Outlook2013TodoAddIn";
RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(subKey, true);
if (rk == null)
{
rk = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(subKey);
}
if ((int)rk.GetValue("RequireShutdownNotification", 0) == 0)
{
rk.SetValue("RequireShutdownNotification", 1, RegistryValueKind.DWord); // "dword:0x1"
}
}
#endregion "Methods"
2013-03-29 17:00:07 +01:00
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion VSTO generated code
}
}