1 //-----------------------------------------------------------------------
2 // <copyright file="BackupSetting.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 Foobar. If not, see <http://www.gnu.org/licenses/>.
23 using System.Collections.Generic;
25 using System.Xml.Linq;
26 using SimpleBackup.Core.Models;
27 using SimpleBackup.Resources;
29 namespace SimpleBackup.Models
32 /// Represents a backup setting.
34 public class BackupSetting : IBackupSetting
36 private readonly List<BackupInfo> backupInfoList = new List<BackupInfo>();
39 /// Initializes a new instance of the <see cref="BackupSetting"/> class.
41 public BackupSetting()
43 this.SettingFileName = string.Empty;
47 string settingFileName,
48 IEnumerable<BackupInfo> backupInfoCollection)
50 this.SettingFileName = settingFileName;
51 this.backupInfoList.AddRange(backupInfoCollection);
52 foreach (BackupInfo backupInfo in this.backupInfoList)
54 backupInfo.SettingFilePropertyChanged += this.OnBackupInfoSettingFilePropertyChanged;
59 /// Occurs when backup setting is modified.
61 public event EventHandler SettingModified;
64 /// Gets the name of the setting.
66 public string SettingName
70 if (string.IsNullOrEmpty(this.SettingFileName))
72 return Resources.Resources.Views_NewBackupSettingName;
75 return this.SettingFileName;
80 /// Gets or sets the name of the setting file.
82 public string SettingFileName { get; set; }
85 /// Gets or sets a value indicating whether this <see cref="BackupSetting"/> is modified.
87 public bool Modified { get; set; }
89 public IReadOnlyCollection<IBackupInfo> BackupInfos
93 return this.backupInfoList.AsReadOnly();
101 return this.backupInfoList.Count;
106 /// Inserts the specified index.
108 /// <param name="index">The index.</param>
109 /// <param name="backupInfo">The backup information.</param>
110 public void Insert(int index, IBackupInfo backupInfo)
112 backupInfo.SettingFilePropertyChanged += this.OnBackupInfoSettingFilePropertyChanged;
113 this.backupInfoList.Insert(index, (BackupInfo)backupInfo);
115 this.MarkAsModifiedAndPublishNotification();
119 /// Removes the specified removing backup information.
121 /// <param name="removingBackupInfo">The removing backup information.</param>
122 public void Remove(IBackupInfo removingBackupInfo)
124 removingBackupInfo.SettingFilePropertyChanged -= this.OnBackupInfoSettingFilePropertyChanged;
125 this.backupInfoList.Remove((BackupInfo)removingBackupInfo);
127 this.MarkAsModifiedAndPublishNotification();
130 public IBackupInfo FirstOrDefault(Guid id)
132 return this.backupInfoList.FirstOrDefault(p => p.Id == id);
135 public void MoveItem(Guid backupInfoId, ItemMovingDirection movingDirection)
137 IBackupInfo movingBackupInfo = null;
138 int currentIndex = 0;
139 for (int i = 0; i < this.backupInfoList.Count; i++)
141 if (this.backupInfoList[i].Id == backupInfoId)
144 movingBackupInfo = this.backupInfoList[i];
149 if (movingBackupInfo != null)
152 switch (movingDirection)
154 case ItemMovingDirection.MoveUp:
155 if (currentIndex > 0)
157 newIndex = currentIndex - 1;
161 case ItemMovingDirection.MoveDown:
162 if (currentIndex < this.backupInfoList.Count - 1)
164 newIndex = currentIndex + 1;
169 throw new InvalidOperationException("Not supported direction.");
174 IBackupInfo temp = this.backupInfoList[newIndex];
175 this.backupInfoList[newIndex] = this.backupInfoList[currentIndex];
176 this.backupInfoList[currentIndex] = (BackupInfo)temp;
179 this.MarkAsModifiedAndPublishNotification();
183 public XElement ToXml()
185 var backupSettingElement = new XElement("BackupSetting");
186 backupSettingElement.SetAttributeValue("Version", "1.0");
187 backupSettingElement.SetAttributeValue("RequiredToolVersion", "1.0.0.181");
189 var items = new XElement("Items");
190 foreach (IBackupInfo backupInfo in this.BackupInfos)
192 items.Add(backupInfo.ToXml());
195 backupSettingElement.Add(items);
196 return backupSettingElement;
199 private void OnBackupInfoSettingFilePropertyChanged(object sender, EventArgs e)
201 this.MarkAsModifiedAndPublishNotification();
204 private void MarkAsModifiedAndPublishNotification()
206 this.Modified = true;
207 if (this.SettingModified != null)
209 this.SettingModified(this, new EventArgs());