Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show entry icons in tray menu #150

Merged
merged 2 commits into from
May 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 34 additions & 10 deletions KeeTrayTOTP/Menu/TrayMenuItemProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using KeePass.Plugins;
Expand Down Expand Up @@ -57,7 +58,7 @@ internal IEnumerable<ToolStripMenuItem> BuildMenuItemsForRootDropDown(List<PwDoc
{
if (documents.IsNotAtLeastOneDocumentOpen())
{
return new []
return new[]
{
new ToolStripMenuItem(Localization.Strings.NoDatabaseIsOpened, Resources.TOTP_Error)
};
Expand Down Expand Up @@ -91,7 +92,7 @@ private ToolStripMenuItem CreateDatabaseMenuItemForDocument(PwDocument document)
mainDropDownItem.DropDownClosed += OnDropDownClosed;
mainDropDownItem.DropDownItems.Add(CreatePseudoToolStripMenuItem());
}

mainDropDownItem.Tag = document;
return mainDropDownItem;
}
Expand Down Expand Up @@ -199,24 +200,47 @@ private void OnDropDownClosed(object sender, EventArgs e)

protected ToolStripMenuItem CreateMenuItemFromPwEntry(PwEntry entry, PwDatabase pwDatabase)
{
var context = new SprContext(entry, pwDatabase, SprCompileFlags.All, false,
false);
var context = new SprContext(entry, pwDatabase, SprCompileFlags.All, false, false);
var entryTitle = entry.Strings.ReadSafe(PwDefs.TitleField);
var entryUsername = SprEngine.Compile(entry.Strings.ReadSafe(PwDefs.UserNameField), context);

string trayTitle = TrimMenuItemTitleIfNecessary(entryTitle, entryUsername);

var menuItem = new ToolStripMenuItem(trayTitle, Resources.TOTP_Key, OnNotifyMenuTOTPClick);
menuItem.Tag = entry;
if (!Plugin.TOTPEntryValidator.SettingsValidate(entry))
var validEntry = Plugin.TOTPEntryValidator.SettingsValidate(entry);
var menuItem = new ToolStripMenuItem(trayTitle)
{
menuItem.Enabled = false;
menuItem.Image = Resources.TOTP_Error;
}
Tag = entry,
Image = validEntry ? GetEntryImage(entry, pwDatabase) : Resources.TOTP_Error,
Enabled = validEntry,
};
menuItem.Click += OnNotifyMenuTOTPClick;

return menuItem;
}

private Image GetEntryImage(PwEntry pe, PwDatabase pwDatabase)
{
if (pwDatabase != null)
{
if (!pe.CustomIconUuid.Equals(PwUuid.Zero))
{
int w = DpiUtil.ScaleIntX(16);
int h = DpiUtil.ScaleIntY(16);

return pwDatabase.GetCustomIcon(pe.CustomIconUuid, w, h);
}
var iconId = (int)pe.IconId;
if (PluginHost.MainWindow.ClientIcons != null &&
PluginHost.MainWindow.ClientIcons.Images != null &&
PluginHost.MainWindow.ClientIcons.Images.Count > iconId)
{
return PluginHost.MainWindow.ClientIcons.Images[iconId];
}
}

return Resources.TOTP_Key;
}

private string TrimMenuItemTitleIfNecessary(string entryTitle, string entryUsername)
{
if (string.IsNullOrEmpty(entryUsername) ||
Expand Down