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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using KeePass.App;
using KeePass.UI;
using KeePass.Resources;
using KeePassLib;
using KeePassLib.Delegates;
using KeePassLib.Security;
using KeePassLib.Collections;
namespace KeePass.Forms
{
public partial class EditStringForm : Form
{
private ProtectedStringDictionary m_vStringDict = null;
private string m_strStringName = null;
private ProtectedString m_psStringInitialValue = null;
private RichTextBoxContextMenu m_ctxValue = new RichTextBoxContextMenu();
private PwDatabase m_pwContext = null;
private volatile List<string> m_vSuggestedNames = new List<string>();
public EditStringForm()
{
InitializeComponent();
Program.Translation.ApplyTo(this);
}
/// <summary>
/// Initialize the dialog. Needs to be called before the dialog is shown.
/// </summary>
/// <param name="vStringDict">String container. Must not be <c>null</c>.</param>
/// <param name="strStringName">Initial name of the string. May be <c>null</c>.</param>
/// <param name="psStringInitialValue">Initial value. May be <c>null</c>.</param>
public void InitEx(ProtectedStringDictionary vStringDict, string strStringName,
ProtectedString psStringInitialValue, PwDatabase pwContext)
{
Debug.Assert(vStringDict != null); if(vStringDict == null) throw new ArgumentNullException("vStringDict");
m_vStringDict = vStringDict;
m_strStringName = strStringName;
m_psStringInitialValue = psStringInitialValue;
m_pwContext = pwContext;
}
private void OnFormLoad(object sender, EventArgs e)
{
Debug.Assert(m_vStringDict != null); if(m_vStringDict == null) throw new InvalidOperationException();
GlobalWindowManager.AddWindow(this);
m_ctxValue.Attach(m_richStringValue, this);
string strTitle, strDesc;
if(m_strStringName == null)
{
strTitle = KPRes.AddStringField;
strDesc = KPRes.AddStringFieldDesc;
}
else
{
strTitle = KPRes.EditStringField;
strDesc = KPRes.EditStringFieldDesc;
}
BannerFactory.CreateBannerEx(this, m_bannerImage,
Properties.Resources.B48x48_Font, strTitle, strDesc);
this.Icon = new Icon("/usr/share/keepass2/KeePass.ico");
UIUtil.EnableAutoCompletion(m_cmbStringName, true);
UIUtil.PrepareStandardMultilineControl(m_richStringValue, true, true);
if(m_strStringName != null) m_cmbStringName.Text = m_strStringName;
if(m_psStringInitialValue != null)
{
m_richStringValue.Text = m_psStringInitialValue.ReadString();
m_cbProtect.Checked = m_psStringInitialValue.IsProtected;
}
ValidateStringName();
PopulateNamesComboBox();
// UIUtil.SetFocus(m_cmbStringName, this); // See PopulateNamesComboBox
}
private bool ValidateStringNameEx(string str)
{
if(str == null) { Debug.Assert(false); return false; }
if(PwDefs.IsStandardField(str)) return false;
if(str.Length <= 0) return false;
char[] vInvalidChars = new char[] { '{', '}' };
if(str.IndexOfAny(vInvalidChars) >= 0) return false;
string strStart = (m_strStringName != null) ? m_strStringName : string.Empty;
if(!strStart.Equals(str) && m_vStringDict.Exists(str)) return false;
// See ValidateStringName
return true;
}
private bool ValidateStringName()
{
string str = m_cmbStringName.Text;
string strStart = (m_strStringName != null) ? m_strStringName : string.Empty;
char[] vInvalidChars = new char[]{ '{', '}' };
if(PwDefs.IsStandardField(str))
{
m_lblValidationInfo.Text = KPRes.FieldNameInvalid;
m_cmbStringName.BackColor = AppDefs.ColorEditError;
m_btnOK.Enabled = false;
return false;
}
else if(str.Length <= 0)
{
m_lblValidationInfo.Text = KPRes.FieldNamePrompt;
m_cmbStringName.ResetBackColor();
m_btnOK.Enabled = false;
return false;
}
else if(str.IndexOfAny(vInvalidChars) >= 0)
{
m_lblValidationInfo.Text = KPRes.FieldNameInvalid;
m_cmbStringName.BackColor = AppDefs.ColorEditError;
m_btnOK.Enabled = false;
return false;
}
else if(!strStart.Equals(str) && m_vStringDict.Exists(str))
{
m_lblValidationInfo.Text = KPRes.FieldNameExistsAlready;
m_cmbStringName.BackColor = AppDefs.ColorEditError;
m_btnOK.Enabled = false;
return false;
}
else
{
m_lblValidationInfo.Text = string.Empty;
m_cmbStringName.ResetBackColor();
m_btnOK.Enabled = true;
}
// See ValidateStringNameEx
return true;
}
private void OnBtnOK(object sender, EventArgs e)
{
string strName = m_cmbStringName.Text;
if(!ValidateStringName())
{
this.DialogResult = DialogResult.None;
return;
}
if(m_strStringName == null) // Add string field
{
Debug.Assert(!m_vStringDict.Exists(strName));
}
else // Edit string field
{
if(!m_strStringName.Equals(strName))
m_vStringDict.Remove(m_strStringName);
}
ProtectedString ps = new ProtectedString(m_cbProtect.Checked,
m_richStringValue.Text);
m_vStringDict.Set(strName, ps);
}
private void OnBtnCancel(object sender, EventArgs e)
{
}
private void CleanUpEx()
{
m_ctxValue.Detach();
}
private void PopulateNamesComboBox()
{
ThreadStart ts = new ThreadStart(this.PopulateNamesCollectFunc);
Thread th = new Thread(ts);
th.Start();
}
private void PopulateNamesCollectFunc()
{
if(m_pwContext == null) { Debug.Assert(false); return; }
EntryHandler eh = delegate(PwEntry pe)
{
if(pe == null) { Debug.Assert(false); return true; }
foreach(KeyValuePair<string, ProtectedString> kvp in pe.Strings)
{
if(ValidateStringNameEx(kvp.Key) &&
!m_vSuggestedNames.Contains(kvp.Key))
{
m_vSuggestedNames.Add(kvp.Key);
}
}
return true;
};
m_pwContext.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);
m_vSuggestedNames.Sort();
if(m_cmbStringName.InvokeRequired)
m_cmbStringName.Invoke(new VoidDelegate(this.PopulateNamesAddFunc));
else this.PopulateNamesAddFunc();
}
private void PopulateNamesAddFunc()
{
foreach(string str in m_vSuggestedNames)
m_cmbStringName.Items.Add(str);
UIUtil.SetFocus(m_cmbStringName, this);
}
private void OnBtnHelp(object sender, EventArgs e)
{
AppHelp.ShowHelp(AppDefs.HelpTopics.Entry, AppDefs.HelpTopics.EntryStrings);
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
GlobalWindowManager.RemoveWindow(this);
}
private void OnNameTextChanged(object sender, EventArgs e)
{
ValidateStringName();
}
protected override bool ProcessDialogKey(Keys keyData)
{
if(((keyData == Keys.Return) || (keyData == Keys.Enter)) && m_richStringValue.Focused)
return false; // Forward to RichTextBox
return base.ProcessDialogKey(keyData);
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
CleanUpEx();
}
}
}
|