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;
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Manage files and URLs using their default applications.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public sealed class Shell
|
|
|
|
|
|
{
|
|
|
|
|
|
private static Shell _shell;
|
2019-05-18 02:00:56 +02:00
|
|
|
|
private static object _syncRoot = new object();
|
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
|
|
|
|
{
|
2020-05-26 15:11:22 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<object>();
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2020-05-26 15:11:22 +02:00
|
|
|
|
BridgeConnector.Socket.On("shell-showItemInFolderCompleted", () =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
BridgeConnector.Socket.Off("shell-showItemInFolderCompleted");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
BridgeConnector.Socket.Emit("shell-showItemInFolder", fullPath);
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-05-25 17:31:27 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<string>();
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
BridgeConnector.Socket.On("shell-openPathCompleted", (errorMessage) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2020-05-23 12:17:53 +02:00
|
|
|
|
BridgeConnector.Socket.Off("shell-openPathCompleted");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
taskCompletionSource.SetResult((string) errorMessage);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2020-05-25 17:31:27 +02:00
|
|
|
|
BridgeConnector.Socket.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>
|
|
|
|
|
|
public Task<string> OpenExternalAsync(string url, OpenExternalOptions options)
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2020-05-27 00:51:48 +02:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<string>();
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
BridgeConnector.Socket.On("shell-openExternalCompleted", (error) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
BridgeConnector.Socket.Off("shell-openExternalCompleted");
|
|
|
|
|
|
|
2020-05-27 01:06:31 +02:00
|
|
|
|
taskCompletionSource.SetResult((string) 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
|
|
|
|
{
|
2020-05-27 00:51:48 +02:00
|
|
|
|
BridgeConnector.Socket.Emit("shell-openExternal", url);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
BridgeConnector.Socket.Emit("shell-openExternal", url, JObject.FromObject(options, _jsonSerializer));
|
|
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
2021-07-02 02:04:23 +02:00
|
|
|
|
BridgeConnector.Socket.On("shell-trashItem-completed", (success) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-02 02:04:23 +02:00
|
|
|
|
BridgeConnector.Socket.Off("shell-trashItem-completed");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
taskCompletionSource.SetResult((bool) success);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-02 02:04:23 +02:00
|
|
|
|
BridgeConnector.Socket.Emit("shell-trashItem", fullPath);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Play the beep sound.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Beep()
|
|
|
|
|
|
{
|
|
|
|
|
|
BridgeConnector.Socket.Emit("shell-beep");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
public Task<bool> WriteShortcutLinkAsync(string shortcutPath, ShortcutLinkOperation operation, ShortcutDetails options)
|
|
|
|
|
|
{
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
|
|
|
|
|
BridgeConnector.Socket.On("shell-writeShortcutLinkCompleted", (success) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
BridgeConnector.Socket.Off("shell-writeShortcutLinkCompleted");
|
|
|
|
|
|
|
2020-05-27 01:06:31 +02:00
|
|
|
|
taskCompletionSource.SetResult((bool) success);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
BridgeConnector.Socket.Emit("shell-writeShortcutLink", shortcutPath, operation.GetDescription(), JObject.FromObject(options, _jsonSerializer));
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
public Task<ShortcutDetails> ReadShortcutLinkAsync(string shortcutPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<ShortcutDetails>();
|
|
|
|
|
|
|
|
|
|
|
|
BridgeConnector.Socket.On("shell-readShortcutLinkCompleted", (shortcutDetails) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
BridgeConnector.Socket.Off("shell-readShortcutLinkCompleted");
|
|
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
var shortcutObject = shortcutDetails as JObject;
|
|
|
|
|
|
var details = shortcutObject?.ToObject<ShortcutDetails>();
|
|
|
|
|
|
|
|
|
|
|
|
taskCompletionSource.SetResult(details);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
BridgeConnector.Socket.Emit("shell-readShortcutLink", shortcutPath);
|
|
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-27 00:51:48 +02:00
|
|
|
|
private readonly JsonSerializer _jsonSerializer = new JsonSerializer()
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
|
DefaultValueHandling = DefaultValueHandling.Ignore
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2020-05-27 01:06:31 +02:00
|
|
|
|
}
|