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 
32 using KeePassLib;
33 using KeePassLib.Interfaces;
34 
35 namespace KeePass.Forms
36 {
37 	public partial class StatusLoggerForm : Form, IStatusLogger
38 	{
39 		private bool m_bIsModal = false;
40 		private bool m_bCancelled = false;
41 		private bool m_bCloseMode = false;
42 		private uint m_uLastPercent = 0;
43 
44 		private uint uWarnings = 0;
45 		private uint uErrors = 0;
46 
47 		private ImageList m_ilIcons = null;
48 
InitEx(bool bIsModal)49 		public void InitEx(bool bIsModal)
50 		{
51 			m_bIsModal = bIsModal;
52 		}
53 
StartLogging(string strOperation, bool bWriteOperationToLog)54 		public void StartLogging(string strOperation, bool bWriteOperationToLog)
55 		{
56 			if(strOperation != null)
57 			{
58 				this.Text = PwDefs.ShortProductName + " - " + strOperation;
59 
60 				if(bWriteOperationToLog)
61 					this.SetText(strOperation, LogStatusType.Info);
62 			}
63 
64 			m_pbProgress.Value = 0;
65 			m_uLastPercent = 0;
66 		}
67 
EndLogging()68 		public void EndLogging()
69 		{
70 			m_btnCancel.Text = KPRes.Close;
71 			m_bCloseMode = true;
72 
73 			SetText(string.Empty, LogStatusType.AdditionalInfo);
74 
75 			string strFinish = KPRes.Ready + " " + uErrors.ToString() + " " + KPRes.Errors +
76 				", " + uWarnings.ToString() + " " + KPRes.Warnings + ".";
77 			this.SetText(strFinish, LogStatusType.Info);
78 
79 			m_pbProgress.Value = 100;
80 			m_uLastPercent = 100;
81 
82 			Application.DoEvents();
83 		}
84 
SetProgress(uint uPercent)85 		public bool SetProgress(uint uPercent)
86 		{
87 			if(uPercent != m_uLastPercent)
88 			{
89 				m_pbProgress.Value = (int)uPercent;
90 				m_uLastPercent = uPercent;
91 
92 				Application.DoEvents();
93 			}
94 
95 			return !m_bCancelled;
96 		}
97 
SetText(string strNewText, LogStatusType lsType)98 		public bool SetText(string strNewText, LogStatusType lsType)
99 		{
100 			if(strNewText != null)
101 			{
102 				m_lvMessages.Items.Add(strNewText, (int)lsType);
103 				m_lvMessages.EnsureVisible(m_lvMessages.Items.Count - 1);
104 			}
105 
106 			if(lsType == LogStatusType.Warning) ++uWarnings;
107 			else if(lsType == LogStatusType.Error) ++uErrors;
108 
109 			ProcessResize();
110 			Application.DoEvents();
111 			return !m_bCancelled;
112 		}
113 
ContinueWork()114 		public bool ContinueWork()
115 		{
116 			Application.DoEvents();
117 
118 			return !m_bCancelled;
119 		}
120 
StatusLoggerForm()121 		public StatusLoggerForm()
122 		{
123 			InitializeComponent();
124 			GlobalWindowManager.InitializeForm(this);
125 		}
126 
OnFormLoad(object sender, EventArgs e)127 		private void OnFormLoad(object sender, EventArgs e)
128 		{
129 			GlobalWindowManager.AddWindow(this);
130 
131 			this.Icon = AppIcons.Default;
132 			this.Text = PwDefs.ShortProductName;
133 
134 			m_pbProgress.Minimum = 0;
135 			m_pbProgress.Maximum = 100;
136 
137 			List<Image> lImages = new List<Image>();
138 			lImages.Add(Properties.Resources.B16x16_MessageBox_Info);
139 			lImages.Add(Properties.Resources.B16x16_MessageBox_Warning);
140 			lImages.Add(Properties.Resources.B16x16_MessageBox_Critical);
141 			lImages.Add(Properties.Resources.B16x16_Transparent);
142 
143 			m_ilIcons = UIUtil.BuildImageListUnscaled(lImages,
144 				DpiUtil.ScaleIntX(16), DpiUtil.ScaleIntY(16));
145 			m_lvMessages.SmallImageList = m_ilIcons;
146 
147 			m_lvMessages.Columns.Add(KPRes.Status);
148 
149 			ProcessResize();
150 		}
151 
ProcessResize()152 		private void ProcessResize()
153 		{
154 			UIUtil.ResizeColumns(m_lvMessages, true);
155 		}
156 
OnBtnCancel(object sender, EventArgs e)157 		private void OnBtnCancel(object sender, EventArgs e)
158 		{
159 			if(m_bCloseMode)
160 			{
161 				if(m_bIsModal) this.DialogResult = DialogResult.Cancel;
162 				else Close();
163 			}
164 			else
165 			{
166 				m_bCancelled = true;
167 				this.DialogResult = DialogResult.None;
168 			}
169 		}
170 
OnMessagesSelectedIndexChanged(object sender, EventArgs e)171 		private void OnMessagesSelectedIndexChanged(object sender, EventArgs e)
172 		{
173 			ListView.SelectedListViewItemCollection slvic = m_lvMessages.SelectedItems;
174 			if(slvic.Count == 0)
175 			{
176 				m_tbDetails.Text = string.Empty;
177 				return;
178 			}
179 
180 			UIUtil.SetMultilineText(m_tbDetails, slvic[0].Text);
181 		}
182 
OnFormClosed(object sender, FormClosedEventArgs e)183 		private void OnFormClosed(object sender, FormClosedEventArgs e)
184 		{
185 			if(m_ilIcons != null)
186 			{
187 				m_lvMessages.SmallImageList = null;
188 				m_ilIcons.Dispose();
189 				m_ilIcons = null;
190 			}
191 			else { Debug.Assert(false); }
192 
193 			GlobalWindowManager.RemoveWindow(this);
194 		}
195 	}
196 }
197