Duckstation/src/util/host.h

79 lines
3.5 KiB
C
Raw Normal View History

2024-05-05 10:21:54 +00:00
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
2023-08-27 06:00:06 +00:00
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
#include "common/types.h"
#include <ctime>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace Host {
/// Returns true if the specified resource file exists.
bool ResourceFileExists(std::string_view filename, bool allow_override);
2023-08-27 06:00:06 +00:00
/// Reads a file from the resources directory of the application.
/// This may be outside of the "normal" filesystem on platforms such as Mac.
std::optional<std::vector<u8>> ReadResourceFile(std::string_view filename, bool allow_override);
2023-08-27 06:00:06 +00:00
/// Reads a resource file file from the resources directory as a string.
std::optional<std::string> ReadResourceFileToString(std::string_view filename, bool allow_override);
2023-08-27 06:00:06 +00:00
/// Returns the modified time of a resource.
std::optional<std::time_t> GetResourceFileTimestamp(std::string_view filename, bool allow_override);
2023-08-27 06:00:06 +00:00
2024-02-25 08:19:54 +00:00
/// Reports a fatal error on the main thread. This does not assume that the main window exists,
/// unlike ReportErrorAsync(), and will exit the application after the popup is closed.
2024-05-05 10:21:54 +00:00
void ReportFatalError(std::string_view title, std::string_view message);
2024-02-25 08:19:54 +00:00
2023-08-27 06:00:06 +00:00
/// Displays an asynchronous error on the UI thread, i.e. doesn't block the caller.
2024-05-05 10:21:54 +00:00
void ReportErrorAsync(std::string_view title, std::string_view message);
void ReportFormattedErrorAsync(std::string_view title, const char* format, ...);
2023-08-27 06:00:06 +00:00
/// Displays a synchronous confirmation on the UI thread, i.e. blocks the caller.
2024-05-05 10:21:54 +00:00
bool ConfirmMessage(std::string_view title, std::string_view message);
bool ConfirmFormattedMessage(std::string_view title, const char* format, ...);
2023-08-27 06:00:06 +00:00
/// Returns the user agent to use for HTTP requests.
std::string GetHTTPUserAgent();
2023-08-27 06:00:06 +00:00
/// Opens a URL, using the default application.
2024-05-05 10:21:54 +00:00
void OpenURL(std::string_view url);
2023-08-27 06:00:06 +00:00
/// Copies the provided text to the host's clipboard, if present.
2024-05-05 10:21:54 +00:00
bool CopyTextToClipboard(std::string_view text);
2023-08-27 06:00:06 +00:00
/// Returns a localized version of the specified string within the specified context.
/// The pointer is guaranteed to be valid until the next language change.
2024-05-05 10:21:54 +00:00
const char* TranslateToCString(std::string_view context, std::string_view msg);
2023-08-27 06:00:06 +00:00
/// Returns a localized version of the specified string within the specified context.
/// The view is guaranteed to be valid until the next language change.
/// NOTE: When passing this to fmt, positional arguments should be used in the base string, as
/// not all locales follow the same word ordering.
2024-05-05 10:21:54 +00:00
std::string_view TranslateToStringView(std::string_view context, std::string_view msg);
2023-08-27 06:00:06 +00:00
/// Returns a localized version of the specified string within the specified context.
2024-05-05 10:21:54 +00:00
std::string TranslateToString(std::string_view context, std::string_view msg);
2023-08-27 06:00:06 +00:00
/// Clears the translation cache. All previously used strings should be considered invalid.
void ClearTranslationCache();
namespace Internal {
/// Implementation to retrieve a translated string.
2024-05-05 10:21:54 +00:00
s32 GetTranslatedStringImpl(std::string_view context, std::string_view msg, char* tbuf, size_t tbuf_space);
2023-08-27 06:00:06 +00:00
} // namespace Internal
} // namespace Host
// Helper macros for retrieving translated strings.
#define TRANSLATE(context, msg) Host::TranslateToCString(context, msg)
#define TRANSLATE_SV(context, msg) Host::TranslateToStringView(context, msg)
#define TRANSLATE_STR(context, msg) Host::TranslateToString(context, msg)
#define TRANSLATE_FS(context, msg) fmt::runtime(Host::TranslateToStringView(context, msg))
// Does not translate the string at runtime, but allows the UI to in its own way.
#define TRANSLATE_NOOP(context, msg) msg