-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeePassPasswordPracticeExt.cs
132 lines (114 loc) · 3.81 KB
/
KeePassPasswordPracticeExt.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using KeePass;
using KeePass.Forms;
using KeePass.Plugins;
using KeePass.UI;
using KeePassLib;
namespace KeePassPasswordPractice
{
public class KeePassPasswordPracticeExt : Plugin
{
public const string ShortProductName = "PasswordPractice";
public override Image SmallIcon
{
get
{
return Properties.Resources.icon_16x16;
}
}
public override string UpdateUrl
{
get
{
return "https://github.com/Shayan-To/KeePassPasswordPractice/raw/master/keepass.version";
}
}
public override bool Initialize(IPluginHost host)
{
if (host == null)
{
return false;
}
//Debugger.Launch();
GlobalWindowManager.WindowAdded += WindowAddedHandler;
return true;
}
public override void Terminate()
{
GlobalWindowManager.WindowAdded -= WindowAddedHandler;
}
public override ToolStripMenuItem GetMenuItem(PluginMenuType t)
{
if (t == PluginMenuType.Entry)
{
return CreateToolStripItem(() =>
{
var entries = Program.MainForm.GetSelectedEntries();
if (entries != null && entries.Length == 1)
{
ShowDialog(entries[0]);
}
});
}
return null;
}
private static void WindowAddedHandler(object sender, GwmWindowEventArgs e)
{
var entryForm = e.Form as PwEntryForm;
if (entryForm != null)
{
entryForm.Shown += (s2, e2) =>
{
var m_lvHistory = entryForm.Controls.Find("m_lvHistory", true).FirstOrDefault() as CustomListViewEx;
if (m_lvHistory != null)
{
if (m_lvHistory.ContextMenuStrip == null)
{
m_lvHistory.ContextMenuStrip = new ContextMenuStrip();
}
m_lvHistory.ContextMenuStrip.Opening += (o, args) => args.Cancel = m_lvHistory.SelectedIndices.Count != 1;
m_lvHistory.ContextMenuStrip.Items.Add(CreateToolStripItem(() =>
{
var indices = m_lvHistory.SelectedIndices;
if (indices.Count != 1)
{
Debug.Assert(false);
return;
}
var entry = entryForm.EntryRef.History.GetAt((uint) indices[0]);
ShowDialog(entry);
}));
}
};
}
}
private static ToolStripMenuItem CreateToolStripItem(Action onClick)
{
var item = new ToolStripMenuItem
{
Text = "Practice password...",
Image = Properties.Resources.icon_16x16,
ShortcutKeys = Keys.Control | Keys.OemPipe,
ShortcutKeyDisplayString = @"Ctrl+\",
};
item.Click += (sender, args) => onClick();
return item;
}
private static void ShowDialog(PwEntry entry)
{
if (entry == null)
{
return;
}
using (var dialog = new PasswordPracticeForm(entry.Strings.Get("Password")))
{
dialog.ShowDialog();
}
}
}
}