fix(macOS): pick sync folders with the native panel to keep their bookmark

The account wizard and the add folder wizard chose the local folder with
QFileDialog. In the sandboxed app Qt 6.11 releases the folder's access
right after the dialog closes, so nothing outside Qt's file classes can
reach it and no bookmark can be made for it.

Use Mac::SandboxFolderPicker on macOS, which creates the bookmark from
the panel's URL, keep access while the folder is validated and created,
and pass the bookmark on to the folder definition. The panel now opens as
a sheet of the wizard's own window, activating it first, so it cannot end
up detached behind another app after the browser login.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Iva Horn <[email protected]>
This commit is contained in:
Iva Horn committed 2026-09-25 14:33:24 +02:00
1 parent 882f886981
commit b51d28a171
11 files changed
+431 -59

No files matched your search

+2
View File
@@ -929,6 +929,7 @@ void AccountSettings::slotFolderWizardAccepted()
folderWizard->field(QLatin1String("sourceFolder")).toString());
definition.targetPath = FolderDefinition::prepareTargetPath(
folderWizard->property("targetPath").toString());
definition.securityScopedBookmarkData = folderWizard->securityScopedBookmarkData();
if (folderWizard->property("useVirtualFiles").toBool()) {
definition.virtualFilesMode = bestAvailableVfsMode();
@@ -1048,6 +1049,7 @@ void AccountSettings::slotFixSandboxBookmark(Folder *folder)
const QPointer<AccountSettings> settings(this);
const QPointer<Folder> pendingFolder(folder);
Mac::SandboxFolderPicker::select(
window()->windowHandle(),
tr("Grant access to sync folder"),
expectedPath,
[settings, pendingFolder, expectedPath](Mac::SandboxFolderPicker::FolderSelection selection) {
+49 -18
View File
@@ -18,7 +18,9 @@
#include "theme.h"
#ifdef Q_OS_MACOS
#include "common/macsandboxpersistentaccess.h"
#include "common/utility_mac_sandbox.h"
#include "macOS/macsandboxfolderpicker.h"
#endif
#include <QCheckBox>
@@ -32,6 +34,7 @@
#include <QLabel>
#include <QLoggingCategory>
#include <QMessageBox>
#include <QPointer>
#include <QStandardPaths>
#include <QTreeWidget>
#include <QUrl>
@@ -143,31 +146,54 @@ bool FolderWizardLocalPath::isComplete() const
void FolderWizardLocalPath::slotChooseLocalFolder()
{
const bool isInitialSelection = _initialFolderSelection;
QString sf;
#ifdef Q_OS_MACOS
sf = Utility::getRealHomeDirectory();
#else
sf = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
#endif
#ifdef Q_OS_MACOS
// QFileDialog gives up the sandbox access to the chosen folder before returning, so only the
// native panel can provide a bookmark for it.
const QPointer<FolderWizardLocalPath> page(this);
Mac::SandboxFolderPicker::select(window()->windowHandle(),
tr("Select the source folder"),
Utility::getRealHomeDirectory(),
[page, isInitialSelection](Mac::SandboxFolderPicker::FolderSelection selection) {
if (page) {
page->applyChosenLocalFolder(selection.path, selection.bookmarkData, isInitialSelection);
}
});
#else
const auto localFolder = QFileDialog::getExistingDirectory(this,
tr("Select the source folder"),
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
QFileDialog::ShowDirsOnly);
applyChosenLocalFolder(localFolder, {}, isInitialSelection);
#endif
}
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select the source folder"),
sf,
QFileDialog::ShowDirsOnly);
if (!dir.isEmpty()) {
_ui.localFolderLineEdit->setText(QDir::toNativeSeparators(dir));
void FolderWizardLocalPath::applyChosenLocalFolder(const QString &localFolder, const QByteArray &bookmarkData, bool initialSelection)
{
if (!localFolder.isEmpty()) {
_chosenLocalFolder = localFolder;
_chosenLocalFolderBookmarkData = bookmarkData;
#ifdef Q_OS_MACOS
// Keeps sandbox access to the chosen folder while the wizard validates and creates it.
_chosenLocalFolderAccess = Utility::MacSandboxPersistentAccess::createValidFromBookmarkData(bookmarkData);
#endif
_ui.localFolderLineEdit->setText(QDir::toNativeSeparators(localFolder));
_initialFolderSelection = false;
} else {
// If this was the initial folder selection and the user canceled,
// emit signal to close the wizard
if (isInitialSelection) {
Q_EMIT initialFolderSelectionCanceled();
}
} else if (initialSelection) {
// The user cancelled the automatic initial selection, so close the wizard.
Q_EMIT initialFolderSelectionCanceled();
}
Q_EMIT completeChanged();
}
QByteArray FolderWizardLocalPath::securityScopedBookmarkData() const
{
const auto enteredFolder = FolderDefinition::prepareLocalPath(QDir::fromNativeSeparators(_ui.localFolderLineEdit->text()));
if (_chosenLocalFolder.isEmpty() || enteredFolder != FolderDefinition::prepareLocalPath(_chosenLocalFolder)) {
return {};
}
return _chosenLocalFolderBookmarkData;
}
void FolderWizardLocalPath::changeEvent(QEvent *e)
{
@@ -732,6 +758,11 @@ FolderWizard::FolderWizard(AccountPtr account, QWidget *parent)
FolderWizard::~FolderWizard() = default;
QByteArray FolderWizard::securityScopedBookmarkData() const
{
return _folderWizardSourcePage->securityScopedBookmarkData();
}
bool FolderWizard::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::LayoutRequest) {
+21
View File
@@ -11,6 +11,8 @@
#include <QNetworkReply>
#include <QTimer>
#include <memory>
#include "folder.h"
#include "accountfwd.h"
@@ -24,6 +26,11 @@ namespace OCC {
class SelectiveSyncWidget;
namespace Utility
{
class MacSandboxPersistentAccess;
}
class ownCloudInfo;
/**
@@ -54,6 +61,9 @@ public:
void setFolderMap(const Folder::Map &fm) { _folderMap = fm; }
/** @brief Bookmark of the folder chosen in the picker, empty if the entered path differs from it. */
[[nodiscard]] QByteArray securityScopedBookmarkData() const;
Q_SIGNALS:
void initialFolderSelectionCanceled();
@@ -64,12 +74,20 @@ protected Q_SLOTS:
void slotChooseLocalFolder();
private:
friend class FolderWizardLocalPathTestAccess;
void changeStyle();
void applyChosenLocalFolder(const QString &localFolder, const QByteArray &bookmarkData, bool initialSelection);
Ui_FolderWizardSourcePage _ui{};
Folder::Map _folderMap;
AccountPtr _account;
bool _initialFolderSelection = true;
QString _chosenLocalFolder;
QByteArray _chosenLocalFolderBookmarkData;
#ifdef Q_OS_MACOS
std::unique_ptr<Utility::MacSandboxPersistentAccess> _chosenLocalFolderAccess;
#endif
};
@@ -168,6 +186,9 @@ public:
bool eventFilter(QObject *watched, QEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
/** @brief Bookmark of the source folder chosen in the picker, see FolderWizardLocalPath. */
[[nodiscard]] QByteArray securityScopedBookmarkData() const;
private:
FolderWizardLocalPath *_folderWizardSourcePage;
FolderWizardRemotePath *_folderWizardTargetPage = nullptr;
+6 -2
View File
@@ -10,6 +10,8 @@
#include <functional>
class QWindow;
namespace OCC::Mac::SandboxFolderPicker
{
@@ -22,8 +24,10 @@ struct FolderSelection {
* Select a local folder with the native macOS open panel.
*
* The bookmark is created from the URL returned by NSOpenPanel before its
* security scope can be lost by converting it to a path.
* security scope can be lost by converting it to a path. The panel is shown as a
* sheet of parentWindow, which is activated first; without one it falls back to
* the key or main window.
*/
void select(const QString &caption, const QString &initialPath, std::function<void(FolderSelection)> completionHandler);
void select(QWindow *parentWindow, const QString &caption, const QString &initialPath, std::function<void(FolderSelection)> completionHandler);
} // namespace OCC::Mac::SandboxFolderPicker
+14 -6
View File
@@ -6,6 +6,7 @@
#include "macsandboxfolderpicker.h"
#include <QLoggingCategory>
#include <QWindow>
#include <utility>
@@ -15,15 +16,14 @@ Q_LOGGING_CATEGORY(lcMacSandboxFolderPicker, "nextcloud.gui.mac.sandbox.folderpi
namespace OCC::Mac::SandboxFolderPicker {
void select(const QString &caption,
const QString &initialPath,
std::function<void(FolderSelection)> completionHandler)
void select(QWindow *parentWindow, const QString &caption, const QString &initialPath, std::function<void(FolderSelection)> completionHandler)
{
qCInfo(lcMacSandboxFolderPicker) << "Opening native folder panel." << "initialPath" << initialPath;
NSOpenPanel *const panel = [NSOpenPanel openPanel];
panel.canChooseFiles = NO;
panel.canChooseDirectories = YES;
panel.canCreateDirectories = YES;
panel.allowsMultipleSelection = NO;
panel.resolvesAliases = YES;
panel.message = caption.toNSString();
@@ -34,7 +34,15 @@ void select(const QString &caption,
// Use AppKit's asynchronous panel API instead of runModal(). Qt's Cocoa
// event dispatcher can dismiss a nested native modal loop immediately.
NSWindow *const parentWindow = NSApp.keyWindow ?: NSApp.mainWindow;
// Activating the parent first keeps the sheet on it while another app is in front.
NSWindow *sheetParent = nil;
if (parentWindow) {
parentWindow->requestActivate();
sheetParent = reinterpret_cast<NSView *>(parentWindow->winId()).window;
}
if (!sheetParent) {
sheetParent = NSApp.keyWindow ?: NSApp.mainWindow;
}
const auto completionHandlerBlock = ^(NSModalResponse response) {
qCInfo(lcMacSandboxFolderPicker) << "Native folder panel returned." << static_cast<NSInteger>(response);
FolderSelection selection;
@@ -77,8 +85,8 @@ void select(const QString &caption,
completionHandler(std::move(selection));
};
if (parentWindow) {
[panel beginSheetModalForWindow:parentWindow completionHandler:completionHandlerBlock];
if (sheetParent) {
[panel beginSheetModalForWindow:sheetParent completionHandler:completionHandlerBlock];
} else {
[panel beginWithCompletionHandler:completionHandlerBlock];
}
+2
View File
@@ -171,6 +171,7 @@ bool OwncloudSetupWizard::startQmlWizard()
}
_qmlWizardWindow->setIcon(Theme::instance()->applicationIcon());
_qmlController->setWindow(_qmlWizardWindow);
#ifdef Q_OS_MACOS
auto *fgbg = new ForegroundBackground(this);
@@ -241,6 +242,7 @@ bool OwncloudSetupWizard::startQmlWizardForLoginFlow(const QUrl &serverUrl)
}
_qmlWizardWindow->setIcon(Theme::instance()->applicationIcon());
_qmlController->setWindow(_qmlWizardWindow);
#ifdef Q_OS_MACOS
auto *fgbg = new ForegroundBackground(this);
+77 -31
View File
@@ -29,7 +29,9 @@
#endif
#ifdef Q_OS_MACOS
#include "common/macsandboxpersistentaccess.h"
#include "common/utility_mac_sandbox.h"
#include "macOS/macsandboxfolderpicker.h"
#endif
#include <QBuffer>
@@ -43,15 +45,16 @@
#include <QJsonObject>
#include <QLoggingCategory>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkProxy>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPointer>
#include <QSslConfiguration>
#include <QSslCertificate>
#include <QSslConfiguration>
#include <QStorageInfo>
#include <QTimer>
#include <QUuid>
#include <QWindow>
using namespace Qt::StringLiterals;
@@ -94,6 +97,9 @@ bool localFolderContainsData(const QString &localSyncFolder)
AccountWizardController::AccountWizardController(QObject *parent)
: QObject(parent)
, _localNetworkPermissionCheck(LocalNetworkPermission::checkDeniedForConnection)
, _localSyncFolderPicker([this](const QString &caption, const QString &startFolder, std::function<void(const QString &, const QByteArray &)> completion) {
pickLocalSyncFolder(caption, startFolder, std::move(completion));
})
{
initialiseAccount();
@@ -113,6 +119,11 @@ AccountWizardController::AccountWizardController(QObject *parent)
AccountWizardController::~AccountWizardController() = default;
void AccountWizardController::setWindow(QWindow *window)
{
_window = window;
}
void AccountWizardController::initialiseAccount()
{
ConfigFile cfg;
@@ -1363,8 +1374,14 @@ void AccountWizardController::initialiseLocalSyncFolder()
overrideLocalDir);
}
void AccountWizardController::setLocalSyncFolder(const QString &localSyncFolder, bool selectedByUser)
void AccountWizardController::setLocalSyncFolder(const QString &localSyncFolder, bool selectedByUser, const QByteArray &bookmarkData)
{
_localSyncFolderBookmarkData = bookmarkData;
#ifdef Q_OS_MACOS
// Keeps sandbox access to the chosen folder for the checks below and for creating the sync folder.
_localSyncFolderAccess = Utility::MacSandboxPersistentAccess::createValidFromBookmarkData(bookmarkData);
#endif
const auto normalizedLocalSyncFolder = QDir::fromNativeSeparators(localSyncFolder);
const auto localSyncFolderSelected = _localSyncFolderSelected || selectedByUser;
if (_localSyncFolder == normalizedLocalSyncFolder && _localSyncFolderSelected == localSyncFolderSelected) {
@@ -1394,15 +1411,7 @@ void AccountWizardController::promptForInitialLocalSyncFolderIfNeeded()
return;
}
_localSyncFolderPickerOpen = true;
validateLocalSyncFolder();
const auto selectedFolder = openLocalSyncFolderDialog(true);
_localSyncFolderPickerOpen = false;
if (!selectedFolder.isEmpty()) {
setLocalSyncFolder(selectedFolder, true);
} else {
validateLocalSyncFolder();
}
openLocalSyncFolderDialog(true);
});
#endif
}
@@ -1631,17 +1640,13 @@ void AccountWizardController::completeRemoteFolderCheck()
Q_EMIT finished(QDialog::Accepted);
}
bool AccountWizardController::createSyncFolder(AccountState *accountState)
FolderDefinition AccountWizardController::syncFolderDefinition() const
{
if (!accountState) {
setErrorText(tr("Account setup failed while creating the sync folder."));
return false;
}
FolderDefinition folderDefinition;
folderDefinition.localPath = FolderDefinition::prepareLocalPath(_localSyncFolder);
folderDefinition.targetPath = FolderDefinition::prepareTargetPath(_remoteFolder);
folderDefinition.ignoreHiddenFiles = FolderMan::instance()->ignoreHiddenFiles();
folderDefinition.securityScopedBookmarkData = _localSyncFolderBookmarkData;
#ifndef BUILD_FILE_PROVIDER_MODULE
if (_syncMode == VirtualFiles) {
@@ -1655,6 +1660,18 @@ bool AccountWizardController::createSyncFolder(AccountState *accountState)
}
#endif
return folderDefinition;
}
bool AccountWizardController::createSyncFolder(AccountState *accountState)
{
if (!accountState) {
setErrorText(tr("Account setup failed while creating the sync folder."));
return false;
}
const auto folderDefinition = syncFolderDefinition();
auto *folderMan = FolderMan::instance();
folderMan->setSyncEnabled(false);
const auto folder = folderMan->addFolder(accountState, folderDefinition);
@@ -1724,17 +1741,30 @@ void AccountWizardController::setSyncMode(int syncMode)
void AccountWizardController::chooseLocalSyncFolder()
{
const auto selectedFolder = openLocalSyncFolderDialog(false);
if (selectedFolder.isEmpty()) {
openLocalSyncFolderDialog(false);
}
void AccountWizardController::pickLocalSyncFolder(const QString &caption,
const QString &startFolder,
std::function<void(const QString &, const QByteArray &)> completion)
{
#ifdef Q_OS_MACOS
// QFileDialog gives up the sandbox access to the chosen folder before returning, so only the
// native panel can provide a bookmark for it.
Mac::SandboxFolderPicker::select(_window, caption, startFolder, [completion = std::move(completion)](Mac::SandboxFolderPicker::FolderSelection selection) {
completion(selection.path, selection.bookmarkData);
});
#else
completion(QFileDialog::getExistingDirectory(nullptr, caption, startFolder, QFileDialog::ShowDirsOnly), {});
#endif
}
void AccountWizardController::openLocalSyncFolderDialog(bool initialSelection)
{
if (_localSyncFolderPickerOpen) {
return;
}
_localSyncFolderOverride = false;
setLocalSyncFolder(selectedFolder, true);
}
QString AccountWizardController::openLocalSyncFolderDialog(bool initialSelection) const
{
QString startFolder = _localSyncFolder;
if (initialSelection) {
#ifdef Q_OS_MACOS
@@ -1750,10 +1780,26 @@ QString AccountWizardController::openLocalSyncFolderDialog(bool initialSelection
#endif
}
return QFileDialog::getExistingDirectory(nullptr,
tr("Local Sync Folder"),
startFolder,
QFileDialog::ShowDirsOnly);
_localSyncFolderPickerOpen = true;
validateLocalSyncFolder();
const QPointer<AccountWizardController> controller(this);
_localSyncFolderPicker(tr("Local Sync Folder"), startFolder, [controller, initialSelection](const QString &selectedFolder, const QByteArray &bookmarkData) {
if (!controller) {
return;
}
controller->_localSyncFolderPickerOpen = false;
if (selectedFolder.isEmpty()) {
controller->validateLocalSyncFolder();
return;
}
if (!initialSelection) {
controller->_localSyncFolderOverride = false;
}
controller->setLocalSyncFolder(selectedFolder, true, bookmarkData);
});
}
void AccountWizardController::openSelectiveSync()
+23 -2
View File
@@ -23,11 +23,18 @@
#include "networkjobs.h"
class QNetworkReply;
class QWindow;
namespace OCC {
class SelectiveSyncDialog;
class AccountState;
class FolderDefinition;
namespace Utility
{
class MacSandboxPersistentAccess;
}
/**
* Backend for the QML account wizard.
@@ -114,6 +121,9 @@ public:
explicit AccountWizardController(QObject *parent = nullptr);
~AccountWizardController() override;
/** @brief Sets the wizard window that native panels are attached to. */
void setWindow(QWindow *window);
[[nodiscard]] Step currentStep() const;
[[nodiscard]] QString serverUrl() const;
void setServerUrl(const QString &serverUrl);
@@ -259,9 +269,13 @@ private Q_SLOTS:
private:
using LocalNetworkPermissionCheck = std::function<void(const QUrl &, QObject *, std::function<void(bool)>)>;
// Reports the chosen folder, or an empty path when cancelled, and its security-scoped bookmark if any.
using LocalSyncFolderPicker = std::function<void(const QString &, const QString &, std::function<void(const QString &, const QByteArray &)>)>;
friend class AccountWizardControllerTestAccess;
void pickLocalSyncFolder(const QString &caption, const QString &startFolder, std::function<void(const QString &, const QByteArray &)> completion);
// The wizard account is not registered yet, so its own server settings have to be used.
[[nodiscard]] ManagedVirtualFilesMode accountManagedVirtualFilesMode() const;
void initialiseAccount();
@@ -278,7 +292,7 @@ private:
AccountState *applyAccountChanges();
void clearOneShotOverrides();
void initialiseLocalSyncFolder();
void setLocalSyncFolder(const QString &localSyncFolder, bool selectedByUser = false);
void setLocalSyncFolder(const QString &localSyncFolder, bool selectedByUser = false, const QByteArray &bookmarkData = {});
void promptForInitialLocalSyncFolderIfNeeded();
void validateLocalSyncFolder();
[[nodiscard]] qint64 availableLocalSpace() const;
@@ -288,7 +302,8 @@ private:
void createRemoteFolder();
void completeRemoteFolderCheck();
[[nodiscard]] bool createSyncFolder(AccountState *accountState);
[[nodiscard]] QString openLocalSyncFolderDialog(bool initialSelection) const;
[[nodiscard]] FolderDefinition syncFolderDefinition() const;
void openLocalSyncFolderDialog(bool initialSelection);
[[nodiscard]] QUrl localFolderServerUrl() const;
[[nodiscard]] QString sanitizedRemoteFolderEntityPath() const;
void setCurrentStep(Step step);
@@ -312,6 +327,8 @@ private:
AccountPtr _account;
LocalNetworkPermissionCheck _localNetworkPermissionCheck;
LocalSyncFolderPicker _localSyncFolderPicker;
QPointer<QWindow> _window;
std::unique_ptr<Flow2Auth> _flow2Auth;
QPointer<SelectiveSyncDialog> _selectiveSyncDialog;
enum class ProxyAuthentication {
@@ -343,6 +360,10 @@ private:
QString _avatarUrl;
QString _syncEverythingDescription;
QString _localSyncFolder;
QByteArray _localSyncFolderBookmarkData;
#ifdef Q_OS_MACOS
std::unique_ptr<Utility::MacSandboxPersistentAccess> _localSyncFolderAccess;
#endif
QString _localSyncFolderError;
QString _localSyncFolderFreeSpace;
bool _localSyncFolderValid = false;
+3
View File
@@ -253,6 +253,9 @@ nextcloud_add_test(BrowserReAuthController)
nextcloud_add_test(ConnectionValidator)
nextcloud_add_test(Folder)
nextcloud_add_test(FolderMan)
nextcloud_add_test(FolderWizard)
# folderwizard.h includes the ui_*.h headers generated for nextcloudCore.
target_include_directories(FolderWizardTest PRIVATE "$<TARGET_PROPERTY:nextcloudCore,BINARY_DIR>/nextcloudCore_autogen/include")
nextcloud_add_test(ForceSyncNow)
nextcloud_add_test(RemoteWipe)
nextcloud_add_test(Migration)
+140
View File
@@ -5,6 +5,7 @@
#include "account.h"
#include "configfile.h"
#include "folder.h"
#include "gui/localnetworkpermission.h"
#include "gui/wizard/accountwizardcontroller.h"
#include "managedsettingstestutils.h"
@@ -21,6 +22,7 @@
#include <QTest>
using namespace OCC;
using namespace Qt::StringLiterals;
namespace OCC {
@@ -49,6 +51,31 @@ public:
{
controller.chooseSyncModeAfterCapabilities();
}
static void setLocalSyncFolderPicker(AccountWizardController &controller, AccountWizardController::LocalSyncFolderPicker picker)
{
controller._localSyncFolderPicker = std::move(picker);
}
static FolderDefinition syncFolderDefinition(const AccountWizardController &controller)
{
return controller.syncFolderDefinition();
}
static void openInitialLocalSyncFolderDialog(AccountWizardController &controller)
{
controller.openLocalSyncFolderDialog(true);
}
static void setLocalSyncFolderOverride(AccountWizardController &controller, bool localSyncFolderOverride)
{
controller._localSyncFolderOverride = localSyncFolderOverride;
}
static bool localSyncFolderOverride(const AccountWizardController &controller)
{
return controller._localSyncFolderOverride;
}
};
}
@@ -76,6 +103,119 @@ private Q_SLOTS:
ConfigFile::setDeviceSourcesFactory({});
}
void choosingLocalSyncFolderKeepsItsBookmarkForTheSyncFolder()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
const auto bookmarkData = "bookmark"_ba;
AccountWizardController controller;
AccountWizardControllerTestAccess::setLocalSyncFolderPicker(controller, [&](const QString &, const QString &, const auto &completion) {
completion(dir.path(), bookmarkData);
});
controller.chooseLocalSyncFolder();
QCOMPARE(controller.localSyncFolder(), QDir::fromNativeSeparators(dir.path()));
const auto definition = AccountWizardControllerTestAccess::syncFolderDefinition(controller);
QCOMPARE(definition.localPath, FolderDefinition::prepareLocalPath(dir.path()));
QCOMPARE(definition.securityScopedBookmarkData, bookmarkData);
}
void cancellingLocalSyncFolderPickerKeepsTheChosenFolder()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
const auto bookmarkData = "bookmark"_ba;
auto cancel = false;
AccountWizardController controller;
AccountWizardControllerTestAccess::setLocalSyncFolderPicker(controller, [&](const QString &, const QString &, const auto &completion) {
completion(cancel ? QString{} : dir.path(), cancel ? QByteArray{} : bookmarkData);
});
controller.chooseLocalSyncFolder();
cancel = true;
controller.chooseLocalSyncFolder();
QCOMPARE(controller.localSyncFolder(), QDir::fromNativeSeparators(dir.path()));
QCOMPARE(AccountWizardControllerTestAccess::syncFolderDefinition(controller).securityScopedBookmarkData, bookmarkData);
}
void choosingLocalSyncFolderWithoutBookmarkDropsThePreviousBookmark()
{
QTemporaryDir firstDir;
QTemporaryDir secondDir;
QVERIFY(firstDir.isValid());
QVERIFY(secondDir.isValid());
auto chosenDir = firstDir.path();
auto chosenBookmarkData = "bookmark"_ba;
AccountWizardController controller;
AccountWizardControllerTestAccess::setLocalSyncFolderPicker(controller, [&](const QString &, const QString &, const auto &completion) {
completion(chosenDir, chosenBookmarkData);
});
controller.chooseLocalSyncFolder();
chosenDir = secondDir.path();
chosenBookmarkData.clear();
controller.chooseLocalSyncFolder();
QCOMPARE(controller.localSyncFolder(), QDir::fromNativeSeparators(secondDir.path()));
QVERIFY(AccountWizardControllerTestAccess::syncFolderDefinition(controller).securityScopedBookmarkData.isEmpty());
}
void localSyncFolderPickerOpensOnceWhilePending()
{
auto pickerCalls = 0;
std::function<void(const QString &, const QByteArray &)> pendingCompletion;
AccountWizardController controller;
AccountWizardControllerTestAccess::setLocalSyncFolderPicker(controller, [&](const QString &, const QString &, const auto &completion) {
++pickerCalls;
pendingCompletion = completion;
});
controller.chooseLocalSyncFolder();
controller.chooseLocalSyncFolder();
QCOMPARE(pickerCalls, 1);
pendingCompletion({}, {});
controller.chooseLocalSyncFolder();
QCOMPARE(pickerCalls, 2);
}
void localSyncFolderPickerCompletionAfterControllerIsGoneIsIgnored()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
std::function<void(const QString &, const QByteArray &)> pendingCompletion;
{
AccountWizardController controller;
AccountWizardControllerTestAccess::setLocalSyncFolderPicker(controller, [&](const QString &, const QString &, const auto &completion) {
pendingCompletion = completion;
});
controller.chooseLocalSyncFolder();
}
QVERIFY(pendingCompletion);
pendingCompletion(dir.path(), "bookmark"_ba);
}
void initialLocalSyncFolderSelectionKeepsTheOverride()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
AccountWizardController controller;
AccountWizardControllerTestAccess::setLocalSyncFolderPicker(controller, [&](const QString &, const QString &, const auto &completion) {
completion(dir.path(), {});
});
AccountWizardControllerTestAccess::setLocalSyncFolderOverride(controller, true);
AccountWizardControllerTestAccess::openInitialLocalSyncFolderDialog(controller);
QVERIFY(AccountWizardControllerTestAccess::localSyncFolderOverride(controller));
controller.chooseLocalSyncFolder();
QVERIFY(!AccountWizardControllerTestAccess::localSyncFolderOverride(controller));
}
void switchesAwayFromVirtualFilesWhenEnforcedOff()
{
ConfigFile::setDeviceSourcesFactory([] {
+94
View File
@@ -0,0 +1,94 @@
/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QSignalSpy>
#include <QTemporaryDir>
#include <QtTest>
#include "account.h"
#include "foldermantestutils.h"
#include "folderwizard.h"
#include "syncenginetestutils.h"
using namespace OCC;
using namespace Qt::StringLiterals;
namespace OCC
{
class FolderWizardLocalPathTestAccess
{
public:
static void applyChosenLocalFolder(FolderWizardLocalPath &page, const QString &localFolder, const QByteArray &bookmarkData, bool initialSelection)
{
page.applyChosenLocalFolder(localFolder, bookmarkData, initialSelection);
}
static void setEnteredLocalFolder(FolderWizardLocalPath &page, const QString &localFolder)
{
page._ui.localFolderLineEdit->setText(localFolder);
}
};
}
class TestFolderWizard : public QObject
{
Q_OBJECT
FolderManTestHelper helper;
private Q_SLOTS:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
}
void chosenFolderKeepsItsBookmark()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
FolderWizardLocalPath page(createAccount());
FolderWizardLocalPathTestAccess::applyChosenLocalFolder(page, dir.path(), "bookmark"_ba, true);
QCOMPARE(page.securityScopedBookmarkData(), "bookmark"_ba);
FolderWizardLocalPathTestAccess::setEnteredLocalFolder(page, dir.path() + u"/"_s);
QCOMPARE(page.securityScopedBookmarkData(), "bookmark"_ba);
}
void editedFolderDropsTheBookmark()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
FolderWizardLocalPath page(createAccount());
FolderWizardLocalPathTestAccess::applyChosenLocalFolder(page, dir.path(), "bookmark"_ba, true);
FolderWizardLocalPathTestAccess::setEnteredLocalFolder(page, dir.path() + u"/other"_s);
QVERIFY(page.securityScopedBookmarkData().isEmpty());
}
void cancellingInitialSelectionClosesWithoutBookmark()
{
FolderWizardLocalPath page(createAccount());
QSignalSpy canceled(&page, &FolderWizardLocalPath::initialFolderSelectionCanceled);
FolderWizardLocalPathTestAccess::applyChosenLocalFolder(page, {}, {}, true);
QCOMPARE(canceled.count(), 1);
QVERIFY(page.securityScopedBookmarkData().isEmpty());
}
private:
static AccountPtr createAccount()
{
auto account = Account::create();
account->setCredentials(new FakeCredentials{new FakeQNAM({})});
account->setUrl(QUrl(u"http://example.de"_s));
return account;
}
};
QTEST_MAIN(TestFolderWizard)
#include "testfolderwizard.moc"