host.h (4687B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.0) 3 4 #pragma once 5 6 #include "common/heap_array.h" 7 #include "common/small_string.h" 8 #include "common/types.h" 9 10 #include <ctime> 11 #include <optional> 12 #include <string> 13 #include <string_view> 14 15 namespace Host { 16 /// Returns true if the specified resource file exists. 17 bool ResourceFileExists(std::string_view filename, bool allow_override); 18 19 /// Reads a file from the resources directory of the application. 20 /// This may be outside of the "normal" filesystem on platforms such as Mac. 21 std::optional<DynamicHeapArray<u8>> ReadResourceFile(std::string_view filename, bool allow_override); 22 23 /// Reads a resource file file from the resources directory as a string. 24 std::optional<std::string> ReadResourceFileToString(std::string_view filename, bool allow_override); 25 26 /// Returns the modified time of a resource. 27 std::optional<std::time_t> GetResourceFileTimestamp(std::string_view filename, bool allow_override); 28 29 /// Reads a potentially-compressed file from the resources directory of the application. 30 std::optional<DynamicHeapArray<u8>> ReadCompressedResourceFile(std::string_view filename, bool allow_override); 31 32 /// Reports a fatal error on the main thread. This does not assume that the main window exists, 33 /// unlike ReportErrorAsync(), and will exit the application after the popup is closed. 34 void ReportFatalError(std::string_view title, std::string_view message); 35 36 /// Displays an asynchronous error on the UI thread, i.e. doesn't block the caller. 37 void ReportErrorAsync(std::string_view title, std::string_view message); 38 39 /// Displays a synchronous confirmation on the UI thread, i.e. blocks the caller. 40 bool ConfirmMessage(std::string_view title, std::string_view message); 41 42 /// Returns the user agent to use for HTTP requests. 43 std::string GetHTTPUserAgent(); 44 45 /// Opens a URL, using the default application. 46 void OpenURL(std::string_view url); 47 48 /// Copies the provided text to the host's clipboard, if present. 49 bool CopyTextToClipboard(std::string_view text); 50 51 /// Returns a localized version of the specified string within the specified context. 52 /// The pointer is guaranteed to be valid until the next language change. 53 const char* TranslateToCString(std::string_view context, std::string_view msg); 54 55 /// Returns a localized version of the specified string within the specified context. 56 /// The view is guaranteed to be valid until the next language change. 57 /// NOTE: When passing this to fmt, positional arguments should be used in the base string, as 58 /// not all locales follow the same word ordering. 59 std::string_view TranslateToStringView(std::string_view context, std::string_view msg); 60 61 /// Returns a localized version of the specified string within the specified context. 62 std::string TranslateToString(std::string_view context, std::string_view msg); 63 64 /// Returns a localized version of the specified string within the specified context, adjusting for plurals using %n. 65 std::string TranslatePluralToString(const char* context, const char* msg, const char* disambiguation, int count); 66 SmallString TranslatePluralToSmallString(const char* context, const char* msg, const char* disambiguation, int count); 67 68 /// Clears the translation cache. All previously used strings should be considered invalid. 69 void ClearTranslationCache(); 70 71 namespace Internal { 72 /// Implementation to retrieve a translated string. 73 s32 GetTranslatedStringImpl(std::string_view context, std::string_view msg, char* tbuf, size_t tbuf_space); 74 } // namespace Internal 75 } // namespace Host 76 77 // Helper macros for retrieving translated strings. 78 #define TRANSLATE(context, msg) Host::TranslateToCString(context, msg) 79 #define TRANSLATE_SV(context, msg) Host::TranslateToStringView(context, msg) 80 #define TRANSLATE_STR(context, msg) Host::TranslateToString(context, msg) 81 #define TRANSLATE_FS(context, msg) fmt::runtime(Host::TranslateToStringView(context, msg)) 82 #define TRANSLATE_PLURAL_STR(context, msg, disambiguation, count) \ 83 Host::TranslatePluralToString(context, msg, disambiguation, count) 84 #define TRANSLATE_PLURAL_SSTR(context, msg, disambiguation, count) \ 85 Host::TranslatePluralToSmallString(context, msg, disambiguation, count) 86 #define TRANSLATE_PLURAL_FS(context, msg, disambiguation, count) \ 87 fmt::runtime(Host::TranslatePluralToSmallString(context, msg, disambiguation, count).view()) 88 89 // Does not translate the string at runtime, but allows the UI to in its own way. 90 #define TRANSLATE_NOOP(context, msg) msg