UI/FormRegionPrivacyStatus.cs
author Thomas
Fri, 03 Jul 2020 09:06:22 +0200
changeset 3183 86dc98614618
parent 3034 780e81a4254d
permissions -rw-r--r--
Save setting correctly to Registry
     1 ´╗┐using pEp.UI;
     2 using pEpCOMServerAdapterLib;
     3 using System;
     4 using Outlook = Microsoft.Office.Interop.Outlook;
     5 
     6 namespace pEp
     7 {
     8     /// <summary>
     9     /// Partial class for the privacy status form region that is displayed below every outlook message.
    10     /// </summary>
    11     internal partial class FormRegionPrivacyStatus
    12     {
    13         #region Form Region Factory
    14 
    15         [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
    16         [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegionPrivacyStatus")]
    17         public partial class FormRegionPrivacyStatusFactory
    18         {
    19             // Occurs before the form region is initialized.
    20             // To prevent the form region from appearing, set e.Cancel to true.
    21             // Use e.OutlookItem to get a reference to the current Outlook item.
    22             private void FormRegionPrivacyStatus_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
    23             {
    24                 Outlook.MailItem omi;
    25 
    26                 try
    27                 {
    28                     omi = (Outlook.MailItem)e.OutlookItem;
    29                 }
    30                 catch
    31                 {
    32                     // Never load if it's not a MailItem
    33                     e.Cancel = true;
    34                     return;
    35                 }
    36                 finally
    37                 {
    38                     omi = null;
    39                 }
    40 
    41                 //WindowFormRegionCollection formRegions;
    42 
    43                 /* There is a Microsoft bug at least in Outlook 2013 and Windows 8.1
    44                  * This bug causes multiple PrivacyStatus form regions to appear stacked on top of each other.
    45                  * To trigger this bug, on an unencrypted server, click reply to compose an in-line response.
    46                  * Then click to another tab such as People or Tasks. Then click back on the mail tab to view 
    47                  * the original email again. Two form regions will be visible: 
    48                  * (1) for the status during the in-line reply and 
    49                  * (2) for the status of only the received message.
    50                  * 
    51                  * To fix this bug, any existing form regions are found and closed when initializing a new privacy status
    52                  * form region.
    53                  */
    54                 /*
    55                 try
    56                 {
    57                     formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
    58                     if (formRegions.FormRegionPrivacyStatus != null)
    59                     {
    60                         // Note: there seems to be no way to actually close the form region.
    61                         // Therefore, this is as close as possible to actually closing it.
    62                         // The actual form regions will be cleaned up as soon as another email is selected.
    63                         formRegions.FormRegionPrivacyStatus.OutlookFormRegion.Visible = false;
    64                     }
    65                 }
    66                 catch { }
    67                 */
    68             }
    69         }
    70 
    71         #endregion
    72 
    73         /* Store the last calculated rating so that we can initialize the form region
    74          * using this value. This is done for UX reasons so that we transition from one 
    75          * color to the next without having a default value (Undefined) in between, which
    76          * would make the whole experience more flickery.
    77          */
    78         public static pEpRating LastCalculatedRating { get; private set; } = pEpRating.pEpRatingUndefined;
    79 
    80 
    81         /// <summary>
    82         /// Event handler that is called when the form region is displayed.
    83         /// This is called each time the form region loses then regains visibility 
    84         /// (for example an other email is selected then back to this one).
    85         /// </summary>
    86         private void FormRegionPrivacyStatus_FormRegionShowing(object sender, EventArgs e)
    87         {
    88             Outlook.MailItem omi;
    89             try
    90             {
    91                 // The Privacy Status bar is only visible if enabled in options
    92                 this.OutlookFormRegion.Visible = (Globals.ThisAddIn.Settings.IsPrivacyStatusBarEnabled &&
    93                                                   (FormRegionPrivacyStatus.LastCalculatedRating.ToColor() != pEpColor.pEpColorNoColor));
    94 
    95                 omi = this.OutlookItem as Outlook.MailItem;
    96                 WatchedWindow window = omi?.GetWatchedParentWindow();
    97                 if (window != null)
    98                 {
    99                     this.UpdateFormRegion(window.PrivacyState);
   100                 }
   101                 else
   102                 {
   103                     Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Error getting parent window. Couldn't update privacy status bar.");
   104                 }
   105             }
   106             catch (Exception ex)
   107             {
   108                 Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Error occured. " + ex.ToString());
   109             }
   110             finally
   111             {
   112                 omi = null;
   113             }
   114         }
   115 
   116         /// <summary>
   117         /// Updates the privacy status bar matching the given privacy state.
   118         /// </summary>
   119         /// <param name="privacyState">The privacy state to show in the status bar.</param>
   120         public void UpdateFormRegion(PrivacyState privacyState)
   121         {
   122             try
   123             {
   124                 // Find the WPF user control and update its state
   125                 if (privacyState != null)
   126                 {
   127                     // Do not show the status bar if there is no color
   128                     this.OutlookFormRegion.Visible = (Globals.ThisAddIn.Settings.IsPrivacyStatusBarEnabled &&
   129                                                       (privacyState.Rating.ToColor() != pEpColor.pEpColorNoColor));
   130 
   131                     ((this.Controls[0].Controls[0] as System.Windows.Forms.Integration.ElementHost).Child as UserControlPrivacyStatus)?.Update(privacyState);
   132                     FormRegionPrivacyStatus.LastCalculatedRating = privacyState.Rating;
   133                 }
   134             }
   135             catch (Exception ex)
   136             {
   137                 Log.Error("UpdateFormRegion: Error updating form region. " + ex.ToString());
   138             }
   139         }
   140     }
   141 }