2017-10-21 04:37:01 +02:00
|
|
|
|
using ElectronNET.API.Entities;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2020-05-27 00:51:48 +02:00
|
|
|
|
using ElectronNET.API.Extensions;
|
2021-12-06 16:44:31 -06:00
|
|
|
|
using ElectronNET.API.Interfaces;
|
2021-08-26 14:22:54 +02:00
|
|
|
|
using System.Runtime.Versioning;
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Manage files and URLs using their default applications.
|
|
|
|
|
|
/// </summary>
|
2021-12-06 16:44:31 -06:00
|
|
|
|
public sealed class Shell : IShell
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
private static Shell _shell;
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly object _syncRoot = new();
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
internal Shell() { }
|
|
|
|
|
|
|
|
|
|
|
|
internal static Shell Instance
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_shell == null)
|
|
|
|
|
|
{
|
2017-11-04 00:16:14 +01:00
|
|
|
|
lock (_syncRoot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_shell == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_shell = new Shell();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _shell;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Show the given file in a file manager. If possible, select the file.
|
|
|
|
|
|
/// </summary>
|
2020-05-26 15:11:22 +02:00
|
|
|
|
/// <param name="fullPath">The full path to the directory / file.</param>
|
|
|
|
|
|
public Task ShowItemInFolderAsync(string fullPath)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-08-17 16:28:07 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.On("shell-showItemInFolderCompleted", () =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("shell-showItemInFolderCompleted");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("shell-showItemInFolder", fullPath);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-05-25 17:31:27 +02:00
|
|
|
|
/// Open the given file in the desktop's default manner.
|
2017-10-21 04:37:01 +02:00
|
|
|
|
/// </summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// <param name="path">The path to the directory / file.</param>
|
2020-05-25 17:31:27 +02:00
|
|
|
|
/// <returns>The error message corresponding to the failure if a failure occurred, otherwise <see cref="string.Empty"/>.</returns>
|
|
|
|
|
|
public Task<string> OpenPathAsync(string path)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-08-17 16:28:07 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2021-07-12 21:27:40 +02:00
|
|
|
|
BridgeConnector.On<string>("shell-openPathCompleted", (errorMessage) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("shell-openPathCompleted");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2021-07-12 21:33:35 +02:00
|
|
|
|
taskCompletionSource.SetResult(errorMessage);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("shell-openPath", path);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Open the given external protocol URL in the desktop’s default manner.
|
|
|
|
|
|
/// (For example, mailto: URLs in the user’s default mail agent).
|
|
|
|
|
|
/// </summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// <param name="url">Max 2081 characters on windows.</param>
|
|
|
|
|
|
/// <returns>The error message corresponding to the failure if a failure occurred, otherwise <see cref="string.Empty"/>.</returns>
|
|
|
|
|
|
public Task<string> OpenExternalAsync(string url)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2020-05-27 00:51:48 +02:00
|
|
|
|
return OpenExternalAsync(url, null);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Open the given external protocol URL in the desktop’s default manner.
|
|
|
|
|
|
/// (For example, mailto: URLs in the user’s default mail agent).
|
|
|
|
|
|
/// </summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// <param name="url">Max 2081 characters on windows.</param>
|
|
|
|
|
|
/// <param name="options">Controls the behavior of OpenExternal.</param>
|
|
|
|
|
|
/// <returns>The error message corresponding to the failure if a failure occurred, otherwise <see cref="string.Empty"/>.</returns>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
public Task<string> OpenExternalAsync(string url, OpenExternalOptions options)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-08-17 16:28:07 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2021-07-12 21:27:40 +02:00
|
|
|
|
BridgeConnector.On<string>("shell-openExternalCompleted", (error) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("shell-openExternalCompleted");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2021-07-12 21:33:35 +02:00
|
|
|
|
taskCompletionSource.SetResult(error);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
if (options == null)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("shell-openExternal", url);
|
2020-05-27 00:51:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-08-23 11:57:42 +02:00
|
|
|
|
BridgeConnector.Emit("shell-openExternal", url, options);
|
2020-05-27 00:51:48 +02:00
|
|
|
|
}
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// Move the given file to trash and returns a <see cref="bool"/> status for the operation.
|
2017-10-21 04:37:01 +02:00
|
|
|
|
/// </summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// <param name="fullPath">The full path to the directory / file.</param>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
/// <returns> Whether the item was successfully moved to the trash.</returns>
|
2021-07-02 02:04:23 +02:00
|
|
|
|
public Task<bool> TrashItemAsync(string fullPath)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<bool>("shell-trashItem", "shell-trashItem-completed", fullPath);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Play the beep sound.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Beep()
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("shell-beep");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates or updates a shortcut link at shortcutPath.
|
|
|
|
|
|
/// </summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// <param name="shortcutPath">The path to the shortcut.</param>
|
|
|
|
|
|
/// <param name="operation">Default is <see cref="ShortcutLinkOperation.Create"/></param>
|
|
|
|
|
|
/// <param name="options">Structure of a shortcut.</param>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
/// <returns>Whether the shortcut was created successfully.</returns>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("windows")]
|
2017-10-21 04:37:01 +02:00
|
|
|
|
public Task<bool> WriteShortcutLinkAsync(string shortcutPath, ShortcutLinkOperation operation, ShortcutDetails options)
|
|
|
|
|
|
{
|
2021-08-23 11:57:42 +02:00
|
|
|
|
return BridgeConnector.OnResult<bool>("shell-writeShortcutLink", "shell-writeShortcutLinkCompleted", shortcutPath, operation.GetDescription(), options);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Resolves the shortcut link at shortcutPath.
|
|
|
|
|
|
/// An exception will be thrown when any error happens.
|
|
|
|
|
|
/// </summary>
|
2020-05-27 00:51:48 +02:00
|
|
|
|
/// <param name="shortcutPath">The path tot the shortcut.</param>
|
|
|
|
|
|
/// <returns><see cref="ShortcutDetails"/> of the shortcut.</returns>
|
2021-08-26 14:22:54 +02:00
|
|
|
|
[SupportedOSPlatform("windows")]
|
2017-10-21 04:37:01 +02:00
|
|
|
|
public Task<ShortcutDetails> ReadShortcutLinkAsync(string shortcutPath)
|
|
|
|
|
|
{
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<ShortcutDetails>("shell-readShortcutLink", "shell-readShortcutLinkCompleted", shortcutPath);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-27 01:06:31 +02:00
|
|
|
|
}
|