-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInstanceNameDialog.xaml.cs
More file actions
135 lines (117 loc) · 3.11 KB
/
InstanceNameDialog.xaml.cs
File metadata and controls
135 lines (117 loc) · 3.11 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
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Waher.IoTGateway.Setup.Windows
{
/// <summary>
/// Interaction logic for InstanceNameDialog.xaml
/// </summary>
public partial class InstanceNameDialog : Window
{
/// <summary>
/// Interaction logic for InstanceNameDialog.xaml
/// </summary>
/// <param name="Port">Port number.</param>
public InstanceNameDialog(int Port)
{
this.InitializeComponent();
this.Instance = string.Empty;
this.Port = Port;
this.PortNumber.Text = Port.ToString();
}
public string Instance { get; set; }
public int Port { get; set; }
private async void CheckInput(object Sender, TextChangedEventArgs e)
{
try
{
await this.CheckInput();
}
catch (Exception ex)
{
MainWindow.ShowError(ex);
}
}
private async Task<bool> CheckInput()
{
string s = this.InstanceName.Text;
if (string.IsNullOrEmpty(s))
{
this.ShowMessage("Instance name cannot be empty.");
return false;
}
if (s != s.Trim())
{
this.ShowMessage("Instance name cannot contain white-space.");
return false;
}
foreach (char ch in s)
{
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))
{
continue;
}
this.ShowMessage("Invalid character in instance name: " + ch);
return false;
}
string Programs = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), App.AppName + s);
if (Directory.Exists(Programs))
{
this.ShowMessage("Instance name conflicts with an existing installation.");
return false;
}
if (!int.TryParse(this.PortNumber.Text, out int Port) ||
Port <= 0 || Port > ushort.MaxValue)
{
this.ShowMessage("The port number must be an integer between 1 and 65535. 80 is the default. Other common numbers include 8080, 8081, 8082, etc.");
return false;
}
bool? b = await MainWindow.CheckPortNumber(Port);
if (!b.HasValue)
{
this.ShowMessage("Unable to open Port number. Is a network available?");
return false;
}
else if (!b.Value)
{
this.ShowMessage("Port number occupied by another service. Try another.");
return false;
}
this.ShowMessage(string.Empty);
this.Instance = s;
this.Port = Port;
return true;
}
private void ShowMessage(string s)
{
this.Message.Text = s;
this.OkButton.IsEnabled = string.IsNullOrEmpty(s);
}
private async void Window_Initialized(object Sender, System.EventArgs e)
{
try
{
await this.CheckInput();
}
catch (Exception ex)
{
MainWindow.ShowError(ex);
}
}
private void OkButton_Click(object Sender, RoutedEventArgs e)
{
if (MessageBox.Show(this, "Please press OK to confirm you want to install a new instance named \"" + this.Instance + "\" of " +
App.AppNameDisplayable + " on your computer, using port number " + this.Port.ToString() + ".",
"Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
{
this.DialogResult = true;
}
else
e.Handled = true;
}
}
}