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