-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfiguration.cs
97 lines (83 loc) · 3.63 KB
/
Configuration.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
using System;
using System.Net;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace WalkieTalkie
{
public static class Configuration
{
private static string userName;
private static System.Drawing.Size windowSize;
private static IPAddress lanIP;
private static bool groupChatNotifications;
private static XmlDocument settingsDoc;
public static string UserName
{
get { return userName; }
set { userName = value; }
}
public static System.Drawing.Size WindowSize
{
get { return windowSize; }
set { windowSize = value; }
}
public static IPAddress LANIP
{
get { return lanIP; }
set { lanIP = value; }
}
public static bool GroupChatNotifications
{
get { return groupChatNotifications; }
set { groupChatNotifications = value; }
}
static Configuration()
{
try
{
string defaultConfig = "<?xml version=\"1.0\" encoding=\"utf-8\"?><WalkieTalkie><Settings>" +
"<UserName /><WindowSize>799,498</WindowSize><LanIP /><GroupChatNotifications>yes</GroupChatNotifications></Settings></WalkieTalkie>";
string[] tempStrings;
byte[] addressBytes = new byte[4];
settingsDoc = new XmlDocument();
if (!File.Exists("walkietalkie.xml"))
settingsDoc.LoadXml(defaultConfig);
else
settingsDoc.Load("walkietalkie.xml");
userName = settingsDoc.GetElementsByTagName("UserName")[0].InnerText;
tempStrings = settingsDoc.GetElementsByTagName("WindowSize")[0].InnerText.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
windowSize = new Size(Convert.ToInt32(tempStrings[0]), Convert.ToInt32(tempStrings[1]));
lanIP = null;
tempStrings = settingsDoc.GetElementsByTagName("LanIP")[0].InnerText.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
if (tempStrings.Length == 4)
{
addressBytes[0] = Convert.ToByte(tempStrings[0]);
addressBytes[1] = Convert.ToByte(tempStrings[1]);
addressBytes[2] = Convert.ToByte(tempStrings[2]);
addressBytes[3] = Convert.ToByte(tempStrings[3]);
lanIP = new IPAddress(addressBytes);
}
groupChatNotifications = (settingsDoc.GetElementsByTagName("GroupChatNotifications")[0].InnerText == "yes") ? true : false;
}
catch (Exception) { }
}
public static void SaveConfiguration()
{
try
{
settingsDoc.GetElementsByTagName("UserName")[0].InnerText = userName;
settingsDoc.GetElementsByTagName("WindowSize")[0].InnerText = windowSize.Width.ToString() + "," + windowSize.Height.ToString();
settingsDoc.GetElementsByTagName("LanIP")[0].InnerText = LANIP.ToString();
settingsDoc.GetElementsByTagName("GroupChatNotifications")[0].InnerText = (groupChatNotifications ? "yes" : "no");
settingsDoc.Save("walkietalkie.xml");
}
catch (Exception)
{
MessageBox.Show("Couldn't save configuration data. Please inform the developer.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}