File: IOConnectionForm.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 (477 lines) | stat: -rw-r--r-- 13,799 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
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
/*
  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.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

using KeePass.App;
using KeePass.Resources;
using KeePass.UI;
using KeePass.Util;

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

namespace KeePass.Forms
{
	public partial class IOConnectionForm : Form
	{
		private bool m_bSave = false;
		private IOConnectionInfo m_ioc = new IOConnectionInfo();
		private bool m_bCanRememberCred = true;
		private bool m_bTestConnection = false;

		private readonly List<KeyValuePair<IocPropertyInfo, Control>> m_lProps =
			new List<KeyValuePair<IocPropertyInfo, Control>>();

		public IOConnectionInfo IOConnectionInfo
		{
			get { return m_ioc; }
		}

		public void InitEx(bool bSave, IOConnectionInfo ioc, bool bCanRememberCred,
			bool bTestConnection)
		{
			m_bSave = bSave;
			if(ioc != null) m_ioc = ioc.CloneDeep();
			m_bCanRememberCred = bCanRememberCred;
			m_bTestConnection = bTestConnection;
		}

		public IOConnectionForm()
		{
			InitializeComponent();

			SecureTextBoxEx.InitEx(ref m_tbPassword);
			GlobalWindowManager.InitializeForm(this);
		}

		private void OnFormLoad(object sender, EventArgs e)
		{
			// Must work without a parent window
			Debug.Assert(this.StartPosition == FormStartPosition.CenterScreen);

			// The password text box should not be focused by default
			// in order to avoid a Caps Lock warning tooltip bug;
			// https://sourceforge.net/p/keepass/bugs/1807/
			Debug.Assert((m_tbPassword.TabIndex >= 2) && !m_tbPassword.Focused);

			InitAdvancedTab(); // After translation, before resize

			GlobalWindowManager.AddWindow(this);

			string strTitle = (m_bSave ? KPRes.UrlSaveTitle : KPRes.UrlOpenTitle);
			string strDesc = (m_bSave ? KPRes.UrlSaveDesc : KPRes.UrlOpenDesc);

			BannerFactory.CreateBannerEx(this, m_bannerImage,
				KeePass.Properties.Resources.B48x48_WWW, strTitle, strDesc);
			this.Icon = AppIcons.Default;
			this.Text = strTitle;

			FontUtil.AssignDefaultBold(m_lblUrl);
			FontUtil.AssignDefaultBold(m_lblUserName);
			FontUtil.AssignDefaultBold(m_lblPassword);
			FontUtil.AssignDefaultBold(m_lblRemember);

			m_tbUrl.Text = (m_ioc.IsLocalFile() ? string.Empty : m_ioc.Path);
			m_tbUserName.Text = m_ioc.UserName;
			m_tbPassword.Text = m_ioc.Password;

			m_cmbCredSaveMode.Items.Add(KPRes.CredSaveNone);
			m_cmbCredSaveMode.Items.Add(KPRes.CredSaveUserOnly);
			m_cmbCredSaveMode.Items.Add(KPRes.CredSaveAll);

			if(m_ioc.CredSaveMode == IOCredSaveMode.UserNameOnly)
				m_cmbCredSaveMode.SelectedIndex = 1;
			else if(m_ioc.CredSaveMode == IOCredSaveMode.SaveCred)
				m_cmbCredSaveMode.SelectedIndex = 2;
			else
				m_cmbCredSaveMode.SelectedIndex = 0;

			if(!m_bCanRememberCred)
			{
				m_cmbCredSaveMode.SelectedIndex = 0;
				m_cmbCredSaveMode.Enabled = false;
			}

			ThreadPool.QueueUserWorkItem(delegate(object state)
			{
				try { InitAutoCompletions(); }
				catch(Exception) { Debug.Assert(false); }
			});
		}

		private void OnFormShown(object sender, EventArgs e)
		{
			if((m_tbUrl.TextLength > 0) && (m_tbUserName.TextLength > 0))
				UIUtil.ResetFocus(m_tbPassword, this);
			else if(m_tbUrl.TextLength > 0)
				UIUtil.SetFocus(m_tbUserName, this);
			else UIUtil.SetFocus(m_tbUrl, this);
		}

		private void OnBtnOK(object sender, EventArgs e)
		{
			string strUrl = m_tbUrl.Text;
			if(strUrl.IndexOf("://") < 0)
			{
				// m_ttInvalidUrl.Show(KPRes.InvalidUrl, m_tbUrl);
				MessageService.ShowWarning(strUrl, KPRes.InvalidUrl);
				this.DialogResult = DialogResult.None;
				return;
			}

			m_ioc.Path = strUrl;
			m_ioc.UserName = m_tbUserName.Text;
			m_ioc.Password = m_tbPassword.TextEx.ReadString();

			if(m_cmbCredSaveMode.SelectedIndex == 1)
				m_ioc.CredSaveMode = IOCredSaveMode.UserNameOnly;
			else if(m_cmbCredSaveMode.SelectedIndex == 2)
				m_ioc.CredSaveMode = IOCredSaveMode.SaveCred;
			else
				m_ioc.CredSaveMode = IOCredSaveMode.NoSave;

			IocProperties p = m_ioc.Properties;
			foreach(KeyValuePair<IocPropertyInfo, Control> kvp in m_lProps)
			{
				IocPropertyInfo pi = kvp.Key;
				string strName = pi.Name;
				Type t = pi.Type;
				Control c = kvp.Value;

				try
				{
					if(t == typeof(string))
					{
						TextBox tb = (c as TextBox);
						if(tb != null) p.Set(strName, tb.Text.Trim());
						else { Debug.Assert(false); }
					}
					else if(t == typeof(bool))
					{
						ComboBox cmb = (c as ComboBox);
						if(cmb != null)
						{
							int iSel = cmb.SelectedIndex;
							if(iSel == 0) p.SetBool(strName, null);
							else p.SetBool(strName, (iSel == 1));
						}
						else { Debug.Assert(false); }
					}
					else if(t == typeof(long))
					{
						TextBox tb = (c as TextBox);
						if(tb != null)
						{
							string str = tb.Text.Trim();
							if(str.Length == 0) p.SetLong(strName, null);
							else
							{
								// Validate and store number
								long l = long.Parse(str, NumberFormatInfo.InvariantInfo);
								p.SetLong(strName, l);
							}
						}
						else { Debug.Assert(false); }
					}
					else { Debug.Assert(false); }
				}
				catch(Exception ex)
				{
					string strMsg = KPRes.Field + " '" + pi.DisplayName + "':" +
						MessageService.NewLine + StrUtil.FormatException(ex, null);
					// if(!VistaTaskDialog.ShowMessageBox(strMsg, KPRes.ValidationFailed,
					//	PwDefs.ShortProductName, VtdIcon.Warning, this))
					MessageService.ShowWarning(strMsg);

					this.DialogResult = DialogResult.None;
					return;
				}
			}

			if(m_bTestConnection && !m_bSave)
			{
				if(!TestConnectionEx())
					this.DialogResult = DialogResult.None;
			}
		}

		private bool TestConnectionEx()
		{
			bool bResult = true;
			bool bOK = m_btnOK.Enabled, bCancel = m_btnCancel.Enabled;
			bool bCombo = m_cmbCredSaveMode.Enabled;

			m_btnOK.Enabled = m_btnCancel.Enabled = m_tbUrl.Enabled =
				m_tbUserName.Enabled = m_tbPassword.Enabled =
				m_btnHelp.Enabled = m_cmbCredSaveMode.Enabled = false;

			Application.DoEvents();

			try
			{
				if(!IOConnection.FileExists(m_ioc, true))
					throw new FileNotFoundException();
			}
			catch(Exception ex)
			{
				MessageService.ShowWarning(m_ioc.GetDisplayName(), ex);
				bResult = false;
			}

			m_btnOK.Enabled = bOK;
			m_btnCancel.Enabled = bCancel;
			m_cmbCredSaveMode.Enabled = bCombo;
			m_btnHelp.Enabled = m_tbUserName.Enabled = m_tbUrl.Enabled =
				m_tbPassword.Enabled = true;
			return bResult;
		}

		private void OnBtnCancel(object sender, EventArgs e)
		{
		}

		private void OnBtnHelp(object sender, EventArgs e)
		{
			AppHelp.ShowHelp(AppDefs.HelpTopics.IOConnections, null);
		}

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

		private static Dictionary<string, List<IocPropertyInfo>> GetPropertyInfos()
		{
			Dictionary<string, List<IocPropertyInfo>> d =
				new Dictionary<string, List<IocPropertyInfo>>(StrUtil.CaseIgnoreComparer);

			foreach(IocPropertyInfo pi in IocPropertyInfoPool.PropertyInfos)
			{
				if(pi == null) { Debug.Assert(false); continue; }

				List<string> lPrt = new List<string>(pi.Protocols);
				lPrt.Sort(StrUtil.CaseIgnoreComparer);
				StringBuilder sbPrt = new StringBuilder();
				foreach(string str in lPrt)
				{
					if(sbPrt.Length > 0) sbPrt.Append(" / ");
					sbPrt.Append(str);
				}
				string strPrt = sbPrt.ToString();

				List<IocPropertyInfo> l;
				if(d.TryGetValue(strPrt, out l)) l.Add(pi);
				else d[strPrt] = new List<IocPropertyInfo> { pi };
			}

			return d;
		}

		private void InitAdvancedTab()
		{
			IocProperties p = m_ioc.Properties;
			Dictionary<string, List<IocPropertyInfo>> dProps = GetPropertyInfos();

			const int d = 7;
			int hLabel = m_lblUrl.Height;
			int hTextBox = m_tbUrl.Height;
			int hComboBox = m_cmbCredSaveMode.Height;
			Font f = m_lblUrl.Font;

			Debug.Assert(m_pnlAdv.AutoScroll);
			m_pnlAdv.AutoScrollMargin = new Size(1, d);

			int wPanel = m_pnlAdv.ClientSize.Width - UIUtil.GetVScrollBarWidth() - 1;
			int wGroup = wPanel - (2 * d);
			int wCell = (wPanel - (3 * d)) / 2;
			int xText = d - 1;
			int xInput = d + wCell + d - 1;
			int xGroup = xText;

			if(this.RightToLeft == RightToLeft.Yes)
			{
				int iTemp = xText;
				xText = xInput;
				xInput = iTemp;
			}

			int y = 1;
			int iID = 0;

			m_pnlAdv.SuspendLayout();

			foreach(KeyValuePair<string, List<IocPropertyInfo>> kvp in dProps)
			{
				string strGroup = kvp.Key;
				y += d;

				Label lblGroup = new Label();
				lblGroup.Name = "cGroup" + iID.ToString(NumberFormatInfo.InvariantInfo);
				++iID;
				lblGroup.AutoEllipsis = true;
				lblGroup.AutoSize = false;
				lblGroup.Dock = DockStyle.None;
				lblGroup.Location = new Point(xGroup, y);
				lblGroup.Size = new Size(wGroup, hLabel);
				lblGroup.Text = strGroup;
				lblGroup.TextAlign = ContentAlignment.MiddleLeft;
				// lblGroup.BackColor = Color.Red;
				FontUtil.AssignDefaultBold(lblGroup);

				m_pnlAdv.Controls.Add(lblGroup);
				y += hLabel;

				foreach(IocPropertyInfo pi in kvp.Value)
				{
					string strName = pi.Name;
					string strText = pi.DisplayName + ":";
					Type t = pi.Type;
					y += d;

					int hText = hLabel;
					int wRem = TextRenderer.MeasureText(strText, f).Width;
					while(wRem >= wCell)
					{
						hText += (hLabel + 1);
						wRem -= wCell;
					}

					Label lblText = new Label();
					lblText.Name = "cText" + iID.ToString(NumberFormatInfo.InvariantInfo);
					++iID;
					lblText.AutoEllipsis = true;
					lblText.AutoSize = false;
					lblText.Dock = DockStyle.None;
					lblText.Size = new Size(wCell, hText);
					lblText.Text = strText;
					lblText.TextAlign = ContentAlignment.MiddleLeft;
					// lblText.BackColor = Color.Green;

					Control cInput = null;
					if((t == typeof(string)) || (t == typeof(long)))
					{
						TextBox tb = new TextBox();
						tb.Size = new Size(wCell, hTextBox);

						tb.Text = (p.Get(strName) ?? string.Empty);

						cInput = tb;
					}
					else if(t == typeof(bool))
					{
						ComboBox cmb = new ComboBox();
						cmb.DropDownStyle = ComboBoxStyle.DropDownList;
						cmb.Size = new Size(wCell, hComboBox);

						cmb.Items.Add(KPRes.Automatic);
						cmb.Items.Add(KPRes.Yes);
						cmb.Items.Add(KPRes.No);

						bool? ob = p.GetBool(strName);
						if(ob.HasValue) cmb.SelectedIndex = (ob.Value ? 1 : 2);
						else cmb.SelectedIndex = 0;

						cInput = cmb;
					}
					else { Debug.Assert(false); continue; }

					cInput.Dock = DockStyle.None;
					cInput.Name = "cInput" + iID.ToString(NumberFormatInfo.InvariantInfo);
					++iID;

					int hDiff = lblText.Height - cInput.Height;
					if(hDiff >= 0)
					{
						lblText.Location = new Point(xText, y);
						cInput.Location = new Point(xInput, y + (hDiff / 2));

						y += lblText.Height;
					}
					else
					{
						lblText.Location = new Point(xText, y - (hDiff / 2));
						cInput.Location = new Point(xInput, y);

						y += cInput.Height;
					}

					m_pnlAdv.Controls.Add(lblText);
					m_pnlAdv.Controls.Add(cInput);

					m_lProps.Add(new KeyValuePair<IocPropertyInfo, Control>(
						pi, cInput));
				}
			}

			m_pnlAdv.ResumeLayout(true);
		}

		private static void InitAutoCompletion(TextBox tb, Dictionary<string, bool> d)
		{
			if(d.Count == 0) return;

			string[] v = new string[d.Count];
			d.Keys.CopyTo(v, 0);
			Array.Sort<string>(v, StrUtil.CaseIgnoreComparer);

			// Do not append, because long suggestions hide the start
			UIUtil.EnableAutoCompletion(tb, false, v); // Invokes
		}

		private void InitAutoCompletions()
		{
			Dictionary<string, bool> dUrls = new Dictionary<string, bool>();
			Dictionary<string, bool> dUsers = new Dictionary<string, bool>();

			MainForm mf = Program.MainForm;
			MruList l = ((mf != null) ? mf.FileMruList : null);
			if(l == null) { Debug.Assert(false); return; }

			for(uint u = 0; u < l.ItemCount; ++u)
			{
				IOConnectionInfo ioc = (l.GetItem(u).Value as IOConnectionInfo);
				if(ioc == null) { Debug.Assert(false); continue; }
				if(ioc.IsLocalFile()) continue;

				string str = ioc.Path;
				if(!string.IsNullOrEmpty(str)) dUrls[str] = true;

				str = ioc.UserName;
				if(!string.IsNullOrEmpty(str)) dUsers[str] = true;
			}

			InitAutoCompletion(m_tbUrl, dUrls);
			InitAutoCompletion(m_tbUserName, dUsers);
		}
	}
}