using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace Outlook2013TodoAddIn.Forms
{
///
/// New form to display new email notifications
///
public partial class NewMailAlert : Form
{
#region "Variables"
///
/// To control how long the form displays
///
private Timer timer;
///
/// Don't close the for if the mouse is over it
///
#endregion "Variables"
private bool mouseIsOver = false;
///
#region "Properties"
/// Attached email message to open or flag
///
public Microsoft.Office.Interop.Outlook.MailItem Email { get; set; }
///
/// Whether to show without the form activation
///
protected override bool ShowWithoutActivation
{
get { return true; }
}
///
/// This is used so we can show without activation and be the TopMost form
/// The TopMost property of the form MUST be set to false for this to work
/// https://connect.microsoft.com/VisualStudio/feedback/details/401311/showwithoutactivation-is-not-supported-with-topmost
///
protected override CreateParams CreateParams
{
get
{
CreateParams value = base.CreateParams;
value.ExStyle |= Constants.WS_EX_TOPMOST;
return value;
}
}
#endregion "Properties"
#region "Methods"
///
/// Default constructor
///
/// Mail item
/// Time in ms to keep the alert on
public NewMailAlert(Microsoft.Office.Interop.Outlook.MailItem newMail, int interval)
{
InitializeComponent();
this.LoadFolders();
this.Email = newMail; // Assign it to open or flag later
this.lnkSender.Text = newMail.Sender.Name;
this.lnkSubject.Text = newMail.Subject;
this.txtBody.Text = newMail.Body;
this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width - 10;
this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height - 10;
this.timer = new Timer();
timer.Interval = interval;
timer.Tick += timer_Tick;
timer.Start();
}
///
/// Button delete clicked
///
/// Sender
/// EventArgs
private void btnDelete_Click(object sender, EventArgs e)
{
this.Email.Delete();
this.Close();
}
///
/// Button flag clicked
///
/// Sender
/// EventArgs
private void btnFlag_Click(object sender, EventArgs e)
{
//Microsoft.Office.Interop.Outlook.OlFlagIcon.olYellowFlagIcon
this.Email.FlagRequest = Constants.FollowUp;
this.Email.Save();
this.Close();
}
///
/// Button envelope clicked
///
/// Sender
/// EventArgs
private void btnEnvelope_Click(object sender, EventArgs e)
{
this.ShowEmail();
}
///
/// Button close clicked
///
/// Sender
/// EventArgs
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// Sender hyperlink clicked
///
/// Sender
/// LinkLabelLinkClickedEventArgs
private void lnkSender_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.ShowEmail();
}
///
/// Subject hyperlink clicked
///
/// Sender
/// LinkLabelLinkClickedEventArgs
private void lnkSubject_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.ShowEmail();
}
///
/// Body textbox clicked
///
/// Sender
/// EventArgs
private void txtBody_Click(object sender, EventArgs e)
{
this.ShowEmail();
}
///
/// Show the email and close the form
///
private void ShowEmail()
{
this.Email.Display();
this.Close();
}
///
/// Process timer ticks
///
/// Sender
/// EventArgs
private void timer_Tick(object sender, EventArgs e)
{
if (!mouseIsOver)
{
timer.Stop();
this.Close();
}
}
///
/// Processed when the mouse enters the form
///
/// EventArgs
protected override void OnMouseEnter(EventArgs ea)
{
base.OnMouseEnter(ea);
Point mousePos = PointToClient(Cursor.Position);
mouseIsOver = ClientRectangle.Contains(mousePos);
}
///
/// Processed when the mouse exits the form
///
/// EventArgs
protected override void OnMouseLeave(EventArgs ea)
{
base.OnMouseLeave(ea);
Point mousePos = PointToClient(Cursor.Position);
mouseIsOver = ClientRectangle.Contains(mousePos);
}
///
/// Method that loads all the folders in all stores
///
private void LoadFolders()
{
comboMoveTo.Items.Clear();
foreach (Outlook.Store store in Globals.ThisAddIn.Application.Session.Stores)
{
if (Properties.Settings.Default.Accounts != null && Properties.Settings.Default.Accounts.Contains(store.DisplayName))
{
Outlook.Folder inboxFolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
LoadFolders(inboxFolder, "");
}
}
}
///
/// Recursive method to retrieve a folder's data, and all its children (if any)
///
/// Folder
/// Padding to apply
private void LoadFolders(Outlook.Folder f, string padding)
{
ComboBoxItem cbi = new ComboBoxItem(padding + f.Name, f);
comboMoveTo.Items.Add(cbi);
if (f.Folders.Count != 0)
{
// Get list of folder names and sort alphabetically
List sf = new List();
foreach (Outlook.Folder subf in f.Folders) sf.Add(subf.Name);
sf.Sort();
// Recursively call this method to add the children
sf.ForEach(n =>
{
LoadFolders(f.Folders[n] as Outlook.Folder, padding + " -");
});
}
}
///
/// When a folder is selected in the combobox, move the email item there
///
/// Sender
/// EventArgs
private void comboMoveTo_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboMoveTo.SelectedIndex != -1)
{
Outlook.Folder selFolder = (this.comboMoveTo.SelectedItem as ComboBoxItem).Value as Outlook.Folder;
this.Email.Move(selFolder);
this.Close();
}
}
#endregion "Methods"
}
}