Skip to content

Commit

Permalink
Sync colors with print & print preview
Browse files Browse the repository at this point in the history
Starting with KeePass 2.51 it is possible to color passwords in a printout.
You may now choose to sync those color values with the ones from ColoredPassword
  • Loading branch information
Rookiestyle committed May 15, 2022
1 parent 3662a96 commit b7702c7
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 377 deletions.
10 changes: 9 additions & 1 deletion Translations/ColoredPassword.de.language.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Translation>
<TranslationVersion>5</TranslationVersion>
<!--
Increment the TranslationVersion every time the translation file is updated
Also update the version.info file
-->
<TranslationVersion>6</TranslationVersion>
<item>
<key>Active</key>
<value>Aktiv</value>
Expand Down Expand Up @@ -41,4 +45,8 @@
<key>ColorPwGenDisplay</key>
<value>Passwörter farbig anzeigen</value>
</item>
<item>
<key>SyncColors</key>
<value>Synchronisiere Farben</value>
</item>
</Translation>
8 changes: 8 additions & 0 deletions Translations/ColoredPassword.template.language.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Translation>
<!--
Increment the TranslationVersion every time the translation file is updated
Also update the version.info file
-->
<TranslationVersion>0</TranslationVersion>
<item>
<key>Active</key>
Expand Down Expand Up @@ -41,4 +45,8 @@
<key>ColorPwGenDisplay</key>
<value>Color passwords in password generator</value>
</item>
<item>
<key>SyncColors</key>
<value>Snychronize font colors</value>
</item>
</Translation>
67 changes: 65 additions & 2 deletions src/ColoredPassword.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using KeePass.App.Configuration;
using KeePass.Plugins;
Expand Down Expand Up @@ -229,10 +230,70 @@ private void OnWindowAdded(object sender, GwmWindowEventArgs e)
{
if (e.Form is KeePass.Forms.KeyPromptForm) e.Form.Shown += OnKeyPromptFormShown;
else if (e.Form is KeePass.Forms.PwGeneratorForm) e.Form.Shown += OnPwGeneratorFormShown;
else if (e.Form is KeePass.Forms.PrintForm && Tools.KeePassVersion >= ColorConfig.KP_2_51) e.Form.Shown += OnPrintFormShown;
}

//Color passwords in password generator
private void OnPwGeneratorFormShown(object sender, EventArgs e)
private void OnPrintFormShown(object sender, EventArgs e)
{
var fPrint = sender as KeePass.Forms.PrintForm;
if (fPrint == null) return;
fPrint.Shown -= OnPrintFormShown;
if (!ColorConfig.Active) return;
if (!ColorConfig.SyncColorsWithPrintForm) return;
fPrint.FormClosed += OnPrintFormClosed;
//Adjust ColorButtonEx
Button cbe = Tools.GetControl("m_btnColorPU", fPrint) as Button;
if (cbe != null) SetColorButtonColor(cbe, ColorConfig.ForeColorDefault);
cbe = Tools.GetControl("m_btnColorPL", sender as Form) as Button;
if (cbe != null)
{
if (ColorConfig.LowercaseDifferent) SetColorButtonColor(cbe, ColorConfig.ForeColorLower);
else SetColorButtonColor(cbe, ColorConfig.ForeColorDefault);
}
cbe = Tools.GetControl("m_btnColorPD", sender as Form) as Button;
if (cbe != null) SetColorButtonColor(cbe, ColorConfig.ForeColorDigit);
cbe = Tools.GetControl("m_btnColorPO", sender as Form) as Button;
if (cbe != null) SetColorButtonColor(cbe, ColorConfig.ForeColorSpecial);

var mUpdateWebBrowser = fPrint.GetType().GetMethod("UpdateWebBrowser", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
if (mUpdateWebBrowser != null) mUpdateWebBrowser.Invoke(fPrint, new object[] { false });
}

private void OnPrintFormClosed(object sender, FormClosedEventArgs e)
{
var fPrint = sender as KeePass.Forms.PrintForm;
if (fPrint == null) return;
fPrint.FormClosed -= OnPrintFormClosed;
if (fPrint.DialogResult != DialogResult.OK) return;

Button cbe = Tools.GetControl("m_btnColorPU", fPrint) as Button;
if (cbe != null) ColorConfig.ForeColorDefault = GetColorButtonColor(cbe, ColorConfig.ForeColorDefault);
cbe = Tools.GetControl("m_btnColorPL", sender as Form) as Button;
if (cbe != null) ColorConfig.ForeColorLower = GetColorButtonColor(cbe, ColorConfig.ForeColorLower);
cbe = Tools.GetControl("m_btnColorPD", sender as Form) as Button;
if (cbe != null) ColorConfig.ForeColorDigit = GetColorButtonColor(cbe, ColorConfig.ForeColorDigit);
cbe = Tools.GetControl("m_btnColorPO", sender as Form) as Button;
if (cbe != null) ColorConfig.ForeColorSpecial = GetColorButtonColor(cbe, ColorConfig.ForeColorSpecial);
}

private Color GetColorButtonColor(Button cbe, Color cDefault)
{
var pColor = cbe.GetType().GetProperty("SelectedColor");
if (pColor == null) return cDefault;
object o = pColor.GetValue(cbe, null);
if (!(o is Color)) return cDefault;
return (Color)o;
}

private void SetColorButtonColor(Button bPU, Color cTextcolor)
{
var pColor = bPU.GetType().GetProperty("SelectedColor");
if (pColor == null) return;
pColor.SetValue(bPU, cTextcolor, null);
}

//Color passwords in password generator
private void OnPwGeneratorFormShown(object sender, EventArgs e)
{
(sender as Form).Shown -= OnPwGeneratorFormShown;
if (!ColorConfig.Active) return;
Expand Down Expand Up @@ -310,6 +371,7 @@ private void OptionsForm_Shown(object sender, EventArgs e)
o.bBackColorLower.BackColor = ColorConfig.BackColorLower;
o.cbColorEntryView.Checked = ColorConfig.ColorEntryView;
o.cbColorEntryViewKeepBackgroundColor.Checked = ColorConfig.ListViewKeepBackgroundColor;
o.cbSyncColorsWithPrintForm.Checked = ColorConfig.SyncColorsWithPrintForm;
o.cbSinglePwDisplay.Checked = ColorConfig.SinglePwDisplayActive;
o.cbColorPwGen.Checked = ColorConfig.ColorPwGen;
o.ctbExample.ColorText();
Expand Down Expand Up @@ -338,6 +400,7 @@ private void OptionsClosed(object sender, Tools.OptionsFormsEventArgs e)

ColorConfig.ColorEntryView = o.cbColorEntryView.Checked;
ColorConfig.ListViewKeepBackgroundColor = o.cbColorEntryViewKeepBackgroundColor.Checked;
ColorConfig.SyncColorsWithPrintForm = o.cbSyncColorsWithPrintForm.Checked;
SinglePwDisplay.Enabled = ColorConfig.SinglePwDisplayActive = o.cbSinglePwDisplay.Checked;
ColorConfig.ColorPwGen = o.cbColorPwGen.Checked;
ColorConfig.Write();
Expand Down
Loading

0 comments on commit b7702c7

Please sign in to comment.