File: ExpiryControlGroup.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 (131 lines) | stat: -rw-r--r-- 3,274 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
/*
  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.Diagnostics;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

using KeePassLib;
using KeePassLib.Utility;

namespace KeePass.UI
{
	public sealed class ExpiryControlGroup
	{
		private CheckBox m_cb = null;
		private DateTimePicker m_dtp = null;

		public bool Checked
		{
			get
			{
				if(m_cb == null) { Debug.Assert(false); return false; }
				return m_cb.Checked;
			}

			set { UIUtil.SetChecked(m_cb, value); }
		}

		public DateTime Value
		{
			get
			{
				if(m_dtp == null) { Debug.Assert(false); return DateTime.UtcNow; }

				// Force validation/update of incomplete edit
				// (workaround for KPB 3505269)
				if(m_dtp.Focused && m_dtp.Visible)
				{
					m_dtp.Visible = false;
					m_dtp.Visible = true;
				}

				return TimeUtil.ToUtc(m_dtp.Value, false);
			}

			set
			{
				if(m_dtp == null) { Debug.Assert(false); return; }
				m_dtp.Value = TimeUtil.ToLocal(value, true);
			}
		}

		public ExpiryControlGroup()
		{
		}

#if DEBUG
		~ExpiryControlGroup()
		{
			Debug.Assert(m_cb == null); // Owner should call Release()
		}
#endif

		public void Attach(CheckBox cb, DateTimePicker dtp)
		{
			if(cb == null) throw new ArgumentNullException("cb");
			if(dtp == null) throw new ArgumentNullException("dtp");

			m_cb = cb;
			m_dtp = dtp;

			AccessibilityEx.SetContext(dtp, cb);

			// m_dtp.ShowUpDown = true;
			m_dtp.CustomFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern +
				" " + DateTimeFormatInfo.CurrentInfo.LongTimePattern;

			m_dtp.ValueChanged += this.OnExpiryValueChanged;
			// Also handle key press event (workaround for KPB 3505269)
			m_dtp.KeyPress += this.OnExpiryKeyPress;
		}

		public void Release()
		{
			if(m_cb == null) return;

			m_dtp.ValueChanged -= this.OnExpiryValueChanged;
			m_dtp.KeyPress -= this.OnExpiryKeyPress;

			m_cb = null;
			m_dtp = null;
		}

		private void UpdateUI(bool? obSetCheck)
		{
			if(obSetCheck.HasValue)
				UIUtil.SetChecked(m_cb, obSetCheck.Value);

			// UIUtil.SetEnabled(m_dtp, m_cb.Enabled);
		}

		private void OnExpiryValueChanged(object sender, EventArgs e)
		{
			UpdateUI(true);
		}

		private void OnExpiryKeyPress(object sender, KeyPressEventArgs e)
		{
			if(char.IsDigit(e.KeyChar)) UpdateUI(true);
		}
	}
}