Improvement in Directory Path Detection for Shortcuts (#11749)

* Improvement in Directory Path Detection for Shortcuts

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.

* Update main.cpp

* formatting

* Update src/yuzu/main.cpp

Co-authored-by: Tobias <thm.frey@gmail.com>

* Update src/yuzu/main.cpp

Co-authored-by: Tobias <thm.frey@gmail.com>

* Update main.cpp

* Update main.cpp

* Update main.cpp

desktopPath > desktop_Path
applicationsPath > applications_Path

* Update main.cpp

* formatting

* Update main.cpp

This code will attempt to use QStandardPaths to find the applications directory. If that fails, it will resort to using the ~/.local/share/applications directory, which is a common location for application shortcuts in Linux.

* Update main.cpp

* formatting

---------

Co-authored-by: Tobias <thm.frey@gmail.com>
This commit is contained in:
DanielSvoboda 2023-10-13 12:57:49 -03:00 committed by GitHub
parent 1a4874e178
commit 56e5d99684
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -67,6 +67,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual
#define QT_NO_OPENGL #define QT_NO_OPENGL
#include <QClipboard> #include <QClipboard>
#include <QDesktopServices> #include <QDesktopServices>
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileDialog> #include <QFileDialog>
#include <QInputDialog> #include <QInputDialog>
@ -76,6 +77,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual
#include <QPushButton> #include <QPushButton>
#include <QScreen> #include <QScreen>
#include <QShortcut> #include <QShortcut>
#include <QStandardPaths>
#include <QStatusBar> #include <QStatusBar>
#include <QString> #include <QString>
#include <QSysInfo> #include <QSysInfo>
@ -2869,44 +2871,50 @@ void GMainWindow::OnGameListCreateShortcut(u64 program_id, const std::string& ga
#endif // __linux__ #endif // __linux__
std::filesystem::path target_directory{}; 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) { switch (target) {
target_directory = home_path / "Desktop"; case GameListShortcutTarget::Desktop: {
if (!Common::FS::IsDir(target_directory)) { const QString desktop_path =
QMessageBox::critical( QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
this, tr("Create Shortcut"), target_directory = desktop_path.toUtf8().toStdString();
tr("Cannot create shortcut on desktop. Path \"%1\" does not exist.") break;
.arg(QString::fromStdString(target_directory.generic_string())), }
QMessageBox::StandardButton::Ok); case GameListShortcutTarget::Applications: {
return; const QString applications_path =
} QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
} else if (target == GameListShortcutTarget::Applications) { if (applications_path.isEmpty()) {
target_directory = (xdg_data_home == nullptr ? home_path / ".local/share" : xdg_data_home) / const char* home = std::getenv("HOME");
"applications"; if (home != nullptr) {
if (!Common::FS::CreateDirs(target_directory)) { target_directory = std::filesystem::path(home) / ".local/share/applications";
QMessageBox::critical( }
this, tr("Create Shortcut"), } else {
tr("Cannot create shortcut in applications menu. Path \"%1\" " target_directory = applications_path.toUtf8().toStdString();
"does not exist and cannot be created.")
.arg(QString::fromStdString(target_directory.generic_string())),
QMessageBox::StandardButton::Ok);
return;
} }
break;
}
default:
return;
}
const QDir dir(QString::fromStdString(target_directory.generic_string()));
if (!dir.exists()) {
QMessageBox::critical(this, tr("Create Shortcut"),
tr("Cannot create shortcut. Path \"%1\" does not exist.")
.arg(QString::fromStdString(target_directory.generic_string())),
QMessageBox::StandardButton::Ok);
return;
} }
const std::string game_file_name = std::filesystem::path(game_path).filename().string(); const std::string game_file_name = std::filesystem::path(game_path).filename().string();
// Determine full paths for icon and shortcut // Determine full paths for icon and shortcut
#if defined(__linux__) || defined(__FreeBSD__) #if defined(__linux__) || defined(__FreeBSD__)
const char* home = std::getenv("HOME");
const std::filesystem::path home_path = (home == nullptr ? "~" : home);
const char* xdg_data_home = std::getenv("XDG_DATA_HOME");
std::filesystem::path system_icons_path = std::filesystem::path system_icons_path =
(xdg_data_home == nullptr ? home_path / ".local/share/" : xdg_data_home) / (xdg_data_home == nullptr ? home_path / ".local/share/"
: std::filesystem::path(xdg_data_home)) /
"icons/hicolor/256x256"; "icons/hicolor/256x256";
if (!Common::FS::CreateDirs(system_icons_path)) { if (!Common::FS::CreateDirs(system_icons_path)) {
QMessageBox::critical( QMessageBox::critical(