using SecurityContract.BindingModels;
using SecurityContract.Logics;
using System;
using System.Windows.Forms;
using System.Xml.Linq;
using ToolsDesktop.Helpers;
using ToolsDesktop.Interfaces;
using ToolsDesktop.Models;
using ToolsModule.BusinessLogics;
using ToolsModule.Enums;
namespace SecurityWindowsDesktop.SpecialControls
{
///
/// Контрол для работы с бекапом
///
public partial class BackupControl : UserControl, IControl
{
///
/// Класс с бизнес-логикой работы с бекапом
///
private readonly IBackupLogic _businessLogic;
///
/// Событие, вызываемое при закрытии контрола
///
private event Action CloseEvent;
///
/// Контрол для работы с бекапом
///
public BackupControl()
{
InitializeComponent();
_businessLogic = DependencyManager.Instance.Resolve();
Title = "Работа с бекапом";
ControlId = new Guid("cc9844e6-5d92-4c89-b817-4c17ec382bc1");
AccessOperation = AccessOperation.РаботасБекапом;
toolStripButtonClose.Click += (object sender, EventArgs e) =>
{
CloseEvent?.Invoke(ControlId);
Dispose();
};
}
#region IControl
public Guid ControlId { get; private set; }
public string Title { get; private set; }
public AccessOperation AccessOperation { get; private set; }
public IControl GetInstance() => new BackupControl() { ControlId = Guid.NewGuid() };
public void Open(ControlOpenModel model)
{
if (model.CloseList != null)
{
CloseEvent += model.CloseList;
}
Dock = DockStyle.Fill;
}
public string SaveToXml() => new XElement("Control",
new XAttribute("Type", GetType().FullName),
new XAttribute("ControlId", ControlId),
new XAttribute("Title", Title),
new XAttribute("AccessOperation", AccessOperation)).ToString();
public void LoadFromXml(string xml)
{
var control = XElement.Parse(xml);
ControlId = new Guid(control.Attribute("ControlId").Value.ToString());
Title = control.Attribute("Title").Value.ToString();
AccessOperation = (AccessOperation)Enum.Parse(typeof(AccessOperation), control.Attribute("AccessOperation").Value.ToString());
}
#endregion
///
/// Выбор пути для папки сохранения бекапа
///
///
///
private void ButtonSaveSelectFolder_Click(object sender, EventArgs e)
{
var fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
textBoxSaveFolderName.Text = fbd.SelectedPath;
}
}
///
/// Создание бекапа
///
///
///
private void ButtonCreateBackup_Click(object sender, EventArgs e)
{
var cursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
if (_businessLogic.CreateBackUp(new BackupBindingModel
{
FolderName = textBoxSaveFolderName.Text,
FullData = checkBoxFullLoad.Checked,
CreateArchive = checkBoxCreateArchive.Checked
}))
{
DialogHelper.MessageInformation("Сохранение прошло успешно", "Результат");
}
else
{
DialogHelper.MessageException(_businessLogic.Errors, "Ошибки при сохранении");
}
Cursor.Current = cursor;
}
///
/// Выбор пути для папки восстановления бекапа
///
///
///
private void ButtonLoadSelectFolder_Click(object sender, EventArgs e)
{
var fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
textBoxLoadFolderName.Text = fbd.SelectedPath;
}
}
///
/// Выбор пути для файла-архива с бекапом
///
///
///
private void ButtonLoadArchiveSelect_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
textBoxLoadArchiveName.Text = ofd.FileName;
}
}
///
/// Восстановление
///
///
///
private void ButtonRestoreBackup_Click(object sender, EventArgs e)
{
var cursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
if (_businessLogic.RestoreBackUp(new BackupBindingModel
{
FolderName = textBoxLoadFolderName.Text,
ArchiveFileName = textBoxLoadArchiveName.Text
}))
{
DialogHelper.MessageInformation("Сохранение прошло успешно", "Результат");
}
else
{
DialogHelper.MessageException(_businessLogic.Errors, "Ошибки при сохранении");
}
Cursor.Current = cursor;
}
}
}