Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "shortcutsconfiguration.h"
23 :
24 : #include "settings.h"
25 : #include "io/path.h"
26 :
27 : #include "global/configreader.h"
28 :
29 : #include "log.h"
30 :
31 : using namespace muse;
32 : using namespace muse::shortcuts;
33 :
34 : static const muse::Settings::Key CURRENT_SHORTCUTS_PRESET_KEY("shortcuts", "shortcuts/currentPreset");
35 :
36 0 : void ShortcutsConfiguration::init()
37 : {
38 0 : m_config = ConfigReader::read(":/configs/shortcuts.cfg");
39 :
40 0 : settings()->setDefaultValue(CURRENT_SHORTCUTS_PRESET_KEY, Val(""));
41 0 : settings()->valueChanged(CURRENT_SHORTCUTS_PRESET_KEY).onReceive(this, [this](const Val& name) {
42 0 : m_currentPresetNameChanged.send(name.toString());
43 0 : });
44 :
45 0 : fileSystem()->makePath(userShortcutsDirPath());
46 0 : }
47 :
48 0 : QString ShortcutsConfiguration::currentKeyboardLayout() const
49 : {
50 0 : NOT_IMPLEMENTED;
51 0 : return "US-QWERTY";
52 : }
53 :
54 0 : void ShortcutsConfiguration::setCurrentKeyboardLayout(const QString& layout)
55 : {
56 : UNUSED(layout);
57 0 : NOT_IMPLEMENTED;
58 0 : return;
59 : }
60 :
61 0 : io::path_t ShortcutsConfiguration::shortcutsUserAppDataPath() const
62 : {
63 0 : return globalConfiguration()->userAppDataPath() + "/shortcuts.xml";
64 : }
65 :
66 0 : io::path_t ShortcutsConfiguration::shortcutsAppDataPath() const
67 : {
68 : #if defined(Q_OS_MACOS)
69 : return m_config.value("shortcuts_mac").toPath();
70 : #else
71 0 : return m_config.value("shortcuts").toPath();
72 : #endif
73 : }
74 :
75 0 : std::string ShortcutsConfiguration::defaultShortcutsName() const
76 : {
77 : #if defined(Q_OS_MACOS)
78 : return "shortcuts_mac";
79 : #else
80 0 : return "shortcuts";
81 : #endif
82 : }
83 :
84 0 : std::vector<std::string> ShortcutsConfiguration::availableShortcutsPresets() const
85 : {
86 0 : std::vector<std::string> result;
87 :
88 0 : ValList presets = m_config.value("command_shortcuts_presets").toList();
89 0 : for (const Val& preset : presets) {
90 0 : result.push_back(preset.toString());
91 : }
92 :
93 0 : return result;
94 0 : }
95 :
96 0 : std::string ShortcutsConfiguration::currentShortcutsPresetName() const
97 : {
98 0 : return settings()->value(CURRENT_SHORTCUTS_PRESET_KEY).toString();
99 : }
100 :
101 0 : void ShortcutsConfiguration::setCurrentShortcutsPresetName(const std::string& name)
102 : {
103 0 : settings()->setSharedValue(CURRENT_SHORTCUTS_PRESET_KEY, Val(name));
104 0 : }
105 :
106 0 : async::Channel<std::string> ShortcutsConfiguration::currentShortcutsPresetNameChanged() const
107 : {
108 0 : return m_currentPresetNameChanged;
109 : }
110 :
111 0 : io::path_t ShortcutsConfiguration::userShortcutsDirPath() const
112 : {
113 0 : return globalConfiguration()->userAppDataPath() + "/shortcuts";
114 : }
115 :
116 0 : io::path_t ShortcutsConfiguration::commandShortcutsUserAppDataPath(const std::string& shortcutsName) const
117 : {
118 0 : if (!io::isAllowedFileName(shortcutsName)) {
119 0 : LOGE() << "invalid shortcuts name: " << shortcutsName;
120 0 : return io::path_t();
121 : }
122 :
123 0 : return userShortcutsDirPath() + "/" + shortcutsName + ".json";
124 : }
125 :
126 0 : io::path_t ShortcutsConfiguration::commandShortcutsAppDataPath(const std::string& shortcutsName) const
127 : {
128 0 : return m_config.value("command_" + shortcutsName).toPath();
129 : }
|