/* SettingsRepositoryViewModel.cs * part of zaaReloaded2 * * Copyright 2015-2017 Daniel Kraus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Bovender.Mvvm; using Bovender.Mvvm.ViewModels; using Bovender.Mvvm.Messaging; using zaaReloaded2.Controller; using System.Collections.ObjectModel; using System.Runtime.Serialization.Formatters.Soap; using System.IO; using Microsoft.Office.Interop.Word; namespace zaaReloaded2.ViewModels { public class SettingsRepositoryViewModel : ViewModelBase { #region Properties public ObservableCollection SettingsList { get; private set; } /// /// Gets the SettingsViewModel that was most recently selected. Whether /// this view model is still selected can be found out be getting the /// view model's IsSelected property. /// /// /// Due to the way the WPF ListBox (for example) is implemented, selecting /// a list item will trigger an PropertyChanged event twice: Once for the /// item being selected, and once for the item being deselected. Thus we /// can only capture the last item that actually was selected. We cannot /// howeve capture if an item was deselected without a new selection, /// because we cannot logicaly connect two occurrences of the same event /// from different objects. /// public SettingsViewModel LastSelected { get; private set; } #endregion #region Commands public DelegatingCommand AddSettingsCommand { get { if (_addSettingsCommand == null) { _addSettingsCommand = new DelegatingCommand( param => DoAddSettings()); } return _addSettingsCommand; } } public DelegatingCommand EditSettingsCommand { get { if (_editSettingsCommand == null) { _editSettingsCommand = new DelegatingCommand( param => DoEditSettings(), param => CanEditSettings()); } return _editSettingsCommand; } } public DelegatingCommand UseSettingsCommand { get { if (_useSettingsCommand == null) { _useSettingsCommand = new DelegatingCommand( param => DoUseSettings(), param => CanUseSettings()); } return _useSettingsCommand; } } public DelegatingCommand DeleteSettingsCommand { get { if (_deleteSettingsCommand == null) { _deleteSettingsCommand = new DelegatingCommand( param => DoDeleteSettings(), param => CanDeleteSettings()); } return _deleteSettingsCommand; } } public DelegatingCommand CopySettingsCommand { get { if (_copySettingsCommand == null) { _copySettingsCommand = new DelegatingCommand( param => DoCopySettings()); } return _copySettingsCommand; } } public DelegatingCommand ResetSettingsCommand { get { if (_resetSettingsCommand == null) { _resetSettingsCommand = new DelegatingCommand( param => DoResetSettings()); } return _resetSettingsCommand; } } public DelegatingCommand ExportSettingsCommand { get { if (_exportSettingsCommand == null) { _exportSettingsCommand = new DelegatingCommand( param => DoExportSettings(), param => CanExportSettings()); } return _exportSettingsCommand; } } public DelegatingCommand ImportSettingsCommand { get { if (_importSettingsCommand == null) { _importSettingsCommand = new DelegatingCommand( param => DoImportSettings(), param => CanImportSettings()); } return _importSettingsCommand; } } #endregion #region Messages public Message EditSettingsMessage { get { if (_editSettingsMessage == null) { _editSettingsMessage = new Message(); } return _editSettingsMessage; } } public Message UseSettingsMessage { get { if (_useSettingsMessage == null) { _useSettingsMessage = new Message(); } return _useSettingsMessage; } } public Message ConfirmDeleteSettingsMessage { get { if (_confirmDeleteSettingsMessage == null) { _confirmDeleteSettingsMessage = new Message(); } return _confirmDeleteSettingsMessage; } } public Message ConfirmResetSettingsMessage { get { if (_confirmResetSettingsMessage == null) { _confirmResetSettingsMessage = new Message(); } return _confirmResetSettingsMessage; } } public Message ChooseExportFileNameMessage { get { if (_chooseExportFileNameMessage == null) { _chooseExportFileNameMessage = new Message(); } return _chooseExportFileNameMessage; } } public Message ChooseImportFileNameMessage { get { if (_chooseImportFileNameMessage == null) { _chooseImportFileNameMessage = new Message(); } return _chooseImportFileNameMessage; } } public Message ExportErrorMessage { get { if (_exportErrorMessage == null) { _exportErrorMessage = new Message(); } return _exportErrorMessage; } } public Message ImportErrorMessage { get { if (_importErrorMessage == null) { _importErrorMessage = new Message(); } return _importErrorMessage; } } #endregion #region Constructors public SettingsRepositoryViewModel(SettingsRepository repository) { _repository = repository; BuildSettingsList(); if (SettingsList.Count > 0) { SettingsList.First().IsSelected = true; } RequestCloseView += (sender, args) => { _repository.Store(); }; } #endregion #region Private methods void BuildSettingsList() { SettingsList = new ObservableCollection(); foreach (Settings s in _repository.SettingsList) { SettingsViewModel vm = new SettingsViewModel(s); AddSettingsViewModel(vm); } OnPropertyChanged("SettingsList"); } void DoEditSettings() { if (CanEditSettings()) { EditSettingsMessage.Send(new ViewModelMessageContent(LastSelected)); } } bool CanEditSettings() { return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings(); } void DoUseSettings() { UseSettingsMessage.Send(new ViewModelMessageContent(LastSelected)); CloseViewCommand.Execute(null); } bool CanUseSettings() { return Commands.CanFormat(); } void DoAddSettings() { Settings s = new Settings("Neu"); SettingsViewModel vm = new SettingsViewModel(s); _repository.SettingsList.Add(s); AddSettingsViewModel(vm); vm.IsSelected = true; EditSettingsMessage.Send(new ViewModelMessageContent(vm)); } bool CanDeleteSettings() { return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings(); } void DoDeleteSettings() { if (CanDeleteSettings()) { ConfirmDeleteSettingsMessage.Send( new ViewModelMessageContent(LastSelected), param => ConfirmDeleteSettings(param)); } } void ConfirmDeleteSettings(ViewModelMessageContent content) { SettingsViewModel vm = content.ViewModel as SettingsViewModel; if (CanDeleteSettings() && content.Confirmed) { _repository.SettingsList.Remove(vm.RevealModelObject() as Settings); SettingsList.Remove(vm); } } void DoResetSettings() { ConfirmResetSettingsMessage.Send( new ViewModelMessageContent(this), param => ConfirmResetSettings(param)); } void ConfirmResetSettings(ViewModelMessageContent content) { if (content.Confirmed) { _repository = new SettingsRepository(); BuildSettingsList(); } } void DoCopySettings() { if (LastSelected != null) { SettingsViewModel copy = LastSelected.Clone() as SettingsViewModel; if (IsDefaultSettings()) { copy.Name = copy.Name.Replace(SettingsRepository.BUILTIN_LABEL, String.Empty); } _repository.SettingsList.Add(copy.RevealModelObject() as Settings); AddSettingsViewModel(copy); LastSelected.IsSelected = false; copy.IsSelected = true; EditSettingsMessage.Send(new ViewModelMessageContent(copy)); } } void SettingsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { SettingsViewModel vm = sender as SettingsViewModel; if (vm != null && e.PropertyName == "IsSelected") { if (vm.IsSelected) LastSelected = vm; } } /// /// Determines whether the selected SettingsViewModel belongs /// to one of the default settings. /// bool IsDefaultSettings() { if (LastSelected != null) { return SettingsRepository.IsDefaultSettings(LastSelected.RevealModelObject() as Settings); } else { return false; } } /// /// Adds a settings view model to the collection and wires the /// PropertyChanged event. /// /// SettingsViewModel to add. void AddSettingsViewModel(SettingsViewModel settingsViewModel) { settingsViewModel.PropertyChanged += SettingsViewModel_PropertyChanged; SettingsList.Add(settingsViewModel); } void DoExportSettings() { if (CanExportSettings()) { ChooseExportFileNameMessage.Send( new FileNameMessageContent(SuggestImportExportPath(), FILE_FILTER), msg => ConfirmExportSettings(msg)); } } bool CanExportSettings() { return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings(); } void ConfirmExportSettings(FileNameMessageContent message) { if (message.Confirmed) { UserSettings.Default.ImportExportPath = message.Value; Settings settings = LastSelected.RevealModelObject() as Settings; try { settings.SaveToFile(message.Value); } catch (Exception e) { ExportErrorMessage.Send( new ViewModelMessageContent(new IoErrorViewModel(e)) ); } } } void DoImportSettings() { ChooseImportFileNameMessage.Send( new FileNameMessageContent(SuggestImportExportPath(), FILE_FILTER), msg => ConfirmImportSettings(msg)); } bool CanImportSettings() { return true; } void ConfirmImportSettings(FileNameMessageContent message) { if (message.Confirmed) { UserSettings.Default.ImportExportPath = message.Value; try { Settings settings = Settings.LoadFromFile(message.Value); AddSettingsViewModel(new SettingsViewModel(settings)); } catch (Exception e) { ImportErrorMessage.Send( new ViewModelMessageContent(new IoErrorViewModel(e)) ); } } } string SuggestImportExportPath() { string path = UserSettings.Default.ImportExportPath; if (String.IsNullOrEmpty(path)) { return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } else { return path; } } #endregion #region Implementation of ViewModelBase public override object RevealModelObject() { return _repository; } #endregion #region Fields SettingsRepository _repository; DelegatingCommand _useSettingsCommand; DelegatingCommand _addSettingsCommand; DelegatingCommand _editSettingsCommand; DelegatingCommand _deleteSettingsCommand; DelegatingCommand _resetSettingsCommand; DelegatingCommand _copySettingsCommand; DelegatingCommand _exportSettingsCommand; DelegatingCommand _importSettingsCommand; Message _confirmDeleteSettingsMessage; Message _confirmResetSettingsMessage; Message _editSettingsMessage; Message _useSettingsMessage; Message _chooseExportFileNameMessage; Message _chooseImportFileNameMessage; Message _exportErrorMessage; Message _importErrorMessage; #endregion #region Constant /// /// File filter that is used for the import and export /// of settings. /// const string FILE_FILTER = "zaaReloaded-Stildatei (*.zaaReloaded2)|*.zaaReloaded2"; #endregion } }