1.1 --- a/Server/DisplaySettings.cs Sat Jul 23 19:22:56 2016 +0200
1.2 +++ b/Server/DisplaySettings.cs Sun Jul 24 13:30:08 2016 +0200
1.3 @@ -28,6 +28,7 @@
1.4 using System.Runtime.Serialization.Json;
1.5 using System.IO;
1.6 using System.Drawing;
1.7 +using SharpLib.Utils;
1.8
1.9 namespace SharpDisplayManager
1.10 {
1.11 @@ -114,10 +115,12 @@
1.12 /// <summary>
1.13 /// Contain settings for each of our display type.
1.14 /// </summary>
1.15 - [TypeConverter(typeof(DisplaySettingsConverter))]
1.16 + [TypeConverter(typeof(TypeConverterJson<DisplaysSettings>))]
1.17 [DataContract]
1.18 public class DisplaysSettings
1.19 {
1.20 + private List<DisplaySettings> iDisplays;
1.21 +
1.22 public DisplaysSettings()
1.23 {
1.24 Init();
1.25 @@ -125,9 +128,9 @@
1.26
1.27 public void Init()
1.28 {
1.29 - if (Displays == null)
1.30 + if (iDisplays == null)
1.31 {
1.32 - Displays = new List<DisplaySettings>();
1.33 + iDisplays = new List<DisplaySettings>();
1.34 }
1.35 }
1.36
1.37 @@ -135,50 +138,9 @@
1.38 //public int CurrentSettingsIndex { get; set; }
1.39
1.40 [DataMember]
1.41 - public List<DisplaySettings> Displays { get; set; }
1.42 + public List<DisplaySettings> Displays { get { Init(); return iDisplays; } private set { iDisplays = value; } }
1.43
1.44 - public override string ToString()
1.45 - {
1.46 - //Save settings into JSON string
1.47 - MemoryStream stream = new MemoryStream();
1.48 - DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
1.49 - ser.WriteObject(stream, this);
1.50 - // convert stream to string
1.51 - stream.Position = 0;
1.52 - StreamReader reader = new StreamReader(stream);
1.53 - string text = reader.ReadToEnd();
1.54 - return text;
1.55 - }
1.56 +
1.57 }
1.58 -
1.59 - public class DisplaySettingsConverter : TypeConverter
1.60 - {
1.61 - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
1.62 - {
1.63 - if (sourceType == typeof(string))
1.64 - return true;
1.65 - else
1.66 - return base.CanConvertFrom(context, sourceType);
1.67 - }
1.68 -
1.69 - public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
1.70 - {
1.71 - string stringValue = value as string;
1.72 - if (stringValue != null)
1.73 - {
1.74 - //Load settings form JSON string
1.75 - byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
1.76 - MemoryStream stream = new MemoryStream(byteArray);
1.77 - DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
1.78 - DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
1.79 - settings.Init();
1.80 - return settings;
1.81 - }
1.82 - else
1.83 - return base.ConvertFrom(context, culture, value);
1.84 - }
1.85 - };
1.86 -
1.87 -
1.88 }
1.89