Fix importing of styles.

- Repariert: Stile konnten nicht importiert werden.
This commit is contained in:
daniel 2017-09-22 17:03:32 +02:00
parent 4c3198afce
commit 253a7f24f5
2 changed files with 33 additions and 9 deletions

View File

@ -66,7 +66,9 @@ namespace zaaReloaded2.Controller
{ {
throw new InvalidDataException("Datei enthält keine Stil-Daten oder ist beschädigt."); throw new InvalidDataException("Datei enthält keine Stil-Daten oder ist beschädigt.");
} }
Logger.Info("LoadFromFile: Successfully loaded '{0}", settings.Name);
settings.Uid = Guid.NewGuid(); settings.Uid = Guid.NewGuid();
Logger.Debug("LoadFromFile: new GUID is '{0}", settings.Uid);
return settings; return settings;
} }
@ -76,14 +78,15 @@ namespace zaaReloaded2.Controller
/// <param name="fileName">File to write to.</param> /// <param name="fileName">File to write to.</param>
public void SaveToFile(string fileName) public void SaveToFile(string fileName)
{ {
Logger.Info("SaveToFile: {0}", fileName); Logger.Info("SaveToFile: Settings name: {0}", Name);
using (StreamWriter writer = new StreamWriter(fileName)) using (StreamWriter writer = new StreamWriter(fileName))
{ {
Serializer serializer = new SerializerBuilder() Serializer serializer = new SerializerBuilder()
//.EnsureRoundtrip() .EnsureRoundtrip()
.Build(); .Build();
serializer.Serialize(writer, this); serializer.Serialize(writer, this);
} }
Logger.Info("SaveToFile: Successfully saved to file '{0}'", fileName);
// SoapFormatter formatter = new SoapFormatter(); // SoapFormatter formatter = new SoapFormatter();
// formatter.Serialize(writer.BaseStream, this); // formatter.Serialize(writer.BaseStream, this);
} }

View File

@ -431,7 +431,7 @@ namespace zaaReloaded2.ViewModels
if (CanExportSettings()) if (CanExportSettings())
{ {
ChooseExportFileNameMessage.Send( ChooseExportFileNameMessage.Send(
new FileNameMessageContent(SuggestImportExportPath(), FILE_FILTER), new FileNameMessageContent(SuggestExportPath(), FILE_FILTER),
msg => ConfirmExportSettings(msg)); msg => ConfirmExportSettings(msg));
} }
} }
@ -445,11 +445,11 @@ namespace zaaReloaded2.ViewModels
{ {
if (message.Confirmed) if (message.Confirmed)
{ {
UserSettings.Default.ImportExportPath = message.Value; SaveImportExportDir(message.Value);
Settings settings = LastSelected.RevealModelObject() as Settings; Settings settings = LastSelected.RevealModelObject() as Settings;
try try
{ {
settings.SaveToFile(message.Value); settings.SaveToFile(message.Value);
} }
catch (Exception e) catch (Exception e)
{ {
@ -463,7 +463,7 @@ namespace zaaReloaded2.ViewModels
void DoImportSettings() void DoImportSettings()
{ {
ChooseImportFileNameMessage.Send( ChooseImportFileNameMessage.Send(
new FileNameMessageContent(SuggestImportExportPath(), FILE_FILTER), new FileNameMessageContent(SuggestImportPath(), FILE_FILTER),
msg => ConfirmImportSettings(msg)); msg => ConfirmImportSettings(msg));
} }
@ -476,10 +476,19 @@ namespace zaaReloaded2.ViewModels
{ {
if (message.Confirmed) if (message.Confirmed)
{ {
UserSettings.Default.ImportExportPath = message.Value; SaveImportExportDir(message.Value);
try try
{ {
Settings settings = Settings.LoadFromFile(message.Value); 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)); AddSettingsViewModel(new SettingsViewModel(settings));
} }
catch (Exception e) catch (Exception e)
@ -491,9 +500,9 @@ namespace zaaReloaded2.ViewModels
} }
} }
string SuggestImportExportPath() string SuggestImportPath()
{ {
string path = UserSettings.Default.ImportExportPath; string path = Bovender.PathHelpers.GetDirectoryPart(UserSettings.Default.ImportExportPath);
if (String.IsNullOrEmpty(path)) if (String.IsNullOrEmpty(path))
{ {
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
@ -504,6 +513,18 @@ namespace zaaReloaded2.ViewModels
} }
} }
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 #endregion
#region Implementation of ViewModelBase #region Implementation of ViewModelBase