welsonjs/WelsonJS.Toolkit/WelsonJS.Launcher/EnvForm.cs

203 lines
7.2 KiB
C#
Raw Normal View History

2024-10-03 09:09:14 +00:00
using System;
using System.Collections.Generic;
2024-10-03 10:48:05 +00:00
using System.IO;
2024-10-03 09:09:14 +00:00
using System.Windows.Forms;
namespace WelsonJS.Launcher
{
public partial class EnvForm : Form
{
2024-10-03 10:48:05 +00:00
private Dictionary<string, string> userVariables = new Dictionary<string, string>();
private string tempFilePath;
2024-10-03 09:09:14 +00:00
public EnvForm()
{
InitializeComponent();
2024-10-03 10:48:05 +00:00
InitializeListView();
// Set the variable file path in the temporary folder
2024-10-03 11:27:41 +00:00
tempFilePath = Path.Combine(Path.GetTempPath(), "welsonjs_default.env");
2024-10-03 10:48:05 +00:00
LoadUserVariables(); // Load variables
}
// Initialize ListView
private void InitializeListView()
{
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.Columns[0].Width = 150;
listView1.Columns[1].Width = 220;
listView1.SelectedIndexChanged += ListView1_SelectedIndexChanged;
}
2024-10-03 11:27:41 +00:00
// Load user-defined variables from the temporary folder in .env format
2024-10-03 10:48:05 +00:00
private void LoadUserVariables()
{
if (File.Exists(tempFilePath))
{
try
{
string fileContent = File.ReadAllText(tempFilePath);
2024-10-03 11:27:41 +00:00
// Split based on new line characters
string[] keyValuePairs = fileContent.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
2024-10-03 10:48:05 +00:00
userVariables = new Dictionary<string, string>();
foreach (string pair in keyValuePairs)
{
2024-10-03 11:27:41 +00:00
// Split by the first occurrence of '='
int indexOfEquals = pair.IndexOf('=');
if (indexOfEquals != -1)
2024-10-03 10:48:05 +00:00
{
2024-10-03 11:27:41 +00:00
string key = pair.Substring(0, indexOfEquals).Trim();
string value = pair.Substring(indexOfEquals + 1).Trim();
// Remove surrounding quotes if present
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2); // Remove the first and last character
}
// Unescape double quotes in the value
value = value.Replace("\\\"", "\"");
2024-10-03 10:48:05 +00:00
userVariables[key] = value;
}
2024-10-03 11:27:41 +00:00
else
{
MessageBox.Show($"Error parsing line: '{pair}'.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
2024-10-03 10:48:05 +00:00
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading variable file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
userVariables = new Dictionary<string, string>();
}
}
else
{
userVariables = new Dictionary<string, string>();
}
UpdateListView();
}
// Update ListView with current variables
private void UpdateListView()
{
listView1.Items.Clear();
foreach (var variable in userVariables)
{
var item = new ListViewItem(variable.Key);
item.SubItems.Add(variable.Value);
listView1.Items.Add(item);
}
}
// Handle ListView selection change
private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
var selectedItem = listView1.SelectedItems[0];
textSetName.Text = selectedItem.Text;
textSetValue.Text = selectedItem.SubItems[1].Text;
checkDeleteVariable.Checked = false;
}
}
// Handle OK button click
private void btnOk_Click(object sender, EventArgs e)
{
var name = textSetName.Text.Trim();
var value = textSetValue.Text.Trim();
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("Please enter a variable name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (checkDeleteVariable.Checked)
{
if (userVariables.ContainsKey(name))
{
userVariables.Remove(name);
MessageBox.Show("Variable deleted.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Variable not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
userVariables[name] = value;
MessageBox.Show("Variable saved.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
UpdateListView();
SaveUserVariables(); // Save variables
ClearInputFields();
}
2024-10-03 11:27:41 +00:00
// Save user-defined variables to the temporary folder in .env format
2024-10-03 10:48:05 +00:00
private void SaveUserVariables()
{
try
{
2024-10-03 11:27:41 +00:00
List<string> lines = new List<string>();
2024-10-03 10:48:05 +00:00
foreach (var variable in userVariables)
{
2024-10-03 11:27:41 +00:00
// Escape double quotes in the value
string value = variable.Value.Replace("\"", "\\\"");
// Enclose the value in double quotes if it contains spaces
if (value.Contains(" "))
{
value = $"\"{value}\"";
}
// Create the line in the format: KEY=VALUE
string line = $"{variable.Key}={value}";
lines.Add(line);
2024-10-03 10:48:05 +00:00
}
2024-10-03 11:27:41 +00:00
// Write lines to the file
File.WriteAllLines(tempFilePath, lines);
2024-10-03 10:48:05 +00:00
}
catch (Exception ex)
{
MessageBox.Show($"Error saving variable file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Clear input fields
private void ClearInputFields()
{
textSetName.Clear();
textSetValue.Clear();
checkDeleteVariable.Checked = false;
}
// Handle "Open Directory" button click
private void btnOpenDirectory_Click(object sender, EventArgs e)
{
var folderDialog = new FolderBrowserDialog();
if (folderDialog.ShowDialog() == DialogResult.OK)
{
textSetValue.Text = folderDialog.SelectedPath;
}
}
// Handle "Open File" button click
private void btnOpenFile_Click(object sender, EventArgs e)
{
var fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
{
textSetValue.Text = fileDialog.FileName;
}
2024-10-03 09:09:14 +00:00
}
}
2024-10-03 10:48:05 +00:00
}