| Flutter Windows Embedder |

system_utils.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/windows/system_utils.h"
6
7 #include <Windows.h>
8
9 #include <sstream>
10
11 #include "flutter/fml/platform/win/wstring_conversion.h"
12
13 namespace flutter {
14
15 std::vector<LanguageInfo> GetPreferredLanguageInfo(
16 const WindowsProcTable& windows_proc_table) {
17 std::vector<std::wstring> languages =
18 GetPreferredLanguages(windows_proc_table);
19 std::vector<LanguageInfo> language_info;
20 language_info.reserve(languages.size());
21
22 for (auto language : languages) {
23 language_info.push_back(ParseLanguageName(language));
24 }
25 return language_info;
26 }
27
28 std::wstring GetPreferredLanguagesFromMUI(
29 const WindowsProcTable& windows_proc_table) {
30 ULONG buffer_size = 0;
31 ULONG count = 0;
32 DWORD flags = MUI_LANGUAGE_NAME | MUI_UI_FALLBACK;
33 if (!windows_proc_table.GetThreadPreferredUILanguages(flags, &count, nullptr,
34 &buffer_size)) {
35 return std::wstring();
36 }
37 std::wstring buffer(buffer_size, '\0');
38 if (!windows_proc_table.GetThreadPreferredUILanguages(
39 flags, &count, buffer.data(), &buffer_size)) {
40 return std::wstring();
41 }
42 return buffer;
43 }
44
45 std::vector<std::wstring> GetPreferredLanguages(
46 const WindowsProcTable& windows_proc_table) {
47 std::vector<std::wstring> languages;
48
49 // Initialize the buffer
50 std::wstring buffer = GetPreferredLanguagesFromMUI(windows_proc_table);
51
52 // Extract the individual languages from the buffer.
53 size_t start = 0;
54 while (true) {
55 // The buffer is terminated by an empty string (i.e., a double null).
56 if (buffer[start] == L'\0') {
57 break;
58 }
59 // Read the next null-terminated language.
60 std::wstring language(buffer.c_str() + start);
61 if (language.empty()) {
62 break;
63 }
64 languages.push_back(language);
65 // Skip past that language and its terminating null in the buffer.
66 start += language.size() + 1;
67 }
68 return languages;
69 }
70
71 LanguageInfo ParseLanguageName(std::wstring language_name) {
72 LanguageInfo info;
73
74 // Split by '-', discarding any suplemental language info (-x-foo).
75 std::vector<std::string> components;
76 std::istringstream stream(fml::WideStringToUtf8(language_name));
77 std::string component;
78 while (getline(stream, component, '-')) {
79 if (component == "x") {
80 break;
81 }
82 components.push_back(component);
83 }
84
85 // Determine which components are which.
86 info.language = components[0];
87 if (components.size() == 3) {
88 info.script = components[1];
89 info.region = components[2];
90 } else if (components.size() == 2) {
91 // A script code will always be four characters long.
92 if (components[1].size() == 4) {
93 info.script = components[1];
94 } else {
95 info.region = components[1];
96 }
97 }
98 return info;
99 }
100
101 std::wstring GetUserTimeFormat() {
102 // Rather than do the call-allocate-call-free dance, just use a sufficiently
103 // large buffer to handle any reasonable time format string.
104 const int kBufferSize = 100;
105 wchar_t buffer[kBufferSize];
106 if (::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_STIMEFORMAT, buffer,
107 kBufferSize) == 0) {
108 return std::wstring();
109 }
110 return std::wstring(buffer, kBufferSize);
111 }
112
113 bool Prefer24HourTime(std::wstring time_format) {
114 return time_format.find(L"H") != std::wstring::npos;
115 }
116
117 } // namespace flutter
flutter::LanguageInfo::script
std::string script
Definition: system_utils.h:26
flutter::GetPreferredLanguagesFromMUI
std::wstring GetPreferredLanguagesFromMUI(const WindowsProcTable &windows_proc_table)
Definition: system_utils.cc:28
flutter::GetPreferredLanguageInfo
std::vector< LanguageInfo > GetPreferredLanguageInfo(const WindowsProcTable &windows_proc_table)
Definition: system_utils.cc:15
flutter::GetUserTimeFormat
std::wstring GetUserTimeFormat()
Definition: system_utils.cc:101
system_utils.h
flutter::Prefer24HourTime
bool Prefer24HourTime(std::wstring time_format)
Definition: system_utils.cc:113
flutter::ParseLanguageName
LanguageInfo ParseLanguageName(std::wstring language_name)
Definition: system_utils.cc:71
flutter::LanguageInfo::language
std::string language
Definition: system_utils.h:24
flutter::LanguageInfo
Definition: system_utils.h:23
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::WindowsProcTable::GetThreadPreferredUILanguages
virtual LRESULT GetThreadPreferredUILanguages(DWORD flags, PULONG count, PZZWSTR languages, PULONG length) const
Definition: windows_proc_table.cc:31
flutter::WindowsProcTable
Definition: windows_proc_table.h:17
flutter::LanguageInfo::region
std::string region
Definition: system_utils.h:25
flutter::GetPreferredLanguages
std::vector< std::wstring > GetPreferredLanguages(const WindowsProcTable &windows_proc_table)
Definition: system_utils.cc:45