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
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2025 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.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using KeePass.App;
using KeePass.App.Configuration;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Delegates;
using KeePassLib.Security;
using KeePassLib.Utility;
namespace KeePass.Forms
{
public partial class ColumnsForm : Form
{
private bool m_bIgnoreHideCheckEvent = false;
public ColumnsForm()
{
InitializeComponent();
GlobalWindowManager.InitializeForm(this);
}
private void OnFormLoad(object sender, EventArgs e)
{
GlobalWindowManager.AddWindow(this);
BannerFactory.CreateBannerEx(this, m_bannerImage,
Properties.Resources.B48x48_View_Detailed,
KPRes.ConfigureColumns, KPRes.ConfigureColumnsDesc);
this.Icon = AppIcons.Default;
this.Text = KPRes.ConfigureColumns;
UIUtil.SetExplorerTheme(m_lvColumns, false);
float fWidth = (float)(m_lvColumns.ClientRectangle.Width -
UIUtil.GetVScrollBarWidth()) / 5.0f;
m_lvColumns.Columns.Add(KPRes.Column, (int)(fWidth * 3.0f));
m_lvColumns.Columns.Add(KPRes.Asterisks + " ***", (int)fWidth);
m_lvColumns.Columns.Add(KPRes.Toggle + " ***", (int)fWidth);
UpdateColumnPropInfo();
ThreadPool.QueueUserWorkItem(new WaitCallback(FillColumnsList));
Debug.Assert(KPRes.RememberHidingPasswordsMain.StartsWith(
StrUtil.RemoveAccelerator(m_cbRmbHidingPasswords.Text)));
m_cbRmbHidingPasswords.Checked = Program.Config.UI.Hiding.RememberHidingPasswordsMain;
}
private void AddAceColumnTh(AceColumn c)
{
string strLvgName = KPRes.StandardFields;
if(c.Type == AceColumnType.CustomString)
strLvgName = KPRes.CustomFields;
else if(c.Type == AceColumnType.PluginExt)
strLvgName = KPRes.PluginProvided;
else if((c.Type == AceColumnType.Size) || (c.Type == AceColumnType.LastPasswordModTime) ||
(c.Type == AceColumnType.GroupPath) || (c.Type == AceColumnType.GroupName))
strLvgName = KPRes.More;
ListViewGroup lvgContainer = null;
foreach(ListViewGroup lvg in m_lvColumns.Groups)
{
if(lvg.Header == strLvgName) { lvgContainer = lvg; break; }
}
if(lvgContainer == null)
{
lvgContainer = new ListViewGroup(strLvgName);
m_lvColumns.Groups.Add(lvgContainer);
}
Debug.Assert(lvgContainer == m_lvColumns.Groups[m_lvColumns.Groups.Count - 1]);
ListViewItem lvi = new ListViewItem(c.GetDisplayName());
lvi.Tag = c;
lvi.SubItems.Add(c.HideWithAsterisks ? KPRes.Yes : KPRes.No);
if(c.Type == AceColumnType.Password)
lvi.SubItems.Add(UIUtil.GetKeysName(Keys.Control | Keys.H));
else if(c.Type == AceColumnType.UserName)
lvi.SubItems.Add(UIUtil.GetKeysName(Keys.Control | Keys.J));
else lvi.SubItems.Add(string.Empty);
bool bChecked = false;
List<AceColumn> lCur = Program.Config.MainWindow.EntryListColumns;
foreach(AceColumn cCur in lCur)
{
if(cCur.Type != c.Type) continue;
if((c.Type != AceColumnType.CustomString) &&
(c.Type != AceColumnType.PluginExt))
{
bChecked = true;
break;
}
else if((c.Type == AceColumnType.CustomString) &&
(cCur.CustomName == c.CustomName))
{
bChecked = true;
break;
}
else if((c.Type == AceColumnType.PluginExt) &&
(cCur.CustomName == c.CustomName))
{
bChecked = true;
break;
}
}
lvi.Checked = bChecked;
lvi.Group = lvgContainer;
m_lvColumns.Items.Add(lvi);
}
private void AddAceColumn(List<AceColumn> lContainer, AceColumn c)
{
m_lvColumns.Invoke(new GAction<AceColumn>(AddAceColumnTh), c);
lContainer.Add(c);
}
private void AddStdAceColumn(List<AceColumn> lContainer, AceColumnType colType)
{
bool bHide = (colType == AceColumnType.Password); // Passwords hidden by default
int nWidth = -1;
List<AceColumn> lCur = Program.Config.MainWindow.EntryListColumns;
foreach(AceColumn cCur in lCur)
{
if(cCur.Type == colType)
{
bHide = cCur.HideWithAsterisks;
nWidth = cCur.Width;
break;
}
}
AddAceColumn(lContainer, new AceColumn(colType, string.Empty, bHide, nWidth));
}
private void FillColumnsList(object state)
{
List<AceColumn> l = new List<AceColumn>();
AddStdAceColumn(l, AceColumnType.Title);
AddStdAceColumn(l, AceColumnType.UserName);
AddStdAceColumn(l, AceColumnType.Password);
AddStdAceColumn(l, AceColumnType.Url);
AddStdAceColumn(l, AceColumnType.Notes);
AddStdAceColumn(l, AceColumnType.ExpiryTime);
AddStdAceColumn(l, AceColumnType.ExpiryTimeDateOnly);
AddStdAceColumn(l, AceColumnType.Attachment);
AddStdAceColumn(l, AceColumnType.AttachmentCount);
AddStdAceColumn(l, AceColumnType.Tags);
AddStdAceColumn(l, AceColumnType.OverrideUrl);
AddStdAceColumn(l, AceColumnType.Uuid);
AddStdAceColumn(l, AceColumnType.AutoTypeEnabled);
AddStdAceColumn(l, AceColumnType.AutoTypeSequences);
AddStdAceColumn(l, AceColumnType.CreationTime);
AddStdAceColumn(l, AceColumnType.LastModificationTime);
if((Program.Config.UI.UIFlags & (ulong)AceUIFlags.ShowLastAccessTime) != 0)
AddStdAceColumn(l, AceColumnType.LastAccessTime);
AddStdAceColumn(l, AceColumnType.HistoryCount);
AddStdAceColumn(l, AceColumnType.Size);
AddStdAceColumn(l, AceColumnType.LastPasswordModTime);
AddStdAceColumn(l, AceColumnType.GroupPath);
AddStdAceColumn(l, AceColumnType.GroupName);
SortedDictionary<string, AceColumn> d =
new SortedDictionary<string, AceColumn>(StrUtil.CaseIgnoreComparer);
List<AceColumn> lCur = Program.Config.MainWindow.EntryListColumns;
foreach(AceColumn cCur in lCur)
{
if((cCur.Type == AceColumnType.CustomString) &&
!d.ContainsKey(cCur.CustomName))
{
d[cCur.CustomName] = new AceColumn(AceColumnType.CustomString,
cCur.CustomName, cCur.HideWithAsterisks, cCur.Width);
}
}
foreach(PwDocument pwDoc in Program.MainForm.DocumentManager.Documents)
{
if(string.IsNullOrEmpty(pwDoc.LockedIoc.Path) && pwDoc.Database.IsOpen)
{
EntryHandler eh = delegate(PwEntry pe)
{
foreach(KeyValuePair<string, ProtectedString> kvp in pe.Strings)
{
if(PwDefs.IsStandardField(kvp.Key)) continue;
if(d.ContainsKey(kvp.Key)) continue;
d[kvp.Key] = new AceColumn(AceColumnType.CustomString,
kvp.Key, kvp.Value.IsProtected, -1);
}
return true;
};
pwDoc.Database.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);
}
}
foreach(KeyValuePair<string, AceColumn> kvpCustom in d)
{
AddAceColumn(l, kvpCustom.Value);
}
d.Clear();
// Add active plugin columns (including those of uninstalled plugins)
foreach(AceColumn cCur in lCur)
{
if(cCur.Type != AceColumnType.PluginExt) continue;
if(d.ContainsKey(cCur.CustomName)) { Debug.Assert(false); continue; }
d[cCur.CustomName] = new AceColumn(AceColumnType.PluginExt,
cCur.CustomName, cCur.HideWithAsterisks, cCur.Width);
}
// Add unused plugin columns
string[] vPlgExtNames = Program.ColumnProviderPool.GetColumnNames();
foreach(string strPlgName in vPlgExtNames)
{
if(d.ContainsKey(strPlgName)) continue; // Do not overwrite
d[strPlgName] = new AceColumn(AceColumnType.PluginExt, strPlgName,
false, -1);
}
foreach(KeyValuePair<string, AceColumn> kvpExt in d)
{
AddAceColumn(l, kvpExt.Value);
}
// m_lvColumns.Invoke(new UpdateUIDelegate(UpdateListEx), false);
}
private void UpdateListEx(bool bGuiToInternal)
{
if(bGuiToInternal)
{
}
else // Internal to GUI
{
foreach(ListViewItem lvi in m_lvColumns.Items)
{
AceColumn c = (lvi.Tag as AceColumn);
if(c == null) { Debug.Assert(false); continue; }
string str = (c.HideWithAsterisks ? KPRes.Yes : KPRes.No);
lvi.SubItems[1].Text = str;
}
}
}
private void UpdateColumnPropInfo()
{
ListView.SelectedListViewItemCollection lvsic = m_lvColumns.SelectedItems;
if((lvsic == null) || (lvsic.Count != 1) || (lvsic[0] == null))
{
m_grpColumn.Text = KPRes.SelectedColumn + ": (" + KPRes.None + ")";
m_cbHide.Checked = false;
m_cbHide.Enabled = false;
}
else
{
ListViewItem lvi = lvsic[0];
AceColumn c = (lvi.Tag as AceColumn);
if(c == null) { Debug.Assert(false); return; }
m_grpColumn.Text = KPRes.SelectedColumn + ": " + c.GetDisplayName();
m_cbHide.Enabled = true;
m_bIgnoreHideCheckEvent = true;
UIUtil.SetChecked(m_cbHide, c.HideWithAsterisks);
m_bIgnoreHideCheckEvent = false;
}
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
GlobalWindowManager.RemoveWindow(this);
}
private void OnBtnOK(object sender, EventArgs e)
{
AceMainWindow mw = Program.Config.MainWindow;
List<AceColumn> l = mw.EntryListColumns;
List<AceColumn> lOld = new List<AceColumn>(l);
l.Clear();
foreach(ListViewItem lvi in m_lvColumns.Items)
{
if(!lvi.Checked) continue;
AceColumn c = (lvi.Tag as AceColumn);
if(c == null) { Debug.Assert(false); continue; }
l.Add(c);
}
mw.EntryListColumnDisplayOrder = ComputeNewDisplayOrder(l,
lOld, mw.EntryListColumnDisplayOrder);
Program.Config.UI.Hiding.RememberHidingPasswordsMain = m_cbRmbHidingPasswords.Checked;
}
private void OnBtnCancel(object sender, EventArgs e)
{
}
private void OnColumnsSelectedIndexChanged(object sender, EventArgs e)
{
UpdateColumnPropInfo();
}
private void OnHideCheckedChanged(object sender, EventArgs e)
{
if(m_bIgnoreHideCheckEvent) return;
bool bChecked = m_cbHide.Checked;
foreach(ListViewItem lvi in m_lvColumns.SelectedItems)
{
AceColumn c = (lvi.Tag as AceColumn);
if(c == null) { Debug.Assert(false); continue; }
if((c.Type == AceColumnType.Password) && c.HideWithAsterisks &&
!AppPolicy.Try(AppPolicyId.UnhidePasswords))
{
// Do not change c.HideWithAsterisks
}
else c.HideWithAsterisks = bChecked;
}
UpdateListEx(false);
}
private sealed class AceColumnWithTag
{
public readonly AceColumn Column;
public readonly string TypeNameEx;
public long Tag;
public AceColumnWithTag(AceColumn c, long lTag)
{
this.Column = c;
this.TypeNameEx = c.GetTypeNameEx();
this.Tag = lTag;
}
#if DEBUG
public override string ToString()
{
return (this.TypeNameEx + ", " + this.Tag.ToString());
}
#endif
public static int CompareByTags(AceColumnWithTag x, AceColumnWithTag y)
{
return x.Tag.CompareTo(y.Tag);
}
}
private static string ComputeNewDisplayOrder(List<AceColumn> lNew,
List<AceColumn> lOld, string strOldOrder)
{
if((lNew == null) || (lOld == null)) { Debug.Assert(false); return string.Empty; }
if(string.IsNullOrEmpty(strOldOrder)) return string.Empty;
List<AceColumnWithTag> lOldS = new List<AceColumnWithTag>();
try
{
int[] vOld = StrUtil.DeserializeIntArray(strOldOrder);
if((vOld == null) || (vOld.Length != lOld.Count))
{
Debug.Assert(false);
return string.Empty;
}
for(int i = 0; i < vOld.Length; ++i)
{
if(lOld[i] == null) { Debug.Assert(false); return string.Empty; }
lOldS.Add(new AceColumnWithTag(lOld[i], vOld[i]));
}
lOldS.Sort(AceColumnWithTag.CompareByTags);
}
catch(Exception) { Debug.Assert(false); return string.Empty; }
List<AceColumnWithTag> l = new List<AceColumnWithTag>();
foreach(AceColumn c in lNew)
{
if(c != null) l.Add(new AceColumnWithTag(c, 0));
else { Debug.Assert(false); return string.Empty; }
}
long m = Math.Max(lNew.Count, lOld.Count);
// Preserve order of previous columns
for(int i = 0; i < lOldS.Count; ++i)
{
string strOldName = lOldS[i].TypeNameEx;
foreach(AceColumnWithTag ct in l)
{
if(ct.TypeNameEx == strOldName)
{
ct.Tag = m * i;
break;
}
}
}
// Insert new columns based on their default position
for(int i = 1; i < l.Count; ++i)
{
if(l[i].Tag == 0) l[i].Tag = l[i - 1].Tag + 1;
}
l.Sort(AceColumnWithTag.CompareByTags);
int[] v = new int[lNew.Count];
for(int i = 0; i < v.Length; ++i)
{
for(int j = 0; j < l.Count; ++j)
{
if(object.ReferenceEquals(l[j].Column, lNew[i]))
{
v[i] = j;
break;
}
}
}
Debug.Assert(Array.IndexOf(v, 0) == Array.LastIndexOf(v, 0));
return StrUtil.SerializeIntArray(v);
}
}
}
|