Implement import and export of Settings.
- NEU: Stile können importiert und exportiert werden.
This commit is contained in:
46
zaaReloaded2/ViewModels/IoErrorViewModel.cs
Executable file
46
zaaReloaded2/ViewModels/IoErrorViewModel.cs
Executable file
@ -0,0 +1,46 @@
|
||||
/* IoErrorViewModel.cs
|
||||
* part of zaaReloaded2
|
||||
*
|
||||
* Copyright 2015 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.ViewModels;
|
||||
|
||||
namespace zaaReloaded2.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple view model for I/O error messages, for use in conjunction
|
||||
/// with Bovender's ShowViewDialogAction and zaaReloaded2.Views.IoErrorView.
|
||||
/// </summary>
|
||||
class IoErrorViewModel : ViewModelBase
|
||||
{
|
||||
public Exception Exception { get; private set; }
|
||||
|
||||
public string Message { get { return Exception.Message; } }
|
||||
|
||||
public IoErrorViewModel(Exception e)
|
||||
{
|
||||
Exception = e;
|
||||
}
|
||||
|
||||
public override object RevealModelObject()
|
||||
{
|
||||
return Exception;
|
||||
}
|
||||
}
|
||||
}
|
@ -24,6 +24,8 @@ using Bovender.Mvvm.ViewModels;
|
||||
using Bovender.Mvvm.Messaging;
|
||||
using zaaReloaded2.Controller;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Runtime.Serialization.Formatters.Soap;
|
||||
using System.IO;
|
||||
|
||||
namespace zaaReloaded2.ViewModels
|
||||
{
|
||||
@ -134,6 +136,33 @@ namespace zaaReloaded2.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@ -186,6 +215,54 @@ namespace zaaReloaded2.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@ -334,6 +411,86 @@ namespace zaaReloaded2.ViewModels
|
||||
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)
|
||||
{
|
||||
Properties.Settings.Default.ImportExportPath = message.Value;
|
||||
Properties.Settings.Default.Save();
|
||||
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)
|
||||
{
|
||||
Properties.Settings.Default.ImportExportPath = message.Value;
|
||||
Properties.Settings.Default.Save();
|
||||
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 = Properties.Settings.Default.ImportExportPath;
|
||||
if (String.IsNullOrEmpty(path))
|
||||
{
|
||||
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||||
}
|
||||
else
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Implementation of ViewModelBase
|
||||
@ -354,10 +511,26 @@ namespace zaaReloaded2.ViewModels
|
||||
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 (*.zaaReloaded)|*.zaaReloaded";
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
Reference in New Issue
Block a user