-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathFormCustomServers.cs
More file actions
3381 lines (2830 loc) · 119 KB
/
Copy pathFormCustomServers.cs
File metadata and controls
3381 lines (2830 loc) · 119 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using CustomControls;
using MsmhToolsClass;
using MsmhToolsClass.MsmhAgnosticServer;
using MsmhToolsWinFormsClass;
using MsmhToolsWinFormsClass.Themes;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Xml.Linq;
namespace SecureDNSClient;
public partial class FormCustomServers : Form
{
private readonly CustomLabel LabelMain = new();
private readonly Stopwatch LabelMainStopWatch = new();
private readonly System.Timers.Timer MainTimer = new(300);
private static XDocument XDoc = new();
private static readonly List<string> ListGroupNames = new();
private static readonly string CustomServersXmlPath = SecureDNS.CustomServersXmlPath;
// Context Menu Group
private static readonly CustomContextMenuStrip MG = new();
private static readonly ToolStripMenuItem MenuGroupNew = new();
private static readonly ToolStripMenuItem MenuGroupRename = new();
private static readonly ToolStripMenuItem MenuGroupRemove = new();
private static readonly ToolStripMenuItem MenuGroupMoveUp = new();
private static readonly ToolStripMenuItem MenuGroupMoveDown = new();
private static readonly ToolStripMenuItem MenuGroupMoveToTop = new();
private static readonly ToolStripMenuItem MenuGroupMoveToBottom = new();
private static readonly ToolStripMenuItem MenuGroupImport = new();
private static readonly ToolStripMenuItem MenuGroupExport = new();
// Context Menu DNSs
private static readonly CustomContextMenuStrip MD = new();
private static readonly ToolStripMenuItem MenuDnsRemove = new();
private static readonly ToolStripMenuItem MenuDnsRemoveAll = new();
private static readonly ToolStripMenuItem MenuDnsMoveSelectedToGroup = new();
private static readonly ToolStripMenuItem MenuDnsMoveUp = new();
private static readonly ToolStripMenuItem MenuDnsMoveDown = new();
private static readonly ToolStripMenuItem MenuDnsMoveToTop = new();
private static readonly ToolStripMenuItem MenuDnsMoveToBottom = new();
private static readonly ToolStripMenuItem MenuDnsImport = new();
private static readonly ToolStripMenuItem MenuDnsExport = new();
private static readonly ToolStripMenuItem MenuDnsExportAsText = new();
private static readonly ToolStripMenuItem MenuDnsSelectAll = new();
private static readonly ToolStripMenuItem MenuDnsInvertSelection = new();
// Export & Import Form Context Menu
private static readonly CustomContextMenuStrip MGE = new();
private static readonly ToolStripMenuItem MenuSA = new();
private static readonly ToolStripMenuItem MenuIS = new();
private bool IsExiting { get; set; } = false;
public FormCustomServers()
{
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
InitializeComponent();
// Invariant Culture
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
StartPosition = FormStartPosition.CenterScreen;
// Load Theme
Theme.LoadTheme(this, Theme.Themes.Dark);
Theme.SetColors(LabelMain);
// Load XML Custom Servers
LoadXmlCS(CustomServersXmlPath);
// Label Main
Controls.Add(LabelMain);
LabelMain.Text = "Getting Ready...";
LabelMain.Dock = DockStyle.Fill;
LabelMain.TextAlign = ContentAlignment.MiddleCenter;
LabelMain.Font = new Font(Font.FontFamily, Font.Size * 1.5f);
SplitContainerMain.Visible = true;
LabelMain.Visible = false;
LabelMain.SendToBack();
Shown += FormCustomServers_Shown;
Move += FormCustomServers_Move;
Resize += FormCustomServers_Resize;
LocationChanged += FormCustomServers_LocationChanged;
MainTimer.Elapsed += MainTimer_Elapsed;
MainTimer.Start();
}
private void MainTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
// Hide Label Main After a period of time
LabelMainHide();
}
private void ShowLabelMain(string text)
{
this.InvokeIt(() =>
{
SplitContainerMain.Visible = false;
LabelMain.Text = text;
LabelMain.Visible = true;
LabelMain.BringToFront();
LabelMainStopWatch.Restart();
});
}
private void HideLabelMain()
{
if (!LabelMain.Visible) return;
this.InvokeIt(() =>
{
LabelMain.Visible = false;
LabelMain.SendToBack();
SplitContainerMain.Visible = true;
});
}
private void LabelMainHide()
{
if (LabelMainStopWatch.ElapsedMilliseconds > 300)
{
HideLabelMain();
LabelMainStopWatch.Restart();
}
}
private async void FormCustomServers_Shown(object? sender, EventArgs e)
{
try
{
LabelMain.Visible = false;
LabelMain.SendToBack();
SplitContainerMain.Visible = true;
ReadGroups(null, false);
// Setting Width Of Controls
await ScreenDPI.SettingWidthOfControls(this);
// Fix Controls Location
int spaceBottom = 6, spaceRight = 6, spaceV = 6, spaceH = 6;
CustomDataGridViewGroups.Location = new Point(spaceRight, FormMain.LabelScreen.Height);
CustomButtonExport.Left = CustomGroupBoxGroups.Width - CustomButtonExport.Width - spaceRight;
CustomButtonExport.Top = CustomGroupBoxGroups.Height - CustomButtonNewGroup.Height - spaceBottom;
CustomButtonImport.Left = CustomButtonExport.Left - CustomButtonImport.Width - spaceH;
CustomButtonImport.Top = CustomButtonExport.Top;
CustomButtonNewGroup.Left = CustomDataGridViewGroups.Left;
CustomButtonNewGroup.Top = CustomButtonImport.Top;
CustomButtonNewGroup.Width = CustomButtonImport.Left - CustomButtonNewGroup.Left - spaceH;
CustomDataGridViewGroups.Width = CustomGroupBoxGroups.Width - (spaceH * 2);
CustomDataGridViewGroups.Height = CustomButtonNewGroup.Top - CustomDataGridViewGroups.Top - spaceV;
CustomDataGridViewDNSs.Location = new Point(spaceRight, FormMain.LabelScreen.Height);
CustomButtonModifyDNS.Left = CustomGroupBoxDNSs.Width - CustomButtonModifyDNS.Width - spaceRight;
CustomButtonModifyDNS.Top = CustomGroupBoxDNSs.Height - CustomButtonModifyDNS.Height - spaceBottom;
CustomButtonAddServers.Left = CustomButtonModifyDNS.Left;
CustomButtonAddServers.Top = CustomButtonModifyDNS.Top - CustomButtonAddServers.Height - spaceV;
CustomLabelDescription.Left = CustomDataGridViewDNSs.Left;
CustomLabelDNS.Left = CustomLabelDescription.Left;
CustomTextBoxDescription.Left = CustomLabelDescription.Right + (spaceH * 6);
CustomTextBoxDescription.Top = CustomButtonModifyDNS.Top;
CustomTextBoxDescription.Width = CustomButtonModifyDNS.Left - CustomTextBoxDescription.Left - spaceH;
CustomTextBoxDNS.Left = CustomTextBoxDescription.Left;
CustomTextBoxDNS.Top = CustomButtonAddServers.Top + (CustomButtonAddServers.Height - CustomTextBoxDNS.Height);
CustomTextBoxDNS.Width = CustomTextBoxDescription.Width;
CustomLabelDescription.Top = CustomTextBoxDescription.Top + 2;
CustomLabelDNS.Top = CustomTextBoxDNS.Top + 2;
CustomDataGridViewDNSs.Width = CustomGroupBoxDNSs.Width - (spaceH * 2);
CustomDataGridViewDNSs.Height = CustomButtonAddServers.Top - CustomDataGridViewDNSs.Top - spaceV;
int btnNewGroupWidth = TextRenderer.MeasureText(CustomButtonNewGroup.Text, Font).Width;
SplitContainerMain.SplitterDistance = btnNewGroupWidth + (CustomButtonExport.Width * 2) + (spaceRight * 2) + (spaceH * 2);
Controllers.SetDarkControl(this);
}
catch (Exception ex)
{
Debug.WriteLine("FormCustomServers_Shown: " + ex.Message);
}
}
private void FormCustomServers_Move(object? sender, EventArgs e)
{
if (LabelMainStopWatch.IsRunning)
{
ShowLabelMain("Now Moving...");
}
}
private void FormCustomServers_Resize(object? sender, EventArgs e)
{
if (WindowState != FormWindowState.Minimized)
{
if (LabelMainStopWatch.IsRunning)
{
ShowLabelMain("Now Resizing...");
}
}
}
private void FormCustomServers_LocationChanged(object? sender, EventArgs e)
{
LabelMainStopWatch.Restart();
}
public void LoadXmlCS(string path)
{
try
{
if (!File.Exists(path))
{
File.Create(path).Dispose();
XDoc = CreateXmlCS();
XDoc.Save(path, SaveOptions.None);
}
else if (string.IsNullOrWhiteSpace(File.ReadAllText(path)))
{
XDoc = CreateXmlCS();
XDoc.Save(path, SaveOptions.None);
}
else if (!XmlTool.IsValid(File.ReadAllText(path)))
{
CustomMessageBox.Show(this, "XML file is not valid. Returned to default.", "Not Valid", MessageBoxButtons.OK, MessageBoxIcon.Error);
XDoc = CreateXmlCS();
XDoc.Save(path, SaveOptions.None);
}
XDoc = XDocument.Load(path, LoadOptions.None);
}
catch (Exception ex)
{
Debug.WriteLine($"LoadXmlCS: {ex.Message}");
}
}
private static XDocument CreateXmlCS()
{
XDocument doc = new();
XElement group = new("Settings");
group.Add(new XElement("CustomDnsList"));
doc.Add(group);
return doc;
}
private void ReadGroups(string? groupNameToSelect, bool showRow)
{
// Read Groups
ListGroupNames.Clear();
var dgvG = CustomDataGridViewGroups;
var dgvS = CustomDataGridViewDNSs;
int firstVisible = dgvG.FirstDisplayedScrollingRowIndex;
if (dgvG.SelectedRows.Count > 0)
{
int selectedRow = dgvG.SelectedRows[0].Index;
int displayedRowCount = dgvG.DisplayedRowCount(false);
if (selectedRow - firstVisible == displayedRowCount)
firstVisible++;
}
dgvG.Rows.Clear();
if (XDoc.Root == null) return;
var nodesGroups = XDoc.Root.Elements().Elements();
if (!nodesGroups.Any())
{
dgvS.Rows.Clear();
CustomGroupBoxDNSs.Text = "DNS Addresses";
return;
}
dgvG.Rows.Add(nodesGroups.Count());
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement node = nodesGroups.ToList()[a];
XElement? nodeEnabled = node.Element("Enabled");
XElement? nodeName = node.Element("Name");
if (nodeName == null) return;
bool cell0Value = nodeEnabled == null || Convert.ToBoolean(nodeEnabled.Value);
dgvG.Rows[a].Cells[0].Value = cell0Value;
dgvG.Rows[a].Cells[1].Value = nodeName.Value;
dgvG.Rows[a].Height = TextRenderer.MeasureText(nodeName.Value.ToString(), dgvG.DefaultCellStyle.Font).Height + 5;
dgvG.EndEdit();
// Add Names to List
ListGroupNames.Add(nodeName.Value);
}
// Select Group
int rowIndex = 0;
groupNameToSelect ??= dgvG.Rows[0].Cells[1].Value.ToString();
if (string.IsNullOrEmpty(groupNameToSelect)) return;
for (int n = 0; n < dgvG.Rows.Count; n++)
{
var row = dgvG.Rows[n];
string? groupName = row.Cells[1].Value.ToString();
if (string.IsNullOrEmpty(groupName)) continue;
if (groupName.Equals(groupNameToSelect))
{
rowIndex = row.Index;
break;
}
}
if (rowIndex == 0 && showRow == false)
ReadDNSs(groupNameToSelect); // Because SelectionChanged won't fire.
if (showRow)
ShowRow(dgvG, rowIndex, firstVisible);
else
{
dgvG.Rows[rowIndex].Cells[0].Selected = true;
dgvG.Rows[rowIndex].Selected = true;
}
}
private void ReadDNSs(string? groupName)
{
Debug.WriteLine("ReadDNSs Fired: " + groupName);
var dgvG = CustomDataGridViewGroups;
var dgvS = CustomDataGridViewDNSs;
if (string.IsNullOrEmpty(groupName))
{
dgvS.Rows.Clear();
return;
}
if (dgvG.Rows.Count == 0) return;
List<DataGridViewRow> pList = new();
if (XDoc.Root == null) return;
var nodes = XDoc.Root.Elements().Elements();
for (int a = 0; a < nodes.Count(); a++)
{
XElement nodeG = nodes.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName == null) return;
if (groupName == nodeGName.Value)
{
int count = nodeG.Elements("DnsItem").Count();
dgvS.Rows.Clear();
for (int b = 0; b < count; b++)
{
XElement node = nodeG.Elements("DnsItem").ToList()[b];
XElement? nodeEnabled = node.Element("Enabled");
XElement? nodeDns = node.Element("Dns");
XElement? nodeDescription = node.Element("Description");
if (nodeEnabled == null || nodeDns == null || nodeDescription == null)
{
// Clear TextBoxes
CustomTextBoxDNS.Text = string.Empty;
CustomTextBoxDescription.Texts = string.Empty;
return;
}
// Add by AddRange
DataGridViewRow row = new();
row.CreateCells(dgvS, "cell0", "cell1", "cell2");
row.Cells[0].Value = Convert.ToBoolean(nodeEnabled.Value);
row.Cells[1].Value = nodeDns.Value;
row.Cells[2].Value = nodeDescription.Value;
row.Height = TextRenderer.MeasureText(nodeDns.Value.ToString(), dgvS.DefaultCellStyle.Font).Height + 5;
pList.Add(row);
}
dgvS.Rows.AddRange(pList.ToArray());
}
}
}
private static void ShowRow(DataGridView dgv, int rowToSelect, int firstVisibleRow)
{
if (0 <= rowToSelect && rowToSelect < dgv.RowCount)
{
dgv.Rows[0].Cells[0].Selected = false;
dgv.Rows[0].Selected = false;
dgv.Rows[rowToSelect].Cells[0].Selected = true;
dgv.Rows[rowToSelect].Selected = true;
if (rowToSelect < firstVisibleRow || firstVisibleRow == -1)
firstVisibleRow = rowToSelect;
dgv.FirstDisplayedScrollingRowIndex = firstVisibleRow;
}
}
private static void ShowRow(DataGridView dgv, int[] rowsToSelect, int firstVisibleRow)
{
bool zero = false;
for (int n = 0; n < rowsToSelect.Length; n++)
{
int rowToSelect = rowsToSelect[n];
if (rowToSelect == -1) return;
if (0 <= rowToSelect && rowToSelect < dgv.RowCount)
{
if (rowToSelect != 0 && zero == false)
{
dgv.Rows[0].Cells[0].Selected = false;
dgv.Rows[0].Selected = false;
}
else
{
dgv.Rows[0].Cells[0].Selected = true;
dgv.Rows[0].Selected = true;
zero = true;
}
dgv.Rows[rowToSelect].Cells[0].Selected = true;
dgv.Rows[rowToSelect].Selected = true;
}
if (rowToSelect < firstVisibleRow || firstVisibleRow == -1)
firstVisibleRow = rowToSelect;
dgv.FirstDisplayedScrollingRowIndex = firstVisibleRow;
}
}
private static XElement GetXmlGroupByName(XDocument xDoc, string groupName)
{
XElement group = new("Default");
if (xDoc.Root != null)
{
var nodesGroups = xDoc.Root.Elements().Elements();
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement nodeG = nodesGroups.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName != null && nodeGName.Value == groupName)
{
group = nodeG;
break;
}
}
}
return group;
}
private static void RenameGroup(XDocument xDoc, string oldName, string newName)
{
if (xDoc.Root == null) return;
var nodesGroups = xDoc.Root.Elements().Elements();
for (int n = 0; n < nodesGroups.Count(); n++)
{
XElement nodeG = nodesGroups.ToList()[n];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName != null && nodeGName.Value == oldName)
{
nodeGName.Value = newName;
break;
}
}
}
private void RemoveGroup(string groupName)
{
if (XDoc.Root != null)
{
var nodesGroups = XDoc.Root.Elements().Elements();
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement nodeG = nodesGroups.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName != null && nodeGName.Value == groupName)
{
nodeG.Remove();
break;
}
}
}
// Find Previous Group Name Otherwise Next
int index = ListGroupNames.GetIndex(groupName);
if (index == -1 || index == 0)
{
// Refresh Groups
ReadGroups(null, true);
return;
}
else
{
string previousGroup = ListGroupNames[index - 1];
// Refresh Groups
ReadGroups(previousGroup, true);
}
}
private void RemoveDNSs(string groupName)
{
var dgvS = CustomDataGridViewDNSs;
int[] rows = new int[dgvS.SelectedRows.Count];
for (int n = 0; n < dgvS.SelectedRows.Count; n++)
{
rows[n] = dgvS.SelectedRows[n].Index;
}
Array.Sort(rows);
if (XDoc.Root == null) return;
var nodesGroups = XDoc.Root.Elements().Elements();
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement nodeG = nodesGroups.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName != null && nodeGName.Value == groupName)
{
int less = 0;
for (int b = 0; b < rows.Length; b++)
{
int row = rows[b];
if (b != 0)
{
less++;
row = rows[b] - less;
}
var node = nodeG.Elements("DnsItem").ToList()[row];
node.Remove();
Debug.WriteLine("DNS Removed At " + row);
}
break;
}
}
}
private void MoveDNSsToGroup(string groupToSend, int[] dnsRows, int rowToInsert, bool addBeforeSelf)
{
if (XDoc.Root == null) return;
var nodesGroups = XDoc.Root.Elements().Elements();
List<XElement> selectedDNSs = new();
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement nodeG = nodesGroups.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName != null && nodeGName.Value == groupToSend)
{
// Add Selected DNSs To List
for (int b = 0; b < dnsRows.Length; b++)
{
int row = dnsRows[b];
XElement dns = nodeG.Elements("DnsItem").ToList()[row];
selectedDNSs.Add(dns);
}
// Remove Selected DNSs
int less = 0;
for (int b = 0; b < dnsRows.Length; b++)
{
int row = dnsRows[b];
if (b != 0)
{
less++;
row = dnsRows[b] - less;
}
var node = nodeG.Elements("DnsItem").ToList()[row];
node.Remove();
}
// Copy Selected DNSs
selectedDNSs.Reverse();
for (int b = 0; b < selectedDNSs.Count; b++)
{
XElement dns = selectedDNSs[b];
CopyDnsToGroup(groupToSend, dns, rowToInsert, addBeforeSelf);
}
break;
}
}
}
private void CopyDnsToGroup(string groupToSend, XElement dnsToCopy, int? rowToInsert, bool? addBeforeSelf)
{
var dgvG = CustomDataGridViewGroups;
if (dgvG.RowCount == 0) return;
if (XDoc.Root == null) return;
var nodesGroups = XDoc.Root.Elements().Elements();
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement nodeG = nodesGroups.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
if (nodeGName != null && nodeGName.Value == groupToSend)
{
if (rowToInsert == null)
{
nodeG.Add(dnsToCopy);
}
else
{
List<XElement> dnss = nodeG.Elements("DnsItem").ToList();
for (int b = 0; b < dnss.Count; b++)
{
XElement dns = dnss[b];
if (rowToInsert == b)
{
if (addBeforeSelf != null)
{
if ((bool)addBeforeSelf)
dns.AddBeforeSelf(dnsToCopy);
else
dns.AddAfterSelf(dnsToCopy);
break;
}
}
}
}
}
}
}
private XElement CreateDNS()
{
XElement dns = new("DnsItem");
XElement enabled = new("Enabled");
enabled.Value = "True";
dns.Add(enabled);
XElement dnsAddress = new("Dns");
dnsAddress.Value = CustomTextBoxDNS.Text.Trim();
dns.Add(dnsAddress);
XElement dnsDescription = new("Description");
dnsDescription.Value = CustomTextBoxDescription.Text.Trim();
dns.Add(dnsDescription);
return dns;
}
private static XElement CreateDNS(string dnsAddress, string dnsDescription)
{
XElement dns = new("DnsItem");
XElement enabled = new("Enabled");
enabled.Value = "True";
dns.Add(enabled);
XElement dnsAddressElement = new("Dns");
dnsAddressElement.Value = dnsAddress.Trim();
dns.Add(dnsAddressElement);
XElement dnsDescriptionElement = new("Description");
dnsDescriptionElement.Value = dnsDescription.Trim();
dns.Add(dnsDescriptionElement);
return dns;
}
//======================================= Groups ======================================
private void CustomDataGridViewGroups_SelectionChanged(object sender, EventArgs e)
{
var dgvG = CustomDataGridViewGroups;
var dgvS = CustomDataGridViewDNSs;
if (dgvG.RowCount < 1) return;
if (dgvG.SelectedCells.Count < 1) return;
int currentRow = dgvG.SelectedCells[0].RowIndex;
if (dgvG.Rows[currentRow].Cells[1].Value == null) return;
string? group = dgvG.Rows[currentRow].Cells[1].Value.ToString();
string dnsNoMsg = string.Empty;
if (dgvS.RowCount > 0 && dgvS.SelectedCells.Count > 0)
{
int dnsNo = dgvS.SelectedCells[0].RowIndex + 1;
dnsNoMsg = $" - DNS No: {dnsNo}";
}
string msg = $"DNSs for group \"{group}\"{dnsNoMsg}";
CustomGroupBoxDNSs.Text = msg;
if (!string.IsNullOrEmpty(group))
ReadDNSs(group);
}
private void CustomDataGridViewGroups_KeyDown(object sender, KeyEventArgs e)
{
// Assign Menu Shortcuts to KeyDown (Use Shortcuts When Menu is Not Displayed)
CreateMenuGroup();
void checkShortcut(ToolStripMenuItem item)
{
if (item.ShortcutKeys == e.KeyData)
{
//item.PerformClick(); // Doesn't work correctly
if (item.Text.Contains("New group..."))
MenuGroupNew_Click(null, null);
else if (item.Text.Contains("Rename group..."))
MenuGroupRename_Click(null, null);
else if (item.Text.Contains("Remove"))
MenuGroupRemove_Click(null, null);
else if (item.Text.Contains("Move up"))
MenuGroupMoveUp_Click(null, null);
else if (item.Text.Contains("Move down"))
MenuGroupMoveDown_Click(null, null);
else if (item.Text.Contains("Move to top"))
MenuGroupMoveToTop_Click(null, null);
else if (item.Text.Contains("Move to bottom"))
MenuGroupMoveToBottom_Click(null, null);
else if (item.Text.Contains("Import"))
MenuGroupImport_Click(null, null);
else if (item.Text.Contains("Export"))
MenuGroupExport_Click(null, null);
else
item.PerformClick();
return;
}
foreach (ToolStripMenuItem child in item.DropDownItems.OfType<ToolStripMenuItem>())
{
checkShortcut(child);
}
}
foreach (ToolStripMenuItem item in MG.Items.OfType<ToolStripMenuItem>())
{
checkShortcut(item);
}
// Make ContextMenu Shortcuts Work (Move up and Move Down)
if (e.Control && e.KeyCode == Keys.Up || e.Control && e.KeyCode == Keys.Down)
{
e.SuppressKeyPress = true;
}
}
private void CustomDataGridViewGroups_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dgvG = CustomDataGridViewGroups;
if (dgvG.Rows.Count == 0) return;
if (dgvG.SelectedCells.Count <= 0) return;
if (dgvG.Rows[e.RowIndex].Cells[1].Value == null) return;
string? group = dgvG.Rows[e.RowIndex].Cells[1].Value.ToString();
//Debug.WriteLine(group);
// Save CheckBox State for Groups
if (e.ColumnIndex == 0)
{
if (XDoc.Root == null) return;
var nodesGroups = XDoc.Root.Elements().Elements();
for (int a = 0; a < nodesGroups.Count(); a++)
{
XElement nodeG = nodesGroups.ToList()[a];
XElement? nodeGName = nodeG.Element("Name");
XElement? nodeGEnabled = nodeG.Element("Enabled");
if (nodeGName != null && nodeGEnabled != null && group == nodeGName.Value)
{
string? isEnabled = dgvG.Rows[a].Cells[0].Value.ToString();
nodeGEnabled.Value = !string.IsNullOrEmpty(isEnabled) ? isEnabled : "False";
// Save xDocument to File
try
{
XDoc.Save(CustomServersXmlPath, SaveOptions.None);
}
catch (Exception) { }
}
}
}
}
private void CustomDataGridViewGroups_MouseDown(object sender, MouseEventArgs e)
{
// Context Menu
if (e.Button == MouseButtons.Right)
{
var dgvG = CustomDataGridViewGroups;
dgvG.Select(); // Set Focus on Control
CreateMenuGroup();
int currentMouseOverRow = dgvG.HitTest(e.X, e.Y).RowIndex;
int totalRows = dgvG.Rows.Count;
if (currentMouseOverRow != -1 && currentMouseOverRow < totalRows)
{
if (dgvG.Rows[currentMouseOverRow].Cells.Count > 0)
dgvG.Rows[currentMouseOverRow].Cells[0].Selected = true;
dgvG.Rows[currentMouseOverRow].Selected = true;
}
if (currentMouseOverRow == -1)
{
MG.Items.Remove(MenuGroupRename);
MG.Items.Remove(MenuGroupRemove);
MG.Items.Remove(MenuGroupMoveUp);
MG.Items.Remove(MenuGroupMoveDown);
MG.Items.Remove(MenuGroupMoveToTop);
MG.Items.Remove(MenuGroupMoveToBottom);
MG.Items.RemoveAt(2);
}
else if (currentMouseOverRow == 0)
{
MG.Items.Remove(MenuGroupMoveUp);
MG.Items.Remove(MenuGroupMoveToTop);
}
else if (currentMouseOverRow == totalRows - 1)
{
MG.Items.Remove(MenuGroupMoveDown);
MG.Items.Remove(MenuGroupMoveToBottom);
}
if (totalRows == 0)
{
MG.Items.Remove(MenuGroupExport);
}
else if (totalRows == 1)
{
MG.Items.Remove(MenuGroupMoveDown);
MG.Items.Remove(MenuGroupMoveToBottom);
if (currentMouseOverRow != -1)
MG.Items.RemoveAt(3);
}
Theme.SetColors(MG);
MG.RoundedCorners = 5;
MG.Show(dgvG, new Point(e.X, e.Y));
}
}
private void CreateMenuGroup()
{
// Context Menu
MG.Items.Clear();
MG.Font = Font;
MenuGroupNew.Font = Font;
MenuGroupNew.Text = "New group...";
MenuGroupNew.ShortcutKeys = Keys.Control | Keys.Shift | Keys.N;
MenuGroupNew.Click -= MenuGroupNew_Click;
MenuGroupNew.Click += MenuGroupNew_Click;
MG.Items.Add(MenuGroupNew);
MenuGroupRename.Font = Font;
MenuGroupRename.Text = "Rename group...";
MenuGroupRename.ShortcutKeys = Keys.F2;
MenuGroupRename.Click -= MenuGroupRename_Click;
MenuGroupRename.Click += MenuGroupRename_Click;
MG.Items.Add(MenuGroupRename);
MenuGroupRemove.Font = Font;
MenuGroupRemove.Text = "Remove";
MenuGroupRemove.ShortcutKeys = Keys.Delete;
MenuGroupRemove.Click -= MenuGroupRemove_Click;
MenuGroupRemove.Click += MenuGroupRemove_Click;
MG.Items.Add(MenuGroupRemove);
MG.Items.Add("-");
MenuGroupMoveUp.Font = Font;
MenuGroupMoveUp.Text = "Move up";
MenuGroupMoveUp.ShortcutKeys = Keys.Control | Keys.Up;
MenuGroupMoveUp.Click -= MenuGroupMoveUp_Click;
MenuGroupMoveUp.Click += MenuGroupMoveUp_Click;
MG.Items.Add(MenuGroupMoveUp);
MenuGroupMoveDown.Font = Font;
MenuGroupMoveDown.Text = "Move down";
MenuGroupMoveDown.ShortcutKeys = Keys.Control | Keys.Down;
MenuGroupMoveDown.Click -= MenuGroupMoveDown_Click;
MenuGroupMoveDown.Click += MenuGroupMoveDown_Click;
MG.Items.Add(MenuGroupMoveDown);
MenuGroupMoveToTop.Font = Font;
MenuGroupMoveToTop.Text = "Move to top";
MenuGroupMoveToTop.ShortcutKeys = Keys.Control | Keys.Home;
MenuGroupMoveToTop.Click -= MenuGroupMoveToTop_Click;
MenuGroupMoveToTop.Click += MenuGroupMoveToTop_Click;
MG.Items.Add(MenuGroupMoveToTop);
MenuGroupMoveToBottom.Font = Font;
MenuGroupMoveToBottom.Text = "Move to bottom";
MenuGroupMoveToBottom.ShortcutKeys = Keys.Control | Keys.End;
MenuGroupMoveToBottom.Click -= MenuGroupMoveToBottom_Click;
MenuGroupMoveToBottom.Click += MenuGroupMoveToBottom_Click;
MG.Items.Add(MenuGroupMoveToBottom);
MG.Items.Add("-");
MenuGroupImport.Font = Font;
MenuGroupImport.Text = "Import";
MenuGroupImport.ShortcutKeys = Keys.Control | Keys.I;
MenuGroupImport.Click -= MenuGroupImport_Click;
MenuGroupImport.Click += MenuGroupImport_Click;
MG.Items.Add(MenuGroupImport);
MenuGroupExport.Font = Font;
MenuGroupExport.Text = "Export";
MenuGroupExport.ShortcutKeys = Keys.Control | Keys.E;
MenuGroupExport.Click -= MenuGroupExport_Click;
MenuGroupExport.Click += MenuGroupExport_Click;
MG.Items.Add(MenuGroupExport);
Theme.SetColors(MG);
}
private void MenuGroupNew_Click(object? sender, EventArgs? e)
{
string newGroupName = string.Empty;
switch (CustomInputBox.Show(this, ref newGroupName, "New Group Name:", false, "Create group"))
{
case DialogResult.OK:
break;
case DialogResult.Cancel:
return;
default:
return;
}
newGroupName = newGroupName.Trim();
if (string.IsNullOrWhiteSpace(newGroupName))
{
string msg = "Name cannot be empty or white space.";
CustomMessageBox.Show(this, msg, "Message");
return;
}
if (newGroupName.ToLower().Equals("msasanmh"))
{
string msg = $"\"{newGroupName}\" is predefined, choose another name.";
CustomMessageBox.Show(this, msg, "Message");
return;
}
if (ListGroupNames.Contains(newGroupName))
{
string msg = $"\"{newGroupName}\" is already exist, choose another name.";
CustomMessageBox.Show(this, msg, "Message");
return;
}
if (XDoc.Root == null) return;
var nodes = XDoc.Root.Elements();
for (int a = 0; a < nodes.Count(); a++)
{
XElement node = nodes.ToList()[a];
XElement group = new("Group");
group.Add(new XElement("Name", newGroupName));
group.Add(new XElement("Enabled", "True"));
node.Add(group);
}
// Refresh Groups
ReadGroups(newGroupName, false);
// Save xDocument to File
try
{
XDoc.Save(CustomServersXmlPath, SaveOptions.None);
}
catch (Exception) { }
Debug.WriteLine($"New Group Created: {newGroupName}");
}
private void MenuGroupRename_Click(object? sender, EventArgs? e)
{
var dgv = CustomDataGridViewGroups;
if (dgv.SelectedCells.Count < 1) return;
int currentRow = dgv.SelectedCells[0].RowIndex;
string? groupName = dgv.Rows[currentRow].Cells[1].Value.ToString();
if (string.IsNullOrEmpty(groupName)) return;
string groupNameOld = groupName, groupNameNew = groupName;