48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
|
|
namespace Password_Manager
|
|||
|
|
{
|
|||
|
|
public delegate string SearchQuery();
|
|||
|
|
public class PasswordListBox : ListBox
|
|||
|
|
{
|
|||
|
|
public event ProfileDataRequest? CurrentProfilePathRequest;
|
|||
|
|
public event SearchQuery? SearchQueryRequest;
|
|||
|
|
|
|||
|
|
public PasswordListBox() : base() { }
|
|||
|
|
|
|||
|
|
public void ReloadResults(object sender = null, EventArgs arg = null)
|
|||
|
|
{
|
|||
|
|
DirectoryInfo d;
|
|||
|
|
FileInfo[] files = new FileInfo[0];
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
d = new DirectoryInfo(CurrentProfilePathRequest?.Invoke());
|
|||
|
|
files = d.GetFiles("*.gpg");
|
|||
|
|
} catch (ArgumentNullException e)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show(e.ToString(), "Error: Invalid profile path", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string[] arrayTmp = new string[files.Length];
|
|||
|
|
for (int i = 0; i < arrayTmp.Length; i++)
|
|||
|
|
{
|
|||
|
|
arrayTmp[i] = files[i].Name;
|
|||
|
|
}
|
|||
|
|
List<string> elements = new List<string>(arrayTmp);
|
|||
|
|
|
|||
|
|
string[] copy = elements.ToArray();
|
|||
|
|
string searchQuery = SearchQueryRequest?.Invoke();
|
|||
|
|
if (searchQuery != "") //we have a search query
|
|||
|
|
{
|
|||
|
|
foreach (string s in copy)
|
|||
|
|
{
|
|||
|
|
if (!s.Contains(searchQuery))
|
|||
|
|
{
|
|||
|
|
elements.Remove(s);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.DataSource = elements;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|