1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Implemented functionality on all the profile management buttons

This commit is contained in:
SpoinkyNL 2016-05-26 23:32:31 +02:00
parent 5a19bf9e30
commit d9ad83d73e
3 changed files with 174 additions and 43 deletions

View File

@ -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;
}
/// <summary>
/// Makes sure the profile directory structure is in order and places default profiles
/// </summary>
private static void CheckProfiles()
{
// Create the directory structure
@ -94,9 +96,13 @@ namespace Artemis.DAL
return;
Directory.CreateDirectory(ProfileFolder);
Debug.WriteLine("Place presets");
}
/// <summary>
/// Attempts to load a profile from a given path
/// </summary>
/// <param name="path">The absolute path to load the profile from</param>
/// <returns>The loaded profile, or null if invalid</returns>
public static ProfileModel LoadProfileIfValid(string path)
{
try
@ -115,5 +121,47 @@ namespace Artemis.DAL
return null;
}
}
/// <summary>
/// Exports the given profile to the provided path in XML
/// </summary>
/// <param name="selectedProfile">The profile to export</param>
/// <param name="path">The path to save the profile to</param>
public static void ExportProfile(ProfileModel selectedProfile, string path)
{
var serializer = new XmlSerializer(typeof(ProfileModel));
using (var file = new StreamWriter(path))
{
serializer.Serialize(file, selectedProfile);
}
}
/// <summary>
/// Renames the profile on the model and filesystem
/// </summary>
/// <param name="profile">The profile to rename</param>
/// <param name="name">The new name</param>
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);
}
}
}

View File

@ -263,45 +263,6 @@ namespace Artemis.ViewModels
SelectedProfile = Profiles.FirstOrDefault();
}
/// <summary>
/// Adds a new profile to the current game and keyboard
/// </summary>
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
}
}
/// <summary>
/// Adds a new profile to the current game and keyboard
/// </summary>
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);
}
}
}

View File

@ -65,7 +65,7 @@
</Rectangle>
</Button.Content>
</Button>
<Button x:Name="EditProfile" VerticalAlignment="Top" Style="{DynamicResource SquareButtonStyle}"
<Button x:Name="RenameProfile" VerticalAlignment="Top" Style="{DynamicResource SquareButtonStyle}"
Width="26" Height="26" HorizontalAlignment="Right" Margin="10,0,0,0" ToolTip="Rename profile">
<Button.Content>
<Rectangle
@ -89,7 +89,7 @@
</Rectangle>
</Button.Content>
</Button>
<Button x:Name="RemoveProfile" VerticalAlignment="Top" Style="{DynamicResource SquareButtonStyle}"
<Button x:Name="DeleteProfile" VerticalAlignment="Top" Style="{DynamicResource SquareButtonStyle}"
Width="26" Height="26" HorizontalAlignment="Right" Margin="10,0,0,0" ToolTip="Delete profile">
<Button.Content>
<Rectangle