using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Threading.Tasks;
using ElectronNET.API.Extensions;
namespace ElectronNET.API
{
///
/// Manage files and URLs using their default applications.
///
public sealed class Shell
{
private static Shell _shell;
private static object _syncRoot = new object();
internal Shell() { }
internal static Shell Instance
{
get
{
if (_shell == null)
{
lock (_syncRoot)
{
if (_shell == null)
{
_shell = new Shell();
}
}
}
return _shell;
}
}
///
/// Show the given file in a file manager. If possible, select the file.
///
/// The full path to the directory / file.
public Task ShowItemInFolderAsync(string fullPath)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("shell-showItemInFolderCompleted", () =>
{
BridgeConnector.Socket.Off("shell-showItemInFolderCompleted");
});
BridgeConnector.Socket.Emit("shell-showItemInFolder", fullPath);
return taskCompletionSource.Task;
}
///
/// Open the given file in the desktop's default manner.
///
/// The path to the directory / file.
/// The error message corresponding to the failure if a failure occurred, otherwise .
public Task OpenPathAsync(string path)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("shell-openPathCompleted", (errorMessage) =>
{
BridgeConnector.Socket.Off("shell-openPathCompleted");
taskCompletionSource.SetResult((string) errorMessage);
});
BridgeConnector.Socket.Emit("shell-openPath", path);
return taskCompletionSource.Task;
}
///
/// Open the given external protocol URL in the desktop’s default manner.
/// (For example, mailto: URLs in the user’s default mail agent).
///
/// Max 2081 characters on windows.
/// The error message corresponding to the failure if a failure occurred, otherwise .
public Task OpenExternalAsync(string url)
{
return OpenExternalAsync(url, null);
}
///
/// Open the given external protocol URL in the desktop’s default manner.
/// (For example, mailto: URLs in the user’s default mail agent).
///
/// Max 2081 characters on windows.
/// Controls the behavior of OpenExternal.
/// The error message corresponding to the failure if a failure occurred, otherwise .
public Task OpenExternalAsync(string url, OpenExternalOptions options)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("shell-openExternalCompleted", (error) =>
{
BridgeConnector.Socket.Off("shell-openExternalCompleted");
taskCompletionSource.SetResult((string) error);
});
if (options == null)
{
BridgeConnector.Socket.Emit("shell-openExternal", url);
}
else
{
BridgeConnector.Socket.Emit("shell-openExternal", url, JObject.FromObject(options, _jsonSerializer));
}
return taskCompletionSource.Task;
}
///
/// Move the given file to trash and returns a status for the operation.
///
/// The full path to the directory / file.
/// Whether the item was successfully moved to the trash.
public Task TrashItemAsync(string fullPath)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("shell-trashItem-completed", (success) =>
{
BridgeConnector.Socket.Off("shell-trashItem-completed");
taskCompletionSource.SetResult((bool) success);
});
BridgeConnector.Socket.Emit("shell-trashItem", fullPath);
return taskCompletionSource.Task;
}
///
/// Play the beep sound.
///
public void Beep()
{
BridgeConnector.Socket.Emit("shell-beep");
}
///
/// Creates or updates a shortcut link at shortcutPath.
///
/// The path to the shortcut.
/// Default is
/// Structure of a shortcut.
/// Whether the shortcut was created successfully.
public Task WriteShortcutLinkAsync(string shortcutPath, ShortcutLinkOperation operation, ShortcutDetails options)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("shell-writeShortcutLinkCompleted", (success) =>
{
BridgeConnector.Socket.Off("shell-writeShortcutLinkCompleted");
taskCompletionSource.SetResult((bool) success);
});
BridgeConnector.Socket.Emit("shell-writeShortcutLink", shortcutPath, operation.GetDescription(), JObject.FromObject(options, _jsonSerializer));
return taskCompletionSource.Task;
}
///
/// Resolves the shortcut link at shortcutPath.
/// An exception will be thrown when any error happens.
///
/// The path tot the shortcut.
/// of the shortcut.
public Task ReadShortcutLinkAsync(string shortcutPath)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("shell-readShortcutLinkCompleted", (shortcutDetails) =>
{
BridgeConnector.Socket.Off("shell-readShortcutLinkCompleted");
var shortcutObject = shortcutDetails as JObject;
var details = shortcutObject?.ToObject();
taskCompletionSource.SetResult(details);
});
BridgeConnector.Socket.Emit("shell-readShortcutLink", shortcutPath);
return taskCompletionSource.Task;
}
private readonly JsonSerializer _jsonSerializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
}