1 //-----------------------------------------------------------------------
2 // <copyright file="BackupSettingService.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/>.
26 using System.Windows.Input;
27 using System.Xml.Linq;
28 using SimpleBackup.Core.Models;
29 using SimpleBackup.Core.Services;
30 using SimpleBackup.Core.Utilities;
31 using SimpleBackup.Models;
33 namespace SimpleBackup.Services
35 using SimpleBackup.Resources;
37 using Application = System.Windows.Application;
38 using Cursors = System.Windows.Input.Cursors;
39 using MessageBox = System.Windows.MessageBox;
40 using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
41 using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
44 /// Backup Setting service.
46 public class BackupSettingService : IBackupSettingService
49 /// The supported major version
51 private const int SupportedMajorVersion = 1;
54 /// The supported minor version
56 private const int SupportedMinorVersion = 0;
59 /// The synchronize root
61 private static readonly object SyncRoot = new object();
64 /// The instance of SettingFileService.
66 private static volatile BackupSettingService instance;
69 /// Prevents a default instance of the <see cref="BackupSettingService"/> class from being created.
71 private BackupSettingService()
76 /// Occurs when backup setting is created.
78 public event EventHandler<SettingFileEventArgs> BackupSettingCreated;
81 /// Occurs when backup setting is opened.
83 public event EventHandler<SettingFileEventArgs> BackupSettingOpened;
86 /// Occurs when backup setting is modified.
88 public event EventHandler<SettingFileEventArgs> BackupSettingModified;
91 /// Occurs when backup setting is saved.
93 public event EventHandler<SettingFileEventArgs> BackupSettingSaved;
96 /// Occurs when backup setting is closed.
98 public event EventHandler<SettingFileEventArgs> BackupSettingClosed;
101 /// Gets the instance.
103 public static IBackupSettingService Instance
107 if (instance == null)
111 if (instance == null)
113 instance = new BackupSettingService();
123 /// Gets the current setting.
125 public IBackupSetting CurrentSetting { get; private set; }
128 /// Creates the backup setting.
130 /// <returns><c>true</c> if the new backup setting is created; otherwise, <c>false</c>.</returns>
131 public bool CreateBackupSetting()
133 if ((this.CurrentSetting != null) && this.CurrentSetting.Modified)
135 MessageBoxResult confirmationResult = MessageBox.Show(
136 Application.Current.MainWindow,
137 Resources.Views_SaveConfirmationMessage,
138 Resources.Views_ConfirmationDialogTitle,
139 MessageBoxButton.YesNoCancel);
140 if (confirmationResult == MessageBoxResult.Yes)
142 this.SaveCurrentBackupSetting();
144 else if (confirmationResult == MessageBoxResult.Cancel)
150 if (this.CurrentSetting != null)
152 this.CloseCurrentBackupSetting();
155 var backupSetting = new BackupSetting { Modified = true };
156 this.CurrentSetting = backupSetting;
157 this.CurrentSetting.SettingModified += this.OnCurrentSettingModified;
159 if (this.BackupSettingCreated != null)
161 this.BackupSettingCreated(this, new SettingFileEventArgs(this.CurrentSetting.SettingName));
168 /// Opens the backup setting.
170 /// <param name="resultMessage">The result message.</param>
172 /// <c>true</c> if backup setting is successfully loaded; otherwise, <c>false</c>.
174 public bool OpenBackupSetting(out string resultMessage)
176 if ((this.CurrentSetting != null) && this.CurrentSetting.Modified)
178 MessageBoxResult confirmationResult = MessageBox.Show(
179 Application.Current.MainWindow,
180 Resources.Views_SaveConfirmationMessage,
181 Resources.Views_ConfirmationDialogTitle,
182 MessageBoxButton.YesNoCancel);
183 if (confirmationResult == MessageBoxResult.Yes)
185 this.SaveCurrentBackupSetting();
187 else if (confirmationResult == MessageBoxResult.Cancel)
189 resultMessage = string.Empty;
194 string initialDirectory = AppDomain.CurrentDomain.BaseDirectory;
195 if ((this.CurrentSetting != null) &&
196 this.CurrentSetting.SettingFileName.IsValidFilePath())
198 initialDirectory = Path.GetDirectoryName(this.CurrentSetting.SettingFileName);
199 this.CloseBackupSetting();
202 var openFileDialog = new OpenFileDialog
204 Title = Resources.Views_OpenFileDialogTitle,
206 Filter = "Backup Setting File|*.xml",
207 InitialDirectory = initialDirectory,
211 if (openFileDialog.ShowDialog(Application.Current.MainWindow) != true)
213 resultMessage = string.Empty;
217 var fileName = openFileDialog.FileName;
220 Mouse.OverrideCursor = Cursors.Wait;
221 return this.OpenBackupSetting(fileName, out resultMessage);
225 Mouse.OverrideCursor = null;
230 /// Closes the backup setting.
232 public void CloseBackupSetting()
234 if (this.CurrentSetting == null)
239 if (this.CurrentSetting.Modified)
241 MessageBoxResult confirmationResult = MessageBox.Show(
242 Application.Current.MainWindow,
243 Resources.Views_SaveConfirmationMessage,
244 Resources.Views_ConfirmationDialogTitle,
245 MessageBoxButton.YesNoCancel);
246 if (confirmationResult == MessageBoxResult.Yes)
248 this.SaveCurrentBackupSetting();
250 else if (confirmationResult == MessageBoxResult.Cancel)
256 this.CloseCurrentBackupSetting();
260 /// Overwrites the currnt backup setting.
262 /// <returns><c>true</c> if the backup setting is saved; otherwise, <c>false</c>.</returns>
263 public bool OverWriteCurrntBackupSetting()
265 if (this.CurrentSetting == null)
270 if (string.IsNullOrEmpty(this.CurrentSetting.SettingFileName) ||
271 (File.Exists(this.CurrentSetting.SettingFileName) == false))
273 var saveFileDialog = new SaveFileDialog
275 Title = Resources.Views_SaveFileDialogTitle,
277 Filter = "Backup Setting File|*.xml"
280 if (saveFileDialog.ShowDialog(Application.Current.MainWindow) != true)
285 this.CurrentSetting.SettingFileName = saveFileDialog.FileName;
288 this.SaveCurrentBackupSetting();
293 /// Saves the current backup setting as a new file.
295 /// <returns><c>true</c> if the backup setting is saved; otherwise, <c>false</c>.</returns>
296 public bool SaveCurrentBackupSettingAsNewFile()
298 if (this.CurrentSetting == null)
303 var saveFileDialog = new SaveFileDialog
305 Title = Resources.Views_SaveFileDialogTitle,
307 Filter = "Backup Setting File|*.xml"
310 if (saveFileDialog.ShowDialog(Application.Current.MainWindow) != true)
315 this.CurrentSetting.SettingFileName = saveFileDialog.FileName;
317 this.SaveCurrentBackupSetting();
322 /// Opens the default backup setting.
324 /// <param name="resultMessage">The result message.</param>
326 /// <c>true</c> if succeeded, otherwise; <c>false</c>.
328 public bool OpenDefaultBackupSetting(out string resultMessage)
330 return this.OpenBackupSetting(
331 Properties.Settings.Default.DefaultSettingFileName,
336 /// Creates the backup information.
338 /// <param name="index">The index.</param>
339 /// <returns>The created backup information.</returns>
340 /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/></exception>
341 public IBackupInfo CreateBackupInfo(int index)
345 throw new ArgumentOutOfRangeException("index");
348 var backupInfo = new BackupInfo();
349 this.CurrentSetting.Insert(index, backupInfo);
354 /// Copies the backup information.
356 /// <param name="sourceBackupInfo">The source backup information.</param>
357 /// <returns>A copied BackupInfo.</returns>
358 /// <exception cref="System.ArgumentNullException"><paramref name="sourceBackupInfo"/></exception>
359 public IBackupInfo CopyBackupInfo(IBackupInfo sourceBackupInfo)
361 sourceBackupInfo.ThrowsArgumentNullException("sourceBackupInfo");
363 var backupInfo = new BackupInfo(sourceBackupInfo);
368 /// Inserts the backup information.
370 /// <param name="index">The index.</param>
371 /// <param name="backupInfo">The backup information.</param>
372 /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index"/></exception>
373 /// <exception cref="System.ArgumentNullException"><paramref name="backupInfo"/></exception>
374 public void InsertBackupInfo(int index, IBackupInfo backupInfo)
376 if (index > this.CurrentSetting.Count)
378 throw new ArgumentOutOfRangeException("index");
381 backupInfo.ThrowsArgumentNullException("backupInfo");
383 this.CurrentSetting.Insert(index, backupInfo);
387 /// Removes the backup information.
389 /// <param name="backupInfoId">The backup information identifier.</param>
390 public void RemoveBackupInfo(Guid backupInfoId)
392 IBackupInfo removingBackupInfo =
393 this.CurrentSetting.FirstOrDefault(backupInfoId);
394 if (removingBackupInfo == null)
399 this.CurrentSetting.Remove(removingBackupInfo);
403 /// Saves the backup setting.
405 /// <param name="backupSetting">The backup setting.</param>
406 private static void SaveBackupSetting(IBackupSetting backupSetting)
408 var xdoc = new XDocument { Declaration = new XDeclaration("1.0", "utf-8", "yes") };
409 xdoc.Add(backupSetting.ToXml());
411 xdoc.Save(backupSetting.SettingFileName, SaveOptions.None);
415 /// Opens the backup setting.
417 /// <param name="fileName">Name of the file.</param>
418 /// <param name="resultMessage">The result message.</param>
420 /// <c>true</c> if succeeded; otherwise, <c>false</c>.
422 private bool OpenBackupSetting(string fileName, out string resultMessage)
424 resultMessage = string.Empty;
426 if (string.IsNullOrEmpty(fileName))
428 resultMessage = string.Format(
429 Resources.BackupSetting_FailedToOpenSetting_EmptyFileNameFormat,
434 XDocument xml = XDocument.Load(fileName, LoadOptions.None);
435 XElement backupSettingElement = xml.Elements("BackupSetting").FirstOrDefault();
436 if (backupSettingElement == null)
438 resultMessage = string.Format(
439 Resources.BackupSetting_FailedToOpenSetting_NoBackupSettingElementFormat,
444 XAttribute versionAttribute = backupSettingElement.Attributes().FirstOrDefault(p => p.Name == "Version");
445 if (versionAttribute == null)
447 resultMessage = string.Format(
448 Resources.BackupSetting_FailedToOpenSetting_NoVersionElementFormat,
453 string[] versionTextFlagments = versionAttribute.Value.Split('.');
454 if (versionTextFlagments.Length != 2)
456 resultMessage = string.Format(
457 Resources.BackupSetting_FailedToOpenSetting_InvalidVersionFormat,
463 if (int.TryParse(versionTextFlagments[0], out majorVersion) == false)
465 resultMessage = string.Format(
466 Resources.BackupSetting_FailedToOpenSetting_InvalidMajorVersionFormat,
472 if (int.TryParse(versionTextFlagments[1], out minorVersion) == false)
474 resultMessage = string.Format(
475 Resources.BackupSetting_FailedToOpenSetting_InvalidMinorVersionFormat,
480 XAttribute requiredToolVersionAttribute = backupSettingElement.Attributes().FirstOrDefault(p => p.Name == "RequiredToolVersion");
481 if (requiredToolVersionAttribute == null)
483 resultMessage = string.Format(
484 Resources.BackupSetting_FailedToOpenSetting_InvalidRequiredToolVersionFormat,
489 if (majorVersion > SupportedMajorVersion)
491 string message = string.Format(
492 Resources.BackupSetting_IncompatibleMajorVersionFormat,
493 requiredToolVersionAttribute.Value);
495 Application.Current.MainWindow,
497 Resources.Views_DefaultWindowTitle,
499 MessageBoxImage.Error);
500 resultMessage = message;
504 if (minorVersion > SupportedMinorVersion)
506 string message = string.Format(
507 Resources.BackupSetting_IncompatibleMinorVersionFormat,
508 requiredToolVersionAttribute.Value);
510 Application.Current.MainWindow,
512 Resources.Views_DefaultWindowTitle,
514 MessageBoxImage.Warning);
515 resultMessage = message + "\r\n";
518 BackupSetting backupSetting = ModelHelper.RestoreBackupSettingFromXml(backupSettingElement, fileName);
519 this.CurrentSetting = backupSetting;
520 this.CurrentSetting.SettingModified += this.OnCurrentSettingModified;
521 if (this.BackupSettingOpened != null)
523 this.BackupSettingOpened(this, new SettingFileEventArgs(backupSetting.SettingFileName));
526 resultMessage += string.Format(Resources.BackupSetting_SettingOpenedFormat, fileName);
531 /// Saves the current backup setting.
533 private void SaveCurrentBackupSetting()
537 Mouse.OverrideCursor = Cursors.Wait;
539 SaveBackupSetting(this.CurrentSetting);
541 this.CurrentSetting.Modified = false;
545 Mouse.OverrideCursor = null;
548 if (this.BackupSettingSaved != null)
550 this.BackupSettingSaved(this, new SettingFileEventArgs(this.CurrentSetting.SettingFileName));
554 private void CloseCurrentBackupSetting()
556 UndoRedoService.Instance.ClearUndoRedoBuffer();
557 string settingName = this.CurrentSetting.SettingFileName;
558 this.CurrentSetting.SettingModified -= this.OnCurrentSettingModified;
559 this.CurrentSetting = null;
561 if (this.BackupSettingClosed != null)
563 this.BackupSettingClosed(this, new SettingFileEventArgs(settingName));
568 /// Called when current setting is modified.
570 /// <param name="sender">The sender.</param>
571 /// <param name="eventArgs">The <see cref="EventArgs"/> instance containing the event data.</param>
572 private void OnCurrentSettingModified(object sender, EventArgs eventArgs)
574 if (this.BackupSettingModified != null)
576 this.BackupSettingModified(this, new SettingFileEventArgs(this.CurrentSetting.SettingName));