From dc7cbda0265c7a00b733968c95e956fb90984c6d Mon Sep 17 00:00:00 2001 From: DanielSvoboda Date: Wed, 11 Oct 2023 15:33:24 -0300 Subject: [PATCH] Improvement in Directory Path Detection for Shortcuts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This pull request updates how the directory path for shortcuts is determined. The main changes are: 1. Replaced the use of environment variables to determine the path of the desktop and applications menu with `QStandardPaths::writableLocation`. This change addresses an issue where the desktop path was not correctly identified when its location was customized, as shown in the attached screenshot. 2. Added conversion from `QString` to `std::string` using `toUtf8()`, which correctly handles non-ASCII characters in directory paths. This change ensures that directory paths containing Portuguese words like "Área de trabalho" are supported. 3. Replaced directory checking using `Common::FS::IsDir()` with `QDir::exists()`. These changes should improve cross-platform compatibility and code robustness. Because it couldn't locate my desktop, which wasn't on the C drive, but on the F, and even though localization wouldn't work because it was setting it to find the 'Desktop' folder and in the computer's language it says 'Área de trabalho', that will fix for other languages too. --- src/yuzu/main.cpp | 61 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 5427758c1d..eab6c39760 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -81,6 +81,8 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual #include #include #include +#include +#include #ifdef HAVE_SDL2 #include // For SDL ScreenSaver functions @@ -2869,38 +2871,37 @@ void GMainWindow::OnGameListCreateShortcut(u64 program_id, const std::string& ga #endif // __linux__ std::filesystem::path target_directory{}; - // Determine target directory for shortcut -#if defined(WIN32) - const char* home = std::getenv("USERPROFILE"); -#else - const char* home = std::getenv("HOME"); -#endif - const std::filesystem::path home_path = (home == nullptr ? "~" : home); - const char* xdg_data_home = std::getenv("XDG_DATA_HOME"); - if (target == GameListShortcutTarget::Desktop) { - target_directory = home_path / "Desktop"; - if (!Common::FS::IsDir(target_directory)) { - QMessageBox::critical( - this, tr("Create Shortcut"), - tr("Cannot create shortcut on desktop. Path \"%1\" does not exist.") - .arg(QString::fromStdString(target_directory.generic_string())), - QMessageBox::StandardButton::Ok); - return; - } - } else if (target == GameListShortcutTarget::Applications) { - target_directory = (xdg_data_home == nullptr ? home_path / ".local/share" : xdg_data_home) / - "applications"; - if (!Common::FS::CreateDirs(target_directory)) { - QMessageBox::critical( - this, tr("Create Shortcut"), - tr("Cannot create shortcut in applications menu. Path \"%1\" " - "does not exist and cannot be created.") - .arg(QString::fromStdString(target_directory.generic_string())), - QMessageBox::StandardButton::Ok); - return; - } +// Determine target directory for shortcut +if (target == GameListShortcutTarget::Desktop) { + QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + target_directory = desktopPath.toUtf8().toStdString(); + QDir dir(QString::fromStdString(target_directory.generic_string())); + if (!dir.exists()) { + QMessageBox::critical( + this, tr("Create Shortcut"), + tr("Cannot create shortcut on desktop. Path \"%1\" does not exist.") + .arg(QString::fromStdString(target_directory.generic_string())), + QMessageBox::StandardButton::Ok); + return; + } +} else if (target == GameListShortcutTarget::Applications) { + #if defined(__linux__) + QString applicationsPath = + QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); + target_directory = applicationsPath.toUtf8().toStdString(); + QDir dir(QString::fromStdString(target_directory.generic_string())); + if (!dir.exists()) { + QMessageBox::critical( + this, tr("Create Shortcut"), + tr("Cannot create shortcut in applications menu. Path \"%1\" " + "does not exist and cannot be created.") + .arg(QString::fromStdString(target_directory.generic_string())), + QMessageBox::StandardButton::Ok); + return; } + #endif +} const std::string game_file_name = std::filesystem::path(game_path).filename().string(); // Determine full paths for icon and shortcut