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.Text;
23 using System.Drawing;
24 using System.IO;
25 
26 using KeePass.Resources;
27 
28 using KeePassLib;
29 using KeePassLib.Collections;
30 using KeePassLib.Interfaces;
31 using KeePassLib.Serialization;
32 
33 namespace KeePass.DataExchange.Formats
34 {
35 	internal sealed class KeePassXml2x : FileFormatProvider
36 	{
37 		public override bool SupportsImport { get { return true; } }
38 		public override bool SupportsExport { get { return true; } }
39 
40 		public override string FormatName { get { return "KeePass XML (2.x)"; } }
41 		public override string DefaultExtension { get { return "xml"; } }
42 		public override string ApplicationGroup { get { return PwDefs.ShortProductName; } }
43 
44 		public override bool SupportsUuids { get { return true; } }
45 
46 		public override Image SmallIcon
47 		{
48 			get { return KeePass.Properties.Resources.B16x16_Binary; }
49 		}
50 
Import(PwDatabase pwStorage, Stream sInput, IStatusLogger slLogger)51 		public override void Import(PwDatabase pwStorage, Stream sInput,
52 			IStatusLogger slLogger)
53 		{
54 			KdbxFile kdbx = new KdbxFile(pwStorage);
55 			kdbx.Load(sInput, KdbxFormat.PlainXml, slLogger);
56 		}
57 
Export(PwExportInfo pwExportInfo, Stream sOutput, IStatusLogger slLogger)58 		public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
59 			IStatusLogger slLogger)
60 		{
61 			PwDatabase pd = (pwExportInfo.ContextDatabase ?? new PwDatabase());
62 
63 			PwObjectList<PwDeletedObject> vDel = null;
64 			if(!pwExportInfo.ExportDeletedObjects)
65 			{
66 				vDel = pd.DeletedObjects.CloneShallow();
67 				pd.DeletedObjects.Clear();
68 			}
69 
70 			KdbxFile kdb = new KdbxFile(pd);
71 			kdb.Save(sOutput, pwExportInfo.DataGroup, KdbxFormat.PlainXml, slLogger);
72 
73 			// Restore deleted objects list
74 			if(vDel != null) pd.DeletedObjects.Add(vDel);
75 
76 			return true;
77 		}
78 	}
79 }
80