OSDN Git Service

Ver.1.0.0.185 Task187 Interface based design
[simplebackup/SBTGitRepository.git] / Main / Models / BackupSetting.cs
1 //-----------------------------------------------------------------------
2 // <copyright file="BackupSetting.cs" company="Takayoshi Matsuyama">
3 //     Copyright (c) Takayoshi Matsuyama. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
6
7 // This file is part of Simple Backup.
8 //
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.
13 //
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.
18 //
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/>.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Xml.Linq;
26 using SimpleBackup.Core.Models;
27 using SimpleBackup.Resources;
28
29 namespace SimpleBackup.Models
30 {
31     /// <summary>
32     /// Represents a backup setting.
33     /// </summary>
34     public class BackupSetting : IBackupSetting
35     {
36         private readonly List<BackupInfo> backupInfoList = new List<BackupInfo>();
37
38         /// <summary>
39         /// Initializes a new instance of the <see cref="BackupSetting"/> class.
40         /// </summary>
41         public BackupSetting()
42         {
43             this.SettingFileName = string.Empty;
44         }
45
46         public BackupSetting(
47             string settingFileName,
48             IEnumerable<BackupInfo> backupInfoCollection)
49         {
50             this.SettingFileName = settingFileName;
51             this.backupInfoList.AddRange(backupInfoCollection);
52             foreach (BackupInfo backupInfo in this.backupInfoList)
53             {
54                 backupInfo.SettingFilePropertyChanged += this.OnBackupInfoSettingFilePropertyChanged;
55             }
56         }
57
58         /// <summary>
59         /// Occurs when backup setting is modified.
60         /// </summary>
61         public event EventHandler SettingModified;
62
63         /// <summary>
64         /// Gets the name of the setting.
65         /// </summary>
66         public string SettingName
67         {
68             get
69             {
70                 if (string.IsNullOrEmpty(this.SettingFileName))
71                 {
72                     return Resources.Resources.Views_NewBackupSettingName;
73                 }
74
75                 return this.SettingFileName;
76             }
77         }
78
79         /// <summary>
80         /// Gets or sets the name of the setting file.
81         /// </summary>
82         public string SettingFileName { get; set; }
83
84         /// <summary>
85         /// Gets or sets a value indicating whether this <see cref="BackupSetting"/> is modified.
86         /// </summary>
87         public bool Modified { get; set; }
88
89         public IReadOnlyCollection<IBackupInfo> BackupInfos
90         {
91             get
92             {
93                 return this.backupInfoList.AsReadOnly();
94             }
95         }
96
97         public int Count
98         {
99             get
100             {
101                 return this.backupInfoList.Count;
102             }
103         }
104
105         /// <summary>
106         /// Inserts the specified index.
107         /// </summary>
108         /// <param name="index">The index.</param>
109         /// <param name="backupInfo">The backup information.</param>
110         public void Insert(int index, IBackupInfo backupInfo)
111         {
112             backupInfo.SettingFilePropertyChanged += this.OnBackupInfoSettingFilePropertyChanged;
113             this.backupInfoList.Insert(index, (BackupInfo)backupInfo);
114
115             this.MarkAsModifiedAndPublishNotification();
116         }
117
118         /// <summary>
119         /// Removes the specified removing backup information.
120         /// </summary>
121         /// <param name="removingBackupInfo">The removing backup information.</param>
122         public void Remove(IBackupInfo removingBackupInfo)
123         {
124             removingBackupInfo.SettingFilePropertyChanged -= this.OnBackupInfoSettingFilePropertyChanged;
125             this.backupInfoList.Remove((BackupInfo)removingBackupInfo);
126
127             this.MarkAsModifiedAndPublishNotification();
128         }
129
130         public IBackupInfo FirstOrDefault(Guid id)
131         {
132             return this.backupInfoList.FirstOrDefault(p => p.Id == id);
133         }
134
135         public void MoveItem(Guid backupInfoId, ItemMovingDirection movingDirection)
136         {
137             IBackupInfo movingBackupInfo = null;
138             int currentIndex = 0;
139             for (int i = 0; i < this.backupInfoList.Count; i++)
140             {
141                 if (this.backupInfoList[i].Id == backupInfoId)
142                 {
143                     currentIndex = i;
144                     movingBackupInfo = this.backupInfoList[i];
145                     break;
146                 }
147             }
148
149             if (movingBackupInfo != null)
150             {
151                 int newIndex = -1;
152                 switch (movingDirection)
153                 {
154                     case ItemMovingDirection.MoveUp:
155                         if (currentIndex > 0)
156                         {
157                             newIndex = currentIndex - 1;
158                         }
159
160                         break;
161                     case ItemMovingDirection.MoveDown:
162                         if (currentIndex < this.backupInfoList.Count - 1)
163                         {
164                             newIndex = currentIndex + 1;
165                         }
166
167                         break;
168                     default:
169                         throw new InvalidOperationException("Not supported direction.");
170                 }
171
172                 if (newIndex != -1)
173                 {
174                     IBackupInfo temp = this.backupInfoList[newIndex];
175                     this.backupInfoList[newIndex] = this.backupInfoList[currentIndex];
176                     this.backupInfoList[currentIndex] = (BackupInfo)temp;
177                 }
178
179                 this.MarkAsModifiedAndPublishNotification();
180             }
181         }
182
183         public XElement ToXml()
184         {
185             var backupSettingElement = new XElement("BackupSetting");
186             backupSettingElement.SetAttributeValue("Version", "1.0");
187             backupSettingElement.SetAttributeValue("RequiredToolVersion", "1.0.0.181");
188
189             var items = new XElement("Items");
190             foreach (IBackupInfo backupInfo in this.BackupInfos)
191             {
192                 items.Add(backupInfo.ToXml());
193             }
194
195             backupSettingElement.Add(items);
196             return backupSettingElement;
197         }
198
199         private void OnBackupInfoSettingFilePropertyChanged(object sender, EventArgs e)
200         {
201             this.MarkAsModifiedAndPublishNotification();
202         }
203
204         private void MarkAsModifiedAndPublishNotification()
205         {
206             this.Modified = true;
207             if (this.SettingModified != null)
208             {
209                 this.SettingModified(this, new EventArgs());
210             }
211         }
212     }
213 }