From f1438b2b3eec8a1c60532ae5d9fcd654138dc5e4 Mon Sep 17 00:00:00 2001 From: tsujan Date: Tue, 21 Jun 2022 11:14:03 +0430 Subject: [PATCH] Prevent an empty desktop path (#1601) Fall back to `$HOME/Desktop` if `~/.config/user-dirs.dirs` doesn't exist or doesn't contain a desktop path. But if `~/.config/user-dirs.dirs` contains an empty desktop path, accept it. See https://github.com/lxqt/lxqt-session/issues/439 for the story. --- pcmanfm/desktoppreferencesdialog.cpp | 1 - pcmanfm/xdgdir.cpp | 34 ++++++++++++++++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/pcmanfm/desktoppreferencesdialog.cpp b/pcmanfm/desktoppreferencesdialog.cpp index b25f6435..465095c6 100644 --- a/pcmanfm/desktoppreferencesdialog.cpp +++ b/pcmanfm/desktoppreferencesdialog.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include diff --git a/pcmanfm/xdgdir.cpp b/pcmanfm/xdgdir.cpp index 9fd9a7b0..edc5a848 100644 --- a/pcmanfm/xdgdir.cpp +++ b/pcmanfm/xdgdir.cpp @@ -18,10 +18,13 @@ #include "xdgdir.h" #include +#include #include #include #include +static const QRegularExpression desktopRegex(QStringLiteral("XDG_DESKTOP_DIR=\"([^\n]*)\"")); + QString XdgDir::readUserDirsFile() { QFile file(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QStringLiteral("/user-dirs.dirs")); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -34,30 +37,33 @@ QString XdgDir::readUserDirsFile() { QString XdgDir::readDesktopDir() { QString str = readUserDirsFile(); - if(str.isEmpty()) - return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QStringLiteral("/Desktop"); - QRegExp reg(QStringLiteral("XDG_DESKTOP_DIR=\"([^\n]*)\"")); - if(reg.lastIndexIn(str) != -1) { - str = reg.cap(1); - if(str.startsWith(QStringLiteral("$HOME"))) - str = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + str.mid(5); - return str; + if(!str.isEmpty()) { + QRegularExpressionMatch match; + if(str.lastIndexOf(desktopRegex, -1, &match) != -1) { + str = match.captured(1); + if(str.startsWith(QStringLiteral("$HOME"))) { + str = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + str.mid(5); + } + return str; + } } - return QString(); + return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QStringLiteral("/Desktop"); } void XdgDir::setDesktopDir(QString path) { QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); - if(path.startsWith(home)) + if(path.startsWith(home)) { path = QStringLiteral("$HOME") + path.mid(home.length()); + } QString str = readUserDirsFile(); - QRegExp reg(QStringLiteral("XDG_DESKTOP_DIR=\"([^\n]*)\"")); QString line = QStringLiteral("XDG_DESKTOP_DIR=\"") + path + QLatin1Char('\"'); - if(reg.indexIn(str) != -1) - str.replace(reg, line); + if(str.contains(desktopRegex)) { + str.replace(desktopRegex, line); + } else { - if(!str.endsWith(QLatin1Char('\n'))) + if(!str.endsWith(QLatin1Char('\n'))) { str += QLatin1Char('\n'); + } str += line + QLatin1Char('\n'); } QString dir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);