File: OnDemandStatusDialog.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 (253 lines) | stat: -rw-r--r-- 6,210 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
/*
  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.Text;
using System.Threading;
using System.Windows.Forms;

using KeePass.Forms;

using KeePassLib.Interfaces;
using KeePassLib.Utility;

namespace KeePass.UI
{
	public sealed class OnDemandStatusDialog : IStatusLogger
	{
		private readonly bool m_bUseThread;
		private readonly Form m_fOwner;

		private Thread m_th = null;
		private StatusProgressForm m_dlgModal = null;
		private readonly object m_objSync = new object();

		private const uint InitialProgress = 0;
		private const string InitialStatus = null;

		private volatile string m_strTitle = null;
		private volatile bool m_bTerminate = false;
		private volatile uint m_uProgress = InitialProgress;
		private volatile string m_strProgress = InitialStatus;

		public OnDemandStatusDialog(bool bUseThread, Form fOwner)
		{
			m_bUseThread = bUseThread;
			m_fOwner = fOwner;
		}

		public void StartLogging(string strOperation, bool bWriteOperationToLog)
		{
			m_strTitle = strOperation;
		}

		public void EndLogging()
		{
			lock(m_objSync) { m_bTerminate = true; }
			m_th = null;

			if(m_dlgModal != null)
			{
				DestroyStatusDialog(m_dlgModal);
				m_dlgModal = null;
			}
		}

		public bool SetProgress(uint uPercent)
		{
			lock(m_objSync) { m_uProgress = uPercent; }

			return ((m_dlgModal == null) || m_dlgModal.SetProgress(uPercent));
		}

		public bool SetText(string strNewText, LogStatusType lsType)
		{
			if(strNewText == null) return true;
			if(lsType != LogStatusType.Info) return true;

			if(m_bUseThread && (m_th == null))
			{
				ThreadStart ts = new ThreadStart(this.GuiThread);
				m_th = new Thread(ts);
				m_th.Start();
			}
			if(!m_bUseThread && (m_dlgModal == null))
				m_dlgModal = ConstructStatusDialog();

			lock(m_objSync) { m_strProgress = strNewText; }
			return ((m_dlgModal == null) || m_dlgModal.SetText(strNewText, lsType));
		}

		public bool ContinueWork()
		{
			return ((m_dlgModal == null) || m_dlgModal.ContinueWork());
		}

		private void GuiThread()
		{
			uint uProgress = InitialProgress;
			string strProgress = InitialStatus;

			StatusProgressForm dlg = null;
			while(true)
			{
				lock(m_objSync)
				{
					if(m_bTerminate) break;

					if(m_uProgress != uProgress)
					{
						uProgress = m_uProgress;
						if(dlg != null) dlg.SetProgress(uProgress);
					}

					if(m_strProgress != strProgress)
					{
						strProgress = m_strProgress;

						if(dlg == null) dlg = ConstructStatusDialog();

						dlg.SetText(strProgress, LogStatusType.Info);
					}
				}

				Application.DoEvents();
			}

			DestroyStatusDialog(dlg);
		}

		private StatusProgressForm ConstructStatusDialog()
		{
			StatusProgressForm dlg = StatusProgressForm.ConstructEx(
				m_strTitle, false, true, (m_bUseThread ? null : m_fOwner), null);
			dlg.SetProgress(m_uProgress);

			MainForm mfOwner = ((m_fOwner != null) ? (m_fOwner as MainForm) : null);
			if(!m_bUseThread && (mfOwner != null))
			{
				// mfOwner.RedirectActivationPush(dlg);
				mfOwner.UIBlockInteraction(true);
			}

			return dlg;
		}

		private void DestroyStatusDialog(StatusProgressForm dlg)
		{
			if(dlg != null)
			{
				MainForm mfOwner = ((m_fOwner != null) ? (m_fOwner as MainForm) : null);
				if(!m_bUseThread && (mfOwner != null))
				{
					// mfOwner.RedirectActivationPop();
					mfOwner.UIBlockInteraction(false);
				}

				StatusProgressForm.DestroyEx(dlg);

				// Conflict with 3116455
				// if(mfOwner != null) mfOwner.Activate(); // Prevent disappearing
			}
		}
	}

	public sealed class UIBlockerStatusLogger : IStatusLogger
	{
		private readonly MainForm m_mf;

		private string m_strText = string.Empty;
		private int m_tLastAnim = Environment.TickCount;
		private int m_cDots = 1;

		public UIBlockerStatusLogger(Form fParent)
		{
			m_mf = (fParent as MainForm);
		}

		public void StartLogging(string strOperation, bool bWriteOperationToLog)
		{
			if(m_mf != null)
			{
				TaskbarList.SetProgressState(m_mf, TbpFlag.Indeterminate);
				m_mf.UIBlockInteraction(true);
			}
		}

		public void EndLogging()
		{
			if(m_mf != null)
			{
				m_mf.UIBlockInteraction(false);
				TaskbarList.SetProgressState(m_mf, TbpFlag.NoProgress);
			}
		}

		public bool SetProgress(uint uPercent)
		{
			Animate();
			return true;
		}

		public bool SetText(string strNewText, LogStatusType lsType)
		{
			if((m_mf != null) && !string.IsNullOrEmpty(strNewText))
			{
				m_strText = strNewText;
				m_mf.SetStatusEx(strNewText);

				UIUtil.DoEventsByTime(true);
			}
			else UIUtil.DoEventsByTime(false);

			return true;
		}

		public bool ContinueWork()
		{
			Animate();
			return true;
		}

		private void Animate()
		{
			int t = Environment.TickCount, tLast = m_tLastAnim;
			int d = t - tLast;

			if(d >= 1000)
			{
				m_tLastAnim = t;

				if(m_mf != null)
				{
					string strDots = new string('.', m_cDots);
					m_mf.SetStatusEx(StrUtil.TrimDots(m_strText, false) + strDots);
					m_cDots = (m_cDots % 5) + 1; // At least one dot

					UIUtil.DoEventsByTime(true);
					return;
				}
			}

			UIUtil.DoEventsByTime(false);
		}
	}
}