bro i can't even keep track anymore

This commit is contained in:
2023-03-27 09:17:06 +02:00
parent df33170f71
commit 311a5cc4ca
12 changed files with 248 additions and 131 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
Password\ Manager/bin Password\ Manager/bin
Password\ Manager/obj Password\ Manager/obj
.vs/ .vs/
Password\ Manager/PasswordManagerForm.resx

Binary file not shown.

View File

@@ -1,6 +1,6 @@
namespace Password_Manager namespace Password_Manager
{ {
partial class MainForm sealed partial class MainForm : PasswordManagerForm
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
@@ -28,9 +28,9 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
searchBox = new TextBox(); SearchBox = new TextBox();
resultList = new PasswordListBox(); ResultList = new PasswordListBox();
profileSelection = new ComboBox(); ProfileSelection = new ComboBox();
addProfile = new Button(); addProfile = new Button();
removeProfile = new Button(); removeProfile = new Button();
generatePassword = new Button(); generatePassword = new Button();
@@ -38,82 +38,82 @@
// //
// searchBox // searchBox
// //
searchBox.Location = new Point(10, 20); SearchBox.Location = new Point(10, 20);
searchBox.Margin = new Padding(3, 2, 3, 2); SearchBox.Margin = new Padding(3, 2, 3, 2);
searchBox.Name = "searchBox"; SearchBox.Name = "searchBox";
searchBox.PlaceholderText = "Search for a password"; SearchBox.PlaceholderText = "Search for a password";
searchBox.Size = new Size(225, 23); SearchBox.Size = new Size(225, 23);
searchBox.TabIndex = 0; SearchBox.TabIndex = 0;
searchBox.TextChanged += resultList.ReloadResults; SearchBox.TextChanged += ResultList.ReloadResults;
// //
// resultList // resultList
// //
resultList.FormattingEnabled = true; ResultList.FormattingEnabled = true;
resultList.ItemHeight = 15; ResultList.ItemHeight = 15;
resultList.Location = new Point(10, 45); ResultList.Location = new Point(10, 45);
resultList.Margin = new Padding(3, 2, 3, 2); ResultList.Margin = new Padding(3, 2, 3, 2);
resultList.Name = "resultList"; ResultList.Name = "resultList";
resultList.Size = new Size(225, 274); ResultList.Size = new Size(225, 274);
resultList.TabIndex = 1; ResultList.TabIndex = 1;
resultList.CurrentProfilePathRequest += CurrentProfilePathRequest; ResultList.CurrentProfilePathRequest += () => CurrentProfilePathRequest();
resultList.SearchQueryRequest += () => searchBox.Text; ResultList.SearchQueryRequest += () => SearchBox.Text;
// //
// profileSelection // profileSelection
// //
profileSelection.DisplayMember = "Name"; ProfileSelection.DisplayMember = "Name";
profileSelection.DropDownHeight = 100; ProfileSelection.DropDownHeight = 100;
profileSelection.DropDownWidth = 200; ProfileSelection.DropDownWidth = 176;
profileSelection.FormattingEnabled = true; ProfileSelection.FormattingEnabled = true;
profileSelection.IntegralHeight = false; ProfileSelection.IntegralHeight = false;
profileSelection.Location = new Point(513, 20); ProfileSelection.Location = new Point(513, 20);
profileSelection.Margin = new Padding(3, 2, 3, 2); ProfileSelection.Margin = new Padding(3, 2, 3, 2);
profileSelection.Name = "profileSelection"; ProfileSelection.Name = "profileSelection";
profileSelection.Size = new Size(176, 23); ProfileSelection.Size = new Size(176, 23);
profileSelection.TabIndex = 2; ProfileSelection.TabIndex = 2;
profileSelection.SelectionChangeCommitted += ChangeProfile; ProfileSelection.SelectionChangeCommitted += ChangeProfile;
// //
// addProfile // addProfile
// //
addProfile.Location = new Point(513, 45); AddProfile.Location = new Point(513, 45);
addProfile.Margin = new Padding(3, 2, 3, 2); AddProfile.Margin = new Padding(3, 2, 3, 2);
addProfile.Name = "addProfile"; AddProfile.Name = "addProfile";
addProfile.Size = new Size(83, 22); AddProfile.Size = new Size(83, 22);
addProfile.TabIndex = 3; AddProfile.TabIndex = 3;
addProfile.Text = "Add"; AddProfile.Text = "Add";
addProfile.UseVisualStyleBackColor = true; AddProfile.UseVisualStyleBackColor = true;
addProfile.Click += AddProfile; AddProfile.Click += OpenProfileCreator;
// //
// removeProfile // removeProfile
// //
removeProfile.Location = new Point(605, 45); DeleteProfile.Location = new Point(605, 45);
removeProfile.Margin = new Padding(3, 2, 3, 2); DeleteProfile.Margin = new Padding(3, 2, 3, 2);
removeProfile.Name = "removeProfile"; DeleteProfile.Name = "removeProfile";
removeProfile.Size = new Size(83, 22); DeleteProfile.Size = new Size(83, 22);
removeProfile.TabIndex = 4; DeleteProfile.TabIndex = 4;
removeProfile.Text = "Delete"; DeleteProfile.Text = "Delete";
removeProfile.UseVisualStyleBackColor = true; DeleteProfile.UseVisualStyleBackColor = true;
// //
// button1 // button1
// //
generatePassword.Location = new Point(241, 20); GeneratePassword.Location = new Point(241, 20);
generatePassword.Name = "button1"; GeneratePassword.Name = "button1";
generatePassword.Size = new Size(75, 23); GeneratePassword.Size = new Size(75, 23);
generatePassword.TabIndex = 5; GeneratePassword.TabIndex = 5;
generatePassword.Text = "Generate"; GeneratePassword.Text = "Generate";
generatePassword.UseVisualStyleBackColor = true; GeneratePassword.UseVisualStyleBackColor = true;
generatePassword.Click += Generate; GeneratePassword.Click += OpenPasswordGenerator;
// //
// MainForm // MainForm
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(700, 338); ClientSize = new Size(700, 338);
Controls.Add(generatePassword); Controls.Add(GeneratePassword);
Controls.Add(removeProfile); Controls.Add(DeleteProfile);
Controls.Add(addProfile); Controls.Add(AddProfile);
Controls.Add(profileSelection); Controls.Add(ProfileSelection);
Controls.Add(resultList); Controls.Add(ResultList);
Controls.Add(searchBox); Controls.Add(SearchBox);
Margin = new Padding(3, 2, 3, 2); Margin = new Padding(3, 2, 3, 2);
Name = "MainForm"; Name = "MainForm";
Text = "Password Manager"; Text = "Password Manager";
@@ -123,11 +123,11 @@
#endregion #endregion
private TextBox searchBox; protected override TextBox SearchBox { get; }
private PasswordListBox resultList; protected override PasswordListBox ResultList { get; }
private ComboBox profileSelection; protected override ComboBox ProfileSelection { get; }
private Button addProfile; protected override Button AddProfile { get; }
private Button removeProfile; protected override Button DeleteProfile { get; }
private Button generatePassword; protected override Button GeneratePassword { get; }
} }
} }

View File

@@ -1,61 +1,43 @@
namespace Password_Manager namespace Password_Manager
{ {
public delegate void ProfileChanges(string profileName); sealed public partial class MainForm : PasswordManagerForm
public delegate string[] ProfileList();
public delegate void Save();
public partial class MainForm : Form
{ {
public event ProfileDataRequest? CurrentProfilePathRequest; public override event ProfileDataRequest CurrentProfilePathRequest;
public event ProfileDataRequest? CurrentProfileNameRequest; public override event ProfileDataRequest CurrentProfileNameRequest;
public event ProfileList? CurrentProfileListRequest; public override event ProfileList CurrentProfileListRequest;
public event ProfileChanges? CurrentProfileChanged; public override event ProfileChange CurrentProfileChanged;
public event Save? SaveRequest; public override event Save SaveRequest;
public override event NewProfile NewProfileRequest;
public MainForm(ProfileDataRequest CurrentProfileNameRequest, ProfileDataRequest CurrentProfilePathRequest, ProfileList CurrentProfileListRequest, ProfileChanges CurrentProfileChanged) public MainForm(ProfileDataRequest CurrentProfileNameRequest, ProfileDataRequest CurrentProfilePathRequest, ProfileList CurrentProfileListRequest, ProfileChange CurrentProfileChanged, Save SaveRequest, NewProfile NewProfileRequest)
: base(CurrentProfileNameRequest, CurrentProfilePathRequest, CurrentProfileListRequest, CurrentProfileChanged, SaveRequest, NewProfileRequest)
{ {
this.CurrentProfileNameRequest = CurrentProfileNameRequest;
this.CurrentProfilePathRequest = CurrentProfilePathRequest;
this.CurrentProfileListRequest = CurrentProfileListRequest;
this.CurrentProfileChanged = CurrentProfileChanged;
InitializeComponent(); InitializeComponent();
LoadCurrentProfile(); RefreshCurrentProfile();
SaveRequest();
foreach (string s in CurrentProfileListRequest?.Invoke())
{
profileSelection.Items.Add(s);
}
profileSelection.SelectedIndex = 0;
SaveRequest?.Invoke();
} }
private void LoadCurrentProfile() private void OpenProfileCreator(object sender, EventArgs e)
{
this.Text = CurrentProfileNameRequest?.Invoke();
resultList.ReloadResults();
}
private void ChangeProfile(object sender, EventArgs e)
{
CurrentProfileChanged?.Invoke(profileSelection.Text);
foreach (string s in CurrentProfileListRequest?.Invoke())
{
profileSelection.Items.Add(s);
}
}
private void AddProfile(object sender, EventArgs e)
{ {
NewProfileForm npf = new NewProfileForm(); NewProfileForm npf = new NewProfileForm();
npf.ReloadPasswordsRequest += () => resultList.ReloadResults(); npf.ReloadMainFormRequest += () => RefreshCurrentProfile();
npf.Show(); npf.SaveRequest += this.SaveRequest;
npf.NewProfileRequest += this.NewProfileRequest;
npf.ShowDialog();
} }
private void Generate(object sender, EventArgs e) private void OpenPasswordGenerator(object sender, EventArgs e)
{ {
GeneratePassword gp = new GeneratePassword(searchBox.Text, CurrentProfilePathRequest?.Invoke()); string? tmp = CurrentProfilePathRequest();
gp.Show(); if (tmp != null)
{
GeneratePassword gp = new GeneratePassword(SearchBox.Text, tmp);
gp.ShowDialog();
}
else
{
MessageBox.Show("No path specified", "Event error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
} }
} }

View File

@@ -1,14 +1,16 @@
namespace Password_Manager namespace Password_Manager
{ {
public delegate void NewProfile(string profileName, string profilePath);
public delegate void ReloadPasswords(); public delegate void ReloadPasswords();
public partial class NewProfileForm : Form public partial class NewProfileForm : Form
{ {
public event NewProfile NewProfileRequest; public event NewProfile? NewProfileRequest;
public event ReloadPasswords ReloadPasswordsRequest; public event ReloadPasswords? ReloadMainFormRequest;
public event Save? SaveRequest;
public NewProfileForm() public NewProfileForm()
{ {
//test
MessageBox.Show(this.Parent.Text, this.Parent.Text);
InitializeComponent(); InitializeComponent();
} }
@@ -26,13 +28,15 @@
if (nameTextBox.Text != "" && pathTextBox.Text != "") if (nameTextBox.Text != "" && pathTextBox.Text != "")
{ {
NewProfileRequest?.Invoke(nameTextBox.Text, pathTextBox.Text); NewProfileRequest?.Invoke(nameTextBox.Text, pathTextBox.Text);
ReloadPasswordsRequest?.Invoke(); ReloadMainFormRequest?.Invoke();
this.Close(); this.Close();
} }
else else
{ {
MessageBox.Show("You must fill in all fields to continue.", "Error: Empty fields", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("You must fill in all fields to continue.", "Error: Empty fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
SaveRequest?.Invoke();
} }
private void Cancel(object sender, EventArgs e) private void Cancel(object sender, EventArgs e)

View File

@@ -13,5 +13,8 @@
<Compile Update="PasswordListBox.cs"> <Compile Update="PasswordListBox.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Update="PasswordManagerForm.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,61 @@
namespace Password_Manager
{
public delegate void ProfileChange(string profileName); //Fires when we want to change the CurrentProfile field of ProfileHandler based on the combobox selection
public delegate string[] ProfileList(); //Fires when we need a list of Profile names, like for the combobox items
public delegate void Save(); //Fire whenever we want to save the current state of profiles
public delegate void NewProfile(string profileName, string profilePath); //Fires whenever a NewProfileForm has requested the creation of a new profile. This just forwards that request to ProfileHandler
public abstract class PasswordManagerForm : Form
{
protected abstract ComboBox ProfileSelection { get; }
protected abstract PasswordListBox ResultList { get; }
protected abstract TextBox SearchBox { get; }
protected abstract Button AddProfile { get; }
protected abstract Button DeleteProfile { get; }
protected abstract Button GeneratePassword { get; }
public abstract event ProfileDataRequest CurrentProfilePathRequest;
public abstract event ProfileDataRequest CurrentProfileNameRequest;
public abstract event ProfileList CurrentProfileListRequest;
public abstract event ProfileChange CurrentProfileChanged;
public abstract event Save SaveRequest;
public abstract event NewProfile NewProfileRequest;
public PasswordManagerForm(
ProfileDataRequest CurrentProfileNameRequest,
ProfileDataRequest CurrentProfilePathRequest,
ProfileList CurrentProfileListRequest,
ProfileChange CurrentProfileChanged,
Save SaveRequest,
NewProfile NewProfileRequest)
{
this.CurrentProfileNameRequest = CurrentProfileNameRequest;
this.CurrentProfilePathRequest = CurrentProfilePathRequest;
this.CurrentProfileListRequest = CurrentProfileListRequest;
this.CurrentProfileChanged = CurrentProfileChanged;
this.SaveRequest = SaveRequest;
this.NewProfileRequest = NewProfileRequest;
}
protected virtual void ChangeProfile(object sender, EventArgs e)
{
CurrentProfileChanged(ProfileSelection.Text);
}
public virtual void RefreshCurrentProfile()
{
ProfileSelection.SelectedIndex = -1;
string[] items = CurrentProfileListRequest();
for (int i = 0; i < items.Length; i++)
{
ProfileSelection.Items.Add(items[i]);
if (items[i] == CurrentProfileNameRequest())
{
ProfileSelection.SelectedIndex = i;
}
}
this.Text = CurrentProfileNameRequest();
ResultList.ReloadResults();
}
}
}

View File

@@ -1,6 +1,8 @@
namespace Profiles using System.Collections;
namespace Profiles
{ {
abstract class IList<T> abstract class IList<T> : IEnumerable<T>
{ {
protected T[] list; protected T[] list;
@@ -32,5 +34,15 @@
{ {
list = new T[0]; list = new T[0];
} }
public IEnumerator<T> GetEnumerator()
{
return ((IEnumerable<T>)list).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
} }
} }

View File

@@ -7,11 +7,23 @@ namespace Profiles
{ {
public static Profile CurrentProfile; public static Profile CurrentProfile;
public static ProfileList ListOfProfiles; public static ProfileList ListOfProfiles;
private static string filePath = SpecialDirectories.CurrentUserApplicationData + "Profiles.csv"; private static string filePath = SpecialDirectories.CurrentUserApplicationData + "\\Profiles.csv";
public static void AddProfile(string profileName, string profilePath)
{
ListOfProfiles.Add(new Profile(profileName, profilePath));
ChangeProfiles(profileName);
}
public static void ChangeProfiles(string profileName) public static void ChangeProfiles(string profileName)
{ {
CurrentProfile = ListOfProfiles.SearchByName(profileName); try
{
CurrentProfile = ListOfProfiles.SearchByName(profileName);
} catch (ProfileNotFoundException)
{
MessageBox.Show("The selected profile cannot be found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
public static void Init() public static void Init()
@@ -20,14 +32,14 @@ namespace Profiles
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
LoadProfiles(); LoadProfilesFile();
} }
else else
{ {
CreateProfiles(); CreateProfilesFile();
} }
} }
private static void LoadProfiles() private static void LoadProfilesFile()
{ {
try try
{ {
@@ -43,7 +55,7 @@ namespace Profiles
{ {
//File is messed up, creating new one //File is messed up, creating new one
sr.Close(); sr.Close();
CreateProfiles(); CreateProfilesFile();
break; break;
} }
} }
@@ -56,11 +68,53 @@ namespace Profiles
private static void TrimLastLine() private static void TrimLastLine()
{ {
List<string> lines = File.ReadAllLines(filePath).ToList(); try
File.WriteAllLines(filePath, lines.GetRange(0, lines.Count - 1).ToArray()); {
StreamReader sr = new StreamReader(filePath);
List<string> validLines = new List<string>();
while (!sr.EndOfStream)
{
string? line = sr.ReadLine();
if (line != null)
{
validLines.Add(line);
}
}
sr.Close();
File.WriteAllLines(filePath, validLines);
} catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} catch (UnauthorizedAccessException e)
{
MessageBox.Show(e.ToString(), "Can't access profiles file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
private static void CreateProfiles() public static void SaveProfiles()
{
try
{
StreamWriter sw = new StreamWriter(filePath);
foreach (Profile p in ListOfProfiles)
{
sw.WriteLine($"{p.Name},{p.Path}");
}
sw.Close();
} catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} catch (UnauthorizedAccessException e)
{
MessageBox.Show(e.ToString(), "Can't access profiles file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void CreateProfilesFile()
{ {
try try
{ {
@@ -69,10 +123,9 @@ namespace Profiles
string[] parameters = new string[2]; string[] parameters = new string[2];
NewProfileForm np = new NewProfileForm(); NewProfileForm np = new NewProfileForm();
Profile firstProfile = null;
np.NewProfileRequest += (string profileName, string profilePath) => np.NewProfileRequest += (string profileName, string profilePath) =>
{ {
firstProfile = new Profile(profileName, profilePath); Profile firstProfile = new Profile(profileName, profilePath);
ListOfProfiles.Add(firstProfile); ListOfProfiles.Add(firstProfile);
ChangeProfiles(firstProfile.Name); ChangeProfiles(firstProfile.Name);
sw.WriteLine($"{firstProfile.Name},{firstProfile.Path}"); sw.WriteLine($"{firstProfile.Name},{firstProfile.Path}");

View File

@@ -38,7 +38,7 @@
public override Profile SearchByName(string name) public override Profile SearchByName(string name)
{ {
Profile result = null; Profile? result = null;
for (int i = 0; i < this.Length; i++) for (int i = 0; i < this.Length; i++)
{ {

View File

@@ -2,7 +2,7 @@ using Profiles;
namespace Password_Manager namespace Password_Manager
{ {
public delegate string ProfileDataRequest(); public delegate string ProfileDataRequest(); //Fire whenever a specific field of ProfileHandler.CurrentProfile is needed
internal static class Program internal static class Program
{ {
/// <summary> /// <summary>
@@ -13,7 +13,6 @@ namespace Password_Manager
{ {
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
ProfileHandler.Init(); ProfileHandler.Init();
Application.Run(mainForm); Application.Run(mainForm);
} }
@@ -21,7 +20,9 @@ namespace Password_Manager
() => ProfileHandler.CurrentProfile.Name, () => ProfileHandler.CurrentProfile.Name,
() => ProfileHandler.CurrentProfile.Path, () => ProfileHandler.CurrentProfile.Path,
() => ProfileHandler.ListOfProfiles.GetNameList(), () => ProfileHandler.ListOfProfiles.GetNameList(),
(profileName) => ProfileHandler.ChangeProfiles(profileName) (profileName) => ProfileHandler.ChangeProfiles(profileName),
() => ProfileHandler.SaveProfiles(),
(profileName, profilePath) => ProfileHandler.AddProfile(profileName, profilePath)
); //needed at creation so that MainForm may pass this method down to other classes in its constructor ); //needed at creation so that MainForm may pass this method down to other classes in its constructor
} }
} }