Example #1
0
    /// <summary>
    /// Gets the type code.
    /// </summary>
    /// <param name="service">The service.</param>
    /// <param name="dictionaryObject">The dictionary object.</param>
    /// <param name="dictionaryObjectType">Type of the dictionary object.</param>
    /// <param name="valueAsString">The value as string.</param>
    /* private */ static void GetTypeCode(ExchangeServiceBase service, object dictionaryObject, ref UserConfigurationDictionaryObjectType dictionaryObjectType, ref String valueAsString)
    {
        // Handle all other types by TypeCode
        switch (Type.GetTypeCode(dictionaryObject.GetType()))
        {
        case TypeCode.Boolean:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.Boolean;
            valueAsString        = EwsUtilities.BoolToXSBool((bool)dictionaryObject);
            break;

        case TypeCode.Byte:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.Byte;
            valueAsString        = ((byte)dictionaryObject).ToString();
            break;

        case TypeCode.DateTime:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime;
            valueAsString        = service.ConvertDateTimeToUniversalDateTimeString((DateTime)dictionaryObject);
            break;

        case TypeCode.Int32:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer32;
            valueAsString        = ((int)dictionaryObject).ToString();
            break;

        case TypeCode.Int64:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer64;
            valueAsString        = ((long)dictionaryObject).ToString();
            break;

        case TypeCode.String:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.String;
            valueAsString        = (string)dictionaryObject;
            break;

        case TypeCode.UInt32:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger32;
            valueAsString        = ((uint)dictionaryObject).ToString();
            break;

        case TypeCode.UInt64:
            dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger64;
            valueAsString        = ((ulong)dictionaryObject).ToString();
            break;

        default:
            EwsUtilities.Assert(
                false,
                "UserConfigurationDictionary.WriteObjectValueToXml",
                "Unsupported type: " + dictionaryObject.GetType().ToString());
            break;
        }
    }
        /// <summary>
        /// Gets the type code.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="dictionaryObject">The dictionary object.</param>
        /// <param name="dictionaryObjectType">Type of the dictionary object.</param>
        /// <param name="valueAsString">The value as string.</param>
        private static void GetTypeCode(ExchangeServiceBase service, object dictionaryObject, ref UserConfigurationDictionaryObjectType dictionaryObjectType, ref string valueAsString)
        {
            // Handle all other types by TypeCode
            Type type = dictionaryObject.GetType();

            if (type == typeof(bool))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.Boolean;
                valueAsString        = EwsUtilities.BoolToXSBool((bool)dictionaryObject);
            }
            else if (type == typeof(byte))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.Byte;
                valueAsString        = ((byte)dictionaryObject).ToString();
            }
            else if (type == typeof(DateTime))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime;
                valueAsString        = service.ConvertDateTimeToUniversalDateTimeString((DateTime)dictionaryObject);
            }
            else if (type == typeof(Int32))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer32;
                valueAsString        = ((int)dictionaryObject).ToString();
            }
            else if (type == typeof(Int64))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer64;
                valueAsString        = ((long)dictionaryObject).ToString();
            }
            else if (type == typeof(string))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.String;
                valueAsString        = (string)dictionaryObject;
            }
            else if (type == typeof(UInt32))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger32;
                valueAsString        = ((uint)dictionaryObject).ToString();
            }
            else if (type == typeof(UInt64))
            {
                dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger64;
                valueAsString        = ((ulong)dictionaryObject).ToString();
            }
            else
            {
                EwsUtilities.Assert(
                    false,
                    "UserConfigurationDictionary.WriteObjectValueToXml",
                    "Unsupported type: " + dictionaryObject.GetType().ToString());
            }
        }
    /// <summary>
    /// Writes XML elements.
    /// </summary>
    /// <param name="writer">The writer.</param>
    void WriteElementsToXml(EwsServiceXmlWriter writer)
    {
        writer.WriteStartElement(
            XmlNamespace.Types,
            XmlElementNames.ConversationAction);
        try
        {
            String actionValue = String.Empty;
            switch (this.Action)
            {
            case ConversationActionType.AlwaysCategorize:
                actionValue = XmlElementNames.AlwaysCategorize;
                break;

            case ConversationActionType.AlwaysDelete:
                actionValue = XmlElementNames.AlwaysDelete;
                break;

            case ConversationActionType.AlwaysMove:
                actionValue = XmlElementNames.AlwaysMove;
                break;

            case ConversationActionType.Delete:
                actionValue = XmlElementNames.Delete;
                break;

            case ConversationActionType.Copy:
                actionValue = XmlElementNames.Copy;
                break;

            case ConversationActionType.Move:
                actionValue = XmlElementNames.Move;
                break;

            case ConversationActionType.SetReadState:
                actionValue = XmlElementNames.SetReadState;
                break;

            case ConversationActionType.SetRetentionPolicy:
                actionValue = XmlElementNames.SetRetentionPolicy;
                break;

            case ConversationActionType.Flag:
                actionValue = XmlElementNames.Flag;
                break;

            default:
                throw new ArgumentError("ConversationAction");
            }

            // Emit the action element
            writer.WriteElementValue(
                XmlNamespace.Types,
                XmlElementNames.Action,
                actionValue);

            // Emit the conversation id element
            this.ConversationId.WriteToXml(
                writer,
                XmlNamespace.Types,
                XmlElementNames.ConversationId);

            if (this.Action == ConversationActionType.AlwaysCategorize ||
                this.Action == ConversationActionType.AlwaysDelete ||
                this.Action == ConversationActionType.AlwaysMove)
            {
                // Emit the ProcessRightAway element
                writer.WriteElementValue(
                    XmlNamespace.Types,
                    XmlElementNames.ProcessRightAway,
                    EwsUtilities.BoolToXSBool(this.ProcessRightAway));
            }

            if (this.Action == ConversationActionType.AlwaysCategorize)
            {
                // Emit the categories element
                if (this.Categories != null && this.Categories.Count > 0)
                {
                    this.Categories.WriteToXml(
                        writer,
                        XmlNamespace.Types,
                        XmlElementNames.Categories);
                }
            }
            else if (this.Action == ConversationActionType.AlwaysDelete)
            {
                // Emit the EnableAlwaysDelete element
                writer.WriteElementValue(
                    XmlNamespace.Types,
                    XmlElementNames.EnableAlwaysDelete,
                    EwsUtilities.BoolToXSBool(this.EnableAlwaysDelete));
            }
            else if (this.Action == ConversationActionType.AlwaysMove)
            {
                // Emit the Move Folder Id
                if (this.DestinationFolderId != null)
                {
                    writer.WriteStartElement(XmlNamespace.Types, XmlElementNames.DestinationFolderId);
                    this.DestinationFolderId.WriteToXml(writer);
                    writer.WriteEndElement();
                }
            }
            else
            {
                if (this.ContextFolderId != null)
                {
                    writer.WriteStartElement(
                        XmlNamespace.Types,
                        XmlElementNames.ContextFolderId);

                    this.ContextFolderId.WriteToXml(writer);

                    writer.WriteEndElement();
                }

                if (this.ConversationLastSyncTime.HasValue)
                {
                    writer.WriteElementValue(
                        XmlNamespace.Types,
                        XmlElementNames.ConversationLastSyncTime,
                        this.ConversationLastSyncTime.Value);
                }

                if (this.Action == ConversationActionType.Copy)
                {
                    EwsUtilities.Assert(
                        this.DestinationFolderId != null,
                        "ApplyconversationActionRequest",
                        "DestinationFolderId should be set when performing copy action");

                    writer.WriteStartElement(
                        XmlNamespace.Types,
                        XmlElementNames.DestinationFolderId);
                    this.DestinationFolderId.WriteToXml(writer);
                    writer.WriteEndElement();
                }
                else if (this.Action == ConversationActionType.Move)
                {
                    EwsUtilities.Assert(
                        this.DestinationFolderId != null,
                        "ApplyconversationActionRequest",
                        "DestinationFolderId should be set when performing move action");

                    writer.WriteStartElement(
                        XmlNamespace.Types,
                        XmlElementNames.DestinationFolderId);
                    this.DestinationFolderId.WriteToXml(writer);
                    writer.WriteEndElement();
                }
                else if (this.Action == ConversationActionType.Delete)
                {
                    EwsUtilities.Assert(
                        this.DeleteType.HasValue,
                        "ApplyconversationActionRequest",
                        "DeleteType should be specified when deleting a conversation.");

                    writer.WriteElementValue(
                        XmlNamespace.Types,
                        XmlElementNames.DeleteType,
                        this.DeleteType.Value);
                }
                else if (this.Action == ConversationActionType.SetReadState)
                {
                    EwsUtilities.Assert(
                        this.IsRead.HasValue,
                        "ApplyconversationActionRequest",
                        "IsRead should be specified when marking/unmarking a conversation as read.");

                    writer.WriteElementValue(
                        XmlNamespace.Types,
                        XmlElementNames.IsRead,
                        this.IsRead.Value);

                    if (this.SuppressReadReceipts.HasValue)
                    {
                        writer.WriteElementValue(
                            XmlNamespace.Types,
                            XmlElementNames.SuppressReadReceipts,
                            this.SuppressReadReceipts.Value);
                    }
                }
                else if (this.Action == ConversationActionType.SetRetentionPolicy)
                {
                    EwsUtilities.Assert(
                        this.RetentionPolicyType.HasValue,
                        "ApplyconversationActionRequest",
                        "RetentionPolicyType should be specified when setting a retention policy on a conversation.");

                    writer.WriteElementValue(
                        XmlNamespace.Types,
                        XmlElementNames.RetentionPolicyType,
                        this.RetentionPolicyType.Value);

                    if (this.RetentionPolicyTagId.HasValue)
                    {
                        writer.WriteElementValue(
                            XmlNamespace.Types,
                            XmlElementNames.RetentionPolicyTagId,
                            this.RetentionPolicyTagId.Value);
                    }
                }
                else if (this.Action == ConversationActionType.Flag)
                {
                    EwsUtilities.Assert(
                        this.Flag != null,
                        "ApplyconversationActionRequest",
                        "Flag should be specified when flagging conversation items.");

                    writer.WriteStartElement(XmlNamespace.Types, XmlElementNames.Flag);
                    this.Flag.WriteElementsToXml(writer);
                    writer.WriteEndElement();
                }
            }
        }
        finally
        {
            writer.WriteEndElement();
        }
    }
Example #4
0
        /// <summary>
        /// Try to convert object to a string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="strValue">The string representation of value.</param>
        /// <returns>True if object was converted, false otherwise.</returns>
        /// <remarks>A null object will be "successfully" converted to a null string.</remarks>
        internal bool TryConvertObjectToString(object value, out string strValue)
        {
            strValue = null;
            bool converted = true;

            if (value != null)
            {
                // All value types should implement IConvertible. There are a couple of special cases
                // that need to be handled directly. Otherwise use IConvertible.ToString()
                IConvertible convertible = value as IConvertible;
                if (value.GetType().IsEnum)
                {
                    strValue = EwsUtilities.SerializeEnum((Enum)value);
                }
                else if (convertible != null)
                {
                    switch (convertible.GetTypeCode())
                    {
                    case TypeCode.Boolean:
                        strValue = EwsUtilities.BoolToXSBool((bool)value);
                        break;

                    case TypeCode.DateTime:
                        strValue = this.Service.ConvertDateTimeToUniversalDateTimeString((DateTime)value);
                        break;

                    default:
                        strValue = convertible.ToString(CultureInfo.InvariantCulture);
                        break;
                    }
                }
                else
                {
                    // If the value type doesn't implement IConvertible but implements IFormattable, use its
                    // ToString(format,formatProvider) method to convert to a string.
                    IFormattable formattable = value as IFormattable;
                    if (formattable != null)
                    {
                        // Null arguments mean that we use default format and default locale.
                        strValue = formattable.ToString(null, null);
                    }
                    else if (value is ISearchStringProvider)
                    {
                        // If the value type doesn't implement IConvertible or IFormattable but implements
                        // ISearchStringProvider convert to a string.
                        // Note: if a value type implements IConvertible or IFormattable we will *not* check
                        // to see if it also implements ISearchStringProvider. We'll always use its IConvertible.ToString
                        // or IFormattable.ToString method.
                        ISearchStringProvider searchStringProvider = value as ISearchStringProvider;
                        strValue = searchStringProvider.GetSearchString();
                    }
                    else if (value is byte[])
                    {
                        // Special case for byte arrays. Convert to Base64-encoded string.
                        strValue = Convert.ToBase64String((byte[])value);
                    }
                    else
                    {
                        converted = false;
                    }
                }
            }

            return(converted);
        }
 /// <summary>
 /// Convert instance to string.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <returns>String representation of Boolean property.</returns>
 internal override string ToString(object value)
 {
     return(EwsUtilities.BoolToXSBool((bool)value));
 }