1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 
20 using System;
21 using System.Collections.Generic;
22 using System.ComponentModel;
23 using System.Diagnostics;
24 using System.Drawing;
25 using System.Text;
26 using System.Windows.Forms;
27 
28 using KeePass.App;
29 using KeePass.Resources;
30 using KeePass.UI;
31 using KeePass.Util;
32 
33 using KeePassLib;
34 using KeePassLib.Utility;
35 
36 namespace KeePass.Forms
37 {
38 	public partial class XmlReplaceForm : Form
39 	{
40 		private PwDatabase m_pd = null;
41 
42 		private Image m_imgWarning = null;
43 
InitEx(PwDatabase pd)44 		public void InitEx(PwDatabase pd)
45 		{
46 			m_pd = pd;
47 		}
48 
XmlReplaceForm()49 		public XmlReplaceForm()
50 		{
51 			InitializeComponent();
52 			GlobalWindowManager.InitializeForm(this);
53 		}
54 
OnFormLoad(object sender, EventArgs e)55 		private void OnFormLoad(object sender, EventArgs e)
56 		{
57 			if(m_pd == null) { Debug.Assert(false); throw new InvalidOperationException(); }
58 
59 			GlobalWindowManager.AddWindow(this);
60 
61 			BannerFactory.CreateBannerEx(this, m_bannerImage,
62 				KeePass.Properties.Resources.B48x48_Binary, KPRes.XmlReplace,
63 				KPRes.XmlReplaceDesc);
64 
65 			this.Icon = AppIcons.Default;
66 			this.Text = KPRes.XmlReplace;
67 
68 			m_imgWarning = UIUtil.IconToBitmap(SystemIcons.Warning,
69 				DpiUtil.ScaleIntX(16), DpiUtil.ScaleIntY(16));
70 			m_picWarning.Image = m_imgWarning;
71 
72 			UIUtil.AccSetName(m_picWarning, KPRes.Warning);
73 
74 			FontUtil.AssignDefaultBold(m_rbRemove);
75 			FontUtil.AssignDefaultBold(m_rbReplace);
76 
77 			m_rbReplace.Checked = true;
78 			m_rbInnerText.Checked = true;
79 
80 			EnableControlsEx();
81 		}
82 
CleanUpEx()83 		private void CleanUpEx()
84 		{
85 			if(m_imgWarning != null)
86 			{
87 				m_picWarning.Image = null;
88 				m_imgWarning.Dispose();
89 				m_imgWarning = null;
90 			}
91 			else { Debug.Assert(false); }
92 		}
93 
OnFormClosed(object sender, FormClosedEventArgs e)94 		private void OnFormClosed(object sender, FormClosedEventArgs e)
95 		{
96 			CleanUpEx();
97 			GlobalWindowManager.RemoveWindow(this);
98 		}
99 
EnableControlsEx()100 		private void EnableControlsEx()
101 		{
102 			m_pnlReplace.Enabled = m_rbReplace.Checked;
103 		}
104 
OnRemoveCheckedChanged(object sender, EventArgs e)105 		private void OnRemoveCheckedChanged(object sender, EventArgs e)
106 		{
107 			EnableControlsEx();
108 		}
109 
OnBtnOK(object sender, EventArgs e)110 		private void OnBtnOK(object sender, EventArgs e)
111 		{
112 			this.Enabled = false;
113 
114 			try
115 			{
116 				XmlReplaceOptions opt = new XmlReplaceOptions();
117 				XmlReplaceFlags f = XmlReplaceFlags.StatusUI;
118 				opt.ParentForm = this;
119 
120 				opt.SelectNodesXPath = m_tbSelNodes.Text;
121 
122 				if(m_rbRemove.Checked) opt.Operation = XmlReplaceOp.RemoveNodes;
123 				else if(m_rbReplace.Checked) opt.Operation = XmlReplaceOp.ReplaceData;
124 				else { Debug.Assert(false); }
125 
126 				if(m_rbInnerText.Checked) opt.Data = XmlReplaceData.InnerText;
127 				else if(m_rbInnerXml.Checked) opt.Data = XmlReplaceData.InnerXml;
128 				else if(m_rbOuterXml.Checked) opt.Data = XmlReplaceData.OuterXml;
129 				else { Debug.Assert(false); }
130 
131 				if(m_cbCase.Checked) f |= XmlReplaceFlags.CaseSensitive;
132 				if(m_cbRegex.Checked) f |= XmlReplaceFlags.Regex;
133 
134 				opt.FindText = m_tbMatch.Text;
135 				opt.ReplaceText = m_tbReplace.Text;
136 
137 				opt.Flags = f;
138 				XmlUtil.Replace(m_pd, opt);
139 				this.Enabled = true;
140 			}
141 			catch(Exception ex)
142 			{
143 				this.Enabled = true;
144 				MessageService.ShowWarning(ex);
145 				this.DialogResult = DialogResult.None;
146 			}
147 		}
148 
OnBtnCancel(object sender, EventArgs e)149 		private void OnBtnCancel(object sender, EventArgs e)
150 		{
151 		}
152 
OnBtnHelp(object sender, EventArgs e)153 		private void OnBtnHelp(object sender, EventArgs e)
154 		{
155 			AppHelp.ShowHelp(AppDefs.HelpTopics.XmlReplace, null);
156 		}
157 	}
158 }
159