-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationWindow.xaml.cs
More file actions
413 lines (350 loc) · 15.9 KB
/
ConfigurationWindow.xaml.cs
File metadata and controls
413 lines (350 loc) · 15.9 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
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Text.Json;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.Reflection;
using System.Diagnostics;
namespace NodaStack
{
public partial class ConfigurationWindow : Window
{
private ConfigurationManager configManager;
private NodaStackConfiguration? originalConfig;
private CancellationTokenSource cancellationTokenSource = new();
public ConfigurationWindow(ConfigurationManager configManager)
{
InitializeComponent();
this.configManager = configManager;
this.originalConfig = JsonClone(configManager.Configuration);
LoadConfiguration();
SetupEventHandlers();
var assembly = Assembly.GetExecutingAssembly();
var versionAttribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
CurrentVersionText.Text = versionAttribute?.Version ?? "0.0.0.0";
CheckLatestVersion();
}
private void LoadConfiguration()
{
var config = configManager.GetConfiguration();
originalConfig = JsonClone(config);
ApachePortTextBox.Text = config.Ports["Apache"].ToString();
PhpPortTextBox.Text = config.Ports["PHP"].ToString();
MySqlPortTextBox.Text = config.Ports["MySQL"].ToString();
PhpMyAdminPortTextBox.Text = config.Ports["phpMyAdmin"].ToString();
// MailHog (Web Port only for UI)
MailHogWebPortTextBox.Text = config.Ports.TryGetValue("MailHogWeb", out int mhPort) ? mhPort.ToString() : "8025";
// Version texts removed from UI - versions are managed in Docker files
AutoStartCheckBox.IsChecked = config.Settings.AutoStartServices;
NotificationsCheckBox.IsChecked = config.Settings.ShowNotifications;
DetailedLoggingCheckBox.IsChecked = config.Settings.EnableLogging;
DarkModeCheckBox.IsChecked = config.Settings.DarkMode;
AutoRefreshCheckBox.IsChecked = config.Settings.AutoRefreshProjects;
SslSupportCheckBox.IsChecked = config.Settings.EnableSsl;
MySqlPasswordBox.Password = config.Settings.MySqlPassword;
MySqlDatabaseBox.Text = config.Settings.MySqlDefaultDatabase;
ProjectsDirectoryBox.Text = config.Settings.ProjectsPath;
// Load Behavior Settings
MinimizeToTrayCheckBox.IsChecked = config.Settings.MinimizeToTray;
StartMinimizedCheckBox.IsChecked = config.Settings.StartMinimized;
ShowTrayNotificationsCheckBox.IsChecked = config.Settings.ShowTrayNotifications;
AutoStartDockerCheckBox.IsChecked = config.Settings.AutoStartDocker;
KeepDockerRunningCheckBox.IsChecked = config.Settings.KeepDockerRunning;
// Set default status text
ApachePortStatus.Text = "Checking...";
PhpPortStatus.Text = "Checking...";
MySqlPortStatus.Text = "Checking...";
PhpMyAdminPortStatus.Text = "Checking...";
_ = Task.Run(async () =>
{
await Task.Delay(100);
Dispatcher.Invoke(() => CheckPorts_Click(null, null));
});
}
private void CheckForUpdates_Click(object sender, RoutedEventArgs e)
{
var mainWindow = Owner as MainWindow;
if (mainWindow != null)
{
mainWindow.CheckForUpdatesManually(sender, e);
}
}
private void SetupEventHandlers()
{
ApachePortTextBox.TextChanged += (s, e) => DelayedPortCheck();
PhpPortTextBox.TextChanged += (s, e) => DelayedPortCheck();
MySqlPortTextBox.TextChanged += (s, e) => DelayedPortCheck();
PhpMyAdminPortTextBox.TextChanged += (s, e) => DelayedPortCheck();
MailHogWebPortTextBox.TextChanged += (s, e) => DelayedPortCheck();
}
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
private Timer? portCheckTimer;
private void DelayedPortCheck()
{
portCheckTimer?.Dispose();
portCheckTimer = new Timer(async _ =>
{
await Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
CheckPortStatus(ApachePortTextBox, ApachePortStatus);
CheckPortStatus(PhpPortTextBox, PhpPortStatus);
CheckPortStatus(MySqlPortTextBox, MySqlPortStatus);
CheckPortStatus(PhpMyAdminPortTextBox, PhpMyAdminPortStatus);
CheckPortStatus(MailHogWebPortTextBox, MailHogWebPortStatus);
});
});
}, null, 500, Timeout.Infinite);
}
private void CheckPortStatus(TextBox portTextBox, TextBlock statusTextBlock)
{
if (int.TryParse(portTextBox.Text, out int port) && port > 0 && port <= 65535)
{
Task.Run(() =>
{
bool isAvailable = configManager.IsPortAvailable(port);
Dispatcher.Invoke(() =>
{
if (isAvailable)
{
statusTextBlock.Text = "✓ Available";
statusTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(76, 175, 80)); // Green
portTextBox.Background = Application.Current.Resources["TextBoxBackgroundBrush"] as SolidColorBrush ?? Brushes.White;
}
else
{
statusTextBlock.Text = "✗ Port in use";
statusTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(244, 67, 54)); // Red
portTextBox.Background = new SolidColorBrush(Color.FromRgb(255, 235, 238)); // Light red
}
});
});
}
else
{
statusTextBlock.Text = "✗ Invalid port";
statusTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(244, 67, 54)); // Red
portTextBox.Background = new SolidColorBrush(Color.FromRgb(255, 235, 238)); // Light red
}
}
private void CheckPorts_Click(object? sender, RoutedEventArgs? e)
{
Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
CheckPortStatus(ApachePortTextBox, ApachePortStatus);
CheckPortStatus(PhpPortTextBox, PhpPortStatus);
CheckPortStatus(MySqlPortTextBox, MySqlPortStatus);
CheckPortStatus(PhpMyAdminPortTextBox, PhpMyAdminPortStatus);
CheckPortStatus(MailHogWebPortTextBox, MailHogWebPortStatus);
});
});
}
private void ResetPorts_Click(object sender, RoutedEventArgs e)
{
var defaultConfig = new NodaStackConfiguration();
ApachePortTextBox.Text = defaultConfig.Ports["Apache"].ToString();
PhpPortTextBox.Text = defaultConfig.Ports["PHP"].ToString();
MySqlPortTextBox.Text = defaultConfig.Ports["MySQL"].ToString();
PhpMyAdminPortTextBox.Text = defaultConfig.Ports["phpMyAdmin"].ToString();
DelayedPortCheck();
}
private void BrowseProjectsPath_Click(object sender, RoutedEventArgs e)
{
var currentPath = ProjectsDirectoryBox.Text;
if (System.IO.Directory.Exists(currentPath))
{
System.Diagnostics.Process.Start("explorer.exe", currentPath);
}
else
{
System.Diagnostics.Process.Start("explorer.exe", System.IO.Directory.GetCurrentDirectory());
}
MessageBox.Show("Please copy the desired directory path and paste it in the text box.", "Select Directory", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void ResetAll_Click(object sender, RoutedEventArgs e)
{
var result = MessageBox.Show(
"Are you sure you want to reset all settings to defaults?\n\nThis action cannot be undone.",
"Reset All Settings",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
configManager.ResetToDefaults();
LoadConfiguration();
}
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
cancellationTokenSource.Cancel();
DialogResult = false;
Close();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
try
{
if (!ValidatePorts())
{
MessageBox.Show("Please fix port configuration issues before saving.", "Invalid Configuration", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var newPorts = new Dictionary<string, int>
{
{ "Apache", int.Parse(ApachePortTextBox.Text) },
{ "PHP", int.Parse(PhpPortTextBox.Text) },
{ "MySQL", int.Parse(MySqlPortTextBox.Text) },
{ "phpMyAdmin", int.Parse(PhpMyAdminPortTextBox.Text) },
{ "MailHogWeb", int.Parse(MailHogWebPortTextBox.Text) },
// Preserve SMTP port if exists, or default
{ "MailHogSMTP", configManager.Configuration.Ports.TryGetValue("MailHogSMTP", out int smtp) ? smtp : 1025 }
};
configManager.UpdatePorts(newPorts);
// Versions are managed in Docker files, not in UI
var newSettings = new NodaStackSettings
{
AutoStartServices = AutoStartCheckBox.IsChecked ?? false,
ShowNotifications = NotificationsCheckBox.IsChecked ?? true,
EnableLogging = DetailedLoggingCheckBox.IsChecked ?? true,
DarkMode = DarkModeCheckBox.IsChecked ?? false,
AutoRefreshProjects = AutoRefreshCheckBox.IsChecked ?? true,
EnableSsl = SslSupportCheckBox.IsChecked ?? false,
MySqlPassword = MySqlPasswordBox.Password,
MySqlDefaultDatabase = MySqlDatabaseBox.Text,
ProjectsPath = ProjectsDirectoryBox.Text,
DefaultBrowser = "default",
LogRetentionDays = 7,
AutoCheckUpdates = true,
AutoInstallUpdates = false,
Language = "en",
MinimizeToTray = MinimizeToTrayCheckBox.IsChecked ?? false,
StartMinimized = StartMinimizedCheckBox.IsChecked ?? false,
ShowTrayNotifications = ShowTrayNotificationsCheckBox.IsChecked ?? true,
AutoStartDocker = AutoStartDockerCheckBox.IsChecked ?? false,
KeepDockerRunning = KeepDockerRunningCheckBox.IsChecked ?? true,
};
configManager.UpdateSettings(newSettings);
// Sauvegarder explicitement la configuration
configManager.SaveConfiguration();
MessageBox.Show("Configuration saved successfully!", "Configuration Saved", MessageBoxButton.OK, MessageBoxImage.Information);
DialogResult = true;
Close();
}
catch (Exception ex)
{
MessageBox.Show($"Error saving configuration: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private bool ValidatePorts()
{
var ports = new[]
{
ApachePortTextBox.Text,
PhpPortTextBox.Text,
MySqlPortTextBox.Text,
PhpMyAdminPortTextBox.Text,
MailHogWebPortTextBox.Text
};
var parsedPorts = new List<int>();
foreach (var portText in ports)
{
if (!int.TryParse(portText, out int port) || port <= 0 || port > 65535)
{
return false;
}
if (parsedPorts.Contains(port))
{
MessageBox.Show($"Port {port} is used multiple times. Each service must use a different port.", "Duplicate Port", MessageBoxButton.OK, MessageBoxImage.Warning);
return false;
}
parsedPorts.Add(port);
}
return true;
}
private NodaStackConfiguration? JsonClone(NodaStackConfiguration original)
{
var json = JsonSerializer.Serialize(original);
return JsonSerializer.Deserialize<NodaStackConfiguration>(json);
}
protected override void OnClosed(EventArgs e)
{
cancellationTokenSource.Cancel();
portCheckTimer?.Dispose();
base.OnClosed(e);
}
private async void CheckLatestVersion()
{
try
{
var updateManager = new UpdateManager();
var (hasUpdate, info) = await updateManager.CheckForUpdatesAsync();
if (info != null)
{
LatestVersionText.Text = info.Version;
if (hasUpdate)
{
LatestVersionText.Foreground = new SolidColorBrush(Colors.Green);
DownloadUpdateButton.Visibility = Visibility.Visible;
}
else
{
LatestVersionText.Foreground = new SolidColorBrush(Colors.Black);
DownloadUpdateButton.Visibility = Visibility.Collapsed;
}
}
else
{
LatestVersionText.Text = "Unknown";
LatestVersionText.Foreground = new SolidColorBrush(Colors.Gray);
DownloadUpdateButton.Visibility = Visibility.Collapsed;
}
}
catch (Exception)
{
LatestVersionText.Text = "Error checking";
LatestVersionText.Foreground = new SolidColorBrush(Colors.Red);
DownloadUpdateButton.Visibility = Visibility.Collapsed;
}
}
private async void DownloadAndInstallUpdate_Click(object sender, RoutedEventArgs e)
{
var mainWindow = Owner as MainWindow;
if (mainWindow != null)
{
await mainWindow.DownloadAndInstallUpdate();
}
}
// Méthodes pour les nouveaux événements Click
private void BrowseProjectsButton_Click(object sender, RoutedEventArgs e)
{
BrowseProjectsPath_Click(sender, e);
}
private void CheckUpdatesButton_Click(object sender, RoutedEventArgs e)
{
CheckForUpdates_Click(sender, e);
}
private void ResetButton_Click(object sender, RoutedEventArgs e)
{
ResetAll_Click(sender, e);
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
Cancel_Click(sender, e);
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
Save_Click(sender, e);
}
}
}