-
Notifications
You must be signed in to change notification settings - Fork 1
/
Settings.cs
189 lines (153 loc) · 6.72 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Security.Permissions;
using System.Security;
using System.Security.Principal;
using System.Security.AccessControl;
namespace CryptotextEditor
{
internal class Settings
{
internal static System.Drawing.Size EditorSize = new System.Drawing.Size(600, 400);
internal static string CurrentFilePath = "";
internal static bool CurrentFileSaved = true;
internal static bool WordWrap = true;
internal static bool DetectUrls = true;
internal static bool AutoSave = true;
internal static string LastOpenedFile = "";
internal static System.Drawing.Point editorLocation = new System.Drawing.Point(100, 100);
internal static string editorLangPath = "lang\\english.xml";
internal static bool editorToolBox = true;
internal static readonly string tmpPath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\Cryptotext Editor";
private static string settingsPath = tmpPath + "\\settings.xml";
internal static void Load()
{
if (!System.IO.File.Exists(settingsPath))
{
Save();
}
XmlDocument xFile = new XmlDocument();
xFile.Load(settingsPath);
int editorSizeWidth = 600;
int editorSizeHeight = 400;
int editorLocationX = 100;
int editorLocationY = 100;
try
{
editorSizeWidth = Convert.ToInt32(xFile.GetElementsByTagName("EditorSize").Item(0).Attributes.GetNamedItem("Width").InnerText);
editorSizeHeight = Convert.ToInt32(xFile.GetElementsByTagName("EditorSize").Item(0).Attributes.GetNamedItem("Height").InnerText);
}
catch { }
finally
{
EditorSize = new Size(editorSizeWidth, editorSizeHeight);
}
try
{
WordWrap = Convert.ToBoolean(xFile.GetElementsByTagName("WordWrap").Item(0).InnerText);
}
catch { }
try
{
DetectUrls = Convert.ToBoolean(xFile.GetElementsByTagName("DetectUrls").Item(0).InnerText);
}
catch { }
try
{
AutoSave = Convert.ToBoolean(xFile.GetElementsByTagName("AutoSave").Item(0).InnerText);
}
catch { }
try
{
LastOpenedFile = xFile.GetElementsByTagName("LastOpenedFile").Item(0).InnerText;
}
catch { }
try
{
editorLocationX = Convert.ToInt32(xFile.GetElementsByTagName("editorLocation").Item(0).Attributes.GetNamedItem("x").InnerText);
editorLocationY = Convert.ToInt32(xFile.GetElementsByTagName("editorLocation").Item(0).Attributes.GetNamedItem("y").InnerText);
if(Screen.PrimaryScreen.Bounds.Width < editorLocationX | editorLocationX < 0)
{
editorLocationX = 0;
}
if(Screen.PrimaryScreen.Bounds.Height < editorLocationY | editorLocationY < 0)
{
editorLocationY = 0;
}
}
catch { }
finally
{
editorLocation = new Point(editorLocationX, editorLocationY);
}
try
{
editorLangPath = xFile.GetElementsByTagName("editorLangPath").Item(0).InnerText;
}
catch { }
try
{
editorToolBox = Convert.ToBoolean(xFile.GetElementsByTagName("editorToolBox").Item(0).InnerText);
}
catch { }
}
internal static void Save()
{
XmlTextWriter XMLobj;
try
{
if(!System.IO.Directory.Exists(Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\Cryptotext Editor"))
System.IO.Directory.CreateDirectory(Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\Cryptotext Editor");
XMLobj = new XmlTextWriter(settingsPath, System.Text.Encoding.UTF8);
}
catch(UnauthorizedAccessException)
{
return;
}
XMLobj.Formatting = Formatting.Indented;
XMLobj.WriteStartDocument();
XMLobj.WriteStartElement("Settings");
XMLobj.WriteAttributeString("program", "Cryptotext Editor");
XMLobj.WriteStartElement("EditorSize");
XMLobj.WriteAttributeString("Width", EditorSize.Width.ToString());
XMLobj.WriteAttributeString("Height", EditorSize.Height.ToString());
XMLobj.WriteValue(EditorSize.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("CurrentFilePath");
XMLobj.WriteValue(CurrentFilePath);
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("CurrentFileSaved");
XMLobj.WriteValue(CurrentFileSaved.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("WordWrap");
XMLobj.WriteValue(WordWrap.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("DetectUrls");
XMLobj.WriteValue(DetectUrls.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("AutoSave");
XMLobj.WriteValue(AutoSave.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("LastOpenedFile");
XMLobj.WriteValue(LastOpenedFile);
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("editorLocation");
XMLobj.WriteAttributeString("x", editorLocation.X.ToString());
XMLobj.WriteAttributeString("y", editorLocation.Y.ToString());
XMLobj.WriteValue(editorLocation.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("editorLangPath");
XMLobj.WriteValue(editorLangPath);
XMLobj.WriteEndElement();
XMLobj.WriteStartElement("editorToolBox");
XMLobj.WriteValue(editorToolBox.ToString());
XMLobj.WriteEndElement();
XMLobj.WriteEndElement();
XMLobj.WriteEndDocument();
XMLobj.Close();
}
}
}