diff --git a/src/MountPrompt.qml b/src/MountPrompt.qml index 378f595c..f5f328e4 100644 --- a/src/MountPrompt.qml +++ b/src/MountPrompt.qml @@ -1,5 +1,7 @@ import QtQuick +import QtQuick.Controls import QtQuick.Layouts +import QtCore // GVfs's password request while connecting to another machine whose keys // aren't set up, or its question about a server it can't vouch for (a key @@ -10,6 +12,12 @@ FolioDialog { readonly property var mounter: backend.mounter readonly property bool question: mounter.choices.length > 0 + property bool remember: false + + Settings { + category: "connect" + property alias rememberPassword: prompt.remember + } title: question ? qsTr("Check the server") : qsTr("Sign in") cardWidth: 600 @@ -27,12 +35,16 @@ FolioDialog { forceActiveFocus() } else if (visible) { user.text = mounter.user + domain.text = mounter.domain + rememberBox.checked = prompt.remember password.forceActiveFocus() } } function submit() { - prompt.mounter.providePassword(user.text, password.text) + if (prompt.mounter.canRemember) + prompt.remember = rememberBox.checked + prompt.mounter.providePassword(user.text, password.text, domain.text, rememberBox.checked) } // A click outside the card does nothing: GVfs waits for an answer, @@ -56,6 +68,13 @@ FolioDialog { Layout.fillWidth: true placeholderText: qsTr("User") } + PlainField { + id: domain + visible: !prompt.question && prompt.mounter.needsDomain + Layout.fillWidth: true + placeholderText: qsTr("Domain or workgroup") + onAccepted: password.forceActiveFocus() + } PlainField { id: password visible: !prompt.question @@ -65,6 +84,39 @@ FolioDialog { onAccepted: prompt.submit() } + AbstractButton { + id: rememberBox + visible: !prompt.question && prompt.mounter.canRemember + checkable: true + text: qsTr("Remember the password in the keyring") + Accessible.name: text + Keys.onReturnPressed: toggle() + Keys.onEnterPressed: toggle() + contentItem: RowLayout { + spacing: 10 + Rectangle { + implicitWidth: 18 + implicitHeight: 18 + radius: Math.min(3, theme.radius) + color: rememberBox.checked ? theme.accent : "transparent" + border.width: rememberBox.checked ? 0 : 1 + border.color: rememberBox.visualFocus ? theme.accent : Qt.alpha(theme.foreground, 0.4) + Glyph { + anchors.centerIn: parent + visible: rememberBox.checked + name: "check" + size: 14 + stroke: 2 + color: theme.accentForeground + } + } + FolioText { + text: rememberBox.text + color: rememberBox.visualFocus ? theme.brightForeground : theme.foreground + } + } + } + RowLayout { Layout.alignment: Qt.AlignRight spacing: 10 @@ -85,6 +137,11 @@ FolioDialog { text: qsTr("Cancel") onClicked: prompt.mounter.cancel() } + PlainButton { + visible: !prompt.question && prompt.mounter.guestAllowed + text: qsTr("Connect as Guest") + onClicked: prompt.mounter.connectAsGuest() + } PlainButton { visible: !prompt.question text: qsTr("Connect") diff --git a/src/i18n/en.json b/src/i18n/en.json index c2bd349a..47755d88 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -79,6 +79,7 @@ "Compressing needs bsdtar, from the libarchive package.": "Compressing needs bsdtar, from the libarchive package.", "Computer": "Computer", "Connect": "Connect", + "Connect as Guest": "Connect as Guest", "Connecting to %1…": "Connecting to %1…", "Copied %1, but couldn't remove the original.": "Copied %1, but couldn't remove the original.", "Copy": "Copy", @@ -151,6 +152,7 @@ "one": "Do this for the other %Ln clash", "other": "Do this for the other %Ln clashes" }, + "Domain or workgroup": "Domain or workgroup", "Drives can't be changed without udisks2.": "Drives can't be changed without udisks2.", "Drop to pin": "Drop to pin", "Duplicate": "Duplicate", @@ -318,6 +320,7 @@ }, "Redoing…": "Redoing…", "Reload": "Reload", + "Remember the password in the keyring": "Remember the password in the keyring", "Remove from the sidebar": "Remove from the sidebar", "Rename": "Rename", "Rename…": "Rename…", diff --git a/src/mounter.cpp b/src/mounter.cpp index 5a520ff4..b1f85764 100644 --- a/src/mounter.cpp +++ b/src/mounter.cpp @@ -32,17 +32,23 @@ struct MounterCallbacks { return; } self->m_prompt = QString::fromUtf8(message).trimmed(); + self->m_choices.clear(); for (const char **choice = choices; choice && *choice; ++choice) self->m_choices << QString::fromUtf8(*choice); self->setAsking(true); } - static void password(GMountOperation *op, const char *message, const char *user, const char *, - GAskPasswordFlags, gpointer data) { + static void password(GMountOperation *op, const char *message, const char *user, const char *domain, + GAskPasswordFlags flags, gpointer data) { g_signal_stop_emission_by_name(op, "ask-password"); auto *self = static_cast(data); self->m_prompt = QString::fromUtf8(message).trimmed(); self->m_user = QString::fromUtf8(user ? user : ""); + self->m_needsDomain = (flags & G_ASK_PASSWORD_NEED_DOMAIN) != 0; + self->m_domain = QString::fromUtf8(domain ? domain : ""); + self->m_guestAllowed = (flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED) != 0; + self->m_canRemember = (flags & G_ASK_PASSWORD_SAVING_SUPPORTED) != 0; + self->m_choices.clear(); self->setAsking(true); } @@ -154,14 +160,32 @@ void Mounter::mount(const QString &uri, bool verified) { g_object_unref(file); } -void Mounter::providePassword(const QString &user, const QString &password) { +void Mounter::providePassword(const QString &user, const QString &password, + const QString &domain, bool remember) { if (!m_operation || !m_asking) return; + const bool savePermanently = remember && m_canRemember; + // GVfs asks again on the same operation after a failed login, so a + // domain cleared since then is set empty rather than left as it was. + const bool setDomain = m_needsDomain; setAsking(false); + g_mount_operation_set_anonymous(m_operation, FALSE); if (!user.isEmpty()) g_mount_operation_set_username(m_operation, user.toUtf8().constData()); + if (setDomain) + g_mount_operation_set_domain(m_operation, domain.toUtf8().constData()); g_mount_operation_set_password(m_operation, password.toUtf8().constData()); - g_mount_operation_set_password_save(m_operation, G_PASSWORD_SAVE_FOR_SESSION); + g_mount_operation_set_password_save(m_operation, + savePermanently ? G_PASSWORD_SAVE_PERMANENTLY + : G_PASSWORD_SAVE_FOR_SESSION); + g_mount_operation_reply(m_operation, G_MOUNT_OPERATION_HANDLED); +} + +void Mounter::connectAsGuest() { + if (!m_operation || !m_asking || !m_guestAllowed) + return; + setAsking(false); + g_mount_operation_set_anonymous(m_operation, TRUE); g_mount_operation_reply(m_operation, G_MOUNT_OPERATION_HANDLED); } @@ -184,6 +208,10 @@ void Mounter::setAsking(bool asking) { m_asking = asking; if (!asking) { m_prompt.clear(); + m_needsDomain = false; + m_domain.clear(); + m_guestAllowed = false; + m_canRemember = false; m_choices.clear(); } emit askingChanged(); diff --git a/src/mounter.h b/src/mounter.h index 5bc22a50..e174e4f8 100644 --- a/src/mounter.h +++ b/src/mounter.h @@ -19,6 +19,10 @@ class Mounter : public QObject { Q_PROPERTY(bool asking READ asking NOTIFY askingChanged) Q_PROPERTY(QString prompt READ prompt NOTIFY askingChanged) Q_PROPERTY(QString user READ user NOTIFY askingChanged) + Q_PROPERTY(bool needsDomain READ needsDomain NOTIFY askingChanged) + Q_PROPERTY(QString domain READ domain NOTIFY askingChanged) + Q_PROPERTY(bool guestAllowed READ guestAllowed NOTIFY askingChanged) + Q_PROPERTY(bool canRemember READ canRemember NOTIFY askingChanged) // A question's answers, in GVfs's words; empty for a password request. Q_PROPERTY(QStringList choices READ choices NOTIFY askingChanged) @@ -30,6 +34,10 @@ class Mounter : public QObject { bool asking() const { return m_asking; } QString prompt() const { return m_prompt; } QString user() const { return m_user; } + bool needsDomain() const { return m_needsDomain; } + QString domain() const { return m_domain; } + bool guestAllowed() const { return m_guestAllowed; } + bool canRemember() const { return m_canRemember; } QStringList choices() const { return m_choices; } // Mounts the location; `mounted` carries its local GVfs path. A @@ -47,9 +55,12 @@ class Mounter : public QObject { // Each address mounted this session and the folder GVfs opened for it. static QHash &known(); - // The user's answer to a password request; remembered for this session - // only, never written to a keyring without asking. - Q_INVOKABLE void providePassword(const QString &user, const QString &password); + // The user's answer to a password request; remembered for this session, + // or in the keyring when `remember` is asked for. + Q_INVOKABLE void providePassword(const QString &user, const QString &password, + const QString &domain = {}, bool remember = false); + // Connects anonymously when the server allows guest access. + Q_INVOKABLE void connectAsGuest(); // The user's answer to a question, by its place among the choices. Q_INVOKABLE void choose(int choice); // Closes the password request and abandons the mount. @@ -63,6 +74,7 @@ class Mounter : public QObject { private: // GIO's C callbacks, in mounter.cpp. friend struct MounterCallbacks; + friend class FolioTests; void setAsking(bool asking); void finish(); @@ -72,5 +84,9 @@ class Mounter : public QObject { bool m_asking = false; QString m_prompt; QString m_user; + bool m_needsDomain = false; + QString m_domain; + bool m_guestAllowed = false; + bool m_canRemember = false; QStringList m_choices; }; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6a0aeeb4..45f6df5c 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -61,6 +61,12 @@ #include "translator.h" #include "windows.h" +// GIO's headers use `signals` as a struct member, which Qt defines as a +// keyword. +#undef signals +#include +#define signals Q_SIGNALS + // Puts XDG_DATA_HOME, where the trash and recent files live, in `path` for // as long as the returned guard does. [[nodiscard]] static auto dataHomeAt(const QString &path) @@ -4314,6 +4320,31 @@ private slots: QVERIFY(!open.busy()); } + void mounterSendsTheDomainAsGiven() + { + // GVfs asks again on the same operation after a failed login; a + // domain cleared in between is sent empty, not as it was before. + QTemporaryDir dir; + Mounter mounter; + mounter.mount(QUrl::fromLocalFile(dir.path()).toString(), false); + GMountOperation *operation = mounter.m_operation; + QVERIFY(operation); + g_object_ref(operation); + const auto release = qScopeGuard([&] { g_object_unref(operation); }); + const auto ask = [&] { + g_signal_emit_by_name(operation, "ask-password", "Sign in", "me", "WORK", + GAskPasswordFlags(G_ASK_PASSWORD_NEED_PASSWORD | G_ASK_PASSWORD_NEED_DOMAIN)); + }; + ask(); + QVERIFY(mounter.asking()); + QCOMPARE(mounter.domain(), QString("WORK")); + mounter.providePassword("me", "wrong", "WORK"); + QCOMPARE(QString::fromUtf8(g_mount_operation_get_domain(operation)), QString("WORK")); + ask(); + mounter.providePassword("me", "right", ""); + QVERIFY(QString::fromUtf8(g_mount_operation_get_domain(operation)).isEmpty()); + } + void trashFollowsXdgDataHome() { // g_file_trash puts things in $XDG_DATA_HOME/Trash, wherever that