1 //-----------------------------------------------------------------------
2 // <copyright file="ModelFactory.cs" company="Takayoshi Matsuyama">
3 // Copyright (c) Takayoshi Matsuyama. All rights reserved.
5 //-----------------------------------------------------------------------
7 // This file is part of Simple Backup.
9 // Simple Backup is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
14 // Simple Backup is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with Simple Backup. If not, see <http://www.gnu.org/licenses/>.
22 namespace SimpleBackup.Models
25 using System.Collections.Generic;
27 using System.Xml.Linq;
28 using SimpleBackup.Core.Models;
31 /// Creates model objectrs.
33 internal class ModelFactory : IModelFactory
36 /// Creates the backup setting.
39 /// The backup setting.
41 public IBackupSetting CreateBackupSetting()
43 return new BackupSetting();
47 /// Creates the backup setting.
49 /// <param name="settingFileName">Name of the setting file.</param>
50 /// <param name="backupInfoCollection">The backup information collection.</param>
52 /// The backup setting.
54 public IBackupSetting CreateBackupSetting(
55 string settingFileName,
56 IEnumerable<IBackupInfo> backupInfoCollection)
58 return new BackupSetting(settingFileName, backupInfoCollection);
62 /// Creates the backup information.
65 /// The backup inforamtion.
67 public IBackupInfo CreateBackupInfo()
69 return new BackupInfo();
73 /// Creates the backup information.
75 /// <param name="sourceBackupInfo">The source backup information.</param>
77 /// The backup inforamtion.
79 public IBackupInfo CreateBackupInfo(IBackupInfo sourceBackupInfo)
81 return new BackupInfo(sourceBackupInfo);
85 /// Creates the backup information.
87 /// <param name="label">The label.</param>
88 /// <param name="backupSourceType">Type of the backup source.</param>
89 /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
90 /// <param name="isZipArchive">if set to <c>true</c> [is zip archive].</param>
91 /// <param name="sourcePath">The source path.</param>
92 /// <param name="destinationFolderPath">The destination folder path.</param>
94 /// The backup inforamtion.
96 public IBackupInfo CreateBackupInfo(
98 BackupSourceType backupSourceType,
102 string destinationFolderPath)
104 return new BackupInfo(
110 destinationFolderPath);
114 /// Creates the backup information.
116 /// <param name="id">The identifier.</param>
117 /// <param name="label">The label.</param>
118 /// <param name="backupSourceType">Type of the backup source.</param>
119 /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
120 /// <param name="isZipArchive">if set to <c>true</c> [is zip archive].</param>
121 /// <param name="sourcePath">The source path.</param>
122 /// <param name="destinationFolderPath">The destination folder path.</param>
124 /// The backup inforamtion.
126 public IBackupInfo CreateBackupInfo(
129 BackupSourceType backupSourceType,
133 string destinationFolderPath)
135 return new BackupInfo(
142 destinationFolderPath);
146 /// Creates the backup information from binary.
148 /// <param name="binaryData">The binary data.</param>
149 /// <param name="copySuffixNeeded">if set to <c>true</c> copy suffix is needed.</param>
151 /// The backup inforamtion.
153 public IBackupInfo CreateBackupInfoFromBinary(byte[] binaryData, bool copySuffixNeeded)
155 return BackupInfo.FromBinary(binaryData, copySuffixNeeded);
159 /// Creates the result information.
161 /// <param name="category">The category.</param>
162 /// <param name="description">The description.</param>
164 /// The result inforamtion.
166 public IResultInfo CreateResultInfo(BackupState category, string description)
168 return new ResultInfo(category, description);
172 /// Creates the destination path information.
174 /// <param name="backupSourceType">Type of the backup source.</param>
175 /// <param name="path">The path.</param>
177 /// The destination path information.
179 public IDestinationPathInfo CreateDestinationPathInfo(BackupSourceType backupSourceType, string path)
181 return new DestinationPathInfo(backupSourceType, path);
185 /// Restores the backup setting from XML.
187 /// <param name="xml">The XML.</param>
188 /// <param name="fileName">Name of the file.</param>
189 /// <returns>The restored backup setting.</returns>
190 public IBackupSetting RestoreBackupSettingFromXml(XElement xml, string fileName)
192 if (xml.Name != "BackupSetting")
197 XElement itemsElement = xml.Elements("Items").FirstOrDefault();
198 if (itemsElement == null)
203 var backupInfoList = new List<IBackupInfo>();
204 foreach (XElement element in itemsElement.Elements())
206 IBackupInfo backupInfo = RestoreBackupInfoFromXml(element);
207 backupInfoList.Add(backupInfo);
210 return new BackupSetting(fileName, backupInfoList);
214 /// Restores the backup information from XML.
216 /// <param name="xml">The XML.</param>
217 /// <returns>The restored backup information.</returns>
218 private static IBackupInfo RestoreBackupInfoFromXml(XElement xml)
220 if (xml.Name != "BackupInfo")
225 XElement idElement = xml.Elements("Id").FirstOrDefault();
226 XElement labelElement = xml.Elements("Label").FirstOrDefault();
227 XElement backupSourceTypeElement = xml.Elements("BackupSourceType").FirstOrDefault();
228 XElement enabledElement = xml.Elements("IsEnabled").FirstOrDefault();
229 XElement zipArchiveElement = xml.Elements("IsZipArchive").FirstOrDefault();
230 XElement sourcePathElement = xml.Elements("SourcePath").FirstOrDefault();
231 XElement destinationFolderPathElement = xml.Elements("DestinationFolderPath").FirstOrDefault();
233 if ((idElement == null) ||
234 (labelElement == null) ||
235 (backupSourceTypeElement == null) ||
236 (enabledElement == null) ||
237 (zipArchiveElement == null) ||
238 (sourcePathElement == null) ||
239 (destinationFolderPathElement == null))
245 if (Guid.TryParse(idElement.Value, out id) == false)
250 BackupSourceType backupSourceType;
251 if (Enum.TryParse(backupSourceTypeElement.Value, out backupSourceType) == false)
257 if (bool.TryParse(enabledElement.Value, out isEnabled) == false)
263 if (bool.TryParse(zipArchiveElement.Value, out isZipArchive) == false)
268 return BackupInfo.CreateBackupInfo(
274 sourcePathElement.Value,
275 destinationFolderPathElement.Value);