Server/DisplaySettings.cs
author sl
Sun, 18 Jan 2015 19:31:13 +0100
changeset 98 7f72edef7ca3
parent 50 ba4cad33db58
child 100 7e02ac8b13ba
permissions -rw-r--r--
Version.
sl@49
     1
using System;
sl@49
     2
using System.Collections;
sl@49
     3
using System.Collections.Generic;
sl@49
     4
using System.Text;
sl@49
     5
using System.Configuration;
sl@49
     6
using System.Xml;
sl@49
     7
using System.ComponentModel;
sl@49
     8
using System.Runtime.Serialization;
sl@49
     9
using System.Runtime.Serialization.Json;
sl@49
    10
using System.IO;
sl@49
    11
using System.Drawing;
sl@49
    12
sl@49
    13
namespace SharpDisplayManager
sl@49
    14
{
sl@49
    15
    /// <summary>
sl@50
    16
    /// Display settings for the specified hardware type
sl@49
    17
    /// </summary>
sl@49
    18
    [DataContract]
sl@50
    19
    public class DisplaySettings
sl@49
    20
    {
sl@50
    21
        public DisplaySettings()
sl@49
    22
        {
sl@49
    23
            Brightness = 1;
sl@49
    24
            DisplayType = 0;
sl@49
    25
            TimerInterval = 150;
sl@49
    26
            ReverseScreen = false;
sl@57
    27
            InverseColors = true;
sl@49
    28
            ShowBorders = false;
sl@49
    29
            FontName = "Microsoft Sans Serif, 9.75pt";
sl@49
    30
        }
sl@49
    31
sl@49
    32
        [DataMember]
sl@49
    33
        public int Brightness { get; set; }
sl@49
    34
sl@50
    35
        /// <summary>
sl@50
    36
        /// See Display.TMiniDisplayType
sl@50
    37
        /// </summary>
sl@49
    38
        [DataMember]
sl@49
    39
        public int DisplayType { get; set; }
sl@49
    40
sl@49
    41
        [DataMember]
sl@49
    42
        public int TimerInterval { get; set; }
sl@49
    43
sl@49
    44
        [DataMember]
sl@49
    45
        public bool ReverseScreen { get; set; }
sl@49
    46
sl@49
    47
        [DataMember]
sl@57
    48
        public bool InverseColors { get; set; }
sl@57
    49
sl@57
    50
        [DataMember]
sl@49
    51
        public bool ShowBorders { get; set; }
sl@49
    52
sl@49
    53
        [DataMember]
sl@49
    54
        public string FontName { get; set; }
sl@49
    55
sl@49
    56
        public Font Font
sl@49
    57
        {
sl@49
    58
            get
sl@49
    59
            {
sl@49
    60
                FontConverter cvt = new FontConverter();
sl@49
    61
                Font font = cvt.ConvertFromInvariantString(FontName) as Font;
sl@49
    62
                return font;
sl@49
    63
            }
sl@49
    64
sl@49
    65
            set
sl@49
    66
            {
sl@49
    67
                FontConverter cvt = new FontConverter();
sl@49
    68
                FontName = cvt.ConvertToInvariantString(value);
sl@49
    69
            }
sl@49
    70
        }
sl@49
    71
    };
sl@49
    72
sl@49
    73
sl@50
    74
    /// <summary>
sl@50
    75
    /// Contain settings for each of our display type. 
sl@50
    76
    /// </summary>
sl@49
    77
    [TypeConverter(typeof(DisplaySettingsConverter))]
sl@49
    78
    [DataContract]
sl@50
    79
    public class DisplaysSettings
sl@49
    80
    {
sl@50
    81
        public DisplaysSettings()
sl@49
    82
        {
sl@49
    83
            Init();
sl@49
    84
        }
sl@49
    85
sl@49
    86
        public void Init()
sl@49
    87
        {
sl@49
    88
            if (Displays == null)
sl@49
    89
            {
sl@50
    90
                Displays = new List<DisplaySettings>();
sl@49
    91
            }
sl@49
    92
        }
sl@49
    93
sl@49
    94
        //[DataMember]
sl@49
    95
        //public int CurrentSettingsIndex { get; set; }
sl@49
    96
sl@49
    97
        [DataMember]
sl@50
    98
        public List<DisplaySettings> Displays { get; set; }
sl@49
    99
sl@49
   100
        public override string ToString()
sl@49
   101
        {
sl@49
   102
            //Save settings into JSON string
sl@49
   103
            MemoryStream stream = new MemoryStream();
sl@50
   104
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@49
   105
            ser.WriteObject(stream, this);
sl@49
   106
            // convert stream to string
sl@49
   107
            stream.Position = 0;
sl@49
   108
            StreamReader reader = new StreamReader(stream);
sl@49
   109
            string text = reader.ReadToEnd();
sl@49
   110
            return text;
sl@49
   111
        }
sl@49
   112
    }
sl@49
   113
sl@49
   114
    public class DisplaySettingsConverter : TypeConverter
sl@49
   115
    {
sl@49
   116
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
sl@49
   117
        {
sl@49
   118
            if (sourceType == typeof(string))
sl@49
   119
                return true;
sl@49
   120
            else
sl@49
   121
                return base.CanConvertFrom(context, sourceType);
sl@49
   122
        }
sl@49
   123
sl@49
   124
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
sl@49
   125
        {
sl@49
   126
            string stringValue = value as string;
sl@49
   127
            if (stringValue != null)
sl@49
   128
            {   
sl@49
   129
                //Load settings form JSON string
sl@49
   130
                byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
sl@49
   131
                MemoryStream stream = new MemoryStream(byteArray);
sl@50
   132
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@50
   133
                DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
sl@49
   134
                settings.Init();
sl@49
   135
                return settings;
sl@49
   136
            }
sl@49
   137
            else
sl@49
   138
                return base.ConvertFrom(context, culture, value);
sl@49
   139
        }
sl@49
   140
    };
sl@49
   141
sl@49
   142
sl@49
   143
}
sl@49
   144