-
Notifications
You must be signed in to change notification settings - Fork 95
/
PreferencesDialog.xeto.cs
210 lines (201 loc) · 8.51 KB
/
PreferencesDialog.xeto.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using Eto.Forms;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Eto.Serialization.Xaml;
using System;
using System.Linq;
using System.Net;
using System.Collections.Generic;
using NextTrace;
using System.Diagnostics;
namespace OpenTrace
{
public partial class PreferencesDialog : Dialog
{
private ObservableCollection<TracerouteResult> tracerouteResultCollection = new ObservableCollection<TracerouteResult>();
private TextOutputForm textOutputForm = new TextOutputForm();
private static NextTraceWrapper CurrentInstance { get; set; }
UserSettings userSettings = new UserSettings();
public PreferencesDialog()
{
XamlReader.Load(this);
ApplyUserSettings();
}
private void ApplyUserSettings()
{
foreach (var setting in userSettings.GetType().GetProperties())
{
TextBox settingTextBox = this.FindChild<TextBox>(setting.Name);
if (settingTextBox != null)
{
settingTextBox.Text = (string)setting.GetValue(userSettings, null);
}
CheckBox settingCheckBox = this.FindChild<CheckBox>(setting.Name);
if (settingCheckBox != null)
{
settingCheckBox.Checked = (bool)setting.GetValue(userSettings, null);
}
DropDown settingDropDown = this.FindChild<DropDown>(setting.Name);
if (settingDropDown != null)
{
settingDropDown.SelectedKey = (string)setting.GetValue(userSettings, null);
}
TextArea settingTextArea = this.FindChild<TextArea>(setting.Name);
if (settingTextArea != null)
{
settingTextArea.Text = (string)setting.GetValue(userSettings, null);
}
}
this.FindChild<NumericStepper>("gridSizePercentage").Value = UserSettings.gridSizePercentage * 100;
this.FindChild<NumericStepper>("maskedHops").Value = UserSettings.maskedHops;
}
private void CancelButton_Click(object sender, EventArgs e)
{
Close();
}
private void SaveButton_Click(object sender, EventArgs e)
{
foreach (var setting in userSettings.GetType().GetProperties())
{
TextBox settingTextBox = this.FindChild<TextBox>(setting.Name);
if (settingTextBox != null)
{
setting.SetValue(userSettings, settingTextBox.Text);
}
CheckBox settingCheckBox = this.FindChild<CheckBox>(setting.Name);
if (settingCheckBox != null)
{
setting.SetValue(userSettings, settingCheckBox.Checked);
}
DropDown settingDropDown = this.FindChild<DropDown>(setting.Name);
if (settingDropDown != null)
{
setting.SetValue(userSettings, settingDropDown.SelectedKey);
}
TextArea settingTextArea = this.FindChild<TextArea>(setting.Name);
if (settingTextArea != null)
{
setting.SetValue(userSettings, settingTextArea.Text);
}
}
UserSettings.gridSizePercentage = this.FindChild<NumericStepper>("gridSizePercentage").Value / 100;
UserSettings.maskedHops = (int)this.FindChild<NumericStepper>("maskedHops").Value;
UserSettings.SaveSettings();
IPDBLoader.Load();
Close();
}
private void HandleMMDBSelect(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filters.Add(new FileFilter("MaxMind DB", ".mmdb"));
openFileDialog.Filters.Add(new FileFilter("All Files", ".*"));
openFileDialog.CheckFileExists = true;
openFileDialog.MultiSelect = false;
openFileDialog.Title = "Select MaxMind DB";
openFileDialog.ShowDialog(this);
if (openFileDialog.FileName != null && openFileDialog.FileName != "")
{
TextBox settingTextBox = this.FindChild<TextBox>("localDBPath");
settingTextBox.Text = openFileDialog.FileName;
}
}
private void HandleMMDBPreview(object sender, EventArgs e)
{
TextBox settingTextBox = this.FindChild<TextBox>("localDBPath");
if (settingTextBox == null || settingTextBox.Text == "")
{
return;
}
var oldPath = UserSettings.localDBPath;
UserSettings.localDBPath = settingTextBox.Text;
IPDBLoader.Load();
settingTextBox = this.FindChild<TextBox>("testMMDBIP");
if (settingTextBox == null || settingTextBox.Text == "")
{
return;
}
var result = IPDBLoader.DB.Find<Dictionary<string, object>>(IPAddress.Parse(settingTextBox.Text));
textOutputForm.ClearOutput();
reduceResult(0, result);
textOutputForm.Show();
}
private void reduceResult(int depth, Dictionary<string, object> result)
{
var prefix = Enumerable.Range(1, depth).Aggregate("", (current, _) => current + " ");
foreach (var item in result)
{
if (item.Value is Dictionary<string, object>)
{
textOutputForm.AppendOutput(prefix + (item.Key + ": "));
reduceResult(depth + 1, (Dictionary<string, object>)item.Value);
}
else if (item.Value is List<object>)
{
textOutputForm.AppendOutput(prefix + (item.Key + ": "));
reduceResult(depth + 1, (List<object>)item.Value);
}
else
{
textOutputForm.AppendOutput(prefix + (item.Key + ": " + item.Value));
}
}
}
private void reduceResult(int depth, List<object> result)
{
var prefix = Enumerable.Range(1, depth).Aggregate("", (current, _) => current + " ");
var i = 0;
foreach (var item in result)
{
if (item is Dictionary<string, object>)
{
textOutputForm.AppendOutput(prefix + (i + ": "));
reduceResult(depth + 1, (Dictionary<string, object>)item);
}
else if (item is List<object>)
{
textOutputForm.AppendOutput(prefix + (i + ": "));
reduceResult(depth + 1, (List<object>)item);
}
else
{
textOutputForm.AppendOutput(prefix + item);
}
i++;
}
}
private void HandleMMDBPreset(object sender, EventArgs e)
{
var setting = this.FindChild<DropDown>("localDBPreset");
switch (setting.SelectedKey)
{
case "geoip2-city":
UserSettings.localDBAddr = "{.country.names.zh-CN} {.subdivisions.0.names.zh-CN} {.city.names.zh-CN}";
UserSettings.localDBOrg = "";
UserSettings.localDBLat = "{.location.latitude}";
UserSettings.localDBLon = "{.location.longitude}";
UserSettings.localDBASN = "";
UserSettings.localDBHostname = "";
break;
case "ipinfo-loc":
UserSettings.localDBAddr = "{.country} {.region} {.city}";
UserSettings.localDBOrg = "";
UserSettings.localDBLat = "{.lat}";
UserSettings.localDBLon = "{.lng}";
UserSettings.localDBASN = "";
UserSettings.localDBHostname = "";
break;
case "ipinfo-org":
UserSettings.localDBAddr = "";
UserSettings.localDBOrg = "{.name} {.hosting} {.domain}";
UserSettings.localDBLat = "";
UserSettings.localDBLon = "";
UserSettings.localDBASN = "";
UserSettings.localDBHostname = "";
break;
default:
return;
}
ApplyUserSettings();
}
}
}