-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsDialog.cs
124 lines (108 loc) · 4 KB
/
SettingsDialog.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
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace AudiobookshelfTray
{
public partial class SettingsDialog : Form
{
private readonly AppTray _app;
private readonly string _initialPort;
private readonly string _initialDataFolder;
public SettingsDialog(AppTray app)
{
InitializeComponent();
_app = app;
_initialPort = _app.GetServerPort();
_initialDataFolder = _app.GetServerDataDir();
textBoxPort.Text = _initialPort;
textBoxDataFolder.Text = _initialDataFolder;
}
private void CancelClicked(object sender, EventArgs e)
{
}
private void BrowseClicked(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new()
{
Description = "Select the folder where your server configuration and metadata should be stored.",
SelectedPath = textBoxDataFolder.Text
};
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxDataFolder.Text = dialog.SelectedPath;
}
}
private void SaveClicked(object sender, EventArgs e)
{
if (ValidatePort() && ValidateDataFolder())
{
if (textBoxDataFolder.Text != _initialDataFolder || textBoxPort.Text != _initialPort)
{
_app.SaveServerPort(textBoxPort.Text);
_app.SaveServerDataDir(textBoxDataFolder.Text);
DialogResult result = MessageBox.Show("Changing server settings requires a server restart. Do you want to restart it now?",
"Audiobookshelf", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
return;
_app.StopServerClicked(sender, e);
_app.StartServerClicked(sender, e);
}
this.Close();
}
else
{
MessageBox.Show("One of the server settings is invalid. Please correct it before saving.", "Audiobookshelf", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void PortValidating(object sender, CancelEventArgs e)
{
if (!ValidatePort())
e.Cancel = true;
}
private bool ValidatePort()
{
int portNumber;
try
{
portNumber = Int32.Parse(textBoxPort.Text);
// Ensure port number is within valid range
if (portNumber < 1 || portNumber > 65535)
{
errorProviderPort.SetError(labelPort, "Invalid port number. Please enter a value between 1 and 65535.");
return false;
}
}
catch (FormatException)
{
errorProviderPort.SetError(labelPort, "Please enter a valid integer for the port number.");
return false;
}
errorProviderPort.SetError(labelPort, "");
return true;
}
private void PortTextChanged(object sender, EventArgs e)
{
ValidatePort();
}
private void DataFolderValidating(object sender, CancelEventArgs e)
{
if (!ValidateDataFolder())
e.Cancel = true;
}
private bool ValidateDataFolder()
{
string datafolder = textBoxDataFolder.Text;
if (!System.IO.Directory.Exists(datafolder))
{
errorProviderDataFolder.SetError(labelDataFolder, "Please enter a valid folder path.");
return false;
}
errorProviderDataFolder.SetError(labelDataFolder, "");
return true;
}
private void DataFolderTextChanged(object sender, EventArgs e)
{
ValidateDataFolder();
}
}
}