| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/ConfigControls/ConfigUserPath.h" |
| 5 | |
| 6 | #include <QLineEdit> |
| 7 | #include <QString> |
| 8 | |
| 9 | #include "Common/FileUtil.h" |
| 10 | |
| 11 | #include "DolphinQt/Config/ConfigControls/ConfigText.h" |
| 12 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 13 | |
| 14 | ConfigUserPath::ConfigUserPath(const unsigned int dir_index, |
| 15 | const Config::Info<std::string>& setting) |
| 16 | : ConfigUserPath(dir_index, setting, nullptr) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | ConfigUserPath::ConfigUserPath(const unsigned int dir_index, |
| 21 | const Config::Info<std::string>& setting, Config::Layer* layer) |
| 22 | : ConfigText(setting, layer), m_dir_index(dir_index) |
| 23 | { |
| 24 | OnConfigChanged(); |
| 25 | |
| 26 | connect(this, &QLineEdit::editingFinished, this, &ConfigUserPath::Update); |
| 27 | } |
| 28 | |
| 29 | // Display the effective path: if Config has a value, use it; otherwise fall back to UserPath. |
| 30 | // Config values only serve to initialize UserPath at startup, an empty config meaning "use the |
| 31 | // current UserPath". |
| 32 | void ConfigUserPath::RefreshText() |
| 33 | { |
| 34 | const std::string config_value = ReadValue(m_setting); |
| 35 | const std::string userpath_value = File::GetUserPath(m_dir_index); |
| 36 | const QString text = QString::fromStdString(config_value.empty() ? userpath_value : config_value); |
| 37 | setText(text); |
| 38 | } |
| 39 | |
| 40 | void ConfigUserPath::Update() |
| 41 | { |
| 42 | const QString new_text = text().trimmed(); |
| 43 | if (new_text.isEmpty()) |
| 44 | { |
| 45 | ModalMessageBox::warning(this, tr("Empty Value"), |
| 46 | tr("This field cannot be left empty. Please enter a value.")); |
| 47 | RefreshText(); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | const std::string value = new_text.toStdString(); |
| 52 | File::SetUserPath(m_dir_index, value); |
| 53 | SaveValue(m_setting, value); |
| 54 | } |
| 55 | |
| 56 | void ConfigUserPath::OnConfigChanged() |
| 57 | { |
| 58 | RefreshText(); |
| 59 | } |