2023-03-27 00:19:30 +02:00
|
|
|
|
using Password_Manager;
|
|
|
|
|
|
using Microsoft.VisualBasic.FileIO;
|
2023-03-24 14:03:09 +01:00
|
|
|
|
|
2023-03-27 00:19:30 +02:00
|
|
|
|
namespace Profiles
|
2023-03-24 14:03:09 +01:00
|
|
|
|
{
|
|
|
|
|
|
static class ProfileHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
public static Profile CurrentProfile;
|
|
|
|
|
|
public static ProfileList ListOfProfiles;
|
2023-03-27 09:17:06 +02:00
|
|
|
|
private static string filePath = SpecialDirectories.CurrentUserApplicationData + "\\Profiles.csv";
|
|
|
|
|
|
|
|
|
|
|
|
public static void AddProfile(string profileName, string profilePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
ListOfProfiles.Add(new Profile(profileName, profilePath));
|
|
|
|
|
|
ChangeProfiles(profileName);
|
|
|
|
|
|
}
|
2023-03-24 14:03:09 +01:00
|
|
|
|
|
2023-03-27 00:19:30 +02:00
|
|
|
|
public static void ChangeProfiles(string profileName)
|
2023-03-24 14:03:09 +01:00
|
|
|
|
{
|
2023-03-27 09:17:06 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProfile = ListOfProfiles.SearchByName(profileName);
|
|
|
|
|
|
} catch (ProfileNotFoundException)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("The selected profile cannot be found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
}
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
ListOfProfiles = new ProfileList();
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
|
|
{
|
2023-03-27 09:17:06 +02:00
|
|
|
|
LoadProfilesFile();
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-03-27 09:17:06 +02:00
|
|
|
|
CreateProfilesFile();
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-27 09:17:06 +02:00
|
|
|
|
private static void LoadProfilesFile()
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
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();
|
2023-03-27 09:17:06 +02:00
|
|
|
|
CreateProfilesFile();
|
2023-03-27 00:19:30 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
sr.Close();
|
|
|
|
|
|
} catch (IOException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void TrimLastLine()
|
|
|
|
|
|
{
|
2023-03-27 09:17:06 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2023-03-27 00:19:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-27 09:17:06 +02:00
|
|
|
|
private static void CreateProfilesFile()
|
2023-03-27 00:19:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
StreamWriter sw = new StreamWriter(filePath);
|
|
|
|
|
|
|
|
|
|
|
|
string[] parameters = new string[2];
|
|
|
|
|
|
|
|
|
|
|
|
NewProfileForm np = new NewProfileForm();
|
|
|
|
|
|
np.NewProfileRequest += (string profileName, string profilePath) =>
|
|
|
|
|
|
{
|
2023-03-27 09:17:06 +02:00
|
|
|
|
Profile firstProfile = new Profile(profileName, profilePath);
|
2023-03-27 00:19:30 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2023-03-24 14:03:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|