diff --git a/Artemis/Artemis/DAL/ProfileProvider.cs b/Artemis/Artemis/DAL/ProfileProvider.cs index da672dd5f..6f1ce47c8 100644 --- a/Artemis/Artemis/DAL/ProfileProvider.cs +++ b/Artemis/Artemis/DAL/ProfileProvider.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Xml.Serialization; @@ -87,6 +86,9 @@ namespace Artemis.DAL return profiles; } + /// + /// Makes sure the profile directory structure is in order and places default profiles + /// private static void CheckProfiles() { // Create the directory structure @@ -94,9 +96,13 @@ namespace Artemis.DAL return; Directory.CreateDirectory(ProfileFolder); - Debug.WriteLine("Place presets"); } + /// + /// Attempts to load a profile from a given path + /// + /// The absolute path to load the profile from + /// The loaded profile, or null if invalid public static ProfileModel LoadProfileIfValid(string path) { try @@ -115,5 +121,47 @@ namespace Artemis.DAL return null; } } + + /// + /// Exports the given profile to the provided path in XML + /// + /// The profile to export + /// The path to save the profile to + public static void ExportProfile(ProfileModel selectedProfile, string path) + { + var serializer = new XmlSerializer(typeof(ProfileModel)); + using (var file = new StreamWriter(path)) + { + serializer.Serialize(file, selectedProfile); + } + } + + /// + /// Renames the profile on the model and filesystem + /// + /// The profile to rename + /// The new name + public static void RenameProfile(ProfileModel profile, string name) + { + if (string.IsNullOrEmpty(name)) + return; + + // Remove the old file + var path = ProfileFolder + $@"\{profile.KeyboardName}\{profile.GameName}\{profile.Name}.xml"; + if (File.Exists(path)) + File.Delete(path); + + // Update the profile, creating a new file + profile.Name = name; + AddOrUpdate(profile); + } + + public static void DeleteProfile(ProfileModel profile) + { + // Remove the file + var path = ProfileFolder + $@"\{profile.KeyboardName}\{profile.GameName}\{profile.Name}.xml"; + if (File.Exists(path)) + File.Delete(path); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs index c52b9979b..a7d9c2920 100644 --- a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs +++ b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs @@ -263,45 +263,6 @@ namespace Artemis.ViewModels SelectedProfile = Profiles.FirstOrDefault(); } - /// - /// Adds a new profile to the current game and keyboard - /// - public async void AddProfile() - { - var name = await DialogService.ShowInputDialog("Add new profile", - "Please provide a profile name unique to this game and keyboard."); - - // Null when the user cancelled - if (name == null) - return; - - if (name.Length < 1) - { - DialogService.ShowMessageBox("Invalid profile name", "Please provide a valid profile name"); - return; - } - - var profile = new ProfileModel - { - Name = name, - KeyboardName = ActiveKeyboard.Name, - GameName = _gameModel.Name - }; - - if (ProfileProvider.GetAll().Contains(profile)) - { - var overwrite = await DialogService.ShowQuestionMessageBox("Overwrite existing profile", - "A profile with this name already exists for this game. Would you like to overwrite it?"); - if (!overwrite.Value) - return; - } - - ProfileProvider.AddOrUpdate(profile); - - LoadProfiles(); - SelectedProfile = profile; - } - public void EditLayer() { if (SelectedLayer == null) @@ -620,6 +581,115 @@ namespace Artemis.ViewModels } } + /// + /// Adds a new profile to the current game and keyboard + /// + public async void AddProfile() + { + var name = await DialogService.ShowInputDialog("Add new profile", + "Please provide a profile name unique to this game and keyboard."); + + // Null when the user cancelled + if (name == null) + return; + + if (name.Length < 1) + { + DialogService.ShowMessageBox("Invalid profile name", "Please provide a valid profile name"); + return; + } + + var profile = new ProfileModel + { + Name = name, + KeyboardName = ActiveKeyboard.Name, + GameName = _gameModel.Name + }; + + if (ProfileProvider.GetAll().Contains(profile)) + { + var overwrite = await DialogService.ShowQuestionMessageBox("Overwrite existing profile", + "A profile with this name already exists for this game. Would you like to overwrite it?"); + if (!overwrite.Value) + return; + } + + ProfileProvider.AddOrUpdate(profile); + + LoadProfiles(); + SelectedProfile = profile; + } + + public async void RenameProfile() + { + if (SelectedProfile == null) + return; + + var oldName = SelectedProfile.Name; + SelectedProfile.Name = + await DialogService.ShowInputDialog("Rename profile", "Please enter a unique new profile name"); + // Verify the name + while (ProfileProvider.GetAll().Contains(SelectedProfile)) + { + SelectedProfile.Name = + await DialogService.ShowInputDialog("Name already in use", "Please enter a unique new profile name"); + + // Null when the user cancelled + if (string.IsNullOrEmpty(SelectedProfile.Name)) + { + SelectedProfile.Name = oldName; + return; + } + } + + var newName = SelectedProfile.Name; + SelectedProfile.Name = oldName; + ProfileProvider.RenameProfile(SelectedProfile, newName); + + LoadProfiles(); + SelectedProfile = Profiles.FirstOrDefault(p => p.Name == newName); + } + + public async void DuplicateProfile() + { + if (SelectedProfile == null) + return; + + var newProfile = GeneralHelpers.Clone(SelectedProfile); + newProfile.Name = + await DialogService.ShowInputDialog("Duplicate profile", "Please enter a unique profile name"); + // Verify the name + while (ProfileProvider.GetAll().Contains(newProfile)) + { + newProfile.Name = + await DialogService.ShowInputDialog("Name already in use", "Please enter a unique profile name"); + + // Null when the user cancelled + if (string.IsNullOrEmpty(SelectedProfile.Name)) + return; + } + + ProfileProvider.AddOrUpdate(newProfile); + LoadProfiles(); + SelectedProfile = Profiles.FirstOrDefault(p => p.Name == newProfile.Name); + } + + public async void DeleteProfile() + { + if (SelectedProfile == null) + return; + + var confirm = await + DialogService.ShowQuestionMessageBox("Delete profile", + $"Are you sure you want to delete the profile named: {SelectedProfile.Name}?\n\n" + + "This cannot be undone."); + if (!confirm.Value) + return; + + ProfileProvider.DeleteProfile(SelectedProfile); + LoadProfiles(); + } + public async void ImportProfile() { var dialog = new OpenFileDialog {Filter = "Artemis profile (*.xml)|*.xml"}; @@ -673,5 +743,18 @@ namespace Artemis.ViewModels SelectedProfile = Profiles.FirstOrDefault(p => p.Name == profile.Name); } + + public void ExportProfile() + { + if (SelectedProfile == null) + return; + + var dialog = new SaveFileDialog {Filter = "Artemis profile (*.xml)|*.xml"}; + var result = dialog.ShowDialog(); + if (result != DialogResult.OK) + return; + + ProfileProvider.ExportProfile(SelectedProfile, dialog.FileName); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/Views/ProfileEditorView.xaml b/Artemis/Artemis/Views/ProfileEditorView.xaml index 816355729..e25765afa 100644 --- a/Artemis/Artemis/Views/ProfileEditorView.xaml +++ b/Artemis/Artemis/Views/ProfileEditorView.xaml @@ -65,7 +65,7 @@ - -