/// <summary>
 /// Allows to mark the selected task as completed (clear task flag for regular items)
 /// </summary>
 /// <param name="sender">Sender</param>
 /// <param name="e">EventArgs</param>
 private void mnuItemMarkComplete_Click(object sender, EventArgs e)
 {
     if (this.lstTasks.SelectedIndices.Count != 0)
     {
         OLTaskItem task = this.lstTasks.SelectedItems[0].Tag as OLTaskItem;
         if (task != null && !task.Completed) // Attempting to complete an already completed task throws an exception
         {
             if (MessageBox.Show("Are you sure you want to complete this task?", "Mark task as completed/Clear task flag", MessageBoxButtons.YesNo) == DialogResult.Yes)
             {
                 if (task.OriginalItem is Outlook.MailItem)
                 {
                     Outlook.MailItem mail = task.OriginalItem as Outlook.MailItem;
                     mail.ClearTaskFlag();
                 }
                 else if (task.OriginalItem is Outlook.ContactItem)
                 {
                     Outlook.ContactItem contact = task.OriginalItem as Outlook.ContactItem;
                     contact.ClearTaskFlag();
                 }
                 else if (task.OriginalItem is Outlook.TaskItem)
                 {
                     Outlook.TaskItem t = task.OriginalItem as Outlook.TaskItem;
                     t.MarkComplete();
                 }
                 else
                 {
                     // Do nothing
                 }
             }
             // At the end, synchronously "refresh" tasks in case they have changed
             this.RetrieveTasks();
         }
     }
 }
 /// <summary>
 /// Open the task
 /// </summary>
 /// <param name="sender">Sender</param>
 /// <param name="e">EventArgs</param>
 private void lstTasks_DoubleClick(object sender, EventArgs e)
 {
     if (this.lstTasks.SelectedIndices.Count != 0)
     {
         OLTaskItem task = this.lstTasks.SelectedItems[0].Tag as OLTaskItem;
         if (task != null)
         {
             if (task.OriginalItem is Outlook.MailItem)
             {
                 Outlook.MailItem mail = task.OriginalItem as Outlook.MailItem;
                 mail.Display(true);
             }
             else if (task.OriginalItem is Outlook.ContactItem)
             {
                 Outlook.ContactItem contact = task.OriginalItem as Outlook.ContactItem;
                 contact.Display(true);
             }
             else if (task.OriginalItem is Outlook.TaskItem)
             {
                 Outlook.TaskItem t = task.OriginalItem as Outlook.TaskItem;
                 t.Display(true);
             }
             else
             {
                 // Do nothing
             }
             // At the end, synchronously "refresh" tasks in case they have changed
             this.RetrieveTasks();
         }
     }
 }
 /// <summary>
 /// Allows to delete the selected task
 /// </summary>
 /// <param name="sender">Sender</param>
 /// <param name="e">EventArgs</param>
 private void mnuItemDeleteTask_Click(object sender, EventArgs e)
 {
     if (this.lstTasks.SelectedIndices.Count != 0)
     {
         OLTaskItem task = this.lstTasks.SelectedItems[0].Tag as OLTaskItem;
         if (task != null)
         {
             if (MessageBox.Show("Are you sure you want to delete this task?", "Delete task", MessageBoxButtons.YesNo) == DialogResult.Yes)
             {
                 if (task.OriginalItem is Outlook.MailItem)
                 {
                     Outlook.MailItem mail = task.OriginalItem as Outlook.MailItem;
                     mail.Delete();
                 }
                 else if (task.OriginalItem is Outlook.ContactItem)
                 {
                     Outlook.ContactItem contact = task.OriginalItem as Outlook.ContactItem;
                     contact.Delete();
                 }
                 else if (task.OriginalItem is Outlook.TaskItem)
                 {
                     Outlook.TaskItem t = task.OriginalItem as Outlook.TaskItem;
                     t.Delete();
                 }
                 else
                 {
                     // Do nothing
                 }
             }
             // At the end, synchronously "refresh" tasks in case they have changed
             this.RetrieveTasks();
         }
     }
 }
        private void lstTasks_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawBackground(); // To avoid repainting (making font "grow")
            OLTaskItem task = e.Item.Tag as OLTaskItem;

            // Color catColor = Color.Empty;
            List <Color> catColors = new List <Color>();

            Font itemFont = this.Font;

            task.Categories.ForEach(cat =>
            {
                Outlook.Category c = Globals.ThisAddIn.Application.Session.Categories[cat] as Outlook.Category;
                if (c != null)
                {
                    catColors.Add(TranslateCategoryColor(c.Color));
                }
            });
            int categoriesRectangleWidth = 30;
            int horizontalSpacing        = 2;

            Rectangle totalRectangle      = e.Bounds;
            Rectangle subjectRectangle    = totalRectangle; subjectRectangle.Height = this.FontHeight;
            Rectangle categoriesRectangle = totalRectangle; categoriesRectangle.Width = categoriesRectangleWidth; categoriesRectangle.Height = this.FontHeight; categoriesRectangle.Offset(10, this.FontHeight);
            Rectangle reminderRectangle   = totalRectangle; reminderRectangle.Height = this.FontHeight; reminderRectangle.Offset(0, this.FontHeight);

            if (task.Categories.Count != 0)
            {
                reminderRectangle.Offset(categoriesRectangleWidth + horizontalSpacing * 4, 0);
            }

            bool  selected = e.State.HasFlag(ListViewItemStates.Selected);
            Color back     = Color.Empty;

            if (selected)
            {
                back = Color.LightCyan;
            }
            using (Brush br = new SolidBrush(back))
                e.Graphics.FillRectangle(br, totalRectangle);

            StringFormat rightFormat = new StringFormat();

            rightFormat.Alignment     = StringAlignment.Far;
            rightFormat.LineAlignment = StringAlignment.Near;
            StringFormat leftFormat = new StringFormat();

            leftFormat.Alignment     = StringAlignment.Near;
            leftFormat.LineAlignment = StringAlignment.Near;

            Brush colorBrush = new SolidBrush(this.ForeColor);

            if (catColors.Count != 0)
            {
                int catWidth = categoriesRectangle.Width / catColors.Count;
                // int catWidth = categoriesRectangle.Width / 3; // TODO: This looks nicer, but more than 3 won't fit
                Rectangle catRect = categoriesRectangle; catRect.Width = catWidth;
                // catColors.ForEach(cc =>
                catColors.Take(3).ToList().ForEach(cc =>
                {
                    // e.Graphics.FillEllipse(new SolidBrush(cc), catRect);
                    e.Graphics.FillRectangle(new SolidBrush(cc), catRect);
                    // e.Graphics.FillRectangle(new SolidBrush(cc), catRect.Left, catRect.Top, catWidth, this.FontHeight);
                    catRect.Offset(catWidth, 0);
                });
            }
            Font mainFont = new Font(this.Font, FontStyle.Bold);
            Font subFont  = this.Font;

            if (task.Completed)
            {
                mainFont = new Font(this.Font, FontStyle.Bold | FontStyle.Strikeout);
                subFont  = new Font(this.Font, FontStyle.Strikeout);
            }
            e.Graphics.DrawString(task.TaskSubject, mainFont, colorBrush, subjectRectangle, leftFormat);
            if (task.Reminder.Year != Constants.NullYear)
            {
                e.Graphics.DrawImage(Properties.Resources.Alert_16xSM, reminderRectangle.Left, reminderRectangle.Top);
                e.Graphics.DrawString(task.Reminder.ToString(), subFont, colorBrush, reminderRectangle.Left + Properties.Resources.Alert_16xSM.Width + horizontalSpacing, reminderRectangle.Top, leftFormat);
            }
            else if (task.StartDate.Year != Constants.NullYear)
            {
                e.Graphics.DrawImage(Properties.Resources.CurrentRow_15x14, reminderRectangle.Left, reminderRectangle.Top);
                e.Graphics.DrawString(task.StartDate.ToShortDateString(), subFont, colorBrush, reminderRectangle.Left + Properties.Resources.CurrentRow_15x14.Width + horizontalSpacing, reminderRectangle.Top, leftFormat);
            }
        }
 /// <summary>
 /// Comparer method to sort tasks based on due date/time
 /// </summary>
 /// <param name="x">First task</param>
 /// <param name="y">Second task</param>
 /// <returns></returns>
 private static int CompareTasks(OLTaskItem x, OLTaskItem y)
 {
     return(x.DueDate.CompareTo(y.DueDate));
 }