File size: 2,563 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace SlobViewer
{
  public class Settings
  {
    public List<string> DictionaryFileNames { get; private set; }

    public bool IsInDarkMode { get; set; }

    public Settings()
    {
      DictionaryFileNames = new List<string>();
    }

    public Settings(XmlReader tr)
    {
      LoadXml(tr);
    }

    public void SaveXml(XmlWriter tw)
    {
      tw.WriteStartElement("DictionarySettings");
      tw.WriteAttributeString("Version", "1");

      {
        tw.WriteStartElement("Dictionaries");
        tw.WriteAttributeString("Count", XmlConvert.ToString(DictionaryFileNames.Count));
        {
          foreach (var fileName in DictionaryFileNames)
          {
            tw.WriteStartElement("Dictionary");
            tw.WriteAttributeString("AbsolutePath", fileName);
            tw.WriteAttributeString("RelativePath", PathResolver.GetPathRelativeToEntryAssembly(fileName));
            tw.WriteEndElement();
          }
        }
        tw.WriteEndElement();//Dictionaries

        tw.WriteElementString("BlackTheme", XmlConvert.ToString(IsInDarkMode));
      }

      tw.WriteEndElement(); // DictionarySettings
    }

    public void LoadXml(XmlReader tr)
    {
      DictionaryFileNames = DictionaryFileNames ?? new List<string>();
      DictionaryFileNames.Clear();

      var version = tr.GetAttribute("Version");
      tr.ReadStartElement("DictionarySettings");
      {
        var dictCount = XmlConvert.ToInt32(tr.GetAttribute("Count"));
        tr.ReadStartElement("Dictionaries");
        {
          for (int i = 0; i < dictCount; ++i)
          {
            var absolutePath = tr.GetAttribute("AbsolutePath");
            var relativePath = tr.GetAttribute("RelativePath");
            tr.ReadStartElement("Dictionary"); // dictionary is empty - so no ReadEndElement
            var resolvedPath = PathResolver.ResolvePathRelativeToEntryAssembly(absolutePath, relativePath);
            if (!string.IsNullOrEmpty(resolvedPath))
              DictionaryFileNames.Add(resolvedPath);
          }
        }
        if (dictCount > 0)
          tr.ReadEndElement(); // Dictionaries

        IsInDarkMode = tr.ReadElementContentAsBoolean("BlackTheme", string.Empty);
      }
      tr.ReadEndElement(); // DictionarySettings

    }
  }
}