File: ExchangeDataForm.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 (399 lines) | stat: -rw-r--r-- 11,804 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
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
/*
  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.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using KeePass.App;
using KeePass.App.Configuration;
using KeePass.DataExchange;
using KeePass.Resources;
using KeePass.UI;

using KeePassLib;
using KeePassLib.Serialization;
using KeePassLib.Utility;

namespace KeePass.Forms
{
	public partial class ExchangeDataForm : Form
	{
		private bool m_bExport = false;
		private PwDatabase m_pd = null;
		private PwGroup m_pg = null;

		private ImageList m_ilFormats = null;

		private FileFormatProvider m_fmtCur = null; // Current selection
		private FileFormatProvider m_fmtFinal = null; // Returned as result
		public FileFormatProvider ResultFormat
		{
			get { return m_fmtFinal; }
		}

		private string[] m_vFiles = null;
		public string[] ResultFiles
		{
			get { return m_vFiles; }
		}

		private PwExportInfo m_piExport = null;
		internal PwExportInfo ExportInfo
		{
			get { return m_piExport; }
			set { m_piExport = value; }
		}

		private sealed class FormatGroupEx
		{
			private readonly ListViewGroup m_lvg;
			public ListViewGroup Group { get { return m_lvg; } }

			private readonly List<ListViewItem> m_lItems = new List<ListViewItem>();
			public List<ListViewItem> Items { get { return m_lItems; } }

			public FormatGroupEx(string strGroupName)
			{
				m_lvg = new ListViewGroup(strGroupName);
			}
		}

		public void InitEx(bool bExport, PwDatabase pd, PwGroup pg)
		{
			m_bExport = bExport;
			m_pd = pd;
			m_pg = pg;
		}

		public ExchangeDataForm()
		{
			InitializeComponent();
			GlobalWindowManager.InitializeForm(this);
		}

		private void OnFormLoad(object sender, EventArgs e)
		{
			if((m_pd == null) && (m_pg == null))
			{
				Debug.Assert(false);
				throw new InvalidOperationException();
			}

			GlobalWindowManager.AddWindow(this);

			string strTitle = (m_bExport ? KPRes.ExportFileTitle : KPRes.ImportFileTitle);
			string strDesc = (m_bExport ? KPRes.ExportFileDesc : KPRes.ImportFileDesc);
			Image img = (m_bExport ? Properties.Resources.B48x48_Folder_Txt :
				Properties.Resources.B48x48_Folder_Download);
			BannerFactory.CreateBannerEx(this, m_bannerImage, img, strTitle, strDesc);

			this.Icon = AppIcons.Default;
			this.Text = strTitle;

			UIUtil.ConfigureToolTip(m_ttRect);
			UIUtil.SetToolTip(m_ttRect, m_btnSelFile, StrUtil.TrimDots(
				KPRes.SelectFile, true), true);

			m_lvFormats.ShowGroups = true;

			int w = m_lvFormats.ClientSize.Width - UIUtil.GetVScrollBarWidth();
			m_lvFormats.Columns.Add(string.Empty, w - 1);

			List<Image> lImages = new List<Image>();
			Dictionary<string, FormatGroupEx> dictGroups =
				new Dictionary<string, FormatGroupEx>();

			foreach(FileFormatProvider f in Program.FileFormatPool)
			{
				if(m_bExport && !f.SupportsExport) continue;
				if(!m_bExport && !f.SupportsImport) continue;

				string strDisplayName = f.DisplayName;
				if(string.IsNullOrEmpty(strDisplayName)) { Debug.Assert(false); continue; }

				string strAppGroup = f.ApplicationGroup;
				if(string.IsNullOrEmpty(strAppGroup)) strAppGroup = KPRes.General;

				FormatGroupEx grp;
				if(!dictGroups.TryGetValue(strAppGroup, out grp))
				{
					grp = new FormatGroupEx(strAppGroup);
					dictGroups[strAppGroup] = grp;
				}

				ListViewItem lvi = new ListViewItem(strDisplayName);
				lvi.Group = grp.Group;
				lvi.Tag = f;

				img = f.SmallIcon;
				if(img == null)
				{
					string strExt = f.DefaultExtension;
					if(!string.IsNullOrEmpty(strExt))
						strExt = UIUtil.GetPrimaryFileTypeExt(strExt);
					if(!string.IsNullOrEmpty(strExt))
						img = FileIcons.GetImageForExtension(strExt, null);
				}
				if(img == null)
					img = Properties.Resources.B16x16_Folder_Inbox;

				int iImage = lImages.IndexOf(img);
				if(iImage < 0) { iImage = lImages.Count; lImages.Add(img); }
				lvi.ImageIndex = iImage;

				grp.Items.Add(lvi);
			}

			foreach(FormatGroupEx formatGroup in dictGroups.Values)
			{
				m_lvFormats.Groups.Add(formatGroup.Group);
				foreach(ListViewItem lvi in formatGroup.Items)
					m_lvFormats.Items.Add(lvi);
			}

			m_ilFormats = UIUtil.BuildImageListUnscaled(lImages,
				DpiUtil.ScaleIntX(16), DpiUtil.ScaleIntY(16));
			m_lvFormats.SmallImageList = m_ilFormats;

			if(m_bExport)
			{
				m_grpFiles.Text = KPRes.Destination;
				UIUtil.SetText(m_lblFiles, KPRes.File + ":");
				UIUtil.SetButtonImage(m_btnSelFile,
					Properties.Resources.B16x16_FileSaveAs, false);

				m_lnkFileFormats.Enabled = false;
				m_lnkFileFormats.Visible = false;
			}
			else // Import
			{
				m_grpFiles.Text = KPRes.Source;
				UIUtil.SetButtonImage(m_btnSelFile,
					Properties.Resources.B16x16_Folder_Yellow_Open, false);

				m_grpExport.Enabled = false;
				m_grpExportPost.Enabled = false;
			}

			FormDataExchange fdx = new FormDataExchange(this, true, true, false);
			AceDefaults aceDef = Program.Config.Defaults;
			fdx.Add(m_cbExportMasterKeySpec, aceDef, "ExportMasterKeySpec");
			fdx.Add(m_cbExportParentGroups, aceDef, "ExportParentGroups");
			fdx.Add(m_cbExportPostOpen, aceDef, "ExportPostOpen");
			fdx.Add(m_cbExportPostShow, aceDef, "ExportPostShow");

			UpdateUIState();
		}

		private void OnFormClosed(object sender, FormClosedEventArgs e)
		{
			if(m_ilFormats != null)
			{
				m_lvFormats.SmallImageList = null; // Detach event handlers
				m_ilFormats.Dispose();
				m_ilFormats = null;
			}

			GlobalWindowManager.RemoveWindow(this);
		}

		private void OnLinkFileFormats(object sender, LinkLabelLinkClickedEventArgs e)
		{
			AppHelp.ShowHelp(AppDefs.HelpTopics.ImportExport, null);
		}

		private static bool CheckFilePath(string strPath)
		{
			if(string.IsNullOrEmpty(strPath)) { Debug.Assert(false); return false; }

			if(strPath.IndexOf(';') >= 0)
			{
				MessageService.ShowWarning(strPath, KPRes.FileNameContainsSemicolonError);
				return false;
			}

			return true;
		}

		private void OnBtnSelFile(object sender, EventArgs e)
		{
			UpdateUIState();
			if(m_fmtCur == null) { Debug.Assert(false); return; }
			if(!m_fmtCur.RequiresFile) return; // Break on double-click

			string strFormat = m_fmtCur.FormatName;
			if(string.IsNullOrEmpty(strFormat)) strFormat = KPRes.Data;

			string strExts = m_fmtCur.DefaultExtension;
			if(string.IsNullOrEmpty(strExts)) strExts = "export";
			string strPriExt = UIUtil.GetPrimaryFileTypeExt(strExts);
			if(strPriExt.Length == 0) strPriExt = "export"; // In case of "|"

			string strFilter = UIUtil.CreateFileTypeFilter(strExts, strFormat, true);

			if(!m_bExport) // Import
			{
				OpenFileDialogEx ofd = UIUtil.CreateOpenFileDialog(KPRes.Import +
					": " + strFormat, strFilter, 1, strPriExt, true,
					AppDefs.FileDialogContext.Import);

				if(ofd.ShowDialog() != DialogResult.OK) return;

				StringBuilder sb = new StringBuilder();
				foreach(string str in ofd.FileNames)
				{
					if(!CheckFilePath(str)) continue;

					if(sb.Length != 0) sb.Append(';');
					sb.Append(str);
				}

				string strFiles = sb.ToString();
				if(strFiles.Length >= m_tbFile.MaxLength)
				{
					MessageService.ShowWarning(KPRes.TooManyFilesError);
					return;
				}

				m_tbFile.Text = strFiles;
			}
			else // Export
			{
				SaveFileDialogEx sfd = UIUtil.CreateSaveFileDialog(KPRes.Export +
					": " + strFormat, null, strFilter, 1, strPriExt,
					AppDefs.FileDialogContext.Export);

				string strSuggestion = KPRes.Database;
				if((m_pd != null) && (m_pd.IOConnectionInfo.Path.Length > 0))
					strSuggestion = UrlUtil.StripExtension(UrlUtil.GetFileName(
						m_pd.IOConnectionInfo.Path));
				sfd.FileName = strSuggestion + "." + strPriExt;

				if(sfd.ShowDialog() != DialogResult.OK) return;

				string strFile = sfd.FileName;
				if(!CheckFilePath(strFile)) return;
				m_tbFile.Text = strFile;
			}

			UpdateUIState();
		}

		private void UpdateUIState()
		{
			ListView.SelectedListViewItemCollection lvsc = m_lvFormats.SelectedItems;
			m_fmtCur = (((lvsc != null) && (lvsc.Count == 1)) ?
				(lvsc[0].Tag as FileFormatProvider) : null);
			bool bFormat = (m_fmtCur != null);

			bool bFileReq = (bFormat && m_fmtCur.RequiresFile);
			UIUtil.SetEnabledFast(bFileReq, m_lblFiles, m_tbFile, m_btnSelFile);

			bool bExportExt = (m_bExport && (m_piExport != null));
			UIUtil.SetEnabledFast((bExportExt && bFormat && m_fmtCur.RequiresKey),
				m_cbExportMasterKeySpec, m_lblExportMasterKeySpec);

			UIUtil.SetEnabledFast((bExportExt && bFormat && (m_pd != null) &&
				(m_pg != m_pd.RootGroup)),
				m_cbExportParentGroups, m_lnkExportParentGroups);

			UIUtil.SetEnabledFast((bExportExt && bFileReq), m_cbExportPostOpen,
				m_cbExportPostShow);

			m_btnOK.Enabled = (bFormat && ((m_tbFile.Text.Length != 0) ||
				!m_fmtCur.RequiresFile));
		}

		private bool PrepareExchangeEx()
		{
			UpdateUIState();
			if(m_fmtCur == null) return false;

			string strFiles = m_tbFile.Text;
			string[] vFiles = strFiles.Split(new char[] { ';' },
				StringSplitOptions.RemoveEmptyEntries);

			if(m_fmtCur.RequiresFile)
			{
				if(vFiles.Length == 0) return false;

				foreach(string strFile in vFiles)
				{
					IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile);
					if(ioc.IsLocalFile() && !UrlUtil.IsAbsolutePath(strFile))
					{
						MessageService.ShowWarning(strFile, KPRes.FilePathFullReq);
						return false;
					}
				}

				// Allow only one file when exporting
				if(m_bExport && !CheckFilePath(strFiles)) return false;
			}
			else vFiles = MemUtil.EmptyArray<string>();

			if(m_piExport != null)
			{
				m_piExport.ExportMasterKeySpec = m_cbExportMasterKeySpec.Checked;
				m_piExport.ExportParentGroups = m_cbExportParentGroups.Checked;
				m_piExport.ExportPostOpen = m_cbExportPostOpen.Checked;
				m_piExport.ExportPostShow = m_cbExportPostShow.Checked;
			}

			m_fmtFinal = m_fmtCur;
			m_vFiles = vFiles;
			return true;
		}

		private void OnBtnOK(object sender, EventArgs e)
		{
			if(!PrepareExchangeEx()) this.DialogResult = DialogResult.None;
		}

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

		private void OnFormatsSelectedIndexChanged(object sender, EventArgs e)
		{
			UpdateUIState();
		}

		private void OnImportFileTextChanged(object sender, EventArgs e)
		{
			UpdateUIState();
		}

		private void OnFormatsItemActivate(object sender, EventArgs e)
		{
			OnBtnSelFile(sender, e);
		}

		private void OnLinkExportParentGroups(object sender, LinkLabelLinkClickedEventArgs e)
		{
			AppHelp.ShowHelp(AppDefs.HelpTopics.ImportExport,
				AppDefs.HelpTopics.ImportExportParents);
		}
	}
}