-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettings.cs
169 lines (150 loc) · 5.81 KB
/
Settings.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**************************************************************************
Copyright (C) 2011-2014 Iván Costales Suárez
This file is part of CompactView.
CompactView is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompactView is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompactView. If not, see <http://www.gnu.org/licenses/>.
CompactView web site <http://sourceforge.net/p/compactview/>.
**************************************************************************/
using System;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace CompactView
{
public class Settings
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public bool Maximized { get; set; }
public int TextColor1 { get; set; }
public int TextColor2 { get; set; }
public int BackColor1 { get; set; }
public int BackColor2 { get; set; }
public int ColorSet { get; set; }
public StringCollection RecentFiles { get; set; }
public int MaxRecentFiles { get; set; }
public Settings()
{
MaxRecentFiles = 10;
RecentFiles = new StringCollection();
}
public bool AddToRecentFiles(string fileName)
{
int i = RecentFiles.IndexOf(fileName);
if (i >= 0)
RecentFiles.RemoveAt(i);
while (RecentFiles.Count >= MaxRecentFiles)
RecentFiles.RemoveAt(MaxRecentFiles - 1);
RecentFiles.Insert(0, fileName);
return true;
}
public bool RemoveFromRecentFiles(string fileName)
{
int i = RecentFiles.IndexOf(fileName);
bool ok = i >= 0;
if (ok)
RecentFiles.RemoveAt(i);
return ok;
}
private string FileName
{
get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Application.ProductName + @"\Settings.xml"); }
}
public void Load()
{
var table = new DataTable("Settings");
try
{
if (!Directory.Exists(FileName))
Directory.CreateDirectory(Path.GetDirectoryName(FileName));
table.ReadXml(FileName);
}
catch
{
}
if (table.Rows.Count <= 0)
return;
DataRow row = table.Rows[0];
if (table.Columns.Contains("X"))
X = row.Field<int>("X");
if (table.Columns.Contains("Y"))
Y = row.Field<int>("Y");
if (table.Columns.Contains("Width"))
Width = row.Field<int>("Width");
if (table.Columns.Contains("Height"))
Height = row.Field<int>("Height");
if (table.Columns.Contains("Maximized"))
Maximized = row.Field<bool>("Maximized");
if (table.Columns.Contains("TextColor1"))
TextColor1 = row.Field<int>("TextColor1");
if (table.Columns.Contains("TextColor2"))
TextColor2 = row.Field<int>("TextColor2");
if (table.Columns.Contains("BackColor1"))
BackColor1 = row.Field<int>("BackColor1");
if (table.Columns.Contains("BackColor2"))
BackColor2 = row.Field<int>("BackColor2");
if (table.Columns.Contains("ColorSet"))
ColorSet = row.Field<int>("ColorSet");
RecentFiles.Clear();
for (int i = 1; i <= MaxRecentFiles; i++)
{
if (table.Columns.Contains($"RecentFiles{i}"))
RecentFiles.Add(row.Field<string>($"RecentFiles{i}"));
}
}
public void Save()
{
var table = new DataTable("Settings");
table.Columns.Add("X", typeof(int));
table.Columns.Add("Y", typeof(int));
table.Columns.Add("Width", typeof(int));
table.Columns.Add("Height", typeof(int));
table.Columns.Add("Maximized", typeof(bool));
table.Columns.Add("TextColor1", typeof(int));
table.Columns.Add("TextColor2", typeof(int));
table.Columns.Add("BackColor1", typeof(int));
table.Columns.Add("BackColor2", typeof(int));
table.Columns.Add("ColorSet", typeof(int));
for (int i = 1; i <= RecentFiles.Count; i++)
table.Columns.Add($"RecentFiles{i}", typeof(string));
DataRow row = table.NewRow();
row[0] = X;
row[1] = Y;
row[2] = Width;
row[3] = Height;
row[4] = Maximized;
row[5] = TextColor1;
row[6] = TextColor2;
row[7] = BackColor1;
row[8] = BackColor2;
row[9] = ColorSet;
for (int i = 0; i < RecentFiles.Count; i++)
row[i + 10] = RecentFiles[i];
table.Rows.Add(row);
try
{
using (var xw = new XmlTextWriter(FileName, Encoding.UTF8))
{
xw.Formatting = Formatting.Indented;
table.WriteXml(xw, XmlWriteMode.WriteSchema);
}
}
catch
{
}
}
}
}