Skip to content

Commit

Permalink
write log file with duplicate keys/entries when doing migration,
Browse files Browse the repository at this point in the history
and notify user.
  • Loading branch information
smorks committed Oct 12, 2021
1 parent e13dfe7 commit 3ba6ef2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
36 changes: 35 additions & 1 deletion KeePassNatMsg/KeePassNatMsgExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ internal void MoveConfig(PwDatabase db, bool fromKpnm)
}
else
{
dupeKeys.Add(cd.Key);
dupeKeys.Add(id);
}
}

Expand All @@ -644,6 +644,40 @@ internal void MoveConfig(PwDatabase db, bool fromKpnm)
}
}

if (dupeKeys.Count > 0 || dupeEntries.Count > 0)
{
var lines = new List<string>();

if (dupeKeys.Count > 0)
{
lines.AddRange(new[] { string.Empty, "Duplicate Keys:", string.Empty });
lines.AddRange(dupeKeys);
lines.Add(string.Empty);
}

if (dupeEntries.Count > 0)
{
lines.AddRange(new[] { string.Empty, "Duplicate Entry Configs:", string.Empty });
lines.AddRange(dupeEntries.Select(x => x.Strings.ReadSafe(PwDefs.TitleField)));
lines.Add(string.Empty);
}

var file = Path.Combine(Path.GetTempPath(), string.Format("KeePassNatMsg-Migration-{0}.log", DateTime.Now));

File.WriteAllLines(file, lines, new System.Text.UTF8Encoding(false));

var title = dupeKeys.Count > 0 && dupeEntries.Count > 0 ? "Duplicate Keys/Entries Found" : dupeKeys.Count > 0 ? "Duplicate Keys Found" : "Duplicate Entries Found";
var result = MessageBox.Show("There were duplicates found. Please check the log file for more information:\r\n\r\n" + file + "\r\n\r\nDo you want to open the log file now?", title, MessageBoxButtons.YesNo);

if (result == DialogResult.Yes)
{
var psi = new System.Diagnostics.ProcessStartInfo(file);
psi.UseShellExecute = true;
psi.Verb = "open";
System.Diagnostics.Process.Start(psi);
}
}

// set db modified
HostInstance.MainWindow.UpdateUI(false, null, false, null, false, null, true);
}
Expand Down
8 changes: 4 additions & 4 deletions KeePassNatMsg/Options/OptionsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private void LoadDatabaseKeys(PwDatabase db)
if (db.IsOpen)
{
var keys = new List<DatabaseKeyItem>();
var dbKey = KeePassNatMsgExt.GetDbKey(chkUseKpxcSettingsKey.Checked);
var dbKey = KeePassNatMsgExt.GetDbKey(_config.UseKeePassXcSettings);

foreach (var cd in db.CustomData)
{
Expand Down Expand Up @@ -334,11 +334,11 @@ private void btnRemoveSelectedKeys_Click(object sender, EventArgs e)

if (db.IsOpen)
{
var dbKey = KeePassNatMsgExt.GetDbKey(chkUseKpxcSettingsKey.Checked);
var dbKey = KeePassNatMsgExt.GetDbKey(_config.UseKeePassXcSettings);

var items = dgvKeys.SelectedRows
.OfType<DataGridViewRow>()
.Select(x => dbKey + (x.DataBoundItem as DatabaseKeyItem) == null ? null : (x.DataBoundItem as DatabaseKeyItem).Name);
.Select(x => dbKey + ((x.DataBoundItem as DatabaseKeyItem) == null ? string.Empty : (x.DataBoundItem as DatabaseKeyItem).Name));

var deleteKeys = db.CustomData
.Where(x => items.Contains(x.Key))
Expand All @@ -354,7 +354,7 @@ private void btnRemoveAllKeys_Click(object sender, EventArgs e)

if (db.IsOpen)
{
var dbKey = KeePassNatMsgExt.GetDbKey(chkUseKpxcSettingsKey.Checked);
var dbKey = KeePassNatMsgExt.GetDbKey(_config.UseKeePassXcSettings);

var deleteKeys = db.CustomData
.Where(x => x.Key.StartsWith(dbKey))
Expand Down

0 comments on commit 3ba6ef2

Please sign in to comment.