File: StatusProgressForm.cs

package info (click to toggle)
keepass2 2.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,496 kB
  • sloc: cs: 87,098; xml: 6,569; cpp: 311; makefile: 49; sh: 9
file content (189 lines) | stat: -rw-r--r-- 5,089 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
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2012 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.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using KeePass.UI;
using KeePass.Util;

using KeePassLib;
using KeePassLib.Interfaces;

namespace KeePass.Forms
{
	public partial class StatusProgressForm : Form, IStatusLogger
	{
		private string m_strTitle = null;
		private bool m_bCanCancel = true;
		private bool m_bMarquee = false;
		private Form m_fOwner = null;

		private volatile bool m_bCancelled = false;
		private bool m_bCanClose = true;

		public bool UserCancelled
		{
			get { return m_bCancelled; }
		}

		public static StatusProgressForm ConstructEx(string strTitle,
			bool bCanCancel, bool bMarqueeProgress, Form fOwner,
			string strInitialOp)
		{
			StatusProgressForm dlg = new StatusProgressForm();
			dlg.InitEx(strTitle, bCanCancel, bMarqueeProgress, fOwner);

			if(fOwner != null) dlg.Show(fOwner);
			else dlg.Show();

			dlg.StartLogging(strInitialOp, false);

			return dlg;
		}

		public static void DestroyEx(StatusProgressForm dlg)
		{
			if(dlg == null) { Debug.Assert(false); return; }

			dlg.EndLogging();
			dlg.Close();
			UIUtil.DestroyForm(dlg);
		}

		public void InitEx(string strTitle, bool bCanCancel, bool bMarqueeProgress,
			Form fOwner)
		{
			m_strTitle = strTitle; // May be null
			m_bCanCancel = bCanCancel;
			m_bMarquee = bMarqueeProgress;
			m_fOwner = fOwner;
		}

		public StatusProgressForm()
		{
			InitializeComponent();
			Program.Translation.ApplyTo(this);
		}

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

			GlobalWindowManager.AddWindow(this);

			m_pbTotal.Minimum = 0;
			m_pbTotal.Maximum = 100;
			m_pbTotal.Value = 0;

			try { if(m_bMarquee) m_pbTotal.Style = ProgressBarStyle.Marquee; }
			catch(Exception) { Debug.Assert(WinUtil.IsWindows9x || WinUtil.IsWindows2000); }

			if(!string.IsNullOrEmpty(m_strTitle)) this.Text = m_strTitle;
			else this.Text = PwDefs.ShortProductName;

			try { if(m_fOwner != null) this.Owner = m_fOwner; }
			catch(Exception) { Debug.Assert(false); } // Throws from other thread

			if(!m_bCanCancel) m_btnCancel.Enabled = false;
		}

		private void OnBtnCancel(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.None;

			if(m_bCancelled) { Debug.Assert(false); return; }
			if(!m_bCanCancel) return;

			m_bCancelled = true;
			m_btnCancel.Enabled = false;
		}

		public delegate void Priv_SetProgressInternal(string strText, int nPercent);

		private void SetProgressInternal(string strText, int nPercent)
		{
			Debug.Assert(!m_lblTotal.InvokeRequired);
			Debug.Assert(!m_pbTotal.InvokeRequired);

			if(strText != null) m_lblTotal.Text = strText;

			if((nPercent >= 0) && (nPercent <= 100))
				m_pbTotal.Value = nPercent; // .NET compares with cached value
		}

		private bool SetProgressGlobal(string strText, int nPercent)
		{
			if(this.InvokeRequired)
				this.Invoke(new Priv_SetProgressInternal(this.SetProgressInternal),
					strText, nPercent);
			else SetProgressInternal(strText, nPercent);

			Application.DoEvents();
			return !m_bCancelled;
		}

		public void StartLogging(string strOperation, bool bWriteOperationToLog)
		{
			SetProgressGlobal(strOperation, -1);
			m_bCanClose = false;
		}

		public void EndLogging()
		{
			m_bCanClose = true;
		}

		public bool SetProgress(uint uPercent)
		{
			return SetProgressGlobal(null, (int)uPercent);
		}

		public bool SetText(string strNewText, LogStatusType lsType)
		{
			return SetProgressGlobal(strNewText, -1);
		}

		public bool ContinueWork()
		{
			Application.DoEvents();
			return !m_bCancelled;
		}

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

		private void OnFormClosing(object sender, FormClosingEventArgs e)
		{
			if(!m_bCanClose)
			{
				Debug.Assert(e.CloseReason == CloseReason.UserClosing);
				e.Cancel = true;
			}
		}
	}
}