Skip to content

Commit

Permalink
Merge pull request EveOPlus#2 from ZeroTrepidation/feature-profileMan…
Browse files Browse the repository at this point in the history
…agement

Feature profile management
  • Loading branch information
ZeroTrepidation authored Jun 17, 2024
2 parents f987400 + 4f1553d commit 6ba46a3
Show file tree
Hide file tree
Showing 11 changed files with 1,266 additions and 849 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ tools/
src/.vs/
*.suo
*.user
*.DotSettings
*.DotSettings
/.vs
4 changes: 2 additions & 2 deletions src/Eve-O-Preview/Configuration/Implementation/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class AppConfig : IAppConfig
public AppConfig()
{
// Default values
this.ConfigFileName = null;
this.ActiveProfileName = null;
}

public string ConfigFileName { get; set; }
public string ActiveProfileName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ namespace EveOPreview.Configuration.Implementation
{
class ConfigurationStorage : IConfigurationStorage
{
private const string CONFIGURATION_FILE_NAME = "EVE-O Preview.json";
private const string DEFAULT_CONFIGURATION_FILE_NAME = "Default Profile";
private const string APP_CONFIG_FILENAME = "config.json";
private const string APP_CONFIG_BASE_DIR = @"./config/";
private const string PROFILES_BASE_DIR = @"./profiles/";
private const string EXTENSION = ".json";

private readonly IAppConfig _appConfig;
private readonly IThumbnailConfiguration _thumbnailConfiguration;
Expand All @@ -18,14 +22,30 @@ public ConfigurationStorage(IAppConfig appConfig, IThumbnailConfiguration thumbn

public void Load()
{
string filename = this.GetConfigFileName();

if (!File.Exists(filename))
initializeDirectories();

string filename;

if(File.Exists(GetAppConfigPath()) && this._appConfig.ActiveProfileName == null)
{
string appConfigRawData = File.ReadAllText(this.GetAppConfigPath());
JsonConvert.PopulateObject(appConfigRawData, this._appConfig);
filename = this.GetProfileName();
} else
{
filename = this.GetProfileName();
this.UpdateActiveProfileName(filename);
}



if (!File.Exists(this.GetProfilePath()))
{
return;
}

string rawData = File.ReadAllText(filename);
string rawData = File.ReadAllText(this.GetProfilePath());

JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
Expand All @@ -43,21 +63,81 @@ public void Load()
public void Save()
{
string rawData = JsonConvert.SerializeObject(this._thumbnailConfiguration, Formatting.Indented);
string filename = this.GetConfigFileName();
string appConfigRawData = JsonConvert.SerializeObject(this._appConfig, Formatting.Indented);

try
{
File.WriteAllText(filename, rawData);
File.WriteAllText(this.GetAppConfigPath(), appConfigRawData);
File.WriteAllText(this.GetProfilePath(), rawData);
}
catch (IOException)
{
// Ignore error if for some reason the updated config cannot be written down
}
}

private string GetConfigFileName()
public void UpdateActiveProfileName(string filename)
{
this._appConfig.ActiveProfileName = filename;
}

public string GetProfilesBaseDir()
{
return PROFILES_BASE_DIR;
}

public bool CreateNewProfile(string profileName, IThumbnailConfiguration config)
{
string rawData = JsonConvert.SerializeObject(config, Formatting.Indented);
try
{
string filename = string.Concat(PROFILES_BASE_DIR, profileName, EXTENSION);
if(File.Exists(filename))
{
return false;
} else
{
File.WriteAllText(string.Concat(filename), rawData);
return true;
}

}
catch (IOException)
{
return false;
}
}

private string GetAppConfigPath()
{
return APP_CONFIG_BASE_DIR + APP_CONFIG_FILENAME;
}

private string GetProfileName()
{
return string.IsNullOrEmpty(this._appConfig.ConfigFileName) ? ConfigurationStorage.CONFIGURATION_FILE_NAME : this._appConfig.ConfigFileName;
return (string.IsNullOrEmpty(this._appConfig.ActiveProfileName) ? ConfigurationStorage.DEFAULT_CONFIGURATION_FILE_NAME : this._appConfig.ActiveProfileName);
}

private string GetProfilePath()
{
return PROFILES_BASE_DIR + GetProfileFileName();
}

private void initializeDirectories()
{
if (!Directory.Exists(APP_CONFIG_BASE_DIR))
{
Directory.CreateDirectory(APP_CONFIG_BASE_DIR);
}
if (!Directory.Exists(PROFILES_BASE_DIR))
{
Directory.CreateDirectory(PROFILES_BASE_DIR);
}
}

private string GetProfileFileName()
{
return string.Concat(GetProfileName(), EXTENSION);
}
}
}
2 changes: 1 addition & 1 deletion src/Eve-O-Preview/Configuration/Interface/IAppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
/// </summary>
public interface IAppConfig
{
string ConfigFileName { get; set; }
string ActiveProfileName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ public interface IConfigurationStorage
{
void Load();
void Save();
void UpdateActiveProfileName(string filename);
string GetProfilesBaseDir();
bool CreateNewProfile(string profileName, IThumbnailConfiguration config);
}
}
3 changes: 3 additions & 0 deletions src/Eve-O-Preview/Eve-O-Preview.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<UseVSHostingProcess>false</UseVSHostingProcess>
<RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>
<PropertyGroup>
<RuntimeIdentifiers>win</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
Expand Down
62 changes: 61 additions & 1 deletion src/Eve-O-Preview/Presenters/Implementation/MainFormPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using EveOPreview.Configuration;
using EveOPreview.Mediator.Messages;
using EveOPreview.View;
using MediatR;
using System.Linq;
using EveOPreview.Configuration.Implementation;

namespace EveOPreview.Presenters
{
Expand All @@ -18,19 +21,21 @@ public class MainFormPresenter : Presenter<IMainFormView>, IMainFormPresenter
#region Private fields
private readonly IMediator _mediator;
private readonly IThumbnailConfiguration _configuration;
private readonly IAppConfig _appConfiguration;
private readonly IConfigurationStorage _configurationStorage;
private readonly IDictionary<string, IThumbnailDescription> _descriptionsCache;
private bool _suppressSizeNotifications;

private bool _exitApplication;
#endregion

public MainFormPresenter(IApplicationController controller, IMainFormView view, IMediator mediator, IThumbnailConfiguration configuration, IConfigurationStorage configurationStorage)
public MainFormPresenter(IApplicationController controller, IMainFormView view, IMediator mediator, IAppConfig appConfiguration, IThumbnailConfiguration configuration, IConfigurationStorage configurationStorage)
: base(controller, view)
{
this._mediator = mediator;
this._configuration = configuration;
this._configurationStorage = configurationStorage;
this._appConfiguration = appConfiguration;

this._descriptionsCache = new Dictionary<string, IThumbnailDescription>();

Expand All @@ -45,6 +50,8 @@ public MainFormPresenter(IApplicationController controller, IMainFormView view,
this.View.ThumbnailStateChanged = this.UpdateThumbnailState;
this.View.DocumentationLinkActivated = this.OpenDocumentationLink;
this.View.ApplicationExitRequested = this.ExitApplication;
this.View.ReloadApplicationWithConfig = this.UpdateConfigAndReload;
this.View.CreateNewProfile = this.CreateNewProfile;
}

private void Activate()
Expand Down Expand Up @@ -122,6 +129,12 @@ private void LoadApplicationSettings()
this.View.ShowThumbnailFrames = this._configuration.ShowThumbnailFrames;
this.View.EnableActiveClientHighlight = this._configuration.EnableActiveClientHighlight;
this.View.ActiveClientHighlightColor = this._configuration.ActiveClientHighlightColor;

//Saving just to ensure our file exists in the system
this._configurationStorage.Save();

this.View.FileOptions = populateFileList();
this.View.CurrentProfileName = this._appConfiguration.ActiveProfileName;
}

private async void SaveApplicationSettings()
Expand Down Expand Up @@ -241,5 +254,52 @@ private void ExitApplication()
this._exitApplication = true;
this.View.Close();
}

private void UpdateConfigAndReload()
{
this._configurationStorage.UpdateActiveProfileName(this.View.CurrentProfileName);
this.View.ApplicationSettingsChanged = null;
this.LoadApplicationSettings();
this.View.ApplicationSettingsChanged = SaveApplicationSettings;
}

private void CreateNewProfile()
{
string newProfileName = this.View.NewProfileName;

IThumbnailConfiguration newConfig;

if (!this.View.CopySettings)
{
newConfig = new ThumbnailConfiguration();

} else
{
newConfig = _configuration;
}
bool success = _configurationStorage.CreateNewProfile(newProfileName, newConfig);
if(success)
{
this.View.FileOptions = populateFileList();
if (this.View.MakeActive)
{
this.View.CurrentProfileName = newProfileName;
this._appConfiguration.ActiveProfileName = newProfileName;
this.UpdateConfigAndReload();
}
} else
{
this.View.NewProfileError = "Profile already exists";
}
this.View.ResetProfileOptions();
}

private List<String> populateFileList()
{
List<String> fileList = new List<String>();
fileList.Add(" - ");
fileList.AddRange(Directory.EnumerateFiles(this._configurationStorage.GetProfilesBaseDir(), "*.json").Select(Path.GetFileNameWithoutExtension));
return fileList;
}
}
}
Loading

0 comments on commit 6ba46a3

Please sign in to comment.