UI/RibbonCustomizations.cs
author Thomas
Tue, 30 Jul 2019 08:18:44 +0200
branchsync
changeset 2687 f7f12cd4dd77
parent 2682 e16dba4f04d7
child 2695 273816414b64
permissions -rw-r--r--
OUT-386: Add Reset Trust context menu entry for contacts
     1 ´╗┐using pEp.UI;
     2 using pEpCOMServerAdapterLib;
     3 using System;
     4 using System.Diagnostics;
     5 using System.IO;
     6 using System.Reflection;
     7 using System.Runtime.InteropServices;
     8 using System.Windows.Forms;
     9 using System.Windows.Input;
    10 using Office = Microsoft.Office.Core;
    11 using Outlook = Microsoft.Office.Interop.Outlook;
    12 
    13 namespace pEp
    14 {
    15     [ComVisible(true)]
    16     public class RibbonCustomizations : Office.IRibbonExtensibility
    17     {
    18         /* Note: Only one instance of the RibbonCustomizations class is created in Outlook.
    19          * Any class variables will therefore be global between any relevant UI.
    20          *
    21          * Additionally, the Ribbon_Load event will also only be called once and only one instance of the ribbon
    22          * will exist. For this reason, it's safe to make the ribbon reference a static variable.
    23          * Making this static allows for a simple refresh ribbon method.
    24          */
    25         private static Office.IRibbonUI ribbon;
    26 
    27         private static bool        DISABLE_FORCE_PROTECTION_DEFAULT    = false;
    28         private static bool        FORCE_PROTECTION_DEFAULT            = false;
    29         private static bool        FORCE_UNENCRYPTED_DEFAULT           = false;
    30         private static bool        IS_DRAFT_DEFAULT                    = false;
    31         private static bool        NEVER_UNSECURE_DEFAULT              = false;
    32         private static pEpRating   RATING_DEFAULT                      = pEpRating.pEpRatingUndefined;
    33 
    34         private enum MessageProperties
    35         {
    36             DisableForceProtection,
    37             ForceProtection,
    38             ForceUnencrypted,
    39             IsDraft,
    40             NeverUnsecure,
    41             Rating
    42         }
    43 
    44         /**************************************************************
    45          * 
    46          * Constructors
    47          * 
    48          *************************************************************/
    49 
    50         /// <summary>
    51         /// Default constructor.
    52         /// </summary>
    53         public RibbonCustomizations()
    54         {
    55         }
    56 
    57         /**************************************************************
    58          * 
    59          * Property accessors
    60          * 
    61          *************************************************************/
    62 
    63         /// <summary>
    64         /// Gets whether the Force Protection option is disabled for the current mail item.
    65         /// </summary>
    66         /// <param name="control">The current button control.</param>
    67         /// <returns>True if the Force Protection option is disabled for the current mail item, otherwise false.</returns>
    68         internal bool GetDisableForceProtection(Office.IRibbonControl control)
    69         {
    70             return RibbonCustomizations.GetRibbonProperty(control, MessageProperties.DisableForceProtection);
    71         }
    72 
    73         /// <summary>
    74         /// Sets whether the Force Protection option should be disabled for the current mail item.
    75         /// </summary>
    76         /// <param name="control">The current button control.</param>
    77         /// <param name="disable">Whether to disable the Force Protection option or not</param>
    78         internal void SetDisableForceProtection(Office.IRibbonControl control, bool disable)
    79         {
    80             try
    81             {
    82                 RibbonCustomizations.SetRibbonProperty(control, MessageProperties.DisableForceProtection, disable);
    83             }
    84             catch (Exception ex)
    85             {
    86                 Log.Error("SetDisableForceProtection: Error setting value. " + ex.ToString());
    87             }
    88         }
    89 
    90         /// <summary>
    91         /// Gets whether the current mail item is forcefully protected.
    92         /// </summary>
    93         /// <param name="control">The current button control.</param>
    94         /// <returns>True if the current mail item is forcefully protected, otherwise false.</returns>
    95         internal bool GetForceProtection(Office.IRibbonControl control)
    96         {
    97             return RibbonCustomizations.GetRibbonProperty(control, MessageProperties.ForceProtection);
    98         }
    99 
   100         /// <summary>
   101         /// Sets whether the current mail item should be forcefully protected or not.
   102         /// </summary>
   103         /// <param name="control">The current button control.</param>
   104         /// <param name="forceProtection">Whether to enable Force Protection or not.</param>
   105         internal void SetForceProtection(Office.IRibbonControl control, bool forceProtection)
   106         {
   107             try
   108             {
   109                 RibbonCustomizations.SetRibbonProperty(control, MessageProperties.ForceProtection, forceProtection);
   110             }
   111             catch (Exception ex)
   112             {
   113                 Log.Error("SetForceProtection: Error setting value. " + ex.ToString());
   114             }
   115         }
   116 
   117         /// <summary>
   118         /// Gets whether the current mail item is forcefully unencrypted.
   119         /// </summary>
   120         /// <param name="control">The current button control.</param>
   121         /// <returns>True if the current mail item is forcefully unencrypted, otherwise false.</returns>
   122         internal bool GetForceUnencrypted(Office.IRibbonControl control)
   123         {
   124             return RibbonCustomizations.GetRibbonProperty(control, MessageProperties.ForceUnencrypted);
   125         }
   126 
   127         /// <summary>
   128         /// Sets whether the current mail item should be forcefully unencrypted or not.
   129         /// </summary>
   130         /// <param name="control">The current button control.</param>
   131         /// <param name="forceUnencrypted">Whether to enable Force Unencrypted or not.</param>
   132         internal void SetForceUnencrypted(Office.IRibbonControl control, bool forceUnencrypted)
   133         {
   134             try
   135             {
   136                 RibbonCustomizations.SetRibbonProperty(control, MessageProperties.ForceUnencrypted, forceUnencrypted);
   137             }
   138             catch (Exception ex)
   139             {
   140                 Log.Error("SetForceUnencrypted: Error setting value. " + ex.ToString());
   141             }
   142         }
   143 
   144         /// <summary>
   145         /// Gets whether the current mail item has the Never Unsecure option set
   146         /// </summary>
   147         /// <param name="control">The current button control.</param>
   148         /// <returns>True if the current mail item has the Never Unsecure option set, otherwise false.</returns>
   149         internal bool GetNeverUnsecure(Office.IRibbonControl control)
   150         {
   151             return RibbonCustomizations.GetRibbonProperty(control, MessageProperties.NeverUnsecure);
   152         }
   153 
   154         /// <summary>
   155         /// Sets whether the current mail item should be forcefully unencrypted or not.
   156         /// </summary>
   157         /// <param name="control">The current button control.</param>
   158         /// <param name="neverUnsecure">Whether to enable Never Unsecure or not.</param>
   159         internal void SetNeverUnsecure(Office.IRibbonControl control, bool neverUnsecure)
   160         {
   161             try
   162             {
   163                 RibbonCustomizations.SetRibbonProperty(control, MessageProperties.NeverUnsecure, neverUnsecure);
   164             }
   165             catch (Exception ex)
   166             {
   167                 Log.Error("SetNeverUnsecure: Error setting value. " + ex.ToString());
   168             }
   169         }
   170 
   171         /// <summary>
   172         /// Gets the current UI rating as determined in the associated form region.
   173         /// </summary>
   174         /// <param name="control">The current button control.</param>
   175         /// <returns>The pEpRating of the current message or undefined if an error occured.</returns>
   176         private pEpRating GetRating(Office.IRibbonControl control)
   177         {
   178             return RibbonCustomizations.GetRibbonProperty(control, MessageProperties.Rating);
   179         }
   180 
   181         /**************************************************************
   182          * 
   183          * Methods
   184          * 
   185          *************************************************************/
   186 
   187         /// <summary>
   188         /// Invalidates the associated ribbon causing it to be refreshed.
   189         /// This is static as Outlook will maintain only one instance of the ribbon.
   190         /// </summary>
   191         public static void Invalidate()
   192         {
   193             RibbonCustomizations.ribbon?.Invalidate();
   194         }
   195 
   196         /// <summary>
   197         /// Gets the FormRegionPrivacyStatus that is associated with a given control's parent
   198         /// window (Inspector / Explorer). Can return null.
   199         /// </summary>
   200         /// <param name="control">The control to get the associated form region for.</param>
   201         /// <returns>The associated FormRegionPrivacyStatus or null if an error occured.</returns>
   202         private FormRegionPrivacyStatus GetFormRegionPrivacyStatus(Office.IRibbonControl control)
   203         {
   204             dynamic window = null;
   205             FormRegionPrivacyStatus formRegion = null;
   206 
   207             try
   208             {
   209                 // Get the parent window
   210                 window = control.Context as Outlook.Explorer;
   211                 if (window == null)
   212                 {
   213                     window = control.Context as Outlook.Inspector;
   214                 }
   215 
   216                 if (window != null)
   217                 {
   218                     formRegion = Globals.FormRegions[window].FormRegionPrivacyStatus;
   219                 }
   220             }
   221             catch (Exception ex)
   222             {
   223                 formRegion = null;
   224                 Log.Error("GetFormRegionPrivacyStatus: Error getting form region. " + ex.ToString());
   225             }
   226             finally
   227             {
   228                 window = null;
   229             }
   230 
   231             return formRegion;
   232         }
   233 
   234         /// <summary>
   235         /// Gets a ribbon property value.
   236         /// </summary>
   237         /// <param name="control">The control to get the property value for.</param>
   238         /// <param name="property">The property to get its value.</param>
   239         /// <returns>The property value.</returns>
   240         private static dynamic GetRibbonProperty(Office.IRibbonControl control, MessageProperties property)
   241         {
   242             WatchedWindow window = Globals.ThisAddIn?.GetWatchedWindow(control?.Context);
   243 
   244             switch (property)
   245             {
   246                 case MessageProperties.DisableForceProtection:
   247                     {
   248                         return window?.DisableForceProtection ?? RibbonCustomizations.DISABLE_FORCE_PROTECTION_DEFAULT;
   249                     }
   250                 case MessageProperties.ForceProtection:
   251                     {
   252                         return window?.ForceProtection ?? RibbonCustomizations.FORCE_PROTECTION_DEFAULT;
   253                     }
   254                 case MessageProperties.ForceUnencrypted:
   255                     {
   256                         return window?.ForceUnencrypted ?? RibbonCustomizations.FORCE_UNENCRYPTED_DEFAULT;
   257                     }
   258                 case MessageProperties.IsDraft:
   259                     {
   260                         return window?.IsDraft ?? RibbonCustomizations.IS_DRAFT_DEFAULT;
   261                     }
   262                 case MessageProperties.NeverUnsecure:
   263                     {
   264                         return window?.NeverUnsecure ?? RibbonCustomizations.NEVER_UNSECURE_DEFAULT;
   265                     }
   266                 case MessageProperties.Rating:
   267                     {
   268                         return window?.Rating ?? RibbonCustomizations.RATING_DEFAULT;
   269                     }
   270                 default:
   271                     break;
   272             }
   273 
   274             return null;
   275         }
   276 
   277         /// <summary>
   278         /// Sets a ribbon property.
   279         /// </summary>
   280         /// <param name="control">The control to set its property for.</param>
   281         /// <param name="property">The property to set.</param>
   282         /// <param name="value">The value to set.</param>
   283         private static void SetRibbonProperty(Office.IRibbonControl control, MessageProperties property, dynamic value)
   284         {
   285             WatchedWindow window = Globals.ThisAddIn?.GetWatchedWindow(control.Context);
   286 
   287             if (window != null)
   288             {
   289                 switch (property)
   290                 {
   291                     case MessageProperties.DisableForceProtection:
   292                         {
   293                             window.DisableForceProtection = (bool)value;
   294                         }
   295                         break;
   296                     case MessageProperties.ForceProtection:
   297                         {
   298                             window.ForceProtection = (bool)value;
   299                         }
   300                         break;
   301                     case MessageProperties.ForceUnencrypted:
   302                         {
   303                             window.ForceUnencrypted = (bool)value;
   304                         }
   305                         break;
   306                     case MessageProperties.IsDraft:
   307                         {
   308                             window.IsDraft = (bool)value;
   309                         }
   310                         break;
   311                     case MessageProperties.NeverUnsecure:
   312                         {
   313                             window.NeverUnsecure = (bool)value;
   314                         }
   315                         break;
   316                     case MessageProperties.Rating:
   317                         {
   318                             window.SetRating((pEpRating)value);
   319                         }
   320                         break;
   321                     default:
   322                         break;
   323                 }
   324             }
   325         }
   326 
   327         /// <summary>
   328         /// Gets the Outlook contact item that is associated with a given control's parent
   329         /// window (Inspector / Explorer). Can return null.
   330         /// </summary>
   331         /// <param name="control">The control to get the associated contact item for.</param>
   332         /// <returns>The associated contact item or null if an error occured.</returns>
   333         private Outlook.ContactItem GetContactItem(Office.IRibbonControl control)
   334         {
   335             Outlook.ContactItem oci = null;
   336             Outlook.Inspector inspector = null;
   337             Outlook.Explorer explorer = null;
   338             Outlook.Selection selection = null;
   339 
   340             try
   341             {
   342                 inspector = control.Context as Outlook.Inspector;
   343                 if (inspector != null)
   344                 {
   345                     oci = inspector.CurrentItem as Outlook.ContactItem;
   346                 }
   347                 else
   348                 {
   349                     explorer = control.Context as Outlook.Explorer;
   350                     if (explorer != null)
   351                     {
   352                         selection = explorer.Selection;
   353                         if (selection.Count > 0)
   354                         {
   355                             oci = selection[1] as Outlook.ContactItem;
   356                         }
   357                     }
   358                 }
   359             }
   360             catch (Exception ex)
   361             {
   362                 oci = null;
   363                 Log.Error("GetFormRegionPrivacyStatus: Error getting form region. " + ex.ToString());
   364             }
   365             finally
   366             {
   367                 inspector = null;
   368                 explorer = null;
   369                 selection = null;
   370             }
   371 
   372             return oci;
   373         }
   374 
   375         /// <summary>
   376         /// Gets whether S/MIME encryption or signature is enabled.
   377         /// </summary>
   378         /// <returns>True if S/MIME encryption or signature is enabled, otherwise false.</returns>
   379         private bool GetIsSMIMEEnabled(Office.IRibbonControl control)
   380         {
   381             bool isEnabled = false;
   382             Outlook.MailItem omi = null;
   383 
   384             try
   385             {
   386                 omi = this.GetMailItem(control);
   387                 isEnabled = (omi?.GetIsSMIMEEnabled() == true);
   388             }
   389             catch (Exception ex)
   390             {
   391                 isEnabled = false;
   392                 Log.Error("RibbonCustomizations.GetIsSMIMEEnabled: Error getting whether S/MIME processing is enabled. " + ex.ToString());
   393             }
   394             finally
   395             {
   396                 omi = null;
   397             }
   398 
   399             return isEnabled;
   400         }
   401 
   402         /// <summary>
   403         /// Gets the Outlook mail item that is associated with a given control's parent
   404         /// window (Inspector / Explorer). Can return null.
   405         /// </summary>
   406         /// <param name="control">The control to get the associated mail item for.</param>
   407         /// <returns>The associated mail item or null if an error occured.</returns>
   408         private Outlook.MailItem GetMailItem(Office.IRibbonControl control)
   409         {
   410             Outlook.MailItem omi = null;
   411             Outlook.Inspector inspector = null;
   412             Outlook.Explorer explorer = null;
   413             Outlook.Selection selection = null;
   414 
   415             try
   416             {
   417                 inspector = control.Context as Outlook.Inspector;
   418                 if (inspector != null)
   419                 {
   420                     omi = inspector.CurrentItem as Outlook.MailItem;
   421                 }
   422                 else
   423                 {
   424                     explorer = control.Context as Outlook.Explorer;
   425                     if (explorer != null)
   426                     {
   427                         selection = explorer.Selection;
   428                         if (selection.Count > 0)
   429                         {
   430                             omi = selection[1] as Outlook.MailItem;
   431                         }
   432                     }
   433                 }
   434             }
   435             catch (Exception ex)
   436             {
   437                 omi = null;
   438                 Log.Error("GetFormRegionPrivacyStatus: Error getting form region. " + ex.ToString());
   439             }
   440             finally
   441             {
   442                 inspector = null;
   443                 explorer = null;
   444                 selection = null;
   445             }
   446 
   447             return omi;
   448         }
   449 
   450         /// <summary>
   451         /// Shows the options form with the given page selected.
   452         /// This will manage/save states as well.
   453         /// </summary>
   454         /// <param name="page">The page to open the options form to.</param>
   455         private void ShowOptions(FormControlOptions.State.OptionPages page = FormControlOptions.State.OptionPages.Accounts)
   456         {
   457             DialogResult result;
   458             FormOptions form;
   459             FormControlOptions.State state;
   460 
   461             try
   462             {
   463                 // Update working state
   464                 state = Globals.ThisAddIn.GetOptionsState();
   465                 state.SelectedPage = page;
   466 
   467                 // Build form and state
   468                 form = new FormOptions
   469                 {
   470                     StartPosition = FormStartPosition.CenterParent,
   471                     DisplayState = state
   472                 };
   473 
   474                 result = form.ShowDialog(); // Must show as dialog to block code
   475 
   476                 if (result == DialogResult.OK)
   477                 {
   478                     Globals.ThisAddIn.SetOptionsState(form.DisplayState);
   479                     RibbonCustomizations.Invalidate();
   480                 }
   481             }
   482             catch (Exception ex)
   483             {
   484                 Globals.StopAndSendCrashReport(ex);
   485             }
   486 
   487             return;
   488         }
   489 
   490         /// <summary>
   491         /// Determines if the active UI mail item has pEp enabled for it.
   492         /// </summary>
   493         /// <returns>True if the mail item has pEp enabled, otherwise false.</returns>
   494         private bool GetIsPEPEnabled(Office.IRibbonControl control)
   495         {
   496             Outlook.MailItem omi = null;
   497             bool isEnabled = PEPSettings.ACCOUNT_SETTING_IS_PEP_ENABLED_DEFAULT;
   498 
   499             try
   500             {
   501                 omi = this.GetMailItem(control);
   502                 isEnabled = omi?.GetIsPEPEnabled() ?? PEPSettings.ACCOUNT_SETTING_IS_PEP_ENABLED_DEFAULT;
   503             }
   504             catch (Exception ex)
   505             {
   506                 isEnabled = PEPSettings.ACCOUNT_SETTING_IS_PEP_ENABLED_DEFAULT;
   507                 Log.Error("GetIsPEPEnabled: Error occured. " + ex.ToString());
   508             }
   509             finally
   510             {
   511                 omi = null;
   512             }
   513 
   514             return isEnabled;
   515         }
   516 
   517         /// <summary>
   518         /// Determines whether the Decrypt Always option is enabled for the current mail item.
   519         /// </summary>
   520         /// <returns>True if the Decrypt Always option is enabled for the current mail item, otherwise false.</returns>
   521         private bool GetIsDecryptAlwaysEnabled(Office.IRibbonControl control)
   522         {
   523             Outlook.MailItem omi = null;
   524             bool isEnabled = PEPSettings.ACCOUNT_SETTING_IS_DECRYPT_ALWAYS_ENABLED_DEFAULT;
   525 
   526             try
   527             {
   528                 omi = this.GetMailItem(control);
   529                 isEnabled = omi?.GetIsDecryptAlwaysEnabled() ?? PEPSettings.ACCOUNT_SETTING_IS_DECRYPT_ALWAYS_ENABLED_DEFAULT;
   530             }
   531             catch (Exception ex)
   532             {
   533                 isEnabled = PEPSettings.ACCOUNT_SETTING_IS_DECRYPT_ALWAYS_ENABLED_DEFAULT;
   534                 Log.Error("GetIsDecryptAlwaysEnabled: Error occured. " + ex.ToString());
   535             }
   536             finally
   537             {
   538                 omi = null;
   539             }
   540 
   541             return isEnabled;
   542         }
   543 
   544         /// <summary>
   545         /// Gets a property of the item associated with the active UI.
   546         /// </summary>
   547         /// <param name="property">The property to get the value for.</param>
   548         /// <returns>The property value.</returns>
   549         private object GetProperty(Office.IRibbonControl control,
   550                                    MailItemExtensions.PEPProperty property)
   551         {
   552             object propValue;
   553             object result = null;
   554             Outlook.MailItem omi = null;
   555             Outlook.ContactItem oci = null;
   556 
   557             // Set the default
   558             result = MailItemExtensions.GetPEPPropertyDefault(property);
   559 
   560             // If the current item is a mail item
   561             omi = this.GetMailItem(control);
   562             if (omi != null)
   563             {
   564                 try
   565                 {
   566                     switch (property)
   567                     {
   568                         case (MailItemExtensions.PEPProperty.ForceUnencrypted):
   569                             {
   570                                 // Return status can be ignored here, using the auto default value is good enough
   571                                 omi.GetPEPProperty(MailItemExtensions.PEPProperty.ForceUnencrypted, out propValue);
   572                                 result = (bool)propValue;
   573                                 break;
   574                             }
   575                         case (MailItemExtensions.PEPProperty.ForceProtection):
   576                             {
   577                                 // Return status can be ignored here, using the auto default value is good enough
   578                                 omi.GetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, out propValue);
   579                                 result = propValue as string;
   580                                 break;
   581                             }
   582                         case (MailItemExtensions.PEPProperty.NeverUnsecure):
   583                             {
   584                                 // Return status can be ignored here, using the auto default value is good enough
   585                                 omi.GetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, out propValue);
   586                                 result = (bool)propValue;
   587                                 break;
   588                             }
   589                         case (MailItemExtensions.PEPProperty.EnableProtection):
   590                             {
   591                                 // Return status can be ignored here, using the auto default value is good enough
   592                                 omi.GetPEPProperty(MailItemExtensions.PEPProperty.EnableProtection, out propValue);
   593                                 result = (bool)propValue;
   594                                 break;
   595                             }
   596                     }
   597                 }
   598                 catch (Exception ex)
   599                 {
   600                     Log.Error("GetProperty: Error occured. " + ex.ToString());
   601                 }
   602                 finally
   603                 {
   604                     omi = null;
   605                 }
   606             }
   607             else
   608             {
   609                 // If the current item is a contact item
   610                 oci = this.GetContactItem(control);
   611                 if (oci != null)
   612                 {
   613                     try
   614                     {
   615                         switch (property)
   616                         {
   617                             case (MailItemExtensions.PEPProperty.ForceUnencrypted):
   618                                 {
   619                                     propValue = oci.GetForceUnencrypted();
   620 
   621                                     // Don't replace the default if the value is null
   622                                     if (propValue != null)
   623                                     {
   624                                         result = (bool)propValue;
   625                                     }
   626 
   627                                     break;
   628                                 }
   629                             case (MailItemExtensions.PEPProperty.NeverUnsecure):
   630                                 {
   631                                     // Not supported
   632                                     break;
   633                                 }
   634                         }
   635                     }
   636                     catch (Exception ex)
   637                     {
   638                         Log.Error("GetProperty: Error occured. " + ex.ToString());
   639 
   640                     }
   641                     finally
   642                     {
   643                         oci = null;
   644                     }
   645                 }
   646             }
   647 
   648             return result;
   649         }
   650 
   651         /// <summary>
   652         /// Sets the given property of the item associated with the active UI.
   653         /// Warning: This will call .Save() on the MailItem.
   654         /// </summary>
   655         /// <param name="property">The property to set the value for.</param>
   656         /// <param name="value">The value of the property to set.</param>
   657         private void SetProperty(Office.IRibbonControl control,
   658                                  MailItemExtensions.PEPProperty property,
   659                                  object value)
   660         {
   661             bool? propertyValue;
   662             Outlook.MailItem omi = null;
   663             Outlook.ContactItem oci = null;
   664 
   665             omi = this.GetMailItem(control);
   666 
   667             // MailItem
   668             if (omi != null)
   669             {
   670                 try
   671                 {
   672                     switch (property)
   673                     {
   674                         case (MailItemExtensions.PEPProperty.ForceProtection):
   675                             {
   676                                 // Return status can be ignored
   677                                 omi.SetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, value);
   678 
   679                                 // Update UI
   680                                 if (string.IsNullOrEmpty(value as string))
   681                                 {
   682                                     Globals.ThisAddIn.GetWatchedWindow(control.Context)?.RequestRatingAndUIUpdate();
   683                                 }
   684                                 else
   685                                 {
   686                                     Globals.ThisAddIn.GetWatchedWindow(control.Context)?.SetRating(pEpRating.pEpRatingReliable);
   687                                 }
   688                                 break;
   689                             }
   690                         case (MailItemExtensions.PEPProperty.ForceUnencrypted):
   691                             {
   692                                 if ((value as bool?) != null)
   693                                 {
   694                                     // Return status can be ignored
   695                                     omi.SetPEPProperty(MailItemExtensions.PEPProperty.ForceUnencrypted, value);
   696 
   697                                     // Update UI
   698                                     this.UpdateUI(control);
   699                                 }
   700 
   701                                 break;
   702                             }
   703                         case (MailItemExtensions.PEPProperty.NeverUnsecure):
   704                             {
   705                                 if ((value as bool?) != null)
   706                                 {
   707                                     // Return status can be ignored
   708                                     omi.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, value);
   709                                 }
   710 
   711                                 break;
   712                             }
   713                         case (MailItemExtensions.PEPProperty.EnableProtection):
   714                             {
   715                                 if ((value as bool?) != null)
   716                                 {
   717                                     // Return status can be ignored
   718                                     omi.SetPEPProperty(MailItemExtensions.PEPProperty.EnableProtection, value);
   719 
   720                                     // Update UI
   721                                     Globals.ThisAddIn.GetWatchedWindow(control.Context)?.RequestRatingAndUIUpdate();
   722                                 }
   723 
   724                                 break;
   725                             }
   726                     }
   727                 }
   728                 catch (Exception ex)
   729                 {
   730                     Log.Error("SetProperty: Error occured. " + ex.ToString());
   731                 }
   732                 finally
   733                 {
   734                     omi = null;
   735                 }
   736             }
   737             else
   738             {
   739                 // ContactItem
   740                 oci = this.GetContactItem(control);
   741                 if (oci != null)
   742                 {
   743                     try
   744                     {
   745                         switch (property)
   746                         {
   747                             case (MailItemExtensions.PEPProperty.ForceUnencrypted):
   748                                 {
   749                                     if ((value as bool?) != null)
   750                                     {
   751                                         if ((value as bool?) == true)
   752                                         {
   753                                             oci.SetForceUnencrypted(true);
   754                                         }
   755                                         else
   756                                         {
   757                                             propertyValue = oci.GetForceUnencrypted();
   758 
   759                                             // Set the mail item value to false ONLY if it was previously true.
   760                                             // This is necessary because the UI will default to false if null (indeterminate).
   761                                             if (propertyValue == true)
   762                                             {
   763                                                 oci.SetForceUnencrypted(false);
   764                                             }
   765                                         }
   766                                     }
   767 
   768                                     break;
   769                                 }
   770                             case (MailItemExtensions.PEPProperty.NeverUnsecure):
   771                                 {
   772                                     // Not supported
   773                                     break;
   774                                 }
   775                         }
   776                     }
   777                     catch (Exception ex)
   778                     {
   779                         Log.Error("SetProperty: Error occured. " + ex.ToString());
   780                     }
   781                     finally
   782                     {
   783                         oci = null;
   784                     }
   785                 }
   786             }
   787         }
   788 
   789         /// <summary>
   790         /// Updates the UI (ribbon and form region).
   791         /// </summary>
   792         /// <param name="control">The control to get its form region.</param>
   793         private void UpdateUI(Office.IRibbonControl control)
   794         {
   795             RibbonCustomizations.Invalidate();
   796             Globals.ThisAddIn.GetWatchedWindow(control?.Context)?.UpdatePrivacyStateAndUI();
   797         }
   798 
   799         /**************************************************************
   800          * 
   801          * Callbacks
   802          * 
   803          *************************************************************/
   804 
   805         ///////////////////////////////////////////////////////////
   806         // ContextMenuKeyImport
   807         ///////////////////////////////////////////////////////////
   808 
   809         /// <summary>
   810         /// Callback to get the store context menu key import button text.
   811         /// </summary>
   812         public string ContextMenuKeyImport_GetLabel(Office.IRibbonControl control)
   813         {
   814             return Properties.Resources.StoreContextMenu_KeyImportText;
   815         }
   816 
   817         /// <summary>
   818         /// Callback to get the visibility of the store context menu key import button.
   819         /// </summary>
   820         public bool ContextMenuKeyImport_GetVisible(Office.IRibbonControl control)
   821         {
   822             bool visible = false;
   823             Outlook.Account account = null;
   824             Outlook.Store store = null;
   825 
   826             try
   827             {
   828                 // If this store has an associated account, show button
   829                 store = control.Context as Outlook.Store;
   830                 account = store?.GetAccount();
   831 
   832                 visible = (account != null);
   833             }
   834             catch (Exception ex)
   835             {
   836                 visible = false;
   837                 Log.Error("ContextMenuKeyImport_GetVisible: Error determining visibility. " + ex.ToString());
   838             }
   839             finally
   840             {
   841                 account = null;
   842                 store = null;
   843             }
   844 
   845             return visible;
   846         }
   847 
   848         /// <summary>
   849         /// Callback to get the store context menu key import button image.
   850         /// </summary>
   851         public System.Drawing.Bitmap ContextMenuKeyImport_GetImage(Office.IRibbonControl control)
   852         {
   853             return Properties.Resources.ImageLogoIcon;
   854         }
   855 
   856         ///////////////////////////////////////////////////////////
   857         // ContextMenuKeyImportButtonPEP
   858         ///////////////////////////////////////////////////////////   
   859 
   860         /// <summary>
   861         /// Callback to get the store context menu key import button text.
   862         /// </summary>
   863         public string ContextMenuKeyImportButtonPEP_GetLabel(Office.IRibbonControl control)
   864         {
   865             return Properties.Resources.StoreContextMenu_OpenPEPKeyImportWizard;
   866         }
   867 
   868         ///////////////////////////////////////////////////////////
   869         // ContextMenuKeyImportButtonPGP
   870         ///////////////////////////////////////////////////////////   
   871 
   872         /// <summary>
   873         /// Callback to get the store context menu key import button text.
   874         /// </summary>
   875         public string ContextMenuKeyImportButtonPGP_GetLabel(Office.IRibbonControl control)
   876         {
   877             return Properties.Resources.StoreContextMenu_OpenPGPKeyImportWizard;
   878         }
   879 
   880         ///////////////////////////////////////////////////////////
   881         // ContextMenuResetContactTrust
   882         ///////////////////////////////////////////////////////////
   883 
   884         /// <summary>
   885         /// Callback to get the store context menu key import button text.
   886         /// </summary>
   887         public string ContextMenuResetContactTrust_GetLabel(Office.IRibbonControl control)
   888         {
   889             return Properties.Resources.PrivacyStatus_ResetTrust;
   890         }
   891 
   892         /// <summary>
   893         /// Callback to get the store context menu key import button image.
   894         /// </summary>
   895         public System.Drawing.Bitmap ContextMenuResetContactTrust_GetImage(Office.IRibbonControl control)
   896         {
   897             return Properties.Resources.ImageLogoIcon;
   898         }
   899 
   900         ///////////////////////////////////////////////////////////
   901         // ButtonPrivacyStatus
   902         ///////////////////////////////////////////////////////////
   903 
   904         /// <summary>
   905         /// Event handler for when the Privacy Status button is clicked.
   906         /// This will open the Options dialog.
   907         /// </summary>
   908         public void ButtonPrivacyStatus_Click(Office.IRibbonControl control)
   909         {
   910             try
   911             {
   912                 Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
   913                 Globals.ThisAddIn.GetWatchedWindow(control.Context)?.BuildAndShowManager();
   914             }
   915             catch (Exception ex)
   916             {
   917                 Globals.StopAndSendCrashReport(ex);
   918             }
   919         }
   920 
   921         /// <summary>
   922         /// Callback to get the image of the ButtonPrivacyStatus.
   923         /// </summary>
   924         public System.Drawing.Bitmap ButtonPrivacyStatus_GetImage(Office.IRibbonControl control)
   925         {
   926             return Globals.ThisAddIn.GetWatchedWindow(control?.Context)?.PrivacyState?.Image;
   927         }
   928 
   929         /// <summary>
   930         /// Callback to get the label of the ButtonPrivacyStatus.
   931         /// </summary>
   932         public string ButtonPrivacyStatus_GetLabel(Office.IRibbonControl control)
   933         {
   934             string label = Globals.ThisAddIn.GetWatchedWindow(control?.Context)?.PrivacyState?.Label;
   935 
   936             // Workaround to show ampersands correctly
   937             if (label?.Contains(" & ") == true)
   938             {
   939                 label = label.Replace(" & ", " && ");
   940             }
   941             return label;
   942         }
   943 
   944         /// <summary>
   945         /// Callback to get the supertip of the ButtonPrivacyStatus.
   946         /// </summary>
   947         public string ButtonPrivacyStatus_GetScreentip(Office.IRibbonControl control)
   948         {
   949             return Properties.Resources.PrivacyStatus_FormText;
   950         }
   951 
   952         /// <summary>
   953         /// Callback to get whether the ButtonPrivacyStatus is visible.
   954         /// </summary>
   955         public bool ButtonPrivacyStatus_GetVisible(Office.IRibbonControl control)
   956         {
   957             /* Visible if pEp enabled.
   958              * If pEp is disabled, only visible in draft windows (so user can Enable Protection)
   959              * or if DecryptAlways option is set.
   960              */
   961             bool visible = true;
   962 
   963             if (this.GetIsPEPEnabled(control) == false)
   964             {
   965                 visible = false;
   966                 dynamic window = null;
   967                 Outlook.MailItem omi = null;
   968 
   969                 try
   970                 {
   971                     // Get the parent window
   972                     window = control.Context as Outlook.Explorer;
   973                     if (window != null)
   974                     {
   975                         visible = this.GetIsDecryptAlwaysEnabled(control);
   976                     }
   977                     else
   978                     {
   979                         window = control.Context as Outlook.Inspector;
   980                         if (window != null)
   981                         {
   982                             omi = this.GetMailItem(control);
   983                             visible = ((omi?.GetIsDraft() == true) ||
   984                                        (this.GetIsDecryptAlwaysEnabled(control)));
   985                         }
   986                     }
   987                 }
   988                 catch (Exception ex)
   989                 {
   990                     Log.Error("ButtonPrivacyStatus_GetVisible: Error occured. " + ex.ToString());
   991                 }
   992                 finally
   993                 {
   994                     omi = null;
   995                     window = null;
   996                 }
   997             }
   998 
   999             return visible;
  1000         }
  1001 
  1002         /// <summary>
  1003         /// Event handler for when the builtin Encrypt Message button is clicked.
  1004         /// </summary>
  1005         public void ButtonSMIME_Clicked(Office.IRibbonControl control, bool pressed, ref bool cancel)
  1006         {
  1007             cancel = false;
  1008             Globals.ThisAddIn.GetWatchedWindow(control.Context)?.RequestRatingAndUIUpdate();
  1009         }
  1010 
  1011         ///////////////////////////////////////////////////////////
  1012         // ToggleButtonForceUnencrypted
  1013         ///////////////////////////////////////////////////////////
  1014 
  1015         /// <summary>
  1016         /// Event handler for when the ToggleButtonForceUnencrypted is clicked.
  1017         /// </summary>
  1018         public void ToggleButtonForceUnencrypted_Click(Office.IRibbonControl control, bool isPressed)
  1019         {
  1020             this.SetForceUnencrypted(control, isPressed);
  1021 
  1022             // Disable Force Protection and Never Unsecure if needed
  1023             if (isPressed)
  1024             {
  1025                 this.SetForceProtection(control, false);
  1026                 this.SetNeverUnsecure(control, false);
  1027             }
  1028 
  1029             this.UpdateUI(control);
  1030         }
  1031 
  1032         /// <summary>
  1033         /// Callback to get whether the ToggleButtonForceUnencrypted is enabled.
  1034         /// </summary>
  1035         public bool ToggleButtonForceUnencrypted_GetEnabled(Office.IRibbonControl control)
  1036         {
  1037             /* The button is disabled if
  1038              *  - Rating is lower than reliable
  1039              *  - Force Protection or Never Unsecure are enabled and not overridden
  1040              */
  1041             if (((this.GetRating(control) < pEpRating.pEpRatingReliable) ||
  1042                  (this.GetNeverUnsecure(control))) ||
  1043                  ((this.GetForceProtection(control)) && (this.GetDisableForceProtection(control) == false)))
  1044             {
  1045                 return false;
  1046             }
  1047             else
  1048             {
  1049                 return true;
  1050             }
  1051         }
  1052 
  1053         /// <summary>
  1054         /// Callback to get the label text for the ToggleButtonForceUnencrypted.
  1055         /// </summary>
  1056         public string ToggleButtonForceUnencrypted_GetLabel(Office.IRibbonControl control)
  1057         {
  1058             return Properties.Resources.Ribbon_ForceUnencrypted;
  1059         }
  1060 
  1061         /// <summary>
  1062         /// Callback to get whether the ToggleButtonForceUnencrypted is pressed.
  1063         /// </summary>
  1064         public bool ToggleButtonForceUnencrypted_GetPressed(Office.IRibbonControl control)
  1065         {
  1066             return this.GetForceUnencrypted(control);
  1067         }
  1068 
  1069         /// <summary>
  1070         /// Callback to get the supertip/description text for the ToggleButtonForceUnencrypted.
  1071         /// </summary>
  1072         public string ToggleButtonForceUnencrypted_GetSupertip(Office.IRibbonControl control)
  1073         {
  1074             return Properties.Resources.Ribbon_ForceUnencryptedDesc;
  1075         }
  1076 
  1077         /// <summary>
  1078         /// Callback to get whether the ToggleButtonForceUnencrypted is visible.
  1079         /// </summary>
  1080         public bool ToggleButtonForceUnencrypted_GetVisible(Office.IRibbonControl control)
  1081         {
  1082             return ((Globals.RELEASE_MODE != Globals.ReleaseMode.Reader) &&
  1083                     (this.GetIsPEPEnabled(control)));
  1084         }
  1085 
  1086         ///////////////////////////////////////////////////////////
  1087         // ToggleButtonEnableProtection
  1088         ///////////////////////////////////////////////////////////
  1089 
  1090         /// <summary>
  1091         /// Event handler for when the ToggleButtonEnableProtection is clicked.
  1092         /// </summary>
  1093         public void ToggleButtonEnableProtection_Click(Office.IRibbonControl control, bool isPressed)
  1094         {
  1095             this.SetProperty(control, MailItemExtensions.PEPProperty.EnableProtection, isPressed);
  1096 
  1097             // If protection is disabled, disable also force protected and never unsecure
  1098             if (isPressed == false)
  1099             {
  1100                 this.SetForceProtection(control, false);
  1101                 this.SetNeverUnsecure(control, false);
  1102             }
  1103         }
  1104 
  1105         /// <summary>
  1106         /// Callback to get whether the ToggleButtonEnableProtection is enabled.
  1107         /// </summary>
  1108         public bool ToggleButtonEnableProtection_GetEnabled(Office.IRibbonControl control)
  1109         {
  1110             // If button is visible, it is also enabled
  1111             return true;
  1112         }
  1113 
  1114         /// <summary>
  1115         /// Callback to get the label text for the ToggleButtonEnableProtection.
  1116         /// </summary>
  1117         public string ToggleButtonEnableProtection_GetLabel(Office.IRibbonControl control)
  1118         {
  1119             return Properties.Resources.Ribbon_EnableProtection;
  1120         }
  1121 
  1122         /// <summary>
  1123         /// Callback to get whether the ToggleButtonEnableProtection is pressed.
  1124         /// </summary>
  1125         public bool ToggleButtonEnableProtection_GetPressed(Office.IRibbonControl control)
  1126         {
  1127             return ((this.GetProperty(control, MailItemExtensions.PEPProperty.EnableProtection) as bool?) ?? false);
  1128         }
  1129 
  1130         /// <summary>
  1131         /// Callback to get the supertip/description text for the ToggleButtonEnableProtection.
  1132         /// </summary>
  1133         public string ToggleButtonEnableProtection_GetSupertip(Office.IRibbonControl control)
  1134         {
  1135             return (Properties.Resources.Ribbon_EnableProtectionDesc);
  1136         }
  1137 
  1138         /// <summary>
  1139         /// Callback to get whether the ToggleButtonEnableProtection is visible.
  1140         /// </summary>
  1141         public bool ToggleButtonEnableProtection_GetVisible(Office.IRibbonControl control)
  1142         {
  1143             return ((Globals.RELEASE_MODE != Globals.ReleaseMode.Reader) &&
  1144                     (this.GetIsPEPEnabled(control)) == false);
  1145         }
  1146 
  1147         ///////////////////////////////////////////////////////////
  1148         // ToggleButtonForceProtection
  1149         ///////////////////////////////////////////////////////////
  1150 
  1151         /// <summary>
  1152         /// Event handler for when the ToggleButtonForceProtection is clicked.
  1153         /// </summary>
  1154         public void ToggleButtonForceProtection_Click(Office.IRibbonControl control, bool isPressed)
  1155         {
  1156             this.SetForceProtection(control, isPressed);
  1157 
  1158             // If protection is disabled, disable also never unsecure
  1159             if (isPressed == false)
  1160             {
  1161                 this.SetNeverUnsecure(control, isPressed);
  1162             }
  1163 
  1164             this.UpdateUI(control);
  1165         }
  1166 
  1167         /// <summary>
  1168         /// Callback to get whether the ToggleButtonForceProtection is enabled
  1169         /// </summary>
  1170         public bool ToggleButtonForceProtection_GetEnabled(Office.IRibbonControl control)
  1171         {
  1172             bool enabled = false;
  1173 
  1174             /* The button is enabled when:
  1175              * 1. No S/MIME option is enabled
  1176              * 2. Force Unencrypted is not enabled
  1177              * 3. Rating is Unsecure
  1178              */
  1179             if ((this.GetIsSMIMEEnabled(control)) ||
  1180                 (this.GetDisableForceProtection(control)))
  1181             {
  1182                 return false;
  1183             }
  1184 
  1185             if (this.GetIsPEPEnabled(control))
  1186             {
  1187                 enabled = ((this.GetRating(control) == pEpRating.pEpRatingUnencrypted) &&
  1188                            (this.GetForceUnencrypted(control) == false));
  1189             }
  1190             else
  1191             {
  1192                 enabled = ((this.GetRating(control) == pEpRating.pEpRatingUnencrypted) &&
  1193                            ((this.GetProperty(control, MailItemExtensions.PEPProperty.EnableProtection) as bool?) ?? false));
  1194             }
  1195 
  1196             return enabled;
  1197         }
  1198 
  1199         /// <summary>
  1200         /// Callback to get the label text for the ToggleButtonForceProtection.
  1201         /// </summary>
  1202         public string ToggleButtonForceProtection_GetLabel(Office.IRibbonControl control)
  1203         {
  1204             return Properties.Resources.Ribbon_ForceProtection;
  1205         }
  1206 
  1207         /// <summary>
  1208         /// Callback to get whether the ToggleButtonForceProtection is pressed.
  1209         /// </summary>
  1210         public bool ToggleButtonForceProtection_GetPressed(Office.IRibbonControl control)
  1211         {
  1212             return ((this.GetForceProtection(control)) &&
  1213                     (this.GetDisableForceProtection(control) == false));
  1214         }
  1215 
  1216         /// <summary>
  1217         /// Callback to get the supertip/description text for the ToggleButtonForceProtection.
  1218         /// </summary>
  1219         public string ToggleButtonForceProtection_GetSupertip(Office.IRibbonControl control)
  1220         {
  1221             return Properties.Resources.Ribbon_ForceProtectionDesc;
  1222         }
  1223 
  1224         /// <summary>
  1225         /// Callback to get whether the ToggleButtonForceProtection is visible.
  1226         /// </summary>
  1227         public bool ToggleButtonForceProtection_GetVisible(Office.IRibbonControl control)
  1228         {
  1229             // Disabled for the moment
  1230             return false;
  1231 
  1232             // Visible if not in Reader mode
  1233             //return (Globals.RELEASE_MODE != Globals.ReleaseMode.Reader);
  1234         }
  1235 
  1236         ///////////////////////////////////////////////////////////
  1237         // ToggleButtonNeverUnsecure
  1238         ///////////////////////////////////////////////////////////
  1239 
  1240         /// <summary>
  1241         /// Event handler for when the ToggleButtonNeverUnsecure is clicked.
  1242         /// </summary>
  1243         public void ToggleButtonNeverUnsecure_Click(Office.IRibbonControl control, bool isPressed)
  1244         {
  1245             this.SetNeverUnsecure(control, isPressed);
  1246             this.UpdateUI(control);
  1247         }
  1248 
  1249         /// <summary>
  1250         /// Callback to get whether the ToggleButtonNeverUnsecure is enabled.
  1251         /// </summary>
  1252         public bool ToggleButtonNeverUnsecure_GetEnabled(Office.IRibbonControl control)
  1253         {
  1254             // Enabled if not forcefully unencrypted, rating >= Reliable (or force protection), no S/MIME enabled and no BCC recipients
  1255             return (((this.GetRating(control) >= pEpRating.pEpRatingReliable) || (this.GetForceProtection(control)) && (this.ToggleButtonForceProtection_GetEnabled(control))) &&
  1256                     (this.GetForceUnencrypted(control) == false) &&
  1257                     (this.GetIsSMIMEEnabled(control) == false));
  1258         }
  1259 
  1260         /// <summary>
  1261         /// Callback to get the label text for the ToggleButtonNeverUnsecure.
  1262         /// </summary>
  1263         public string ToggleButtonNeverUnsecure_GetLabel(Office.IRibbonControl control)
  1264         {
  1265             return Properties.Resources.Ribbon_NeverUnsecure;
  1266         }
  1267 
  1268         /// <summary>
  1269         /// Callback to get whether the ToggleButtonNeverUnsecure is pressed.
  1270         /// </summary>
  1271         public bool ToggleButtonNeverUnsecure_GetPressed(Office.IRibbonControl control)
  1272         {
  1273             return this.GetNeverUnsecure(control);
  1274         }
  1275 
  1276         /// <summary>
  1277         /// Callback to get the supertip/description text for the ToggleButtonNeverUnsecure.
  1278         /// </summary>
  1279         public string ToggleButtonNeverUnsecure_GetSupertip(Office.IRibbonControl control)
  1280         {
  1281             return Properties.Resources.Ribbon_NeverUnsecureDesc;
  1282         }
  1283 
  1284         /// <summary>
  1285         /// Callback to get whether the ToggleButtonNeverUnsecure is visible.
  1286         /// </summary>
  1287         public bool ToggleButtonNeverUnsecure_GetVisible(Office.IRibbonControl control)
  1288         {
  1289             return (Globals.ThisAddIn.Settings.IsNeverUnsecureOptionVisible &&
  1290                     (Globals.RELEASE_MODE != Globals.ReleaseMode.Reader));
  1291         }
  1292 
  1293         ///////////////////////////////////////////////////////////
  1294         // ButtonUpgradePEP
  1295         ///////////////////////////////////////////////////////////
  1296 
  1297         /// <summary>
  1298         /// Event handler for when the Upgrade pEp button is clicked.
  1299         /// </summary>
  1300         public void ButtonUpgradePEP_Click(Office.IRibbonControl control)
  1301         {
  1302             try
  1303             {
  1304                 Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
  1305             }
  1306             catch (Exception ex)
  1307             {
  1308                 Log.Error("ButtonUpgradePEP_Click: Unable to open website link, " + ex.ToString());
  1309             }
  1310         }
  1311 
  1312         /// <summary>
  1313         /// Callback to get whether the ButtonUpgradePEP is enabled.
  1314         /// </summary>
  1315         public bool ButtonUpgradePEP_GetEnabled(Office.IRibbonControl control)
  1316         {
  1317             // Always true, as it has to be clickable while shown
  1318             return true;
  1319         }
  1320 
  1321         /// <summary>
  1322         /// Callback to get the image for the ButtonUpgradePEP.
  1323         /// </summary>
  1324         public System.Drawing.Bitmap ButtonUpgradePEP_GetImage(Office.IRibbonControl control)
  1325         {
  1326             return Properties.Resources.ImageUpgradePEP;
  1327         }
  1328 
  1329         /// <summary>
  1330         /// Callback to get the label text for the ButtonUpgradePEP.
  1331         /// </summary>
  1332         public string ButtonUpgradePEP_GetLabel(Office.IRibbonControl control)
  1333         {
  1334             return (Environment.NewLine + Properties.Resources.Ribbon_UpgradePEP);
  1335         }
  1336 
  1337         /// <summary>
  1338         /// Callback to get the supertip/description text for the ButtonUpgradePEP.
  1339         /// </summary>
  1340         public string ButtonUpgradePEP_GetSupertip(Office.IRibbonControl control)
  1341         {
  1342             return Properties.Resources.Ribbon_UpgradePEPDesc;
  1343         }
  1344 
  1345         /// <summary>
  1346         /// Callback to get whether the ButtonUpgradePEP is visible.
  1347         /// </summary>
  1348         public bool ButtonUpgradePEP_GetVisible(Office.IRibbonControl control)
  1349         {
  1350             // Only visible in reader mode
  1351             return (Globals.RELEASE_MODE == Globals.ReleaseMode.Reader);
  1352         }
  1353 
  1354         ///////////////////////////////////////////////////////////
  1355         // Groups
  1356         ///////////////////////////////////////////////////////////
  1357 
  1358         /// <summary>
  1359         /// Callback to get whether the GroupPEPNewMailMessage is visible.
  1360         /// </summary>
  1361         public bool GroupPEPNewMailMessage_GetVisible(Office.IRibbonControl control)
  1362         {
  1363             // Always needed
  1364             return true;
  1365         }
  1366 
  1367         /**************************************************************
  1368          *
  1369          * Callbacks (Contact Inspector Ribbon)
  1370          *
  1371          *************************************************************/
  1372 
  1373         ///////////////////////////////////////////////////////////
  1374         // ToggleButtonForceUnencryptedContact
  1375         ///////////////////////////////////////////////////////////
  1376 
  1377         /// <summary>
  1378         /// Event handler for when the ToggleButtonForceUnencryptedContact is clicked.
  1379         /// </summary>
  1380         public void ToggleButtonForceUnencryptedContact_Click(Office.IRibbonControl control, bool isPressed)
  1381         {
  1382             this.SetProperty(control, MailItemExtensions.PEPProperty.ForceUnencrypted, isPressed);
  1383             this.UpdateUI(control);
  1384         }
  1385 
  1386         /// <summary>
  1387         /// Callback to get the image for the ToggleButtonForceUnencryptedContact.
  1388         /// </summary>
  1389         public System.Drawing.Bitmap ToggleButtonForceUnencryptedContact_GetImage(Office.IRibbonControl control)
  1390         {
  1391             if ((bool)this.GetProperty(control, MailItemExtensions.PEPProperty.ForceUnencrypted))
  1392             {
  1393                 return Properties.Resources.ImageForceUnencOn;
  1394             }
  1395             else
  1396             {
  1397                 return Properties.Resources.ImageForceUnencOff;
  1398             }
  1399         }
  1400 
  1401         /// <summary>
  1402         /// Callback to get the label text for the ToggleButtonForceUnencryptedContact.
  1403         /// </summary>
  1404         public string ToggleButtonForceUnencryptedContact_GetLabel(Office.IRibbonControl control)
  1405         {
  1406             return Properties.Resources.Ribbon_ForceUnencrypted;
  1407         }
  1408 
  1409         /// <summary>
  1410         /// Callback to get whether the ToggleButtonForceUnencryptedContact is pressed.
  1411         /// </summary>
  1412         public bool ToggleButtonForceUnencryptedContact_GetPressed(Office.IRibbonControl control)
  1413         {
  1414             return (bool)this.GetProperty(control, MailItemExtensions.PEPProperty.ForceUnencrypted);
  1415         }
  1416 
  1417         /// Callback to get the supertip/description text for the ToggleButtonForceUnencryptedContact.
  1418         /// </summary>
  1419         public string ToggleButtonForceUnencryptedContact_GetSupertip(Office.IRibbonControl control)
  1420         {
  1421             return Properties.Resources.Ribbon_ForceUnencryptedDesc;
  1422         }
  1423 
  1424         /// <summary>
  1425         /// Callback to get whether the ToggleButtonForceUnencryptedContact is visible.
  1426         /// </summary>
  1427         public bool ToggleButtonForceUnencryptedContact_GetVisible(Office.IRibbonControl control)
  1428         {
  1429             return true;
  1430         }
  1431 
  1432         /// <summary>
  1433         /// Callback to get whether the GroupPEPContact is visible.
  1434         /// </summary>
  1435         public bool GroupPEPContact_GetVisible(Office.IRibbonControl control)
  1436         {
  1437             return (Globals.ThisAddIn.Settings.IsDisableProtectionForContactsEnabled &&
  1438                     (Globals.RELEASE_MODE != Globals.ReleaseMode.Reader));
  1439         }
  1440 
  1441         /**************************************************************
  1442          *
  1443          * Callbacks (Backstage)
  1444          *
  1445          *************************************************************/
  1446 
  1447         /// <summary>
  1448         /// Callback to get the image for the ButtonAccounts.
  1449         /// </summary>
  1450         public System.Drawing.Bitmap ButtonAccounts_GetImage(Office.IRibbonControl control)
  1451         {
  1452             return (Properties.Resources.ImageBackstageAccounts);
  1453         }
  1454 
  1455         /// <summary>
  1456         /// Callback to get the image for the ButtonCompatibility.
  1457         /// </summary>
  1458         public System.Drawing.Bitmap ButtonCompatibility_GetImage(Office.IRibbonControl control)
  1459         {
  1460             return (Properties.Resources.ImageBackstageCompatibility);
  1461         }
  1462 
  1463         /// <summary>
  1464         /// Callback to get the image for the ButtonCompatibility.
  1465         /// </summary>
  1466         public System.Drawing.Bitmap ButtonSync_GetImage(Office.IRibbonControl control)
  1467         {
  1468             return (Properties.Resources.ImageBackstageCompatibility);
  1469         }
  1470 
  1471         /// <summary>
  1472         /// Callback to get the image for the ImageControlLogo.
  1473         /// </summary>
  1474         public System.Drawing.Bitmap ImageControlLogo_GetImage(Office.IRibbonControl control)
  1475         {
  1476             return (Properties.Resources.ImageLogoMedium);
  1477         }
  1478 
  1479         /// <summary>
  1480         /// Callback to get the helper text for the GroupAccounts.
  1481         /// </summary>
  1482         public string GroupAccounts_GetHelperText(Office.IRibbonControl control)
  1483         {
  1484             return (Properties.Resources.Ribbon_GroupAccountsHelperText);
  1485         }
  1486 
  1487         /// <summary>
  1488         /// Callback to get the helper text for the GroupCompatibility.
  1489         /// </summary>
  1490         public string GroupCompatibility_GetHelperText(Office.IRibbonControl control)
  1491         {
  1492             return (Properties.Resources.Ribbon_GroupCompatibilityHelperText);
  1493         }
  1494 
  1495         /// <summary>
  1496         /// Callback to get the label text for the GroupAccounts.
  1497         /// </summary>
  1498         public string GroupAccounts_GetLabel(Office.IRibbonControl control)
  1499         {
  1500             return (Properties.Resources.Ribbon_GroupAccountsLabel);
  1501         }
  1502 
  1503         /// <summary>
  1504         /// Callback to get the label text for the GroupCompatibility.
  1505         /// </summary>
  1506         public string GroupCompatibility_GetLabel(Office.IRibbonControl control)
  1507         {
  1508             return (Properties.Resources.Ribbon_GroupCompatibilityLabel);
  1509         }
  1510 
  1511         /// <summary>
  1512         /// Callback to get the visibility of the GroupPEPHome.
  1513         /// </summary>
  1514         public bool GroupPEPHome_GetVisible(Office.IRibbonControl control)
  1515         {
  1516             bool visible = true;
  1517 
  1518             /* In Outlook 2010, there is no inline response.
  1519              * IMPORTANT: Never call Explorer.ActiveInlineResponse on Outlook 2010
  1520              *            as this might lead to a crash of Outlook (even inside a
  1521              *            try/catch block)
  1522              */
  1523             if (Globals.OutlookVersion != Globals.Version.Outlook2010)
  1524             {
  1525                 Outlook.Explorer explorer = null;
  1526                 try
  1527                 {
  1528                     // Show only if no inline response is open
  1529                     explorer = control.Context as Outlook.Explorer;
  1530                     if (explorer != null)
  1531                     {
  1532                         visible = (explorer.ActiveInlineResponse == null);
  1533                     }
  1534                 }
  1535                 catch (Exception ex)
  1536                 {
  1537                     Log.Error("GroupPEPHome_GetVisible: Error occured. " + ex.ToString());
  1538                 }
  1539                 finally
  1540                 {
  1541                     explorer = null;
  1542                 }
  1543             }
  1544 
  1545             return visible;
  1546         }
  1547 
  1548         /// <summary>
  1549         /// Callback to get the label text for the ButtonAccounts.
  1550         /// </summary>
  1551         public string ButtonAccounts_GetLabel(Office.IRibbonControl control)
  1552         {
  1553             return (Properties.Resources.Ribbon_ButtonAccountsLabel);
  1554         }
  1555 
  1556         /// <summary>
  1557         /// Callback to get the label text for the ButtonCompatibility.
  1558         /// </summary>
  1559         public string ButtonCompatibility_GetLabel(Office.IRibbonControl control)
  1560         {
  1561             return (Properties.Resources.Ribbon_ButtonCompatibilityLabel);
  1562         }
  1563 
  1564         /// <summary>
  1565         /// Callback to get the label text for the LabelControlName.
  1566         /// </summary>
  1567         public string LabelControlName_GetLabel(Office.IRibbonControl control)
  1568         {
  1569             return (Globals.RELEASE_MODE == Globals.ReleaseMode.Reader ? Globals.PEP_DISPLAY_NAME_READER : Globals.PEP_DISPLAY_NAME);
  1570         }
  1571 
  1572         /// <summary>
  1573         /// Callback to get the label text for the LabelControlVersion.
  1574         /// </summary>
  1575         public string LabelControlVersion_GetLabel(Office.IRibbonControl control)
  1576         {
  1577             return (Globals.GetPEPVersion());
  1578         }
  1579 
  1580         /// <summary>
  1581         /// Callback to get the label text for the LabelControlCopyright.
  1582         /// </summary>
  1583         public string LabelControlCopyright_GetLabel(Office.IRibbonControl control)
  1584         {
  1585             return (Globals.PEP_COPYRIGHT);
  1586         }
  1587 
  1588         /**************************************************************
  1589          *
  1590          * Event Handling
  1591          *
  1592          *************************************************************/
  1593 
  1594         /// <summary>
  1595         /// Event handler for when the ribbon loads.
  1596         /// </summary>
  1597         public void Ribbon_Load(Office.IRibbonUI ribbonUI)
  1598         {
  1599             RibbonCustomizations.ribbon = ribbonUI;
  1600         }
  1601 
  1602         /**************************************************************
  1603          *
  1604          * Event Handling (Context menu)
  1605          *
  1606          *************************************************************/
  1607 
  1608         /// <summary>
  1609         /// Event handler for when the key import context menu button is clicked.
  1610         /// </summary>
  1611         public void ContextMenuKeyImportButton_Click(Office.IRibbonControl control)
  1612         {
  1613             Outlook.Store store = null;
  1614             PEPIdentity ownIdentity = null;
  1615 
  1616             try
  1617             {
  1618                 // Get own pEp identity of this store
  1619                 store = control.Context as Outlook.Store;
  1620                 ownIdentity = store.GetPEPIdentity(true);
  1621 
  1622                 // Open wizard if identity has been successfully retrieved.
  1623                 if (ownIdentity != null)
  1624                 {
  1625                     // Get wizard type (event can be raised from different buttons)
  1626                     var wizardType = (control?.Id.Equals("ContextMenuKeyImportButtonPGP") == true) ? KeySyncWizard.WizardType.PGP : KeySyncWizard.WizardType.pEp;
  1627                     KeySyncWizard.Wizard = new KeySyncWizard(wizardType, true, ownIdentity);
  1628                     KeySyncWizard.Wizard?.Show();
  1629                 }
  1630                 else
  1631                 {
  1632                     Log.Error("ContextMenuKeyImport_Click: Error getting own identity.");
  1633                 }
  1634             }
  1635             catch (Exception ex)
  1636             {
  1637                 Log.Error("ContextMenuKeyImport_Click: Error opening wizard. " + ex.ToString());
  1638             }
  1639             finally
  1640             {
  1641                 store = null;
  1642             }
  1643         }
  1644 
  1645         /// <summary>
  1646         /// Event handler for when the reset contact context menu button is clicked.
  1647         /// </summary>
  1648         public void ContextMenuResetContactTrustButton_Click(Office.IRibbonControl control)
  1649         {
  1650             Outlook.Selection selection;
  1651             Outlook.ContactItem contact;
  1652 
  1653             try
  1654             {
  1655                 // Get the selected contact(s) and reset their trust
  1656                 selection = control.Context as Outlook.Selection;
  1657 
  1658                 for (int i = 1; i <= selection.Count; i++)
  1659                 {
  1660                     contact = selection[i];
  1661                     string id = contact.EntryID;
  1662                     ThisAddIn.PEPEngine.KeyResetUser(id, null);
  1663                     contact = null;
  1664                 }
  1665             }
  1666             catch (Exception ex)
  1667             {
  1668                 Log.Error("ContextMenuResetContactTrustButton_Click: Error occured. " + ex.ToString());
  1669             }
  1670             finally
  1671             {
  1672                 contact = null;
  1673                 selection = null;
  1674             }
  1675         }
  1676 
  1677         /**************************************************************
  1678          *
  1679          * Event Handling (Backstage)
  1680          *
  1681          *************************************************************/
  1682 
  1683         /// <summary>
  1684         /// Event handler for when the accounts button is clicked.
  1685         /// This will open the options form.
  1686         /// </summary>
  1687         public void ButtonAccounts_Click(Office.IRibbonControl control)
  1688         {
  1689             this.ShowOptions(FormControlOptions.State.OptionPages.Accounts);
  1690         }
  1691 
  1692         /// <summary>
  1693         /// Event handler for when the compatiblity button is clicked.
  1694         /// This will open the options form.
  1695         /// </summary>
  1696         public void ButtonCompatibility_Click(Office.IRibbonControl control)
  1697         {
  1698             this.ShowOptions(FormControlOptions.State.OptionPages.Compatibility);
  1699         }
  1700 
  1701         /// <summary>
  1702         /// Event handler for when the about button is clicked.
  1703         /// This will open the options form.
  1704         /// </summary>
  1705         public void ButtonAbout_Click(Office.IRibbonControl control)
  1706         {
  1707             this.ShowOptions(FormControlOptions.State.OptionPages.About);
  1708         }
  1709 
  1710         #region IRibbonExtensibility Members
  1711 
  1712         public string GetCustomUI(string ribbonID)
  1713         {
  1714             switch (ribbonID)
  1715             {
  1716                 case "Microsoft.Outlook.Explorer":
  1717                     if (Globals.OutlookVersion == Globals.Version.Outlook2010)
  1718                     {
  1719                         return GetResourceText("pEp.UI.RibbonCustomizationsExplorer2010.xml");
  1720                     }
  1721                     else
  1722                     {
  1723                         return GetResourceText("pEp.UI.RibbonCustomizationsExplorer.xml");
  1724                     }
  1725                 case "Microsoft.Outlook.Mail.Compose":
  1726                     return GetResourceText("pEp.UI.RibbonCustomizationsCompose.xml");
  1727                 case "Microsoft.Outlook.Mail.Read":
  1728                     return GetResourceText("pEp.UI.RibbonCustomizationsRead.xml");
  1729                 default:
  1730                     return GetResourceText("pEp.UI.RibbonCustomizations.xml");
  1731             }
  1732         }
  1733 
  1734         #endregion
  1735 
  1736         #region Helpers
  1737 
  1738         private static string GetResourceText(string resourceName)
  1739         {
  1740             Assembly asm = Assembly.GetExecutingAssembly();
  1741             string[] resourceNames = asm.GetManifestResourceNames();
  1742             for (int i = 0; i < resourceNames.Length; ++i)
  1743             {
  1744                 if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
  1745                 {
  1746                     using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
  1747                     {
  1748                         if (resourceReader != null)
  1749                         {
  1750                             return resourceReader.ReadToEnd();
  1751                         }
  1752                     }
  1753                 }
  1754             }
  1755             return null;
  1756         }
  1757 
  1758         #endregion
  1759     }
  1760 }