2023-04-02 22:30:32 +02:00
|
|
|
|
using Common;
|
2023-04-02 22:10:42 +02:00
|
|
|
|
namespace GUI;
|
|
|
|
|
|
|
|
|
|
|
|
public delegate void MethodRequest();
|
|
|
|
|
|
public partial class GeneratePassword : Form
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
private string currentPath;
|
|
|
|
|
|
private string recipient;
|
|
|
|
|
|
public event MethodRequest ReloadRequest;
|
|
|
|
|
|
|
|
|
|
|
|
public GeneratePassword(string currentPath, string recipient, MethodRequest ReloadRequest, string? name)
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
passwordName.Text = name;
|
|
|
|
|
|
this.currentPath = currentPath;
|
|
|
|
|
|
this.recipient = recipient;
|
|
|
|
|
|
this.ReloadRequest = ReloadRequest;
|
|
|
|
|
|
}
|
2023-03-27 00:19:30 +02:00
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
public void Generate(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2023-04-16 20:41:33 +02:00
|
|
|
|
if (passwordName.Text == "")
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
2023-04-16 20:41:33 +02:00
|
|
|
|
MessageBox.Show("You must provide a name to continue.", "Error: Empty field", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
else
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
2023-04-16 20:41:33 +02:00
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
try
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
2023-04-16 20:41:33 +02:00
|
|
|
|
int length;
|
|
|
|
|
|
if (!int.TryParse(passwordLength.Text, out length))
|
|
|
|
|
|
{
|
|
|
|
|
|
length = PasswordGenerator.DEFAULT_LENGTH
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-02 22:46:08 +02:00
|
|
|
|
PasswordGenerator.ExceptionOccured += (e) => MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-04-02 22:10:42 +02:00
|
|
|
|
string filePath = $"{currentPath}\\{passwordName.Text}";
|
|
|
|
|
|
File.WriteAllText(
|
|
|
|
|
|
currentPath + $"\\{passwordName.Text}.gpg",
|
|
|
|
|
|
PasswordGenerator.New(
|
|
|
|
|
|
recipient,
|
2023-04-16 20:41:33 +02:00
|
|
|
|
length,
|
2023-04-02 22:10:42 +02:00
|
|
|
|
noSymbols.Checked)
|
|
|
|
|
|
);
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
catch (FormatException)
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
MessageBox.Show("Please enter a valid number for the password's length.", "Error: Invalid field", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (IOException error)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show(error.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
ReloadRequest();
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
2023-03-28 13:10:57 +02:00
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
private void cancel_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|