-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProgramState.cs
70 lines (65 loc) · 1.89 KB
/
ProgramState.cs
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using static System.Net.Mime.MediaTypeNames;
namespace CropperDeck {
public class ProgramState {
public const string Path = "config.json";
public DeckColors CustomColors = new DeckColors();
public bool AutoHideOnDeckOpen = true;
public bool CheckForUpdates = true;
public double LastUpdateCheck = 0;
public string RemoteVersion = null;
public ProgramState() {
//
}
public void InitMissing() {
}
public static ProgramState CreateDefault() {
var state = new ProgramState();
state.InitMissing();
return state;
}
public void Acquire(DeckPicker deckPicker) {
CustomColors = deckPicker.CustomColors;
CustomColors.Enabled = deckPicker.CbCustomColors.Checked;
AutoHideOnDeckOpen = deckPicker.CbAutoHide.Checked;
}
public void Apply(DeckPicker deckPicker) {
deckPicker.CustomColors = CustomColors;
deckPicker.CbCustomColors.Checked = CustomColors.Enabled;
deckPicker.CbAutoHide.Checked = AutoHideOnDeckOpen;
}
public void Save() {
var text = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(Path, text, Encoding.UTF8);
}
public bool TrySave() {
try {
Save();
return true;
} catch (Exception ex) {
MessageBox.Show(ex.ToString(), "Error saving configuration!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public static ProgramState Load() {
if (File.Exists(Path)) {
try {
var text = File.ReadAllText(Path);
var state = JsonConvert.DeserializeObject<ProgramState>(text);
state.InitMissing();
return state;
} catch (Exception e) {
MessageBox.Show(e.ToString(), "Error loading configuration!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return null;
}
}
}