Rethinking of this class:

- private static Config object
- Designated methods for providing fields of said Config object
- Config info isn't fetched from the file all the time, but rather
only once in the Init() method. After that, data is fetched from the
Configuration object
This commit is contained in:
2023-03-28 10:59:44 +02:00
parent 75341f55db
commit 9ffbabdd35

View File

@@ -4,26 +4,28 @@ namespace Password_Manager
{ {
static class ConfigFileManager static class ConfigFileManager
{ {
private static string configPath = SpecialDirectories.CurrentUserApplicationData + "\\config.cfg"; #region Config flags
/*Configuration: (format: fieldname=value)
#region Config flags password_store_path: path to the folder which contains the .gpg files
/*Configuration: (format: fieldname=value) recipient: whose public key to use for encryption when a new password is created
*/
private static const string CONFIGPATH = SpecialDirectories.CurrentUserApplicationData + "\\config.cfg";
private static const char DELIMETER = '=';
private static const string RECIPIENT = "recipient";
private static const string PATH = "password_store_path";
#endregion
pwdStorePath: path to the folder which contains the .gpg files private static Config Configuration;
recipient: whose public key to use for encryption when a new password is created
*/
private const char DELIMETER = '=';
private const string RECIPIENT = "recipient";
private const string PATH = "pwdStorePath";
#endregion public static void Init()
public static string GetPath() {
{
try try
{ {
StreamReader sr = new StreamReader(configPath); StreamReader sr = new StreamReader(CONFIGPATH);
if (sr != null) if (sr != null)
{ {
string? path, recipient;
while (!sr.EndOfStream) while (!sr.EndOfStream)
{ {
string[]? fields = sr.ReadLine().Split(DELIMETER); string[]? fields = sr.ReadLine().Split(DELIMETER);
@@ -31,14 +33,27 @@ namespace Password_Manager
{ {
if (fields[0] == PATH) if (fields[0] == PATH)
{ {
return fields[1]; path = fields[0];
} }
else if (fields[0] == RECIPIENT)
{
recipient = fields[0];
}
} }
else else //probably an empty line or something
{ {
continue; continue;
} }
} }
if (path != null && recipient != null)
{
Configuration = new Config(path, recipient);
}
else
{
throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file.");
}
} }
else else
{ {
@@ -53,15 +68,23 @@ namespace Password_Manager
{ {
MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(e.ToString(), "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
}
return @"C:\Users\Typo\pass"; //TODO: temporary default value public static string GetPath()
{
return Configuration.PasswordStorePath;
} }
public static string GetRecipient()
{
return Configuration.Recipient;
}
private static void CreateDefaultConfig() private static void CreateDefaultConfig()
{ {
try try
{ {
StreamWriter sw = new StreamWriter(configPath); StreamWriter sw = new StreamWriter(CONFIGPATH);
sw.WriteLine(RECIPIENT + DELIMETER + "typo"); sw.WriteLine(RECIPIENT + DELIMETER + "typo");
sw.WriteLine(PATH + DELIMETER + @"C:\Users\Typo\pass"); sw.WriteLine(PATH + DELIMETER + @"C:\Users\Typo\pass");
sw.Close(); sw.Close();