KWallet

kwalletfreedesktopprompt.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2021 Slava Aseev <nullptrnine@basealt.ru>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7#include "kwalletfreedesktopprompt.h"
8
9#include "kwalletd.h"
10#include "kwalletfreedesktopcollection.h"
11#include "kwalletfreedesktoppromptadaptor.h"
12
13KWalletFreedesktopPrompt::KWalletFreedesktopPrompt(KWalletFreedesktopService *service, QDBusObjectPath objectPath, PromptType type, QString responseBusName)
14 : QObject(nullptr)
15 , m_service(service)
16 , m_objectPath(std::move(objectPath))
17 , m_type(type)
18 , m_responseBusName(std::move(responseBusName))
19{
20 (void)new KWalletFreedesktopPromptAdaptor(this);
21}
22
23KWalletFreedesktopService *KWalletFreedesktopPrompt::fdoService() const
24{
25 return m_service;
26}
27
28KWalletD *KWalletFreedesktopPrompt::backend() const
29{
30 return fdoService()->backend();
31}
32
33QDBusObjectPath KWalletFreedesktopPrompt::fdoObjectPath() const
34{
35 return m_objectPath;
36}
37
38void KWalletFreedesktopPrompt::Dismiss()
39{
40 auto msg = QDBusMessage::createTargetedSignal(m_responseBusName,
41 fdoObjectPath().path(),
42 QStringLiteral("org.freedesktop.Secret.Prompt"),
43 QStringLiteral("Completed"));
44 QVariantList args;
45 args << true << QVariant::fromValue(QDBusVariant(QVariant::fromValue(QList<QDBusObjectPath>())));
46 msg.setArguments(args);
49}
50
51void KWalletFreedesktopPrompt::Prompt(const QString &window_id)
52{
53 if (m_type != PromptType::Open && m_type != PromptType::Create) {
54 return;
55 }
56
57 const int wId = window_id.toInt();
58 for (auto properties : std::as_const(m_propertiesList)) {
59 /* When type is "PromptType::Open" the properties.label actually stores
60 * the wallet name
61 */
62 QString walletName = properties.collectionLabel;
63
64 if (m_type == PromptType::Create) {
65 walletName = fdoService()->makeUniqueWalletName(properties.collectionLabel);
66 properties.collectionLabel = walletName;
67 }
68
69 if (!properties.alias.isEmpty()) {
70 fdoService()->createCollectionAlias(properties.alias, walletName);
71 }
72
73 const int tId = backend()->openAsync(walletName, wId, FDO_APPID, false, connection(), message());
74 m_transactionIds.insert(tId);
75 m_transactionIdToCollectionProperties.emplace(tId, std::move(properties));
76 }
77}
78
79void KWalletFreedesktopPrompt::walletAsyncOpened(int transactionId, int walletHandle)
80{
81 const auto found = m_transactionIds.find(transactionId);
82
83 if (found != m_transactionIds.end()) {
84 const auto propertiesPos = m_transactionIdToCollectionProperties.find(transactionId);
85 if (walletHandle < 0 || propertiesPos == m_transactionIdToCollectionProperties.end()) {
86 Dismiss();
87 fdoService()->deletePrompt(fdoObjectPath().path());
88 return;
89 }
90 m_transactionIds.remove(transactionId);
91
92 const QString &walletName = propertiesPos->second.collectionLabel;
93 const auto collectionPath = fdoService()->promptUnlockCollection(walletName, walletHandle);
94 m_result.push_back(propertiesPos->second.objectPath.path() == QStringLiteral("/") ? collectionPath : propertiesPos->second.objectPath);
95 }
96
97 if (m_transactionIds.empty()) {
98 /* At this point there is no remaining transactions, so we able to complete prompt */
99
100 auto msg = QDBusMessage::createTargetedSignal(m_responseBusName,
101 fdoObjectPath().path(),
102 QStringLiteral("org.freedesktop.Secret.Prompt"),
103 QStringLiteral("Completed"));
104 QVariantList args;
105 args << false;
106
107 if (m_type == PromptType::Create && m_result.size() < 2) {
108 /* Single object in dbus variant */
109 args << QVariant::fromValue(QDBusVariant(QVariant::fromValue(m_result.empty() ? QDBusObjectPath("/") : m_result.front())));
110 } else {
111 /* Object array in dbus variant */
113 }
114
115 msg.setArguments(args);
117
118 fdoService()->deletePrompt(fdoObjectPath().path());
119 }
120}
121
122void KWalletFreedesktopPrompt::subscribeForWalletAsyncOpened()
123{
124 connect(backend(), &KWalletD::walletAsyncOpened, this, &KWalletFreedesktopPrompt::walletAsyncOpened);
125 QDBusConnection::sessionBus().registerObject(fdoObjectPath().path(), this);
126}
127
128void KWalletFreedesktopPrompt::appendProperties(const QString &label, const QDBusObjectPath &objectPath, const QString &alias)
129{
130 m_propertiesList.push_back(CollectionProperties{label, objectPath, alias});
131}
132
133#include "moc_kwalletfreedesktopprompt.cpp"
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QString path(const QString &relativePath)
KGuiItem properties()
QString label(StandardShortcut id)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
bool send(const QDBusMessage &message) const const
QDBusConnection sessionBus()
void unregisterObject(const QString &path, UnregisterMode mode)
QDBusMessage createTargetedSignal(const QString &service, const QString &path, const QString &interface, const QString &name)
bool empty() const const
reference front()
void push_back(parameter_type value)
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool empty() const const
iterator end()
iterator find(const T &value)
iterator insert(const T &value)
bool remove(const T &value)
int toInt(bool *ok, int base) const const
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.