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

@@ -0,0 +1,36 @@
namespace Profiles
{
abstract class IList<T>
{
protected T[] list;
public int Length
{
get
{
return list.Length;
}
}
public T this[int index]
{
get
{
return list[index];
}
}
public IList()
{
this.list = new T[0];
}
public abstract void Add(T item);
public abstract void Remove(T item);
public abstract T SearchByName(string name);
public void Clear()
{
list = new T[0];
}
}
}

View File

@@ -1,10 +1,6 @@
using System;
namespace Password_Manager
namespace Profiles
{
public delegate void PasswordStoreChange();
sealed class Profile
public sealed class Profile
{
public string Name { get; } //the name of the password store profile ("personal", "work", or similar)
public string Path { get; } //path of the folder containing the password store

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);
}
}
}
}

View File

@@ -1,6 +1,4 @@
using System;
namespace Password_Manager
namespace Profiles
{
sealed class ProfileList : IList<Profile>
{
@@ -59,5 +57,15 @@ namespace Password_Manager
return result;
}
}
public string[] GetNameList()
{
string[] array = new string[this.Length];
for (int i = 0; i < this.Length; i++)
{
array[i] = this[i].Name;
}
return array;
}
}
}

View File

@@ -1,7 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace Password_Manager
namespace Profiles
{
sealed class ProfileNotFoundException : Exception
{