Server/DisplaySettings.cs
author sl
Sun, 21 Sep 2014 13:40:21 +0200
changeset 56 e86d84480b32
parent 49 eab73d41c9e6
child 57 544132d07c3b
permissions -rw-r--r--
Renaming and deleting files. Adding documentation.
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@49
    27
            ShowBorders = false;
sl@49
    28
            FontName = "Microsoft Sans Serif, 9.75pt";
sl@49
    29
        }
sl@49
    30
sl@49
    31
        [DataMember]
sl@49
    32
        public int Brightness { get; set; }
sl@49
    33
sl@50
    34
        /// <summary>
sl@50
    35
        /// See Display.TMiniDisplayType
sl@50
    36
        /// </summary>
sl@49
    37
        [DataMember]
sl@49
    38
        public int DisplayType { get; set; }
sl@49
    39
sl@49
    40
        [DataMember]
sl@49
    41
        public int TimerInterval { get; set; }
sl@49
    42
sl@49
    43
        [DataMember]
sl@49
    44
        public bool ReverseScreen { get; set; }
sl@49
    45
sl@49
    46
        [DataMember]
sl@49
    47
        public bool ShowBorders { get; set; }
sl@49
    48
sl@49
    49
        [DataMember]
sl@49
    50
        public string FontName { get; set; }
sl@49
    51
sl@49
    52
        public Font Font
sl@49
    53
        {
sl@49
    54
            get
sl@49
    55
            {
sl@49
    56
                FontConverter cvt = new FontConverter();
sl@49
    57
                Font font = cvt.ConvertFromInvariantString(FontName) as Font;
sl@49
    58
                return font;
sl@49
    59
            }
sl@49
    60
sl@49
    61
            set
sl@49
    62
            {
sl@49
    63
                FontConverter cvt = new FontConverter();
sl@49
    64
                FontName = cvt.ConvertToInvariantString(value);
sl@49
    65
            }
sl@49
    66
        }
sl@49
    67
    };
sl@49
    68
sl@49
    69
sl@50
    70
    /// <summary>
sl@50
    71
    /// Contain settings for each of our display type. 
sl@50
    72
    /// </summary>
sl@49
    73
    [TypeConverter(typeof(DisplaySettingsConverter))]
sl@49
    74
    [DataContract]
sl@50
    75
    public class DisplaysSettings
sl@49
    76
    {
sl@50
    77
        public DisplaysSettings()
sl@49
    78
        {
sl@49
    79
            Init();
sl@49
    80
        }
sl@49
    81
sl@49
    82
        public void Init()
sl@49
    83
        {
sl@49
    84
            if (Displays == null)
sl@49
    85
            {
sl@50
    86
                Displays = new List<DisplaySettings>();
sl@49
    87
            }
sl@49
    88
        }
sl@49
    89
sl@49
    90
        //[DataMember]
sl@49
    91
        //public int CurrentSettingsIndex { get; set; }
sl@49
    92
sl@49
    93
        [DataMember]
sl@50
    94
        public List<DisplaySettings> Displays { get; set; }
sl@49
    95
sl@49
    96
        public override string ToString()
sl@49
    97
        {
sl@49
    98
            //Save settings into JSON string
sl@49
    99
            MemoryStream stream = new MemoryStream();
sl@50
   100
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@49
   101
            ser.WriteObject(stream, this);
sl@49
   102
            // convert stream to string
sl@49
   103
            stream.Position = 0;
sl@49
   104
            StreamReader reader = new StreamReader(stream);
sl@49
   105
            string text = reader.ReadToEnd();
sl@49
   106
            return text;
sl@49
   107
        }
sl@49
   108
    }
sl@49
   109
sl@49
   110
    public class DisplaySettingsConverter : TypeConverter
sl@49
   111
    {
sl@49
   112
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
sl@49
   113
        {
sl@49
   114
            if (sourceType == typeof(string))
sl@49
   115
                return true;
sl@49
   116
            else
sl@49
   117
                return base.CanConvertFrom(context, sourceType);
sl@49
   118
        }
sl@49
   119
sl@49
   120
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
sl@49
   121
        {
sl@49
   122
            string stringValue = value as string;
sl@49
   123
            if (stringValue != null)
sl@49
   124
            {   
sl@49
   125
                //Load settings form JSON string
sl@49
   126
                byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
sl@49
   127
                MemoryStream stream = new MemoryStream(byteArray);
sl@50
   128
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@50
   129
                DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
sl@49
   130
                settings.Init();
sl@49
   131
                return settings;
sl@49
   132
            }
sl@49
   133
            else
sl@49
   134
                return base.ConvertFrom(context, culture, value);
sl@49
   135
        }
sl@49
   136
    };
sl@49
   137
sl@49
   138
sl@49
   139
}
sl@49
   140