//-----------------------------------------------------------------------
//
// Copyright (c) Takayoshi Matsuyama. All rights reserved.
//
//-----------------------------------------------------------------------
// This file is part of Simple Backup.
//
// Simple Backup 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 3 of the License, or
// (at your option) any later version.
//
// Simple Backup 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 Simple Backup. If not, see .
namespace SimpleBackup.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using SimpleBackup.Core.Models;
///
/// Creates model objectrs.
///
internal class ModelFactory : IModelFactory
{
///
/// Creates the backup setting.
///
///
/// The backup setting.
///
public IBackupSetting CreateBackupSetting()
{
return new BackupSetting();
}
///
/// Creates the backup setting.
///
/// Name of the setting file.
/// The backup information collection.
///
/// The backup setting.
///
public IBackupSetting CreateBackupSetting(
string settingFileName,
IEnumerable backupInfoCollection)
{
return new BackupSetting(settingFileName, backupInfoCollection);
}
///
/// Creates the backup information.
///
///
/// The backup inforamtion.
///
public IBackupInfo CreateBackupInfo()
{
return new BackupInfo();
}
///
/// Creates the backup information.
///
/// The source backup information.
///
/// The backup inforamtion.
///
public IBackupInfo CreateBackupInfo(IBackupInfo sourceBackupInfo)
{
return new BackupInfo(sourceBackupInfo);
}
///
/// Creates the backup information.
///
/// The label.
/// Type of the backup source.
/// if set to true [is enabled].
/// if set to true [is zip archive].
/// The source path.
/// The destination folder path.
///
/// The backup inforamtion.
///
public IBackupInfo CreateBackupInfo(
string label,
BackupSourceType backupSourceType,
bool isEnabled,
bool isZipArchive,
string sourcePath,
string destinationFolderPath)
{
return new BackupInfo(
label,
backupSourceType,
isEnabled,
isZipArchive,
sourcePath,
destinationFolderPath);
}
///
/// Creates the backup information.
///
/// The identifier.
/// The label.
/// Type of the backup source.
/// if set to true [is enabled].
/// if set to true [is zip archive].
/// The source path.
/// The destination folder path.
///
/// The backup inforamtion.
///
public IBackupInfo CreateBackupInfo(
Guid id,
string label,
BackupSourceType backupSourceType,
bool isEnabled,
bool isZipArchive,
string sourcePath,
string destinationFolderPath)
{
return new BackupInfo(
id,
label,
backupSourceType,
isEnabled,
isZipArchive,
sourcePath,
destinationFolderPath);
}
///
/// Creates the backup information from binary.
///
/// The binary data.
/// if set to true copy suffix is needed.
///
/// The backup inforamtion.
///
public IBackupInfo CreateBackupInfoFromBinary(byte[] binaryData, bool copySuffixNeeded)
{
return BackupInfo.FromBinary(binaryData, copySuffixNeeded);
}
///
/// Creates the result information.
///
/// The category.
/// The description.
///
/// The result inforamtion.
///
public IResultInfo CreateResultInfo(BackupState category, string description)
{
return new ResultInfo(category, description);
}
///
/// Creates the destination path information.
///
/// Type of the backup source.
/// The path.
///
/// The destination path information.
///
public IDestinationPathInfo CreateDestinationPathInfo(BackupSourceType backupSourceType, string path)
{
return new DestinationPathInfo(backupSourceType, path);
}
///
/// Restores the backup setting from XML.
///
/// The XML.
/// Name of the file.
/// The restored backup setting.
public IBackupSetting RestoreBackupSettingFromXml(XElement xml, string fileName)
{
if (xml.Name != "BackupSetting")
{
return null;
}
XElement itemsElement = xml.Elements("Items").FirstOrDefault();
if (itemsElement == null)
{
return null;
}
var backupInfoList = new List();
foreach (XElement element in itemsElement.Elements())
{
IBackupInfo backupInfo = RestoreBackupInfoFromXml(element);
backupInfoList.Add(backupInfo);
}
return new BackupSetting(fileName, backupInfoList);
}
///
/// Restores the backup information from XML.
///
/// The XML.
/// The restored backup information.
private static IBackupInfo RestoreBackupInfoFromXml(XElement xml)
{
if (xml.Name != "BackupInfo")
{
return null;
}
XElement idElement = xml.Elements("Id").FirstOrDefault();
XElement labelElement = xml.Elements("Label").FirstOrDefault();
XElement backupSourceTypeElement = xml.Elements("BackupSourceType").FirstOrDefault();
XElement enabledElement = xml.Elements("IsEnabled").FirstOrDefault();
XElement zipArchiveElement = xml.Elements("IsZipArchive").FirstOrDefault();
XElement sourcePathElement = xml.Elements("SourcePath").FirstOrDefault();
XElement destinationFolderPathElement = xml.Elements("DestinationFolderPath").FirstOrDefault();
if ((idElement == null) ||
(labelElement == null) ||
(backupSourceTypeElement == null) ||
(enabledElement == null) ||
(zipArchiveElement == null) ||
(sourcePathElement == null) ||
(destinationFolderPathElement == null))
{
return null;
}
Guid id;
if (Guid.TryParse(idElement.Value, out id) == false)
{
return null;
}
BackupSourceType backupSourceType;
if (Enum.TryParse(backupSourceTypeElement.Value, out backupSourceType) == false)
{
return null;
}
bool isEnabled;
if (bool.TryParse(enabledElement.Value, out isEnabled) == false)
{
return null;
}
bool isZipArchive;
if (bool.TryParse(zipArchiveElement.Value, out isZipArchive) == false)
{
return null;
}
return BackupInfo.CreateBackupInfo(
id,
labelElement.Value,
backupSourceType,
isEnabled,
isZipArchive,
sourcePathElement.Value,
destinationFolderPathElement.Value);
}
}
}