summaryrefslogtreecommitdiffstats
path: root/ComicRackWebViewer/GlobalSettings.cs
blob: d3d6cefc00f2c35e7747d18df9403d394df190b8 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Data.SQLite;

namespace BCR
{

  public class GlobalSettings
  {
    public bool nancy_request_tracing { get; set; }
    public string nancy_diagnostics_password { get; set; }
    public int webserver_port { get; set; }
    public string url_base { get; set; }
            
    private Dictionary<string, string> mSettings = new Dictionary<string, string>();
    // TODO: maximum image size should be per requesting target device instead of using one global setting.

      
    public GlobalSettings()
    {
      nancy_request_tracing = true;
      nancy_diagnostics_password = "diagnostics";
      webserver_port = 8080;
      url_base = "tablet";
    }
    
    /// <summary>
    /// Read global settings.   
    /// </summary>
    public void Initialize()
    {
      mSettings.Clear();

      using (SQLiteDataReader reader = Database.Instance.ExecuteReader("SELECT key, value FROM settings;"))
      {
        while (reader.Read())
        {
          mSettings[reader["key"].ToString()] = reader["value"].ToString();
        }
      }
      
      webserver_port = GetInt32("webserver_port", 8080);
      nancy_request_tracing = GetBoolean("nancy_request_tracing", true);
      nancy_diagnostics_password = GetString("nancy_diagnostics_password", "diagnostics");
      url_base = GetString("url_base", "tablet");
    }
    
    public void Save()
    {
      // Save to SQLite
      Set("webserver_port", webserver_port.ToString());
      Set("nancy_request_tracing", nancy_request_tracing.ToString());
      Set("nancy_diagnostics_password", nancy_diagnostics_password.ToString());
      Set("url_base", url_base);
    }
    
    
    public string GetString(string key, string defaultValue)
    {
      string value;
      if (mSettings.TryGetValue(key, out value))
        return value;
      
      Set(key, defaultValue);
      return defaultValue;
    }
    
    public int GetInt32(string key, int defaultValue)
    {
      string s;
      if (mSettings.TryGetValue(key, out s))
      {
        return Convert.ToInt32(s);
      }
      
      Set(key, defaultValue.ToString());
      return defaultValue;
    }
    
    public bool GetBoolean(string key, bool defaultValue)
    {
      string s;
      if (mSettings.TryGetValue(key, out s))
      {
        return Convert.ToBoolean(s);
      }
      
      Set(key, defaultValue.ToString());
      return defaultValue;
    }
    
    public void Set(string key, string value)
    {
      string test;
      if (mSettings.TryGetValue(key, out test))
      {
        Database.Instance.ExecuteNonQuery("UPDATE settings SET value='" + value + "' WHERE key='"+key+"';");
      }
      else
      {
        Database.Instance.ExecuteNonQuery("INSERT INTO settings (key,value) VALUES ('"+key+"','"+value+"');");
      }
      
      mSettings[key] = value;
    }
    
    
  }

     
    
}