File: DuplicationForm.cs

package info (click to toggle)
keepass2 2.57%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,516 kB
  • sloc: cs: 120,930; xml: 6,271; cpp: 322; sh: 53; makefile: 49
file content (122 lines) | stat: -rw-r--r-- 3,995 bytes parent folder | download | duplicates (2)
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
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2024 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.Windows.Forms;

using KeePass.App;
using KeePass.App.Configuration;
using KeePass.Resources;
using KeePass.UI;

using KeePassLib;
using KeePassLib.Collections;
using KeePassLib.Security;

namespace KeePass.Forms
{
	public partial class DuplicationForm : Form
	{
		// Copy data from controls to simple member variables, because
		// ApplyTo must work after the dialog has been destroyed.
		private bool m_bExtendTitle = true;
		private bool m_bFieldRefs = false;
		private bool m_bCopyHistory = true;

		public DuplicationForm()
		{
			InitializeComponent();
			GlobalWindowManager.InitializeForm(this);
		}

		private void OnFormLoad(object sender, EventArgs e)
		{
			GlobalWindowManager.AddWindow(this);

			this.Icon = AppIcons.Default;

			FontUtil.AssignDefaultBold(m_cbAppendCopy);
			FontUtil.AssignDefaultBold(m_cbFieldRefs);
			FontUtil.AssignDefaultBold(m_cbCopyHistory);

			FormDataExchange fdx = new FormDataExchange(this, true, true, true);
			AceDuplication aceDup = Program.Config.Defaults.Duplication;
			fdx.Add(m_cbAppendCopy, aceDup, "ExtendTitle");
			fdx.Add(m_cbFieldRefs, aceDup, "CreateFieldReferences");
			fdx.Add(m_cbCopyHistory, aceDup, "CopyHistory");

			UIUtil.SetEnabledFast(m_cbFieldRefs.Enabled, m_lblFieldRefs, m_lnkFieldRefs);
		}

		private void OnFormClosed(object sender, FormClosedEventArgs e)
		{
			GlobalWindowManager.RemoveWindow(this);
		}

		private void OnFieldRefsLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			AppHelp.ShowHelp(AppDefs.HelpTopics.FieldRefs, null);
		}

		private void OnBtnOK(object sender, EventArgs e)
		{
			m_bExtendTitle = m_cbAppendCopy.Checked;
			m_bFieldRefs = m_cbFieldRefs.Checked;
			m_bCopyHistory = m_cbCopyHistory.Checked;
		}

		public void ApplyTo(PwEntry peNew, PwEntry pe, PwDatabase pd)
		{
			if((peNew == null) || (pe == null)) { Debug.Assert(false); return; }
			Debug.Assert(this.IsDisposed);

			Debug.Assert(peNew.Strings.ReadSafe(PwDefs.UserNameField) ==
				pe.Strings.ReadSafe(PwDefs.UserNameField));
			Debug.Assert(peNew.Strings.ReadSafe(PwDefs.PasswordField) ==
				pe.Strings.ReadSafe(PwDefs.PasswordField));

			if(m_bExtendTitle && (pd != null))
			{
				string strTitle = peNew.Strings.ReadSafe(PwDefs.TitleField);
				peNew.Strings.Set(PwDefs.TitleField, new ProtectedString(
					pd.MemoryProtection.ProtectTitle, strTitle + " - " +
					KPRes.CopyOfItem));
			}

			if(m_bFieldRefs && (pd != null))
			{
				string strUser = "{REF:U@I:" + pe.Uuid.ToHexString() + "}";
				peNew.Strings.Set(PwDefs.UserNameField, new ProtectedString(
					pd.MemoryProtection.ProtectUserName, strUser));

				string strPw = "{REF:P@I:" + pe.Uuid.ToHexString() + "}";
				peNew.Strings.Set(PwDefs.PasswordField, new ProtectedString(
					pd.MemoryProtection.ProtectPassword, strPw));
			}

			if(!m_bCopyHistory)
				peNew.History = new PwObjectList<PwEntry>();
		}
	}
}