Skip to content

Commit

Permalink
Merge pull request #30 from DHancock/current
Browse files Browse the repository at this point in the history
Tidy up code for next release
  • Loading branch information
DHancock authored Jun 1, 2020
2 parents 2a1b6ec + 7ea30b7 commit a87accf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 58 deletions.
50 changes: 13 additions & 37 deletions Source/CharacterChooserExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ You should have received a copy of the GNU General Public License

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
#if DEBUG
using System.IO;
#endif

using KeePass.Plugins;
using KeePass.Resources;
using KeePassLib;
using KeePassLib.Security;

using CharacterChooser.Properties;

namespace CharacterChooser
{

Expand All @@ -45,17 +44,8 @@ public CharacterChooserExt()
MenuItems = new List<ToolStripMenuItem>();
}

/// <summary>
/// The <c>Initialize</c> method is called by KeePass when
/// you should initialize your plug in.
/// </summary>
/// <param name="host">Plug in host interface. Through this
/// interface you can access the KeePass main window, the
/// currently opened database, etc.</param>
/// <returns>You must return <c>true</c> in order to signal
/// successful initialization. If you return <c>false</c>,
/// KeePass unloads your plug in (without calling the
/// <c>Terminate</c> method of your plug in).</returns>


public override bool Initialize(IPluginHost host)
{
if (host == null)
Expand All @@ -71,11 +61,11 @@ private void MainWindow_UIStateUpdated(object sender, EventArgs e)
{
if (MenuItems.Count > 0)
{
// only enable if one entry is selected and it has a valid field
bool enable = false;

if (Host.MainWindow.GetSelectedEntriesCount() == 1)
{
// only enable if one of the selected entry's relevant fields has some data
PwEntry entry = Host.MainWindow.GetSelectedEntry(true);

foreach (KeyValuePair<string, ProtectedString> kvp in entry.Strings)
Expand All @@ -96,9 +86,6 @@ private void MainWindow_UIStateUpdated(object sender, EventArgs e)

private static bool FieldIsValid(KeyValuePair<string, ProtectedString> field)
{
Debug.Assert(field.Value != null);
Debug.Assert(field.Key != null);

if (field.Value.IsEmpty)
return false;

Expand All @@ -124,7 +111,7 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t)
// In KeePass 2.41 this is called once and the single menu item added to an
// entry's context menu. In KeePass 2.42.1 it is called twice, the menus are
// added to the context menu and the new Entry drop down menu on the main window.
ToolStripMenuItem menuItem = new ToolStripMenuItem(Properties.Resources.menu_text, null, new ToolStripMenuItem());
ToolStripMenuItem menuItem = new ToolStripMenuItem(Resources.menu_text, null, new ToolStripMenuItem());


menuItem.DropDownOpening += delegate (object sender, EventArgs e)
Expand All @@ -134,7 +121,6 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t)

// rebuild current...
List<ToolStripMenuItem> userFields = new List<ToolStripMenuItem>();

PwEntry entry = Host.MainWindow.GetSelectedEntry(true);

foreach (KeyValuePair<string, ProtectedString> kvp in entry.Strings)
Expand All @@ -143,7 +129,7 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t)
{
ToolStripMenuItem subMenuItem = new ToolStripMenuItem
{
Image = Properties.Resources.Menu_16x,
Image = Resources.Menu_16x,
Tag = kvp.Value
};

Expand All @@ -165,9 +151,6 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t)
}
}

// the plugin's menu item shouldn't have been enabled
Debug.Assert((menuItem.DropDownItems.Count > 0) || (userFields.Count > 0));

if (userFields.Count > 0)
menuItem.DropDownItems.AddRange(userFields.ToArray());
};
Expand All @@ -191,8 +174,7 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType t)


/// <summary>
/// URL of a version information file. See
/// https://keepass.info/help/v2_dev/plg_index.html#upd
/// URL of a version information file
/// </summary>
public override string UpdateUrl
{
Expand All @@ -210,34 +192,28 @@ public override string UpdateUrl


/// <summary>
/// Get a handle to a 16x16 icon representing the plugin.
/// This icon is shown in the plugin management window of
/// KeePass for example.
/// a 16x16 icon representing the plugin
/// </summary>
public override Image SmallIcon
{
get { return Properties.Resources.Plugin_16x; }
get { return Resources.Plugin_16x; }
}



/// <summary>
/// The <c>Terminate</c> function is called by KeePass when
/// you should free all resources, close open files/streams,
/// etc. It is also recommended that you remove all your
/// plugin menu items from the KeePass menu.
/// Clean up when this plugin is unloaded
/// </summary>
public override void Terminate()
{
Host.MainWindow.UIStateUpdated -= MainWindow_UIStateUpdated;

foreach (ToolStripMenuItem menuItem in MenuItems)
{
{
ToolStrip parent = menuItem.GetCurrentParent();

if (parent != null)
parent.Items.Remove(menuItem);

menuItem.Dispose();
}

MenuItems.Clear();
Expand Down
39 changes: 18 additions & 21 deletions Source/CharacterChooserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ private CharacterChooserForm()

internal CharacterChooserForm(IPluginHost host, ToolStripItemCollection fields, ToolStripItem startField) : this()
{
Debug.Assert(host != null);
Debug.Assert(fields != null);
Debug.Assert(startField != null);

Host = host;

// the standard KeePass app icon
Expand Down Expand Up @@ -143,7 +139,7 @@ private void AdjustLayoutForPasswordFont()
{
Font passwordFont = KeePass.Program.Config.UI.PasswordFont.ToFont();

if (!Utils.FontEquals(passwordFont, charTextBox1.Font))
if (!Utils.EqivalentFont(passwordFont, charTextBox1.Font))
{
int verticalOffset = 0;

Expand Down Expand Up @@ -262,8 +258,6 @@ private void FieldComboBox_SelectedIndexChanged(object sender, EventArgs e)

private void IndexComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.Assert(sender is ComboBox);

bool entryProtected = false;
bool characterSelected = false;

Expand All @@ -284,7 +278,7 @@ private bool LoadCharacterTextBox(int selectedIndex, TextBox textBox)
{
ProtectedString pString = (ProtectedString)((ToolStripItem)fieldComboBox.SelectedItem).Tag;

Debug.Assert(selectedIndex <= pString.Length);
// the first combo box item is a dash indicating that there is no character selected

if (selectedIndex < 1)
textBox.Text = string.Empty;
Expand All @@ -307,7 +301,10 @@ private bool LoadCharacterTextBox(int selectedIndex, TextBox textBox)
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData == Keys.Escape) || (keyData == Keys.Enter))
if ((keyData == Keys.Escape) ||
(keyData == Keys.Enter) ||
(keyData == (Keys.Control | Keys.W)) ||
(keyData == (Keys.Alt | Keys.F4)))
{
Close();
return true;
Expand Down Expand Up @@ -348,9 +345,7 @@ private void ProtectButtonEnabledStateChanged(object sender, EventArgs e)

private void CharTextBox_TextChanged(object sender, EventArgs e)
{
Debug.Assert(sender is TextBox);

TextBox tb = sender as TextBox;
TextBox tb = (TextBox)sender;

if (string.IsNullOrEmpty(tb.Text))
tb.Enabled = false;
Expand All @@ -365,8 +360,7 @@ private void CharTextBox_TextChanged(object sender, EventArgs e)

private void ToolTip_Popup(object sender, PopupEventArgs e)
{
Debug.Assert(e.AssociatedControl is TextBox);
e.Cancel = (e.AssociatedControl as TextBox).UseSystemPasswordChar;
e.Cancel = ((TextBox)e.AssociatedControl).UseSystemPasswordChar;
}


Expand Down Expand Up @@ -429,9 +423,6 @@ private sealed class CharacterSelectorRow

public CharacterSelectorRow(ComboBox comboBox, TextBox textBox)
{
Debug.Assert(comboBox != null);
Debug.Assert(textBox != null);

IndexComboBox = comboBox;
CharTextBox = textBox;
}
Expand All @@ -458,13 +449,19 @@ public static int CalculateDropDownWidth(ComboBox comboBox)

return width;
}



public static bool FontEquals(Font left, Font right)
// Check fonts are the same. The Font.Equals() method
// is too strict.
public static bool EqivalentFont(Font left, Font right)
{
Debug.Assert(left != null);
Debug.Assert(right != null);
if (ReferenceEquals(left, right))
return true;

if ((left == null) || (right == null))
return false;

// the font family should never be null
return (left.FontFamily.Equals(right.FontFamily)) &&
(left.SizeInPoints == right.SizeInPoints) &&
(left.Style == right.Style);
Expand Down

0 comments on commit a87accf

Please sign in to comment.