public AddDlg(EncryptedStorage es, bool edit = false, KeyEntry entry = null) { InitializeComponent(); mStorage = es; mEditMode = edit; mKeyEntry = entry; }
private void btOK_Click(object sender, EventArgs e) { if (tbPassword.Text != tbRepeat.Text) { MessageBox.Show("Passwords don't match!", "Check passwords", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (mEditMode && tbPassword.Text == mKeyEntry.Password && tbComment.Text == mKeyEntry.Comment) { DialogResult = System.Windows.Forms.DialogResult.Cancel; return; // nothing to save } KeyEntry newEntry; if (mEditMode) { newEntry = (KeyEntry)mKeyEntry.Clone(); if (tbPassword.Text.Length > 0) newEntry.Password = tbPassword.Text; newEntry.Comment = tbComment.Text; } else { if (tbDomain.Text.Length == 0 || tbUser.Text.Length == 0 || tbPassword.Text.Length == 0) { MessageBox.Show("Please fill at least Domain, User and Password fields!", "Missing data", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } newEntry = new KeyEntry(tbDomain.Text, tbUser.Text, tbPassword.Text, tbComment.Text); } try { PasswordDlg pdlg = new PasswordDlg(true); if (pdlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; bool ok = true; if (mEditMode) ok = mStorage.ReplaceEntry(mKeyEntry, newEntry, pdlg.Password); else ok = mStorage.AddEntry(newEntry, pdlg.Password); if (!ok) MessageBox.Show("Operation failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { string msg = ex.Message; if (ex.InnerException != null) msg += "\r\n" + ex.InnerException.Message; MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } DialogResult = DialogResult.OK; }
public bool AddEntry(KeyEntry entry, string password) { string line = entry.ToLine(); string[] lines = GetKeys(password); if (lines == null) lines = new string[0]; string[] linesNew = new string[lines.Length + 1]; Array.Copy(lines, linesNew, lines.Length); linesNew[linesNew.Length - 1] = line; SaveKeys(linesNew, password); return true; }
public bool RemoveEntry(KeyEntry entry, string password) { string line = entry.ToLine(); string[] lines = GetKeys(password); if (lines == null) return false; var q = from l in lines where l != line select l; if (q.Count() != lines.Length - 1) return false; SaveKeys(q.ToArray(), password); return true; }
public bool ReplaceEntry(KeyEntry oldEntry, KeyEntry newEntry, string password) { if (newEntry.Domain != oldEntry.Domain || newEntry.User != oldEntry.User) throw new Exception("Entry has been modified in a way it shouldn't!"); string oldLine = oldEntry.ToLine(); string newLine = newEntry.ToLine(); string[] lines = GetKeys(password); if (lines == null) return false; bool replaced = false; for (int i = 0; i < lines.Length; ++i) if (lines[i] == oldLine) { lines[i] = newLine; replaced = true; } if (replaced) SaveKeys(lines, password); return replaced; }