Server/DisplaySettings.cs
author StephaneLenclud
Wed, 02 Sep 2015 16:02:24 +0200
changeset 153 95f253aaf588
parent 115 5c61a13c4241
child 212 1a0791daa243
permissions -rw-r--r--
Persisting selection of optical drive to eject.
     1 //
     2 // Copyright (C) 2014-2015 Stéphane Lenclud.
     3 //
     4 // This file is part of SharpDisplayManager.
     5 //
     6 // SharpDisplayManager is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // SharpDisplayManager is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 using System;
    21 using System.Collections;
    22 using System.Collections.Generic;
    23 using System.Text;
    24 using System.Configuration;
    25 using System.Xml;
    26 using System.ComponentModel;
    27 using System.Runtime.Serialization;
    28 using System.Runtime.Serialization.Json;
    29 using System.IO;
    30 using System.Drawing;
    31 
    32 namespace SharpDisplayManager
    33 {
    34     /// <summary>
    35     /// Display settings for the specified hardware type
    36     /// </summary>
    37     [DataContract]
    38     public class DisplaySettings
    39     {
    40         public DisplaySettings()
    41         {
    42             Brightness = 1;
    43             DisplayType = 0;
    44             TimerInterval = 150;
    45             ReverseScreen = false;
    46             InverseColors = true;
    47             ShowBorders = false;
    48 			ShowVolumeLabel = false;
    49             FontName = "Microsoft Sans Serif, 9.75pt";
    50             ScaleToFit = true;
    51             MinFontSize = 15.0f;
    52             Separator = "   ";
    53 			ScrollingSpeedInPixelsPerSecond = 64;
    54         }
    55 
    56 
    57 		[DataMember]
    58 		public bool ShowVolumeLabel { get; set; }
    59 
    60         [DataMember]
    61         public int Brightness { get; set; }
    62 
    63         /// <summary>
    64         /// See Display.TMiniDisplayType
    65         /// </summary>
    66         [DataMember]
    67         public int DisplayType { get; set; }
    68 
    69         [DataMember]
    70         public int TimerInterval { get; set; }
    71 
    72 		[DataMember]
    73 		public int ScrollingSpeedInPixelsPerSecond { get; set; }
    74 
    75         [DataMember]
    76         public bool ReverseScreen { get; set; }
    77 
    78         [DataMember]
    79         public bool InverseColors { get; set; }
    80 
    81         [DataMember]
    82         public bool ShowBorders { get; set; }
    83 
    84         [DataMember]
    85         public bool ScaleToFit { get; set; }
    86 
    87         [DataMember]
    88         public float MinFontSize { get; set; }
    89 
    90         [DataMember]
    91         public string Separator { get; set; }
    92 
    93         [DataMember]
    94         public string FontName { get; set; }
    95 
    96         public Font Font
    97         {
    98             get
    99             {
   100                 FontConverter cvt = new FontConverter();
   101                 Font font = cvt.ConvertFromInvariantString(FontName) as Font;
   102                 return font;
   103             }
   104 
   105             set
   106             {
   107                 FontConverter cvt = new FontConverter();
   108                 FontName = cvt.ConvertToInvariantString(value);
   109             }
   110         }
   111     };
   112 
   113 
   114     /// <summary>
   115     /// Contain settings for each of our display type.
   116     /// </summary>
   117     [TypeConverter(typeof(DisplaySettingsConverter))]
   118     [DataContract]
   119     public class DisplaysSettings
   120     {
   121         public DisplaysSettings()
   122         {
   123             Init();
   124         }
   125 
   126         public void Init()
   127         {
   128             if (Displays == null)
   129             {
   130                 Displays = new List<DisplaySettings>();
   131             }
   132         }
   133 
   134         //[DataMember]
   135         //public int CurrentSettingsIndex { get; set; }
   136 
   137         [DataMember]
   138         public List<DisplaySettings> Displays { get; set; }
   139 
   140         public override string ToString()
   141         {
   142             //Save settings into JSON string
   143             MemoryStream stream = new MemoryStream();
   144             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
   145             ser.WriteObject(stream, this);
   146             // convert stream to string
   147             stream.Position = 0;
   148             StreamReader reader = new StreamReader(stream);
   149             string text = reader.ReadToEnd();
   150             return text;
   151         }
   152     }
   153 
   154     public class DisplaySettingsConverter : TypeConverter
   155     {
   156         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
   157         {
   158             if (sourceType == typeof(string))
   159                 return true;
   160             else
   161                 return base.CanConvertFrom(context, sourceType);
   162         }
   163 
   164         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
   165         {
   166             string stringValue = value as string;
   167             if (stringValue != null)
   168             {
   169                 //Load settings form JSON string
   170                 byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
   171                 MemoryStream stream = new MemoryStream(byteArray);
   172                 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
   173                 DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
   174                 settings.Init();
   175                 return settings;
   176             }
   177             else
   178                 return base.ConvertFrom(context, culture, value);
   179         }
   180     };
   181 
   182 
   183 }
   184