-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
119 lines (98 loc) · 4 KB
/
SettingsForm.cs
File metadata and controls
119 lines (98 loc) · 4 KB
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
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace PersonalFolder {
public partial class SettingsForm : Form {
public static SettingsData data = new SettingsData();
private static string settingsPath = Environment.CurrentDirectory + "\\settings.bin";
static SettingsForm () {
data.cache = new Dictionary<string, List<string>>();
if (File.Exists(settingsPath)) {
var reader = new BinaryReader(File.OpenRead(settingsPath));
try {
data.localPath = reader.ReadString();
data.localTemplatesPath = Path.GetDirectoryName(data.localPath) + "\\#templates";
data.remotePath = reader.ReadString();
data.remoteTemplatesPath = Path.GetDirectoryName(data.remotePath) + "\\#templates";
var cachedCount = reader.ReadByte();
for (var i = 0; i < cachedCount; i++) {
var group = reader.ReadString();
var list = new List<string>();
var listCount = reader.ReadByte();
for (var j = 0; j < listCount; j++) {
list.Add(reader.ReadString());
}
data.cache.Add(group, list);
}
try {
data.lastSync = reader.ReadUInt16();
} catch {
// TODO: remove
}
data.inited = true;
} catch (Exception err) {
MessageBox.Show("Невозможно прочитать файл настроек\n" + err, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
reader.Dispose();
}
}
public SettingsForm () {
InitializeComponent();
if (data.inited) {
localPathInput.Text = data.localPath;
remotePathInput.Text = data.remotePath;
}
}
private void Cancel (object sender, EventArgs e) {
Close();
}
private void Save (object sender, EventArgs e) {
data.localPath = localPathInput.Text;
data.remotePath = remotePathInput.Text;
WriteData();
Close();
}
public static void WriteData () {
var writer = new BinaryWriter(File.OpenWrite(settingsPath));
writer.Write(data.localPath);
writer.Write(data.remotePath);
writer.Write((byte) data.cache.Count);
foreach (var pair in data.cache) {
writer.Write(pair.Key);
writer.Write((byte) pair.Value.Count);
foreach (var user in pair.Value) {
writer.Write(user);
}
}
writer.Write(data.lastSync);
writer.Dispose();
}
private void SelectLocalPath (object sender, EventArgs e) {
localPathInput.Text = OpenPathSelector(localPathInput.Text);
}
private void SelectRemotePath (object sender, EventArgs e) {
remotePathInput.Text = OpenPathSelector(remotePathInput.Text);
}
private string OpenPathSelector (string start) {
folderDialog.SelectedPath = start;
if (folderDialog.ShowDialog(this) == DialogResult.OK) {
return folderDialog.SelectedPath;
} else {
return start;
}
}
private void Import (object sender, EventArgs e) {
new ImportForm().ShowDialog(this);
}
}
public struct SettingsData {
public bool inited;
public string localPath;
public string localTemplatesPath;
public string remotePath;
public string remoteTemplatesPath;
public ushort lastSync;
public Dictionary<string, List<string>> cache;
}
}