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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
/*
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 KeePass.App;
using KeePass.Native;
using KeePass.Resources;
using KeePass.UI;
using KeePass.Util;
using KeePassLib;
using KeePassLib.Security;
using KeePassLib.Collections;
using KeePassLib.Utility;
using NativeLib = KeePassLib.Native.NativeLib;
namespace KeePass.Forms
{
public partial class EditAutoTypeItemForm : Form
{
private AutoTypeConfig m_atConfig = null;
private int m_iAssocIndex = -1;
private bool m_bEditSequenceOnly = false;
private string m_strDefaultSeq = string.Empty;
private ProtectedStringDictionary m_vStringDict = null;
// private Color m_clrOriginalForeground = Color.Black;
// private Color m_clrOriginalBackground = Color.White;
private List<Image> m_vWndImages = new List<Image>();
private RichTextBoxContextMenu m_ctxKeySeq = new RichTextBoxContextMenu();
private RichTextBoxContextMenu m_ctxKeyCodes = new RichTextBoxContextMenu();
private bool m_bBlockUpdates = false;
public EditAutoTypeItemForm()
{
InitializeComponent();
Program.Translation.ApplyTo(this);
}
public void InitEx(AutoTypeConfig atConfig, int iAssocIndex, bool bEditSequenceOnly,
string strDefaultSeq, ProtectedStringDictionary vStringDict)
{
Debug.Assert(atConfig != null); if(atConfig == null) throw new ArgumentNullException("atConfig");
m_atConfig = atConfig;
m_iAssocIndex = iAssocIndex;
m_bEditSequenceOnly = bEditSequenceOnly;
m_strDefaultSeq = (strDefaultSeq ?? string.Empty);
m_vStringDict = (vStringDict ?? new ProtectedStringDictionary());
}
private void OnFormLoad(object sender, EventArgs e)
{
Debug.Assert(m_atConfig != null); if(m_atConfig == null) throw new InvalidOperationException();
Debug.Assert(m_vStringDict != null); if(m_vStringDict == null) throw new InvalidOperationException();
GlobalWindowManager.AddWindow(this);
m_ctxKeySeq.Attach(m_rbKeySeq, this);
m_ctxKeyCodes.Attach(m_rtbPlaceholders, this);
if(!m_bEditSequenceOnly)
{
BannerFactory.CreateBannerEx(this, m_bannerImage,
Properties.Resources.B48x48_KCMSystem, KPRes.ConfigureAutoTypeItem,
KPRes.ConfigureAutoTypeItemDesc);
}
else // Edit keystrokes only
{
BannerFactory.CreateBannerEx(this, m_bannerImage,
Properties.Resources.B48x48_KCMSystem, KPRes.ConfigureKeystrokeSeq,
KPRes.ConfigureKeystrokeSeqDesc);
}
this.Icon = new Icon("/usr/share/keepass2/KeePass.ico");
// FontUtil.AssignDefaultBold(m_lblTargetWindow);
// FontUtil.AssignDefaultBold(m_rbSeqDefault);
// FontUtil.AssignDefaultBold(m_rbSeqCustom);
UIUtil.EnableAutoCompletion(m_cmbWindow, false);
// m_clrOriginalForeground = m_lblOpenHint.ForeColor;
// m_clrOriginalBackground = m_cmbWindow.BackColor;
// m_strOriginalWindowHint = m_lblTargetWindowInfo.Text;
InitPlaceholdersBox();
string strInitSeq = m_atConfig.DefaultSequence;
if(m_iAssocIndex >= 0)
{
AutoTypeAssociation asInit = m_atConfig.GetAt(m_iAssocIndex);
m_cmbWindow.Text = asInit.WindowName;
if(!m_bEditSequenceOnly) strInitSeq = asInit.Sequence;
}
else if(m_bEditSequenceOnly)
m_cmbWindow.Text = "(" + KPRes.Default + ")";
else strInitSeq = string.Empty;
bool bSetDefault = false;
m_bBlockUpdates = true;
if(strInitSeq.Length > 0) m_rbSeqCustom.Checked = true;
else
{
m_rbSeqDefault.Checked = true;
bSetDefault = true;
}
m_bBlockUpdates = false;
if(bSetDefault) m_rbKeySeq.Text = m_strDefaultSeq;
else m_rbKeySeq.Text = strInitSeq;
try
{
if(NativeLib.IsUnix()) PopulateWindowsListUnix();
else PopulateWindowsListWin();
}
catch(Exception) { Debug.Assert(false); }
EnableControlsEx();
}
private void InitPlaceholdersBox()
{
const string VkcBreak = @"<break />";
string[] vSpecialKeyCodes = new string[] {
"TAB", "ENTER", "UP", "DOWN", "LEFT", "RIGHT",
"HOME", "END", "PGUP", "PGDN",
"INSERT", "DELETE", VkcBreak,
"BACKSPACE", "BREAK", "CAPSLOCK",
"ESC", "HELP", "NUMLOCK", "PRTSC", "SCROLLLOCK", VkcBreak,
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"F13", "F14", "F15", "F16", VkcBreak,
"ADD", "SUBTRACT", "MULTIPLY", "DIVIDE"
};
string[] vSpecialPlaceholders = new string[] {
"GROUP", "GROUPPATH", "PASSWORD_ENC", "URL:RMVSCM",
"C:Comment", VkcBreak,
"DELAY 1000", "DELAY=200", "VKEY 65",
"PICKCHARS", "PICKCHARS:Password:C=3",
"NEWPASSWORD", "HMACOTP", "CLEARFIELD", VkcBreak,
"APPDIR", "DB_PATH", "DB_DIR", "DB_NAME", "DB_BASENAME", "DB_EXT",
"ENV_DIRSEP", VkcBreak,
"DT_SIMPLE", "DT_YEAR", "DT_MONTH", "DT_DAY", "DT_HOUR", "DT_MINUTE",
"DT_SECOND", "DT_UTC_SIMPLE", "DT_UTC_YEAR", "DT_UTC_MONTH",
"DT_UTC_DAY", "DT_UTC_HOUR", "DT_UTC_MINUTE", "DT_UTC_SECOND"
};
RichTextBuilder rb = new RichTextBuilder();
rb.AppendLine(KPRes.StandardFields, FontStyle.Bold, null, null, ":", null);
rb.Append("{" + PwDefs.TitleField + "} ");
rb.Append("{" + PwDefs.UserNameField + "} ");
rb.Append("{" + PwDefs.PasswordField + "} ");
rb.Append("{" + PwDefs.UrlField + "} ");
rb.Append("{" + PwDefs.NotesField + "}");
bool bCustomInitialized = false, bFirst = true;
foreach(KeyValuePair<string, ProtectedString> kvp in m_vStringDict)
{
if(!PwDefs.IsStandardField(kvp.Key))
{
if(bCustomInitialized == false)
{
rb.AppendLine();
rb.AppendLine();
rb.AppendLine(KPRes.CustomFields, FontStyle.Bold, null, null, ":", null);
bCustomInitialized = true;
}
if(!bFirst) rb.Append(" ");
rb.Append("{" + PwDefs.AutoTypeStringPrefix + kvp.Key + "}");
bFirst = false;
}
}
rb.AppendLine();
rb.AppendLine();
rb.AppendLine(KPRes.KeyboardKeyModifiers, FontStyle.Bold, null, null, ":", null);
rb.Append(KPRes.KeyboardKeyShift + @": +, ");
rb.Append(KPRes.KeyboardKeyCtrl + @": ^, ");
rb.Append(KPRes.KeyboardKeyAlt + @": %");
rb.AppendLine();
rb.AppendLine();
rb.AppendLine(KPRes.SpecialKeys, FontStyle.Bold, null, null, ":", null);
bFirst = true;
foreach(string strNav in vSpecialKeyCodes)
{
if(strNav == VkcBreak) { rb.AppendLine(); rb.AppendLine(); bFirst = true; }
else
{
if(!bFirst) rb.Append(" ");
rb.Append("{" + strNav + "}");
bFirst = false;
}
}
rb.AppendLine();
rb.AppendLine();
rb.AppendLine(KPRes.OtherPlaceholders, FontStyle.Bold, null, null, ":", null);
bFirst = true;
foreach(string strPH in vSpecialPlaceholders)
{
if(strPH == VkcBreak) { rb.AppendLine(); rb.AppendLine(); bFirst = true; }
else
{
if(!bFirst) rb.Append(" ");
rb.Append("{" + strPH + "}");
bFirst = false;
}
}
rb.Build(m_rtbPlaceholders);
LinkifyRtf(m_rtbPlaceholders);
}
private void OnFormShown(object sender, EventArgs e)
{
// Focusing doesn't work in OnFormLoad
if(m_cmbWindow.Enabled)
UIUtil.SetFocus(m_cmbWindow, this);
else if(m_rbKeySeq.Enabled)
UIUtil.SetFocus(m_rbKeySeq, this);
else UIUtil.SetFocus(m_btnOK, this);
}
private void CleanUpEx()
{
m_cmbWindow.OrderedImageList = null;
foreach(Image img in m_vWndImages)
{
if(img != null) img.Dispose();
}
m_vWndImages.Clear();
m_ctxKeyCodes.Detach();
m_ctxKeySeq.Detach();
}
private void OnBtnOK(object sender, EventArgs e)
{
EnableControlsEx();
Debug.Assert(m_btnOK.Enabled); if(!m_btnOK.Enabled) return;
string strNewSeq = (m_rbSeqCustom.Checked ? m_rbKeySeq.Text : string.Empty);
if(!m_bEditSequenceOnly)
{
AutoTypeAssociation atAssoc;
if(m_iAssocIndex >= 0) atAssoc = m_atConfig.GetAt(m_iAssocIndex);
else
{
atAssoc = new AutoTypeAssociation();
m_atConfig.Add(atAssoc);
}
atAssoc.WindowName = m_cmbWindow.Text;
atAssoc.Sequence = strNewSeq;
}
else m_atConfig.DefaultSequence = strNewSeq;
}
private void OnBtnCancel(object sender, EventArgs e)
{
}
private void OnBtnHelp(object sender, EventArgs e)
{
AppHelp.ShowHelp(AppDefs.HelpTopics.AutoType, null);
}
private void EnableControlsEx()
{
if(m_bBlockUpdates) return;
m_bBlockUpdates = true;
// string strItemName = m_cmbWindow.Text;
// bool bEnableOK = true;
// // string strError = string.Empty;
// if((m_atConfig.Get(strItemName) != null) && !m_bEditSequenceOnly)
// {
// if((m_strOriginalName == null) || !strItemName.Equals(m_strOriginalName))
// {
// bEnableOK = false;
// // strError = KPRes.FieldNameExistsAlready;
// }
// }
// // if((strItemName.IndexOf('{') >= 0) || (strItemName.IndexOf('}') >= 0))
// // {
// // bEnableOK = false;
// // // strError = KPRes.FieldNameInvalid;
// // }
// if(bEnableOK)
// {
// // m_lblTargetWindowInfo.Text = m_strOriginalWindowHint;
// // m_lblTargetWindowInfo.ForeColor = m_clrOriginalForeground;
// m_cmbWindow.BackColor = m_clrOriginalBackground;
// m_btnOK.Enabled = true;
// }
// else
// {
// // m_lblTargetWindowInfo.Text = strError;
// // m_lblTargetWindowInfo.ForeColor = Color.Red;
// m_cmbWindow.BackColor = AppDefs.ColorEditError;
// m_btnOK.Enabled = false;
// }
m_lblTargetWindow.Enabled = !m_bEditSequenceOnly;
m_cmbWindow.Enabled = !m_bEditSequenceOnly;
m_lblOpenHint.Enabled = !m_bEditSequenceOnly;
m_lnkWildcardRegexHint.Enabled = !m_bEditSequenceOnly;
// Workaround for disabled link render bug (gray too dark)
m_lnkWildcardRegexHint.Visible = !m_bEditSequenceOnly;
bool bCustom = m_rbSeqCustom.Checked;
m_rbKeySeq.Enabled = bCustom;
m_lblKeySeqInsertInfo.Enabled = bCustom;
m_rtbPlaceholders.Enabled = bCustom;
m_bBlockUpdates = false;
}
private void ColorizeKeySeq()
{
string strText = m_rbKeySeq.Text;
int iSelStart = m_rbKeySeq.SelectionStart, iSelLen = m_rbKeySeq.SelectionLength;
m_rbKeySeq.SelectAll();
m_rbKeySeq.SelectionBackColor = SystemColors.Window;
int iStart = 0;
while(true)
{
int iPos = strText.IndexOf('{', iStart);
if(iPos < 0) break;
int iEnd = strText.IndexOf('}', iPos + 1);
if(iEnd < 0) break;
m_rbKeySeq.Select(iPos, iEnd - iPos + 1);
m_rbKeySeq.SelectionBackColor = Color.FromArgb(212, 255, 212);
iStart = iEnd;
}
m_rbKeySeq.SelectionStart = iSelStart;
m_rbKeySeq.SelectionLength = iSelLen;
}
private void OnTextChangedKeySeq(object sender, EventArgs e)
{
ColorizeKeySeq();
}
private static void LinkifyRtf(RichTextBox rtb)
{
Debug.Assert(rtb.HideSelection); // Flicker otherwise
string str = rtb.Text;
int iPos = str.IndexOf('{');
while(iPos >= 0)
{
int iEnd = str.IndexOf('}', iPos);
if(iEnd >= 1)
{
rtb.Select(iPos, iEnd - iPos + 1);
UIUtil.RtfSetSelectionLink(rtb);
}
iPos = str.IndexOf('{', iPos + 1);
}
rtb.Select(0, 0);
}
private void OnPlaceholdersLinkClicked(object sender, LinkClickedEventArgs e)
{
if(!m_rbSeqCustom.Checked) m_rbSeqCustom.Checked = true;
int nSelStart = m_rbKeySeq.SelectionStart;
int nSelLength = m_rbKeySeq.SelectionLength;
string strText = m_rbKeySeq.Text;
string strUrl = e.LinkText;
if(nSelLength > 0)
strText = strText.Remove(nSelStart, nSelLength);
m_rbKeySeq.Text = strText.Insert(nSelStart, strUrl);
m_rbKeySeq.Select(nSelStart + strUrl.Length, 0);
UIUtil.SetFocus(m_rbKeySeq, this);
}
private void OnWindowTextUpdate(object sender, EventArgs e)
{
EnableControlsEx();
}
private void OnWindowSelectedIndexChanged(object sender, EventArgs e)
{
EnableControlsEx();
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
GlobalWindowManager.RemoveWindow(this);
}
private void OnWildcardRegexLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
AppHelp.ShowHelp(AppDefs.HelpTopics.AutoType, AppDefs.HelpTopics.AutoTypeWindowFilters);
}
private void OnSeqDefaultCheckedChanged(object sender, EventArgs e)
{
EnableControlsEx();
}
private void OnSeqCustomCheckedChanged(object sender, EventArgs e)
{
EnableControlsEx();
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
CleanUpEx();
}
private void PopulateWindowsListWin()
{
NativeMethods.EnumWindowsProc procEnum = delegate(IntPtr hWnd,
IntPtr lParam)
{
// Wrapped in try-catch, because it's passed to native code
try
{
string strName = NativeMethods.GetWindowText(hWnd, true);
if(!string.IsNullOrEmpty(strName))
{
if(((NativeMethods.GetWindowStyle(hWnd) &
NativeMethods.WS_VISIBLE) != 0) &&
AutoType.IsValidAutoTypeWindow(hWnd, false) &&
!NativeMethods.IsTaskBar(hWnd))
{
m_cmbWindow.Items.Add(strName);
m_vWndImages.Add(UIUtil.GetWindowImage(hWnd, true));
}
}
}
catch(Exception) { Debug.Assert(false); }
return true;
};
NativeMethods.EnumWindows(procEnum, IntPtr.Zero);
m_cmbWindow.OrderedImageList = m_vWndImages;
}
private void PopulateWindowsListUnix()
{
string strWindows = NativeMethods.RunXDoTool(
@"search --onlyvisible --name '.+' getwindowname %@");
if(string.IsNullOrEmpty(strWindows)) return;
strWindows = StrUtil.NormalizeNewLines(strWindows, false);
string[] vWindows = strWindows.Split(new char[]{ '\n' });
List<string> vListed = new List<string>();
for(int i = 0; i < vWindows.Length; ++i)
{
string str = vWindows[i].Trim();
bool bValid = true;
foreach(Form f in Application.OpenForms)
{
if(IsOwnWindow(f, str)) { bValid = false; break; }
}
if(!bValid) continue;
if((str.Length > 0) && (vListed.IndexOf(str) < 0))
{
m_cmbWindow.Items.Add(str);
vListed.Add(str);
}
}
}
private static bool IsOwnWindow(Control cRoot, string strText)
{
if(cRoot == null) { Debug.Assert(false); return false; }
if(cRoot.Text.Trim() == strText) return true;
foreach(Control cSub in cRoot.Controls)
{
if(cSub == cRoot) { Debug.Assert(false); continue; }
if(IsOwnWindow(cSub, strText)) return true;
}
return false;
}
}
}
|