diff --git a/src/common/utility_unix.cpp b/src/common/utility_unix.cpp index fc7d56058b695..555a37531cc98 100644 --- a/src/common/utility_unix.cpp +++ b/src/common/utility_unix.cpp @@ -26,17 +26,28 @@ QVector Utility::queryProcessInfosKeepingFileO void Utility::setupFavLink(const QString &folder) { - // Nautilus: add to ~/.gtk-bookmarks + // Nautilus: add to ~/.config/gtk-3.0/bookmarks QFile gtkBookmarks(QDir::homePath() + QLatin1String("/.config/gtk-3.0/bookmarks")); - QByteArray folderUrl = "file://" + folder.toUtf8(); - if (gtkBookmarks.open(QFile::ReadWrite)) { - QByteArray places = gtkBookmarks.readAll(); - if (!places.contains(folderUrl)) { - places += folderUrl; - gtkBookmarks.reset(); - gtkBookmarks.write(places + '\n'); - } + const auto folderUrl = QUrl::fromLocalFile(folder).toEncoded(); + if (!gtkBookmarks.open(QFile::ReadWrite)) { + qCWarning(lcUtility).nospace() << "failed to set up fav link" + << " folder=" << folder + << " error=" << gtkBookmarks.error() + << " errorString=" << gtkBookmarks.errorString(); + return; + } + + auto places = gtkBookmarks.readAll(); + if (places.contains(folderUrl)) { + qCDebug(lcUtility).nospace() << "fav link already exists" + << " folder=" << folder + << " folderUrl=" << folderUrl; + return; } + + places += folderUrl; + gtkBookmarks.reset(); + gtkBookmarks.write(places + '\n'); } void Utility::removeFavLink(const QString &folder) diff --git a/test/testutility.cpp b/test/testutility.cpp index 0e10fa0ffb2bb..8cb0b1990afae 100644 --- a/test/testutility.cpp +++ b/test/testutility.cpp @@ -347,6 +347,47 @@ private slots: QCOMPARE(fullRepotePathResult, fullRemotePathOriginal); } } + +#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) + void testSetupFavLink() + { + // avoid polluting user bookmarks + const auto originalHome = qgetenv("HOME"); + QTemporaryDir tempHome; + qputenv("HOME", tempHome.path().toLocal8Bit()); + QCOMPARE(QDir::homePath(), tempHome.path()); + + QDir(tempHome.path()).mkpath(".config/gtk-3.0"); + const auto bookmarksFilePath = tempHome.filePath(".config/gtk-3.0/bookmarks"); + + const auto countEntries = [&bookmarksFilePath](const QString &expectedEntry) -> qsizetype { + QFile gtkBookmarks(bookmarksFilePath); + if (!gtkBookmarks.open(QFile::ReadOnly)) { + qCritical() << "opening file" << bookmarksFilePath << "failed with" << gtkBookmarks.errorString(); + return -1; + } + + const auto places = gtkBookmarks.readAll().split('\n'); + return places.count(expectedEntry); + }; + + const auto setupAndCheckFavLink = [&countEntries](const QString &folder, const QString &expectedEntry) -> void { + OCC::Utility::setupFavLink(folder); + QCOMPARE(countEntries(expectedEntry), 1); + // ensure duplicates aren't created + OCC::Utility::setupFavLink(folder); + QCOMPARE(countEntries(expectedEntry), 1); + }; + + setupAndCheckFavLink("/tmp/test", "file:///tmp/test"); + setupAndCheckFavLink("/tmp/test with spaces", "file:///tmp/test%20with%20spaces"); + setupAndCheckFavLink("/tmp/special! characters & more/subpath with space", "file:///tmp/special!%20characters%20&%20more/subpath%20with%20space"); + + // restore defaults for other tests + qputenv("HOME", originalHome); + QCOMPARE(QDir::homePath(), originalHome); + } +#endif }; QTEST_GUILESS_MAIN(TestUtility)