| 1 |
/* |
| 2 |
* Copyright (C) 2015-2024 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "KeyboardLayoutManager.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "URL.h" |
| 14 |
#include "filesystem/Directory.h" |
| 15 |
#include "settings/lib/Setting.h" |
| 16 |
#include "settings/lib/SettingDefinitions.h" |
| 17 |
#include "utils/XBMCTinyXML2.h" |
| 18 |
#include "utils/log.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <cstring> |
| 22 |
|
| 23 |
using namespace KODI; |
| 24 |
using namespace KEYBOARD; |
| 25 |
|
| 26 |
#define KEYBOARD_LAYOUTS_PATH "special://xbmc/system/keyboardlayouts" |
| 27 |
|
| 28 |
CKeyboardLayoutManager::~CKeyboardLayoutManager() |
| 29 |
{ |
| 30 |
Unload(); |
| 31 |
} |
| 32 |
|
| 33 |
bool CKeyboardLayoutManager::Load(const std::string& path /* = "" */) |
| 34 |
{ |
| 35 |
std::string layoutDirectory = path; |
| 36 |
if (layoutDirectory.empty()) |
| 37 |
layoutDirectory = KEYBOARD_LAYOUTS_PATH; |
| 38 |
|
| 39 |
if (!XFILE::CDirectory::Exists(layoutDirectory)) |
| 40 |
{ |
| 41 |
CLog::Log(LOGWARNING, |
| 42 |
"CKeyboardLayoutManager: unable to load keyboard layouts from non-existing directory " |
| 43 |
"\"{}\"", |
| 44 |
layoutDirectory); |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
CFileItemList layouts; |
| 49 |
if (!XFILE::CDirectory::GetDirectory(CURL(layoutDirectory), layouts, ".xml", |
| 50 |
XFILE::DIR_FLAG_DEFAULTS) || |
| 51 |
layouts.IsEmpty()) |
| 52 |
{ |
| 53 |
CLog::Log(LOGWARNING, "CKeyboardLayoutManager: no keyboard layouts found in {}", |
| 54 |
layoutDirectory); |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
CLog::Log(LOGINFO, "CKeyboardLayoutManager: loading keyboard layouts from {}...", |
| 59 |
layoutDirectory); |
| 60 |
size_t oldLayoutCount = m_layouts.size(); |
| 61 |
for (int i = 0; i < layouts.Size(); i++) |
| 62 |
{ |
| 63 |
std::string layoutPath = layouts[i]->GetPath(); |
| 64 |
if (layoutPath.empty()) |
| 65 |
continue; |
| 66 |
|
| 67 |
CXBMCTinyXML2 xmlDoc; |
| 68 |
if (!xmlDoc.LoadFile(layoutPath)) |
| 69 |
{ |
| 70 |
CLog::Log(LOGWARNING, "CKeyboardLayoutManager: unable to open {}", layoutPath); |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
const auto* rootElement = xmlDoc.RootElement(); |
| 75 |
if (rootElement == nullptr) |
| 76 |
{ |
| 77 |
CLog::Log(LOGWARNING, "CKeyboardLayoutManager: missing or invalid XML root element in {}", |
| 78 |
layoutPath); |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
if (std::strcmp(rootElement->Value(), "keyboardlayouts") != 0) |
| 83 |
{ |
| 84 |
CLog::Log(LOGWARNING, "CKeyboardLayoutManager: unexpected XML root element \"{}\" in {}", |
| 85 |
rootElement->Value(), layoutPath); |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
const auto* layoutElement = rootElement->FirstChildElement("layout"); |
| 90 |
while (layoutElement != nullptr) |
| 91 |
{ |
| 92 |
CKeyboardLayout layout; |
| 93 |
if (!layout.Load(layoutElement)) |
| 94 |
CLog::Log(LOGWARNING, "CKeyboardLayoutManager: failed to load {}", layoutPath); |
| 95 |
else if (m_layouts.find(layout.GetIdentifier()) != m_layouts.end()) |
| 96 |
CLog::Log(LOGWARNING, |
| 97 |
"CKeyboardLayoutManager: duplicate layout with identifier \"{}\" in {}", |
| 98 |
layout.GetIdentifier(), layoutPath); |
| 99 |
else |
| 100 |
{ |
| 101 |
CLog::Log(LOGDEBUG, "CKeyboardLayoutManager: keyboard layout \"{}\" successfully loaded", |
| 102 |
layout.GetIdentifier()); |
| 103 |
m_layouts.insert(std::make_pair(layout.GetIdentifier(), layout)); |
| 104 |
} |
| 105 |
|
| 106 |
layoutElement = layoutElement->NextSiblingElement(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return m_layouts.size() > oldLayoutCount; |
| 111 |
} |
| 112 |
|
| 113 |
void CKeyboardLayoutManager::Unload() |
| 114 |
{ |
| 115 |
m_layouts.clear(); |
| 116 |
} |
| 117 |
|
| 118 |
bool CKeyboardLayoutManager::GetLayout(const std::string& name, CKeyboardLayout& layout) const |
| 119 |
{ |
| 120 |
if (name.empty()) |
| 121 |
return false; |
| 122 |
|
| 123 |
KeyboardLayouts::const_iterator it = m_layouts.find(name); |
| 124 |
if (it == m_layouts.end()) |
| 125 |
return false; |
| 126 |
|
| 127 |
layout = it->second; |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
namespace |
| 132 |
{ |
| 133 |
inline bool LayoutSort(const StringSettingOption& i, const StringSettingOption& j) |
| 134 |
{ |
| 135 |
return (i.value < j.value); |
| 136 |
} |
| 137 |
} // namespace |
| 138 |
|
| 139 |
void CKeyboardLayoutManager::SettingOptionsKeyboardLayoutsFiller( |
| 140 |
const SettingConstPtr& setting, |
| 141 |
std::vector<StringSettingOption>& list, |
| 142 |
std::string& current, |
| 143 |
void* data) |
| 144 |
{ |
| 145 |
for (const auto& it : CServiceBroker::GetKeyboardLayoutManager()->m_layouts) |
| 146 |
{ |
| 147 |
std::string name = it.second.GetName(); |
| 148 |
list.emplace_back(name, name); |
| 149 |
} |
| 150 |
|
| 151 |
std::sort(list.begin(), list.end(), LayoutSort); |
| 152 |
} |