File: AppConfigEx.cs

package info (click to toggle)
keepass2 2.60%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,892 kB
  • sloc: cs: 119,878; xml: 6,087; ansic: 2,033; cpp: 738; sh: 50; makefile: 42
file content (886 lines) | stat: -rw-r--r-- 26,043 bytes parent folder | download
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
  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.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

using KeePass.UI;
using KeePass.Util;
using KeePass.Util.XmlSerialization;

using KeePassLib;
using KeePassLib.Delegates;
using KeePassLib.Native;
using KeePassLib.Serialization;
using KeePassLib.Utility;

namespace KeePass.App.Configuration
{
	[XmlType(AppConfigEx.StrXmlTypeName)]
	public sealed partial class AppConfigEx
	{
		internal const string StrXmlTypeName = "Configuration";

		private string m_strSearchString = null;

		public AppConfigEx()
		{
		}

#if DEBUG
		~AppConfigEx()
		{
			Debug.Assert(m_strSearchString == null);
		}
#endif

		private AceMeta m_meta = null;
		public AceMeta Meta
		{
			get
			{
				if(m_meta == null) m_meta = new AceMeta();
				return m_meta;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_meta = value;
			}
		}

		private AceApplication m_aceApp = null;
		public AceApplication Application
		{
			get
			{
				if(m_aceApp == null) m_aceApp = new AceApplication();
				return m_aceApp;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_aceApp = value;
			}
		}

		private AceLogging m_aceLogging = null;
		public AceLogging Logging
		{
			get
			{
				if(m_aceLogging == null) m_aceLogging = new AceLogging();
				return m_aceLogging;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_aceLogging = value;
			}
		}

		private AceMainWindow m_uiMainWindow = null;
		public AceMainWindow MainWindow
		{
			get
			{
				if(m_uiMainWindow == null) m_uiMainWindow = new AceMainWindow();
				return m_uiMainWindow;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_uiMainWindow = value;
			}
		}

		private AceUI m_aceUI = null;
		public AceUI UI
		{
			get
			{
				if(m_aceUI == null) m_aceUI = new AceUI();
				return m_aceUI;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_aceUI = value;
			}
		}

		private AceSecurity m_sec = null;
		public AceSecurity Security
		{
			get
			{
				if(m_sec == null) m_sec = new AceSecurity();
				return m_sec;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_sec = value;
			}
		}

		private AceNative m_native = null;
		public AceNative Native
		{
			get
			{
				if(m_native == null) m_native = new AceNative();
				return m_native;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_native = value;
			}
		}

		private AcePasswordGenerator m_pwGen = null;
		public AcePasswordGenerator PasswordGenerator
		{
			get
			{
				if(m_pwGen == null) m_pwGen = new AcePasswordGenerator();
				return m_pwGen;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_pwGen = value;
			}
		}

		private AceSearch m_aceSearch = null;
		public AceSearch Search
		{
			get
			{
				if(m_aceSearch == null) m_aceSearch = new AceSearch();
				return m_aceSearch;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_aceSearch = value;
			}
		}

		private AceDefaults m_def = null;
		public AceDefaults Defaults
		{
			get
			{
				if(m_def == null) m_def = new AceDefaults();
				return m_def;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_def = value;
			}
		}

		private AceIntegration m_int = null;
		public AceIntegration Integration
		{
			get
			{
				if(m_int == null) m_int = new AceIntegration();
				return m_int;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_int = value;
			}
		}

		private AceCustomConfig m_cc = null;
		[XmlIgnore]
		public AceCustomConfig CustomConfig
		{
			get
			{
				if(m_cc == null) m_cc = new AceCustomConfig();
				return m_cc;
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_cc = value;
			}
		}

		[XmlArray("Custom")]
		[XmlArrayItem("Item")]
		public AceKvp[] CustomSerialized
		{
			get
			{
				return this.CustomConfig.Serialize();
			}
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				this.CustomConfig.Deserialize(value);
			}
		}

		/// <summary>
		/// Prepare for saving the configuration to disk. None of the
		/// modifications in this method need to be rolled back
		/// (for rollback, use <c>OnSavePre</c> / <c>OnSavePost</c>).
		/// </summary>
		private void PrepareSave()
		{
			// Member variables might be null
			AceMeta aceMeta = this.Meta;
			AceApplication aceApp = this.Application;
			AceSearch aceSearch = this.Search;
			AceDefaults aceDef = this.Defaults;

			aceMeta.Version = StrUtil.VersionToString(PwDefs.FileVersion64);
			aceMeta.OmitItemsWithDefaultValues = true;
			aceMeta.DpiFactorX = DpiUtil.FactorX; // For new (not loaded) cfgs.
			aceMeta.DpiFactorY = DpiUtil.FactorY;

			aceApp.LastUsedFile.ClearCredentials(true);

			foreach(IOConnectionInfo iocMru in aceApp.MostRecentlyUsed.Items)
				iocMru.ClearCredentials(true);

			if(!aceDef.RememberKeySources) aceDef.KeySources.Clear();

			// aceApp.TriggerSystem = Program.TriggerSystem;

			SearchUtil.PrepareForSerialize(aceSearch.LastUsedProfile);
			foreach(SearchParameters sp in aceSearch.UserProfiles)
				SearchUtil.PrepareForSerialize(sp);

			const int m = 64; // Maximum number of compatibility items
			List<string> l = aceApp.PluginCompatibility;
			if(l.Count > m) l.RemoveRange(m, l.Count - m); // See reg.
		}

		internal void OnLoad()
		{
			ulong uVersion = this.Meta.GetVersion();
			AceApplication aceApp = this.Application;
			AceDefaults aceDef = this.Defaults;
			AceIntegration aceInt = this.Integration;
			AceMainWindow aceMW = this.MainWindow;
			AceSearch aceSearch = this.Search;
			AceSecurity aceSec = this.Security;

			// aceInt.UrlSchemeOverrides.SetDefaultsIfEmpty();

			ObfuscateCred(false);
			ChangePathsRelAbs(true);

			// Remove invalid columns
			List<AceColumn> lColumns = aceMW.EntryListColumns;
			for(int i = lColumns.Count - 1; i >= 0; --i)
			{
				int t = (int)lColumns[i].Type;
				if((t < 0) || (t >= (int)AceColumnType.Count))
					lColumns.RemoveAt(i);
			}

			SearchUtil.FinishDeserialize(aceSearch.LastUsedProfile);
			foreach(SearchParameters sp in aceSearch.UserProfiles)
				SearchUtil.FinishDeserialize(sp);

			DpiScale();

			// AceFont af = this.UI.PasswordFont;
			// Debug.Assert(af.GraphicsUnit == GraphicsUnit.Point);
			// float fSize = ((af.GraphicsUnit == GraphicsUnit.Point) ? af.Size :
			//	af.ToFont().SizeInPoints);
			// if(fSize > AceUI.PasswordFontSizeMaximum)
			// {
			//	Debug.Assert(false);
			//	af.Size = AceUI.PasswordFontSizeMaximum;
			//	af.GraphicsUnit = GraphicsUnit.Point;
			// }

			if(aceMW.EscMinimizesToTray) // For backward compatibility
			{
				aceMW.EscMinimizesToTray = false; // Default value
				aceMW.EscAction = AceEscAction.MinimizeToTray;
			}

			// As of KeePass 2.54, all built-in URL scheme overrides are disabled
			// by default; silently disable defaults of older versions
			Debug.Assert((new AceUrlSchemeOverrides()).BuiltInOverridesEnabled == 0UL);
			if(uVersion < 0x0002003600000000UL) // < 2.54
			{
				ulong u = aceInt.UrlSchemeOverrides.BuiltInOverridesEnabled;
				if((u == 0x1UL) || (u == 0x1000000UL))
					aceInt.UrlSchemeOverrides.BuiltInOverridesEnabled = 0;
			}

			// When the regular configuration file contains an MRU item with a
			// relative path and the enforced configuration file contains a
			// corresponding one with an absolute path, the path conversion may
			// result in duplicate MRU items;
			// cf. GetNodeKey and MruList.AddItem (CaseIgnoreCmp)
			aceApp.MostRecentlyUsed.Items = new List<IOConnectionInfo>(
				MemUtil.Distinct<IOConnectionInfo, string>(aceApp.MostRecentlyUsed.Items,
				(ioc => ioc.GetDisplayName().ToUpperInvariant()), true));
			// Cf. GetNodeKey and AceDefaults.GetKeyAssocIndex (CaseIgnoreCmp)
			aceDef.KeySources = new List<AceKeyAssoc>(
				MemUtil.Distinct<AceKeyAssoc, string>(aceDef.KeySources,
				(a => a.DatabasePath.ToUpperInvariant()), true));

			if(NativeLib.IsUnix())
			{
				aceSec.PreventScreenCapture = false;
				aceSec.MasterKeyOnSecureDesktop = false;

				aceInt.HotKeyGlobalAutoType = (long)Keys.None;
				aceInt.HotKeyGlobalAutoTypePassword = (long)Keys.None;
				aceInt.HotKeySelectedAutoType = (long)Keys.None;
				aceInt.HotKeyShowWindow = (long)Keys.None;
				aceInt.HotKeyEntryMenu = (long)Keys.None;
			}

			if(MonoWorkarounds.IsRequired(1378))
			{
				AceWorkspaceLocking aceWL = aceSec.WorkspaceLocking;
				aceWL.LockOnSessionSwitch = false;
				aceWL.LockOnSuspend = false;
				aceWL.LockOnRemoteControlChange = false;
			}

			if(MonoWorkarounds.IsRequired(1418))
			{
				aceMW.MinimizeAfterOpeningDatabase = false;
				aceApp.Start.MinimizedAndLocked = false;
			}

			if(MonoWorkarounds.IsRequired(1976))
			{
				aceMW.FocusQuickFindOnRestore = false;
				aceMW.FocusQuickFindOnUntray = false;
			}
		}

		internal void OnSavePre()
		{
			PrepareSave();

			ChangePathsRelAbs(false);
			ObfuscateCred(true);
			RemoveSensitiveInfo(true);
		}

		internal void OnSavePost()
		{
			RemoveSensitiveInfo(false);
			ObfuscateCred(false);
			ChangePathsRelAbs(true);
		}

		private void ChangePathsRelAbs(bool bMakeAbsolute)
		{
			AceApplication aceApp = this.Application;
			AceDefaults aceDef = this.Defaults;

			ChangePathRelAbs(aceApp.LastUsedFile, bMakeAbsolute);

			foreach(IOConnectionInfo iocMru in aceApp.MostRecentlyUsed.Items)
				ChangePathRelAbs(iocMru, bMakeAbsolute);

			List<string> lWDKeys = aceApp.GetWorkingDirectoryContexts();
			foreach(string strWDKey in lWDKeys)
				aceApp.SetWorkingDirectory(strWDKey, ChangePathRelAbsStr(
					aceApp.GetWorkingDirectory(strWDKey), bMakeAbsolute));

			foreach(AceKeyAssoc kfp in aceDef.KeySources)
			{
				kfp.DatabasePath = ChangePathRelAbsStr(kfp.DatabasePath, bMakeAbsolute);
				kfp.KeyFilePath = ChangePathRelAbsStr(kfp.KeyFilePath, bMakeAbsolute);
			}
		}

		private static void ChangePathRelAbs(IOConnectionInfo ioc, bool bMakeAbsolute)
		{
			if(ioc == null) { Debug.Assert(false); return; }

			if(!ioc.IsLocalFile()) return;

			// Update path separators for current system
			ioc.Path = UrlUtil.ConvertSeparators(ioc.Path);

			string strBase = WinUtil.GetExecutable();
			bool bIsAbs = UrlUtil.IsAbsolutePath(ioc.Path);

			if(bMakeAbsolute && !bIsAbs)
				ioc.Path = UrlUtil.MakeAbsolutePath(strBase, ioc.Path);
			else if(!bMakeAbsolute && bIsAbs)
				ioc.Path = UrlUtil.MakeRelativePath(strBase, ioc.Path);
		}

		private static string ChangePathRelAbsStr(string strPath, bool bMakeAbsolute)
		{
			if(strPath == null) { Debug.Assert(false); return string.Empty; }
			if(strPath.Length == 0) return strPath;

			IOConnectionInfo ioc = IOConnectionInfo.FromPath(strPath);
			ChangePathRelAbs(ioc, bMakeAbsolute);
			return ioc.Path;
		}

		private void ObfuscateCred(bool bObf)
		{
			AceApplication aceApp = this.Application;
			AceIntegration aceInt = this.Integration;

			aceApp.LastUsedFile.Obfuscate(bObf);

			foreach(IOConnectionInfo iocMru in aceApp.MostRecentlyUsed.Items)
			{
				if(iocMru == null) { Debug.Assert(false); }
				else iocMru.Obfuscate(bObf);
			}

			if(bObf) aceInt.ProxyUserName = StrUtil.Obfuscate(aceInt.ProxyUserName);
			else aceInt.ProxyUserName = StrUtil.Deobfuscate(aceInt.ProxyUserName);

			if(bObf) aceInt.ProxyPassword = StrUtil.Obfuscate(aceInt.ProxyPassword);
			else aceInt.ProxyPassword = StrUtil.Deobfuscate(aceInt.ProxyPassword);
		}

		private void RemoveSensitiveInfo(bool bRemove)
		{
			SearchParameters sp = this.Search.LastUsedProfile;

			if(bRemove)
			{
				Debug.Assert(m_strSearchString == null);
				m_strSearchString = sp.SearchString;
				sp.SearchString = string.Empty;
			}
			else
			{
				if(m_strSearchString != null)
				{
					sp.SearchString = m_strSearchString;
					m_strSearchString = null;
				}
				else { Debug.Assert(false); }
			}
		}

		private void DpiScale()
		{
			AceMeta aceMeta = this.Meta;
			double dCfgX = aceMeta.DpiFactorX, dCfgY = aceMeta.DpiFactorY;
			double dScrX = DpiUtil.FactorX, dScrY = DpiUtil.FactorY;

			if((dScrX == dCfgX) && (dScrY == dCfgY)) return;

			// When this method returns, all positions and sizes are in pixels
			// for the current screen DPI
			aceMeta.DpiFactorX = dScrX;
			aceMeta.DpiFactorY = dScrY;

			// Backward compatibility; configuration files created by KeePass
			// 2.37 and earlier do not contain DpiFactor* values, they default
			// to 0.0 and all positions and sizes are in pixels for the current
			// screen DPI; so, do not perform any DPI scaling in this case
			if((dCfgX == 0.0) || (dCfgY == 0.0)) return;

			double sX = dScrX / dCfgX, sY = dScrY / dCfgY;
			GFunc<int, int> fX = delegate(int x)
			{
				return (int)Math.Round((double)x * sX);
			};
			GFunc<int, int> fY = delegate(int y)
			{
				return (int)Math.Round((double)y * sY);
			};
			GFunc<string, string> fWsr = delegate(string strRect)
			{
				return UIUtil.ScaleWindowScreenRect(strRect, sX, sY);
			};
			GFunc<string, string> fVX = delegate(string strArray)
			{
				if(string.IsNullOrEmpty(strArray)) return strArray;

				try
				{
					int[] v = StrUtil.DeserializeIntArray(strArray);
					if(v == null) { Debug.Assert(false); return strArray; }

					for(int i = 0; i < v.Length; ++i)
						v[i] = (int)Math.Round((double)v[i] * sX);

					return StrUtil.SerializeIntArray(v);
				}
				catch(Exception) { Debug.Assert(false); }

				return strArray;
			};
			Action<AceFont> fFont = delegate(AceFont f)
			{
				if(f == null) { Debug.Assert(false); return; }

				if(f.GraphicsUnit == GraphicsUnit.Pixel)
					f.Size = (float)(f.Size * sY);
			};

			AceMainWindow mw = this.MainWindow;
			AceUI ui = this.UI;

			if(mw.X != AppDefs.InvalidWindowValue) mw.X = fX(mw.X);
			if(mw.Y != AppDefs.InvalidWindowValue) mw.Y = fY(mw.Y);
			if(mw.Width != AppDefs.InvalidWindowValue) mw.Width = fX(mw.Width);
			if(mw.Height != AppDefs.InvalidWindowValue) mw.Height = fY(mw.Height);

			foreach(AceColumn c in mw.EntryListColumns)
			{
				if(c.Width >= 0) c.Width = fX(c.Width);
			}

			ui.DataViewerRect = fWsr(ui.DataViewerRect);
			ui.DataEditorRect = fWsr(ui.DataEditorRect);
			ui.CharPickerRect = fWsr(ui.CharPickerRect);
			ui.AutoTypeCtxRect = fWsr(ui.AutoTypeCtxRect);
			ui.AutoTypeCtxColumnWidths = fVX(ui.AutoTypeCtxColumnWidths);

			fFont(ui.StandardFont);
			fFont(ui.PasswordFont);
			fFont(ui.DataEditorFont);
		}

		public void Apply(AceApplyFlags f)
		{
			AceApplication aceApp = this.Application;
			AceIntegration aceInt = this.Integration;
			AceNative aceNtv = this.Native;
			AceSecurity aceSec = this.Security;

			if((f & AceApplyFlags.Proxy) != AceApplyFlags.None)
				IOConnection.SetProxy(aceInt.ProxyType, aceInt.ProxyAddress,
					aceInt.ProxyPort, aceInt.ProxyAuthType, aceInt.ProxyUserName,
					aceInt.ProxyPassword);

			if((f & AceApplyFlags.Ssl) != AceApplyFlags.None)
				IOConnection.SslCertsAcceptInvalid = aceSec.SslCertsAcceptInvalid;

			if((f & AceApplyFlags.FileTransactions) != AceApplyFlags.None)
				FileTransactionEx.ExtraSafe = aceApp.FileTxExtra;

			if((f & AceApplyFlags.Native) != AceApplyFlags.None)
				NativeLib.AllowNative = aceNtv.NativeKeyTransformations;
		}

		private static readonly Dictionary<object, string> g_dictXmlPathCache =
			new Dictionary<object, string>();
		public static bool IsOptionEnforced(object oContainer, PropertyInfo pi)
		{
			if(oContainer == null) { Debug.Assert(false); return false; }
			if(pi == null) { Debug.Assert(false); return false; }

			XmlDocument xdEnforced = AppConfigSerializer.EnforcedConfigXml;
			if(xdEnforced == null) return false;

			string strObjPath;
			if(!g_dictXmlPathCache.TryGetValue(oContainer, out strObjPath))
			{
				strObjPath = XmlUtil.GetObjectXmlPath(Program.Config, oContainer);
				if(string.IsNullOrEmpty(strObjPath)) { Debug.Assert(false); return false; }

				g_dictXmlPathCache[oContainer] = strObjPath;
			}

			string strProp = XmlSerializerEx.GetXmlName(pi);
			if(string.IsNullOrEmpty(strProp)) { Debug.Assert(false); return false; }

			string strPre = strObjPath;
			if(!strPre.EndsWith("/")) strPre += "/";
			string strXPath = strPre + strProp;

			XmlNode xn = xdEnforced.SelectSingleNode(strXPath);
			if(xn == null) return false;

			XmContext ctx = new XmContext(null, AppConfigEx.GetNodeOptions,
				AppConfigEx.GetNodeKey);
			return XmlUtil.IsAlwaysEnforced(xn, strXPath, ctx);
		}

		public static bool IsOptionEnforced(object oContainer, string strPropertyName)
		{
			if(oContainer == null) { Debug.Assert(false); return false; }
			if(string.IsNullOrEmpty(strPropertyName)) { Debug.Assert(false); return false; }

			// To improve performance (avoid type queries), check here, too
			XmlDocument xdEnforced = AppConfigSerializer.EnforcedConfigXml;
			if(xdEnforced == null) return false;

			PropertyInfo pi = oContainer.GetType().GetProperty(strPropertyName);
			return IsOptionEnforced(oContainer, pi);
		}

		public static void ClearXmlPathCache()
		{
			g_dictXmlPathCache.Clear();
		}

		internal static void GetNodeOptions(XmNodeOptions o, string strXPath)
		{
			if(o == null) { Debug.Assert(false); return; }
			if(string.IsNullOrEmpty(strXPath)) { Debug.Assert(false); return; }
			Debug.Assert(strXPath.IndexOf('[') < 0);

			switch(strXPath)
			{
				// Sync. with documentation

				case "/Configuration/Application/PluginCompatibility":
				case "/Configuration/Meta/DpiFactorX":
				case "/Configuration/Meta/DpiFactorY":
				case "/Configuration/Meta/PreferUserConfiguration":
				case "/Configuration/Meta/Version":
					o.NodeMode = XmNodeMode.None;
					break;

				case "/Configuration/Application/TriggerSystem":
				case "/Configuration/Application/TriggerSystem/Triggers/Trigger":
				case "/Configuration/Defaults/KeySources/Association":
				case "/Configuration/Integration/UrlSchemeOverrides":
				case "/Configuration/PasswordGenerator/AutoGeneratedPasswordsProfile":
				// case "/Configuration/PasswordGenerator/LastUsedProfile":
				case "/Configuration/PasswordGenerator/UserProfiles":
				case "/Configuration/PasswordGenerator/UserProfiles/Profile":
				case "/Configuration/Search/LastUsedProfile":
				case "/Configuration/Search/UserProfiles/Profile":
					o.ContentMode = XmContentMode.Replace;
					break;

				// Nodes that do not have child elements:
				// case "/Configuration/Application/WorkingDirectories/Item":
				// case "/Configuration/Integration/AutoTypeAbortOnWindows/Window":

				// Nodes where the mode 'Merge' may be more useful:
				// case "/Configuration/Application/MostRecentlyUsed/Items/ConnectionInfo":
				//     (allow users to save credentials)
				// case "/Configuration/Custom/Item":
				//     (empty Value explicitly only)
				// case "/Configuration/Defaults/SearchParameters":
				//     (admin might only want to turn off case-sensitivity)
				// case "/Configuration/Integration/UrlSchemeOverrides/CustomOverrides/Override":
				//     (allow users to enable/disable the item)
				// case "/Configuration/MainWindow/EntryListColumnCollection/Column":
				//     (allow users to change the width)

				default: break;
			}
		}

		internal static string GetNodeKey(XmlNode xn, string strXPath)
		{
			if(xn == null) { Debug.Assert(false); return null; }
			if(string.IsNullOrEmpty(strXPath)) { Debug.Assert(false); return null; }

			Debug.Assert(xn is XmlElement);
			Debug.Assert((strXPath == xn.Name) || strXPath.EndsWith("/" + xn.Name));
			Debug.Assert(strXPath.IndexOf('[') < 0);

			string strA = null, strB = null;
			switch(strXPath)
			{
				// Sync. with documentation

				case "/Configuration/Application/PluginCompatibility/Item":
				case "/Configuration/Application/WorkingDirectories/Item":
				case "/Configuration/Integration/AutoTypeAbortOnWindows/Window":
					strA = XmlUtil.SafeInnerXml(xn);
					break;

				case "/Configuration/Application/MostRecentlyUsed/Items/ConnectionInfo":
					strA = XmlUtil.SafeInnerXml(xn, "Path"); // Cf. OnLoad
					strB = XmlUtil.SafeInnerXml(xn, "UserName"); // Cf. MRU display name
					break;

				case "/Configuration/Application/TriggerSystem/Triggers/Trigger":
					strA = XmlUtil.SafeInnerXml(xn, "Guid");
					break;

				case "/Configuration/Custom/Item":
					strA = XmlUtil.SafeInnerXml(xn, "Key");
					break;

				case "/Configuration/Defaults/KeySources/Association":
					strA = XmlUtil.SafeInnerXml(xn, "DatabasePath"); // Cf. OnLoad
					break;

				case "/Configuration/Integration/UrlSchemeOverrides/CustomOverrides/Override":
					strA = XmlUtil.SafeInnerXml(xn, "Scheme");
					strB = XmlUtil.SafeInnerXml(xn, "UrlOverride");
					break;

				case "/Configuration/MainWindow/EntryListColumnCollection/Column":
					strA = XmlUtil.SafeInnerXml(xn, "Type");
					strB = XmlUtil.SafeInnerXml(xn, "CustomName");
					break;

				case "/Configuration/PasswordGenerator/UserProfiles/Profile":
				case "/Configuration/Search/UserProfiles/Profile":
					strA = XmlUtil.SafeInnerXml(xn, "Name");
					break;

				default: break;
			}

			Debug.Assert((strA == null) || (strA.IndexOf('<') < 0));
			Debug.Assert((strB == null) || (strB.IndexOf('<') < 0));
			Debug.Assert((strB == null) || (strA != null)); // B => A
			Debug.Assert((strA == null) || (strA.Length != 0));

			if(strB != null) return ((strA ?? string.Empty) + " <> " + strB);
			return strA;
		}

		internal static T GetPropertyValue<T>(object oContainer,
			string strPropertyName, T tDefault, out PropertyInfo piForSet)
		{
			piForSet = null;
			if(oContainer == null) { Debug.Assert(false); return tDefault; }
			if(string.IsNullOrEmpty(strPropertyName)) { Debug.Assert(false); return tDefault; }

			piForSet = oContainer.GetType().GetProperty(strPropertyName);
			if(piForSet == null) { Debug.Assert(false); return tDefault; }
			if(piForSet.PropertyType != typeof(T))
			{
				Debug.Assert(false);
				piForSet = null;
				return tDefault;
			}

			T t = (T)piForSet.GetValue(oContainer, null);
			if(IsOptionEnforced(oContainer, piForSet)) piForSet = null;
			return t;
		}

		internal static string GetEmptyXml()
		{
			return ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
				"<" + StrXmlTypeName + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
				"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />");
		}

		internal static XmlDocument CreateEmptyXmlDocument()
		{
			return XmlUtilEx.LoadXmlDocumentFromString(GetEmptyXml());
		}
	}

	[Flags]
	public enum AceApplyFlags
	{
		None = 0,
		Proxy = 0x1,
		Ssl = 0x2,
		FileTransactions = 0x4,
		Native = 0x8,

		All = 0x7FFF
	}

	public sealed class AceMeta
	{
		public AceMeta()
		{
		}

		private string m_strVersion = string.Empty;
		[DefaultValue("")]
		public string Version
		{
			get { return m_strVersion; }
			set
			{
				if(value == null) throw new ArgumentNullException("value");
				m_strVersion = value;
			}
		}

		private bool m_bPrefLocalCfg = false;
		public bool PreferUserConfiguration
		{
			get { return m_bPrefLocalCfg; }
			set { m_bPrefLocalCfg = value; }
		}

		// private bool m_bIsEnforced = false;
		// [XmlIgnore]
		// public bool IsEnforcedConfiguration
		// {
		//	get { return m_bIsEnforced; }
		//	set { m_bIsEnforced = value; }
		// }

		private bool m_bOmitDefaultValues = true;
		// Informational property only (like an XML comment);
		// currently doesn't have any effect (the XmlSerializer
		// always omits default values, independent of this
		// property)
		public bool OmitItemsWithDefaultValues
		{
			get { return m_bOmitDefaultValues; }
			set { m_bOmitDefaultValues = value; }
		}

		private double m_dDpiFactorX = 0.0; // See AppConfigEx.DpiScale()
		[DefaultValue(0.0)]
		public double DpiFactorX
		{
			get { return m_dDpiFactorX; }
			set { m_dDpiFactorX = value; }
		}

		private double m_dDpiFactorY = 0.0; // See AppConfigEx.DpiScale()
		[DefaultValue(0.0)]
		public double DpiFactorY
		{
			get { return m_dDpiFactorY; }
			set { m_dDpiFactorY = value; }
		}

		internal ulong GetVersion()
		{
			if(string.IsNullOrEmpty(m_strVersion)) return 0;
			return StrUtil.ParseVersion(m_strVersion);
		}
	}
}