This commit is contained in:
2023-03-27 00:19:30 +02:00
parent 01a926d316
commit df33170f71
28 changed files with 543 additions and 125 deletions

View File

@@ -1,17 +1,89 @@
using System;
using Password_Manager;
using Microsoft.VisualBasic.FileIO;
namespace Password_Manager
namespace Profiles
{
static class ProfileHandler
{
public static Profile CurrentProfile;
public static ProfileList ListOfProfiles;
private static string filePath = SpecialDirectories.CurrentUserApplicationData + "Profiles.csv";
public static void ProfileChange(object sender, EventArgs e)
public static void ChangeProfiles(string profileName)
{
Program.mainForm.searchBox.Clear();
Program.mainForm.resultList.ReloadResults();
Program.mainForm.profileSelection.DataSource = ListOfProfiles;
CurrentProfile = ListOfProfiles.SearchByName(profileName);
}
public static void Init()
{
ListOfProfiles = new ProfileList();
if (File.Exists(filePath))
{
LoadProfiles();
}
else
{
CreateProfiles();
}
}
private static void LoadProfiles()
{
try
{
StreamReader sr = new StreamReader(filePath);
string[] lines = sr.ReadToEnd().Split("\n");
foreach (string line in lines)
{
string[] fields = line.Split(',');
try
{
ListOfProfiles.Add(new Profile(fields[0], fields[1]));
} catch (IndexOutOfRangeException)
{
//File is messed up, creating new one
sr.Close();
CreateProfiles();
break;
}
}
sr.Close();
} catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void TrimLastLine()
{
List<string> lines = File.ReadAllLines(filePath).ToList();
File.WriteAllLines(filePath, lines.GetRange(0, lines.Count - 1).ToArray());
}
private static void CreateProfiles()
{
try
{
StreamWriter sw = new StreamWriter(filePath);
string[] parameters = new string[2];
NewProfileForm np = new NewProfileForm();
Profile firstProfile = null;
np.NewProfileRequest += (string profileName, string profilePath) =>
{
firstProfile = new Profile(profileName, profilePath);
ListOfProfiles.Add(firstProfile);
ChangeProfiles(firstProfile.Name);
sw.WriteLine($"{firstProfile.Name},{firstProfile.Path}");
sw.Close();
TrimLastLine();
};
np.ShowDialog();
} catch (IOException e)
{
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}