572 lines
18 KiB
C#
Executable File
572 lines
18 KiB
C#
Executable File
/* 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<SettingsViewModel> SettingsList { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 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.
|
|
/// </remarks>
|
|
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<ViewModelMessageContent> EditSettingsMessage
|
|
{
|
|
get
|
|
{
|
|
if (_editSettingsMessage == null)
|
|
{
|
|
_editSettingsMessage = new Message<ViewModelMessageContent>();
|
|
}
|
|
return _editSettingsMessage;
|
|
}
|
|
}
|
|
|
|
public Message<ViewModelMessageContent> UseSettingsMessage
|
|
{
|
|
get
|
|
{
|
|
if (_useSettingsMessage == null)
|
|
{
|
|
_useSettingsMessage = new Message<ViewModelMessageContent>();
|
|
}
|
|
return _useSettingsMessage;
|
|
}
|
|
}
|
|
|
|
public Message<ViewModelMessageContent> ConfirmDeleteSettingsMessage
|
|
{
|
|
get
|
|
{
|
|
if (_confirmDeleteSettingsMessage == null)
|
|
{
|
|
_confirmDeleteSettingsMessage = new Message<ViewModelMessageContent>();
|
|
}
|
|
return _confirmDeleteSettingsMessage;
|
|
}
|
|
}
|
|
|
|
public Message<ViewModelMessageContent> ConfirmResetSettingsMessage
|
|
{
|
|
get
|
|
{
|
|
if (_confirmResetSettingsMessage == null)
|
|
{
|
|
_confirmResetSettingsMessage = new Message<ViewModelMessageContent>();
|
|
}
|
|
return _confirmResetSettingsMessage;
|
|
}
|
|
}
|
|
|
|
public Message<FileNameMessageContent> ChooseExportFileNameMessage
|
|
{
|
|
get
|
|
{
|
|
if (_chooseExportFileNameMessage == null)
|
|
{
|
|
_chooseExportFileNameMessage = new Message<FileNameMessageContent>();
|
|
}
|
|
return _chooseExportFileNameMessage;
|
|
}
|
|
}
|
|
|
|
public Message<FileNameMessageContent> ChooseImportFileNameMessage
|
|
{
|
|
get
|
|
{
|
|
if (_chooseImportFileNameMessage == null)
|
|
{
|
|
_chooseImportFileNameMessage = new Message<FileNameMessageContent>();
|
|
}
|
|
return _chooseImportFileNameMessage;
|
|
}
|
|
}
|
|
|
|
public Message<ViewModelMessageContent> ExportErrorMessage
|
|
{
|
|
get
|
|
{
|
|
if (_exportErrorMessage == null)
|
|
{
|
|
_exportErrorMessage = new Message<ViewModelMessageContent>();
|
|
}
|
|
return _exportErrorMessage;
|
|
}
|
|
}
|
|
|
|
public Message<ViewModelMessageContent> ImportErrorMessage
|
|
{
|
|
get
|
|
{
|
|
if (_importErrorMessage == null)
|
|
{
|
|
_importErrorMessage = new Message<ViewModelMessageContent>();
|
|
}
|
|
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<SettingsViewModel>();
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the selected SettingsViewModel belongs
|
|
/// to one of the default settings.
|
|
/// </summary>
|
|
bool IsDefaultSettings()
|
|
{
|
|
if (LastSelected != null)
|
|
{
|
|
return SettingsRepository.IsDefaultSettings(LastSelected.RevealModelObject() as Settings);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a settings view model to the collection and wires the
|
|
/// PropertyChanged event.
|
|
/// </summary>
|
|
/// <param name="settingsViewModel">SettingsViewModel to add.</param>
|
|
void AddSettingsViewModel(SettingsViewModel settingsViewModel)
|
|
{
|
|
settingsViewModel.PropertyChanged += SettingsViewModel_PropertyChanged;
|
|
SettingsList.Add(settingsViewModel);
|
|
}
|
|
|
|
void DoExportSettings()
|
|
{
|
|
if (CanExportSettings())
|
|
{
|
|
ChooseExportFileNameMessage.Send(
|
|
new FileNameMessageContent(SuggestExportPath(), FILE_FILTER),
|
|
msg => ConfirmExportSettings(msg));
|
|
}
|
|
}
|
|
|
|
bool CanExportSettings()
|
|
{
|
|
return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings();
|
|
}
|
|
|
|
void ConfirmExportSettings(FileNameMessageContent message)
|
|
{
|
|
if (message.Confirmed)
|
|
{
|
|
SaveImportExportDir(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(SuggestImportPath(), FILE_FILTER),
|
|
msg => ConfirmImportSettings(msg));
|
|
}
|
|
|
|
bool CanImportSettings()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void ConfirmImportSettings(FileNameMessageContent message)
|
|
{
|
|
if (message.Confirmed)
|
|
{
|
|
SaveImportExportDir(message.Value);
|
|
try
|
|
{
|
|
Settings settings = Settings.LoadFromFile(message.Value);
|
|
// Attempt to find a unique name by adding a suffix, if necessary
|
|
string suffixedName = settings.Name;
|
|
while (_repository.SettingsList.FirstOrDefault(s => s.Name == suffixedName) != null)
|
|
{
|
|
string suffix = System.DateTime.Now.ToString();
|
|
suffixedName = String.Format("{0} ({1})", settings.Name, suffix);
|
|
}
|
|
settings.Name = suffixedName;
|
|
_repository.SettingsList.Add(settings);
|
|
AddSettingsViewModel(new SettingsViewModel(settings));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ImportErrorMessage.Send(
|
|
new ViewModelMessageContent(new IoErrorViewModel(e))
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
string SuggestImportPath()
|
|
{
|
|
string path = Bovender.PathHelpers.GetDirectoryPart(UserSettings.Default.ImportExportPath);
|
|
if (String.IsNullOrEmpty(path))
|
|
{
|
|
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
|
}
|
|
else
|
|
{
|
|
return path;
|
|
}
|
|
}
|
|
|
|
string SuggestExportPath()
|
|
{
|
|
Settings settings = LastSelected.RevealModelObject() as Settings;
|
|
string fileName = settings.Name;
|
|
return System.IO.Path.Combine(SuggestImportPath(), fileName);
|
|
}
|
|
|
|
void SaveImportExportDir(string path)
|
|
{
|
|
UserSettings.Default.ImportExportPath = Bovender.PathHelpers.GetDirectoryPart(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<ViewModelMessageContent> _confirmDeleteSettingsMessage;
|
|
Message<ViewModelMessageContent> _confirmResetSettingsMessage;
|
|
Message<ViewModelMessageContent> _editSettingsMessage;
|
|
Message<ViewModelMessageContent> _useSettingsMessage;
|
|
Message<FileNameMessageContent> _chooseExportFileNameMessage;
|
|
Message<FileNameMessageContent> _chooseImportFileNameMessage;
|
|
Message<ViewModelMessageContent> _exportErrorMessage;
|
|
Message<ViewModelMessageContent> _importErrorMessage;
|
|
|
|
#endregion
|
|
|
|
#region Constant
|
|
|
|
/// <summary>
|
|
/// File filter that is used for the import and export
|
|
/// of settings.
|
|
/// </summary>
|
|
const string FILE_FILTER = "zaaReloaded-Stildatei (*.zaaReloaded2)|*.zaaReloaded2";
|
|
|
|
#endregion
|
|
}
|
|
}
|