Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/common/utility_unix.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud GmbH
Expand Down Expand Up @@ -26,17 +26,28 @@

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)
Expand Down
41 changes: 41 additions & 0 deletions test/testutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* any purpose.
*/

#include <QtTest>

Check failure on line 11 in test/testutility.cpp

View workflow job for this annotation

GitHub Actions / build

test/testutility.cpp:11:10 [clang-diagnostic-error]

'QtTest' file not found
#include <QTemporaryDir>

#include "common/utility.h"
Expand Down Expand Up @@ -347,6 +347,47 @@
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)
Expand Down
Loading