-
Notifications
You must be signed in to change notification settings - Fork 50
/
EntryMenuItemProvider.cs
116 lines (98 loc) · 4.42 KB
/
EntryMenuItemProvider.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
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using KeePass.Plugins;
using KeePass.UI;
using KeePassLib;
using KeeTrayTOTP.Helpers;
namespace KeeTrayTOTP.Menu
{
public class EntryMenuItemProvider : IMenuItemProvider
{
private readonly KeeTrayTOTPExt _plugin;
private readonly IPluginHost _pluginHost;
public EntryMenuItemProvider(KeeTrayTOTPExt plugin, IPluginHost pluginHost)
{
_plugin = plugin;
_pluginHost = pluginHost;
}
public ToolStripMenuItem ProvideMenuItem()
{
var rootEntryMenuItem = new ToolStripMenuItem(Localization.Strings.TrayTOTPPlugin, Properties.Resources.TOTP);
// The ShortCutKeys assignments are required in this form and
// can not be setup as constructor parameters, because an implementation
// in the mono framework is missing.
// see https://github.com/mono/mono/issues/19755
var entryMenuCopyTotp = new ToolStripMenuItem(Localization.Strings.CopyTOTP, Properties.Resources.TOTP, OnEntryMenuTOTPClick)
{
ShortcutKeys = (Keys)Shortcut.CtrlT
};
var entryMenuSetupTotp = new ToolStripMenuItem(Localization.Strings.SetupTOTP, Properties.Resources.TOTP_Setup, OnEntryMenuSetupClick)
{
ShortcutKeys = (Keys)Shortcut.CtrlShiftI
};
var entryMenuShowQrCode = new ToolStripMenuItem(Localization.Strings.ShowQR, Properties.Resources.TOTP_Setup, OnEntryMenuShowQRClick)
{
ShortcutKeys = (Keys)Shortcut.CtrlShiftJ
};
rootEntryMenuItem.DropDownItems.Add(entryMenuCopyTotp);
rootEntryMenuItem.DropDownItems.Add(entryMenuSetupTotp);
rootEntryMenuItem.DropDownItems.Add(entryMenuShowQrCode);
rootEntryMenuItem.DropDownOpening += (sender, _) =>
{
entryMenuCopyTotp.Enabled = false;
entryMenuSetupTotp.Enabled = false;
entryMenuShowQrCode.Enabled = false;
entryMenuCopyTotp.Visible = _plugin.Settings.EntryContextCopyVisible;
entryMenuSetupTotp.Visible = _plugin.Settings.EntryContextSetupVisible;
if (_pluginHost.MainWindow.GetSelectedEntriesCount() == 1)
{
var currentEntry = _pluginHost.MainWindow.GetSelectedEntry(true);
if (_plugin.TOTPEntryValidator.HasSeed(currentEntry) &&
_plugin.TOTPEntryValidator.SettingsValidate(currentEntry))
{
entryMenuCopyTotp.Enabled = true;
entryMenuShowQrCode.Enabled = true;
}
entryMenuSetupTotp.Enabled = true;
}
};
rootEntryMenuItem.DropDownClosed += (sender, args) => entryMenuCopyTotp.Enabled = true;
return rootEntryMenuItem;
}
private void OnEntryMenuSetupClick(object sender, EventArgs e)
{
if (_pluginHost.MainWindow.GetSelectedEntriesCount() == 1)
{
UIUtil.ShowDialogAndDestroy(new SetupTOTP(_plugin, _pluginHost.MainWindow.GetSelectedEntry(true)));
_pluginHost.MainWindow.RefreshEntriesList();
}
}
private void OnEntryMenuShowQRClick(object sender, EventArgs e)
{
if (_pluginHost.MainWindow.GetSelectedEntriesCount() != 1)
{
return;
}
var entry = _pluginHost.MainWindow.GetSelectedEntry(true);
if (!_plugin.TOTPEntryValidator.HasSeed(entry))
{
return;
}
var rawSeed = _plugin.TOTPEntryValidator.SeedGet(entry).ReadString();
var cleanSeed = Regex.Replace(rawSeed, @"\s+", "").TrimEnd('=');
var issuer = entry.Strings.Get("Title").ReadString();
var username = entry.Strings.Get("UserName").ReadString();
UIUtil.ShowDialogAndDestroy(new ShowQR(cleanSeed, issuer, username));
_pluginHost.MainWindow.RefreshEntriesList();
}
private void OnEntryMenuTOTPClick(object sender, EventArgs e)
{
PwEntry pe = _pluginHost.MainWindow.GetSelectedEntry(false);
if (pe != null)
{
_plugin.TOTPCopyToClipboard(pe);
}
}
}
}