Thorsten Sommer
0587af18f2
- Added import algorithm - Added FromJsonX() methods to data models - Added possibility to work with temp. database file - Added export processor to handle export process & triggers - Added something changed event e.g. as export trigger - Added possibility to import a JSON export - Updated Git icon
206 lines
7.6 KiB
C#
206 lines
7.6 KiB
C#
using System.ComponentModel;
|
|
using Microsoft.Win32;
|
|
using Version = Processor.Version;
|
|
|
|
namespace UI_WinForms.Components;
|
|
|
|
[DefaultEvent(nameof(LoadProject))]
|
|
public partial class LoaderStart : UserControl
|
|
{
|
|
private bool areRecentProjectsVisible = false;
|
|
|
|
public LoaderStart()
|
|
{
|
|
this.InitializeComponent();
|
|
this.labelVersion.Text = Version.Text;
|
|
}
|
|
|
|
#region Recent Projects
|
|
|
|
private static RegistryKey I18NCommanderKey => Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree)!.CreateSubKey("I18N Commander", RegistryKeyPermissionCheck.ReadWriteSubTree);
|
|
|
|
/// <summary>
|
|
/// Gets or sets the list of recent projects without any extras e.g. it does not prune the list of projects.
|
|
/// </summary>
|
|
private static List<string> RecentProjects
|
|
{
|
|
get
|
|
{
|
|
using var regKey = LoaderStart.I18NCommanderKey;
|
|
var recentProjectsValue = regKey.GetValue("recentProjects");
|
|
return recentProjectsValue switch
|
|
{
|
|
string[] list => new List<string>(list),
|
|
_ => new List<string>(),
|
|
};
|
|
}
|
|
|
|
set
|
|
{
|
|
using var regKey = LoaderStart.I18NCommanderKey;
|
|
regKey.SetValue("recentProjects", value.ToArray(), RegistryValueKind.MultiString);
|
|
}
|
|
}
|
|
|
|
private static void UpdateRecentProjectsWithPruning(string latestUsedProjectPath)
|
|
{
|
|
var previousRecentList = LoaderStart.RecentProjects;
|
|
|
|
// Check, if the latest project is identical to the next project we open. In that case, we don't add it to the list.
|
|
if (previousRecentList.Any() && previousRecentList[0] == latestUsedProjectPath)
|
|
return;
|
|
|
|
// Add the next project to the list:
|
|
previousRecentList.Insert(0, latestUsedProjectPath);
|
|
|
|
// Prune the list:
|
|
if (previousRecentList.Count > 6)
|
|
previousRecentList = previousRecentList.Take(6).ToList();
|
|
|
|
LoaderStart.RecentProjects = previousRecentList;
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Opens the recent projects dropdown menu.
|
|
private void buttonOpen_Click(object sender, EventArgs e)
|
|
{
|
|
if(this.DesignMode)
|
|
return;
|
|
|
|
if(this.areRecentProjectsVisible)
|
|
{
|
|
this.areRecentProjectsVisible = false;
|
|
this.contextMenuRecentProjects.Close();
|
|
return;
|
|
}
|
|
|
|
var recentProjects = LoaderStart.RecentProjects;
|
|
this.contextMenuRecentProjects.Items.Clear();
|
|
this.contextMenuRecentProjects.Items.Add("Browse for project...", Resources.Icons.icons8_browse_folder_512, (innerSender, args) => this.BrowseForProject());
|
|
foreach (var recentProject in recentProjects)
|
|
{
|
|
var fileInfo = new FileInfo(recentProject);
|
|
|
|
// Split the file's path into each folder's name:
|
|
var folderNames = fileInfo.DirectoryName!.Split(Path.DirectorySeparatorChar);
|
|
|
|
// Distinguish between I18N Commander projects and JSON imports:
|
|
if (fileInfo.Extension == ".i18nc")
|
|
{
|
|
// Render this entry:
|
|
var item = this.contextMenuRecentProjects.Items.Add($"{folderNames.Last()}: {fileInfo.Name}", Resources.Icons.icons8_document_512, (innerSender, args) => this.OpenRecentProject(innerSender, LoaderAction.LOAD_PROJECT));
|
|
item.Tag = recentProject;
|
|
}
|
|
else if (fileInfo.Extension == ".json")
|
|
{
|
|
// Render this entry:
|
|
var item = this.contextMenuRecentProjects.Items.Add($"{folderNames.Last()}: {fileInfo.Name}", Resources.Icons.icons8_git, (innerSender, args) => this.OpenRecentProject(innerSender, LoaderAction.IMPORT_JSON));
|
|
item.Tag = recentProject;
|
|
}
|
|
}
|
|
|
|
var button = (sender as Button)!;
|
|
this.contextMenuRecentProjects.Show(button, 0, button.Height);
|
|
this.areRecentProjectsVisible = true;
|
|
}
|
|
|
|
private void buttonNew_Click(object sender, EventArgs e)
|
|
{
|
|
if(this.DesignMode)
|
|
return;
|
|
|
|
var saveDialog = new SaveFileDialog
|
|
{
|
|
AddExtension = true,
|
|
CheckPathExists = true,
|
|
CheckFileExists = false,
|
|
CreatePrompt = false,
|
|
OverwritePrompt = true,
|
|
DereferenceLinks = true,
|
|
DefaultExt = ".i18nc",
|
|
Filter = "I18N Commander Files (*.i18nc)|*.i18nc",
|
|
RestoreDirectory = true,
|
|
Title = "Create a new I18N Commander file",
|
|
};
|
|
|
|
var dialogResult = saveDialog.ShowDialog(this);
|
|
if (dialogResult != DialogResult.OK)
|
|
return;
|
|
|
|
var destinationFilePath = saveDialog.FileName;
|
|
|
|
// When the user chose an existing file, we delete it:
|
|
// (note: the user already accepts overwriting the file)
|
|
if (File.Exists(destinationFilePath))
|
|
File.Delete(destinationFilePath);
|
|
|
|
LoaderStart.UpdateRecentProjectsWithPruning(destinationFilePath);
|
|
this.OpenProject(LoaderAction.CREATE_NEW_PROJECT, destinationFilePath);
|
|
}
|
|
|
|
private void BrowseForProject()
|
|
{
|
|
var openDialog = new OpenFileDialog
|
|
{
|
|
AddExtension = true,
|
|
CheckPathExists = true,
|
|
CheckFileExists = true,
|
|
DereferenceLinks = true,
|
|
DefaultExt = ".i18nc",
|
|
|
|
// I18N Commander files (*.i18nc) or JSON files (*.json):
|
|
Filter = "I18N Commander Files (*.i18nc)|*.i18nc|JSON Files (*.json)|*.json",
|
|
Multiselect = false,
|
|
RestoreDirectory = true,
|
|
Title = "Open an I18N Commander file or Import a JSON file",
|
|
};
|
|
var dialogResult = openDialog.ShowDialog(this);
|
|
if (dialogResult != DialogResult.OK)
|
|
return;
|
|
|
|
var projectFilePath = openDialog.FileName;
|
|
LoaderStart.UpdateRecentProjectsWithPruning(projectFilePath);
|
|
|
|
// Check, if the user chose an I18N Commander file or a JSON file:
|
|
if (projectFilePath.ToLowerInvariant().EndsWith(".i18nc", StringComparison.InvariantCulture))
|
|
this.OpenProject(LoaderAction.LOAD_PROJECT, projectFilePath);
|
|
else if (projectFilePath.ToLowerInvariant().EndsWith(".json", StringComparison.InvariantCulture))
|
|
this.OpenProject(LoaderAction.IMPORT_JSON, projectFilePath);
|
|
}
|
|
|
|
private void OpenRecentProject(object? sender, LoaderAction action)
|
|
{
|
|
if (sender is not ToolStripItem item)
|
|
return;
|
|
|
|
var path = (item.Tag as string)!;
|
|
LoaderStart.UpdateRecentProjectsWithPruning(path);
|
|
this.OpenProject(action, path);
|
|
}
|
|
|
|
private void OpenProject(LoaderAction action, string path)
|
|
{
|
|
// Hint: the project file might or might not exist (new project vs. recent project)
|
|
this.LoadProject?.Invoke(this, new LoaderResult(action, path));
|
|
}
|
|
|
|
private void contextMenuRecentProjects_Closing(object sender, ToolStripDropDownClosingEventArgs e)
|
|
{
|
|
this.areRecentProjectsVisible = false;
|
|
}
|
|
|
|
[Category("Settings"), Description("When the user chooses a project to load.")]
|
|
public event EventHandler<LoaderResult>? LoadProject;
|
|
|
|
public readonly record struct LoaderResult(LoaderAction Action, string DataFile);
|
|
|
|
public enum LoaderAction
|
|
{
|
|
NONE,
|
|
|
|
LOAD_PROJECT,
|
|
CREATE_NEW_PROJECT,
|
|
IMPORT_JSON,
|
|
}
|
|
} |