Skip to content
Open
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
48 changes: 43 additions & 5 deletions src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,46 @@ void Backend::arrive(const QString &folder) {
showItem(target.filePath());
}

QString Backend::serverAddress(const QString &text) {
static const QStringList schemes{"sftp", "smb", "ftp", "dav", "davs"};
QString Backend::serverAddress(const QString &text, QString *problem) {
if (problem)
problem->clear();
const QString raw = text.trimmed();
static const QStringList schemes{"sftp", "smb", "nfs", "ftp", "ftps", "dav", "davs"};
// Tolerant, so a path typed with spaces in it is escaped rather than
// refused; QUrl puts the scheme in lower case.
QUrl url(text.trimmed(), QUrl::TolerantMode);
// refused; QUrl puts the scheme in lower case. A Windows UNC path has no
// query or fragment, so its folder names are set decoded.
QUrl url;
if (raw.startsWith("\\\\")) {
const QString unc = raw.mid(2).replace('\\', '/');
url.setScheme("smb");
url.setHost(unc.section('/', 0, 0));
const QString rest = unc.section('/', 1);
url.setPath(rest.isEmpty() ? "/" : "/" + rest, QUrl::DecodedMode);
} else {
url = QUrl(raw, QUrl::TolerantMode);
}
if (url.scheme() == "ssh")
url.setScheme("sftp");
else if (url.scheme() == "cifs")
url.setScheme("smb");
else if (url.scheme() == "webdav")
url.setScheme("dav");
else if (url.scheme() == "webdavs")
url.setScheme("davs");
if (!url.isValid() || !schemes.contains(url.scheme()) || url.host().isEmpty())
return {};
if (!url.password().isEmpty()) {
if (problem)
*problem = tr("Leave the password out of the address; enter it when asked to sign in.");
return {};
}
// A Windows host on its own is fine (its shares are offered to pick
// from), but an NFS server has no such list.
if (url.scheme() == "nfs" && url.path().section('/', 0, 0, QString::SectionSkipEmpty).isEmpty()) {
if (problem)
*problem = tr("Add the exported folder, as in nfs://%1/export.").arg(url.host());
return {};
}
if (url.path().isEmpty())
url.setPath("/");
return url.toString(QUrl::FullyEncoded);
Expand Down Expand Up @@ -608,11 +641,16 @@ bool Backend::goTyped(const QString &text) {
// A server is mounted through GVfs, which also says where on the mount
// the address's path is (a WebDAV address's path can be part of the
// mount itself), and connecting goes there.
if (const QString address = serverAddress(text); !address.isEmpty()) {
QString problem;
if (const QString address = serverAddress(text, &problem); !address.isEmpty()) {
if (!m_mounter.busy())
mount(address, QUrl(address).host(), false);
return true;
}
if (!problem.isEmpty()) {
setMessage(problem);
return false;
}
// A view is typed as the path bar shows it, by its own name.
if (const QString view = text.trimmed(); FolderModel::isView(view))
return go(view, true);
Expand Down
8 changes: 5 additions & 3 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ class Backend : public QObject {
static QString expandTyped(const QString &text, const QString &home, const QString &current);
static QString complete(const QString &text, const QString &home, const QString &current);
static QString mountLabel(const QString &mountFolder);
// A server address (sftp://, smb://, ftp://, dav://, davs://) as GIO
// mounts it, or empty when the text isn't one.
static QString serverAddress(const QString &text);
// A server address (sftp://, smb://, nfs://, ftp://, ftps://, dav://,
// davs://, or \\server\share) as GIO mounts it, or empty when the text
// isn't one. `problem` says why when it's an address that can't be
// mounted as written.
static QString serverAddress(const QString &text, QString *problem = nullptr);

signals:
void sortsChanged();
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"A name can't contain \"/\".": "A name can't contain \"/\".",
"A screenful up or down": "A screenful up or down",
"Add or remove one": "Add or remove one",
"Add the exported folder, as in nfs://%1/export.": "Add the exported folder, as in nfs://%1/export.",
"Archive": "Archive",
"Back / ahead 5 seconds": "Back / ahead 5 seconds",
"Back / forward": "Back / forward",
Expand Down Expand Up @@ -217,6 +218,7 @@
"Keep Both": "Keep Both",
"Keyboard shortcuts": "Keyboard shortcuts",
"Kind": "Kind",
"Leave the password out of the address; enter it when asked to sign in.": "Leave the password out of the address; enter it when asked to sign in.",
"Making a preview…": "Making a preview…",
"Merge": "Merge",
"Merging adds what's in this folder to the one there. What has the same name inside is replaced, and the replaced items go to the trash.": "Merging adds what's in this folder to the one there. What has the same name inside is replaced, and the replaced items go to the trash.",
Expand Down
30 changes: 28 additions & 2 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4480,6 +4480,7 @@ private slots:
QCOMPARE(Backend::mountLabel("sftp:host=studio.example.ts.net,user=me"), QString("studio.example.ts.net"));
QCOMPARE(Backend::mountLabel("sftp:host=files.example.com"), QString("files.example.com"));
QCOMPARE(Backend::mountLabel("smb-share:server=nas,share=media"), QString("nas"));
QCOMPARE(Backend::mountLabel("nfs:host=nas,prefix=%2Fsrv%2Fexport"), QString("nas"));
QCOMPARE(Backend::mountLabel("ftp:host=ftp.example.com"), QString("ftp.example.com"));
QCOMPARE(Backend::mountLabel("dav:host=cloud.example.com,ssl=true,prefix=%2Fremote.php"), QString("cloud.example.com"));
}
Expand All @@ -4491,14 +4492,39 @@ private slots:
// the mount itself.
QCOMPARE(Backend::serverAddress("sftp://nas"), QString("sftp://nas/"));
QCOMPARE(Backend::serverAddress(" SFTP://me@nas:2222/srv/My Files "), QString("sftp://me@nas:2222/srv/My%20Files"));
QCOMPARE(Backend::serverAddress("ssh://me@nas:2222/srv"), QString("sftp://me@nas:2222/srv"));
QCOMPARE(Backend::serverAddress("smb://nas/media/Films"), QString("smb://nas/media/Films"));
QCOMPARE(Backend::serverAddress("smb://nas"), QString("smb://nas/"));
QCOMPARE(Backend::serverAddress("cifs://nas/media"), QString("smb://nas/media"));
QCOMPARE(Backend::serverAddress("\\\\nas"), QString("smb://nas/"));
QCOMPARE(Backend::serverAddress("\\\\nas\\media\\Films"), QString("smb://nas/media/Films"));
QCOMPARE(Backend::serverAddress("\\\\nas\\media\\Project#1"), QString("smb://nas/media/Project%231"));
QCOMPARE(Backend::serverAddress("nfs://nas/srv/export"), QString("nfs://nas/srv/export"));
QCOMPARE(Backend::serverAddress("ftp://ftp.example.com/pub/"), QString("ftp://ftp.example.com/pub/"));
QCOMPARE(Backend::serverAddress("ftps://ftp.example.com/pub/"), QString("ftps://ftp.example.com/pub/"));
QCOMPARE(Backend::serverAddress("davs://cloud.example.com/remote.php/dav/files/me"),
QString("davs://cloud.example.com/remote.php/dav/files/me"));
QCOMPARE(Backend::serverAddress("webdavs://cloud.example.com/dav"), QString("davs://cloud.example.com/dav"));
QCOMPARE(Backend::serverAddress("webdav://10.0.0.5:8080"), QString("dav://10.0.0.5:8080/"));
QCOMPARE(Backend::serverAddress("dav://10.0.0.5:8080"), QString("dav://10.0.0.5:8080/"));
// Paths, other schemes and addresses without a server are not.
for (const char *text : {"~/Work", "/etc", "Work/folio", "file:///etc", "http://example.com", "sftp://", "smb:/nas"})
QVERIFY2(Backend::serverAddress(text).isEmpty(), text);
for (const char *text : {"~/Work", "/etc", "Work/folio", "file:///etc", "http://example.com", "sftp://", "smb:/nas", "\\\\"}) {
QString problem;
QVERIFY2(Backend::serverAddress(text, &problem).isEmpty(), text);
QVERIFY2(problem.isEmpty(), text);
}

// An address that needs an export, or embeds a password, is refused
// with the reason why, and goTyped shows that reason.
QTemporaryDir home;
Backend backend(home.path());
for (const char *incomplete : {"nfs://nas", "nfs://nas/", "sftp://me:secret@nas/"}) {
QString problem;
QVERIFY2(Backend::serverAddress(incomplete, &problem).isEmpty(), incomplete);
QVERIFY2(!problem.isEmpty(), incomplete);
QVERIFY(!backend.goTyped(incomplete));
QCOMPARE(backend.message(), problem);
}
}

void pluginsReadTheirManifests()
Expand Down