-
Notifications
You must be signed in to change notification settings - Fork 3
/
KeePassCompareExt.cs
163 lines (140 loc) · 6.44 KB
/
KeePassCompareExt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using KeePass.Plugins;
using KeePass.Util;
using KeePassLib;
namespace KeePassCompare {
public sealed class KeePassCompareExt : Plugin {
private ToolStripSeparator m_tsSeparator = null;
private ToolStripMenuItem m_tsmiMenuItem = null;
private ToolStripMenuItem m_tsmiMenuItem2 = null;
private IPluginHost m_host = null;
public string UpdateUrl = "https://raw.githubusercontent.com/georgekosmidis/KeePassCompare/master/version.txt";
public override bool Initialize( IPluginHost host ) {
if ( host == null ) return false;
m_host = host;
// Get a reference to the 'Tools' menu item container
ToolStripItemCollection tsMenu = m_host.MainWindow.ToolsMenu.DropDownItems;
// Add a separator at the bottom
m_tsSeparator = new ToolStripSeparator();
tsMenu.Add( m_tsSeparator );
// Add menu item 'KeePassCompare'
m_tsmiMenuItem = new ToolStripMenuItem();
m_tsmiMenuItem.Text = "KeePassCompare Compare!";
m_tsmiMenuItem.Click += this.OnMenuCompare;
tsMenu.Add( m_tsmiMenuItem );
// Add menu item 'KeePassCompare'
m_tsmiMenuItem2 = new ToolStripMenuItem();
m_tsmiMenuItem2.Text = "KeePassCompare Reset Colours";
m_tsmiMenuItem2.Click += this.OnResetColors;
tsMenu.Add( m_tsmiMenuItem2 );
return true;
}
public override void Terminate() {
// Remove all of our menu items
ToolStripItemCollection tsMenu = m_host.MainWindow.ToolsMenu.DropDownItems;
m_tsmiMenuItem.Click -= this.OnMenuCompare;
m_tsmiMenuItem2.Click -= this.OnResetColors;
tsMenu.Remove( m_tsmiMenuItem );
tsMenu.Remove( m_tsmiMenuItem2 );
tsMenu.Remove( m_tsSeparator );
}
private void OnResetColors( object sender, EventArgs e ) {
var d = m_host.MainWindow.DocumentManager.Documents;
for ( int i=0; i < d.Count; i++ ) {
var g = d[i].Database.RootGroup.GetEntries( true );
foreach ( var ent in g ) {
if ( ent.BackgroundColor == System.Drawing.Color.OrangeRed )
ent.BackgroundColor = System.Drawing.Color.White;
}
}
m_host.MainWindow.UpdateUI( false, null, true, null, true, null, false );
MessageBox.Show( "Done!" + Environment.NewLine + "Save the DB to keep changes!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning );
}
private void OnMenuCompare( object sender, EventArgs e ) {
var d = m_host.MainWindow.DocumentManager.Documents;
if ( d.Count <= 1 ) {
MessageBox.Show( "You need two databses to compare!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
return;
}
if ( d.Count > 2 ) {
MessageBox.Show( "Only two databases can be compared!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
return;
}
Cursor.Current = Cursors.WaitCursor;
entries.Clear();
d[0].Database.RootGroup.SortSubGroups( true );
d[1].Database.RootGroup.SortSubGroups( true );
recurse( d[0].Database.RootGroup, d[1].Database.RootGroup );
if ( entries.Count == 0 )
MessageBox.Show( "No differences found!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information );
else {
var s = "";
foreach ( var ent in entries ) {
var g = ent.ParentGroup;
var es = "";
while ( g != null ) {
es = g.Name + "/" + es;
g = g.ParentGroup;
}
s += es + ent.Strings.Get( "Title" ).ReadString() + Environment.NewLine;
}
if (MessageBox.Show( "The following differences were found:" + Environment.NewLine + "(copy to clipboard?)" + Environment.NewLine + s, "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information ) == DialogResult.Yes)
System.Windows.Forms.Clipboard.SetText(s);
}
m_host.MainWindow.UpdateUI( false, null, true, null, true, null, false );
Cursor.Current = Cursors.Default;
}
private void recurse( PwGroup pw1, PwGroup pw2 ) {
loop( pw1, pw2 );
loop( pw2, pw1 );
foreach ( var g1 in pw1.Groups ) {
foreach ( var g2 in pw2.Groups ) {
if ( g1.Name == g2.Name ) {//compG( g1, g2 )
recurse( g1, g2 );
break;
}
}
}
}
private List<PwEntry> entries = new List<PwEntry>();
private void loop( PwGroup g1, PwGroup g2 ) {
foreach ( var e1 in g1.Entries ) {
var found = false;
foreach ( var e2 in g2.Entries ) {
if ( compE( e1, e2 ) ) {
found = true;
break;
}
}
if ( !found ) {
entries.Add( e1 );
e1.BackgroundColor = System.Drawing.Color.OrangeRed;
}
else {
if ( e1.BackgroundColor == System.Drawing.Color.PaleVioletRed )
e1.BackgroundColor = System.Drawing.Color.Transparent;
}
}
}
#region compares
private PwGroupComparer prgc = new PwGroupComparer();
private bool compG( PwGroup pw1, PwGroup pw2 ) {
return prgc.Compare( pw1, pw2 ) > 0;
}
private PwEntryComparer prec = new PwEntryComparer( "", false, true );
private bool compE( PwEntry pe1, PwEntry pe2 ) {
// return prec.Compare( pe1, pe2 ) > 0;
//return pe1.LastModificationTime == pe2.LastModificationTime;
foreach ( var o in pe1.Strings ) {
if ( !pe2.Strings.Exists( o.Key ) || pe2.Strings.Get( o.Key ).ReadString() != o.Value.ReadString() )
return false;
}
return true;
}
#endregion
}
}