Extensions/ContactItemExtensions.cs
author Thomas
Tue, 03 Apr 2018 10:17:13 +0200
branchOUT-369
changeset 2100 91f72cbed562
parent 1516 0a838ded02dd
child 1918 61381aa76302
child 2599 4a54e3221729
permissions -rw-r--r--
Close branch OUT-369
     1 ´╗┐using System.Runtime.InteropServices;
     2 using Outlook = Microsoft.Office.Interop.Outlook;
     3 
     4 namespace pEp
     5 {
     6     /// <summary>
     7     /// Contains extensions for the ContactItem specific for pEp.
     8     /// </summary>
     9     internal static class ContactItemExtensions
    10     {
    11         /// <summary>
    12         /// Gets whether the given contact should be forcefully sent unencrypted messages.
    13         /// This will read the value of the user property.
    14         /// </summary>
    15         /// <param name="contact">The contact item to process with.</param>
    16         /// <returns>True if the mail item is force unencrypted, otherwise false or null.</returns>
    17         public static bool? GetForceUnencrypted(this Outlook.ContactItem contact)
    18         {
    19             bool? value = null;
    20             Outlook.UserProperties properties;
    21             Outlook.UserProperty up;
    22 
    23             if (Globals.ThisAddIn.Settings.IsDisableProtectionForContactsEnabled)
    24             {
    25                 properties = contact.UserProperties;
    26                 up = properties.Find(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED);
    27 
    28                 if (up == null)
    29                 {
    30                     value = null;
    31                 }
    32                 else
    33                 {
    34                     value = up.Value;
    35                 }
    36 
    37                 // Release objects
    38                 if (properties != null)
    39                 {
    40                     // Marshal.ReleaseComObject(properties);
    41                     properties = null;
    42                 }
    43 
    44                 if (up != null)
    45                 {
    46                     // Marshal.ReleaseComObject(up);
    47                     up = null;
    48                 }
    49             }
    50 
    51             return (value);
    52         }
    53 
    54         /// <summary>
    55         /// Sets whether the given contact should be forcefully sent unencrypted messages.
    56         /// This will set the value of the user property.
    57         /// </summary>
    58         /// <param name="contact">The contact item to process with.</param>
    59         /// <param name="forceUnencrypted">The force unencrypted status to set.</param>
    60         public static void SetForceUnencrypted(this Outlook.ContactItem contact,
    61                                                bool forceUnencrypted)
    62         {
    63             Outlook.UserProperties properties = contact.UserProperties;
    64             Outlook.UserProperty up = properties.Find(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED);
    65 
    66             if (up == null)
    67             {
    68                 up = contact.UserProperties.Add(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED,
    69                                                 Outlook.OlUserPropertyType.olYesNo, false);
    70             }
    71 
    72             up.Value = forceUnencrypted;
    73 
    74             // Release objects
    75             if (properties != null)
    76             {
    77                 // Marshal.ReleaseComObject(properties);
    78                 properties = null;
    79             }
    80 
    81             if (up != null)
    82             {
    83                 // Marshal.ReleaseComObject(up);
    84                 up = null;
    85             }
    86 
    87             return;
    88         }
    89     }
    90 }