KWallet

querydriver.cpp
1/*
2 This file is part of the KDE
3 SPDX-FileCopyrightText: 2015 Valentin Rusu <kde@rusu.info>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "querydriver.h"
9
10#include <iostream>
11
12#include <QDebug>
13#include <QJsonDocument>
14#include <QJsonObject>
15#include <QScreen>
16#include <QTimer>
17
18#include <KLocalizedString>
19
20QueryDriver::QueryDriver(int &argc, char *argv[])
21 : QApplication(argc, argv)
22 , entryFolder(QStringLiteral("Passwords"))
23{
24 QTimerEvent *timerEvent = new QTimerEvent(100);
25 postEvent(this, timerEvent);
26}
27
28QueryDriver::~QueryDriver()
29{
30 // NOTE: no need to close the wallet, let's leave it open for next time
31 // we'll query it preventing too many annoying password prompts
32 // if (theWallet != NULL && theWallet->isOpen()) {
33 // Wallet::closeWallet(walletName, false);
34 // }
35 delete theWallet;
36}
37void QueryDriver::timerEvent(QTimerEvent *event)
38{
40 if (verbose) {
41 qDebug() << "timer event";
42 }
43
44 auto wl = Wallet::walletList();
45 if (wl.indexOf(walletName) == -1) {
46 std::cout << i18n("Wallet %1 not found", walletName).toUtf8().constData() << std::endl;
47 exit(1);
48 } else {
49 if (verbose) {
50 qDebug() << "standby opening wallet " << walletName;
51 }
52
53 theWallet = Wallet::openWallet(walletName, 0, Wallet::Asynchronous);
54 connect(theWallet, &KWallet::Wallet::walletOpened, this, &QueryDriver::walletOpened);
55 }
56}
57
58void QueryDriver::setWalletName(const QString &name)
59{
60 walletName = name;
61}
62
63void QueryDriver::setMode(Mode mode)
64{
65 this->mode = mode;
66}
67void QueryDriver::walletOpened(bool success)
68{
69 if (!success) {
70 std::cout << i18n("Failed to open wallet %1. Aborting", walletName).toUtf8().constData() << std::endl;
71 exit(2);
72 } else {
73 switch (mode) {
74 case List:
75 readEntries();
76 break;
77 case Read:
78 readValue();
79 break;
80 case Write:
81 writeValue();
82 break;
83 default:
84 Q_ASSERT(0);
85 }
86 }
87}
88
89void QueryDriver::readEntries()
90{
91 theWallet = Wallet::openWallet(walletName, 0);
92 if (entryFolder.isEmpty()) {
93 const auto fl = theWallet->folderList();
94 for (auto &f : fl) {
95 std::cout << f.toUtf8().constData() << std::endl;
96 Q_ASSERT(theWallet->setFolder(f));
97 const auto el = theWallet->entryList();
98 for (auto &e : el) {
99 std::cout << "\t" << e.toUtf8().constData() << std::endl;
100 }
101 }
102 } else {
103 if (!theWallet->setFolder(entryFolder)) {
104 std::cout << i18n("The folder %1 does not exist!", entryFolder).toUtf8().constData() << std::endl;
105 exit(4);
106 return;
107 }
108 const auto el = theWallet->entryList();
109 for (auto &e : el) {
110 std::cout << e.toUtf8().constData() << std::endl;
111 }
112 }
113 quit();
114}
115
116void QueryDriver::readValue()
117{
118 if (verbose) {
119 qDebug() << "reading" << entryName << "from" << entryFolder << "from" << walletName;
120 }
121 if (!theWallet->setFolder(entryFolder)) {
122 std::cout << i18n("The folder %1 does not exist!", entryFolder).toUtf8().constData() << std::endl;
123 exit(4);
124 return;
125 }
126 Wallet::EntryType kind = theWallet->entryType(entryName);
127 if (kind == Wallet::Password) {
128 readPasswordValue();
129 } else if (kind == Wallet::Map) {
130 readMapValue();
131 } else {
132 std::cout << i18n("Failed to read entry %1 value from the %2 wallet.", entryName, walletName).toUtf8().constData() << std::endl;
133 exit(4);
134 }
135}
136
137void QueryDriver::readMapValue()
138{
140 int rc = theWallet->readMap(entryName, map);
141 if (rc != 0) {
142 std::cout << i18n("Failed to read entry %1 value from the %2 wallet", entryName, walletName).toUtf8().constData() << std::endl;
143 exit(4);
144 return;
145 }
146 QJsonObject json;
147 for (auto &e : map.keys()) {
148 json.insert(e, QJsonValue::fromVariant(QVariant(map.value(e))));
149 }
150 std::cout << QJsonDocument(json).toJson().constData() << std::endl;
151 quit();
152}
153
154void QueryDriver::readPasswordValue()
155{
156 QString entryValue;
157 int rc = theWallet->readPassword(entryName, entryValue);
158 if (rc != 0) {
159 std::cout << i18n("Failed to read entry %1 value from the %2 wallet", entryName, walletName).toUtf8().constData() << std::endl;
160 exit(4);
161 return;
162 }
163 const QStringList el = entryValue.split(QStringLiteral("\n"), Qt::SkipEmptyParts);
164 for (auto &e : el) {
165 std::cout << e.toUtf8().constData() << std::endl;
166 }
167 quit();
168}
169
170void QueryDriver::writeValue()
171{
172 if (verbose) {
173 qDebug() << "writing" << entryName << "to" << entryFolder << "to" << walletName;
174 }
175 theWallet->setFolder(entryFolder);
176
177 QString passwordContents;
178 for (std::string line; std::getline(std::cin, line);) {
179 if (!passwordContents.isEmpty()) {
180 passwordContents += '\n';
181 }
182 passwordContents += QString::fromStdString(line);
183 if (!std::cin) {
184 break;
185 }
186 }
187 Wallet::EntryType kind = theWallet->entryType(entryName);
188 if (kind == Wallet::Password) {
189 if (verbose) {
190 qDebug() << "about to write" << passwordContents;
191 }
192 int rc = theWallet->writePassword(entryName, passwordContents);
193 if (rc != 0) {
194 std::cout << i18n("Failed to write entry %1 value to %2 wallet", entryName, walletName).toUtf8().constData() << std::endl;
195 exit(4);
196 return;
197 }
198 } else if (kind == Wallet::Map) {
199 const QJsonDocument json = QJsonDocument::fromJson(passwordContents.toLatin1());
200 if (!json.isNull()) {
201 QJsonObject values = json.object();
203 for (auto &e : values.keys()) {
204 map.insert(e, values.value(e).toString());
205 }
206 if (verbose) {
207 qDebug() << "about to write" << map;
208 }
209 int rc = theWallet->writeMap(entryName, map);
210 if (rc != 0) {
211 std::cout << i18n("Failed to write entry %1 value to %2 wallet", entryName, walletName).toUtf8().constData() << std::endl;
212 exit(4);
213 return;
214 }
215 }
216 }
217 quit();
218}
219
220#include "moc_querydriver.cpp"
virtual QStringList folderList()
Obtain the list of all folders contained in the wallet.
Definition kwallet.cpp:404
static QStringList walletList()
List all the wallets available.
Definition kwallet.cpp:159
virtual QStringList entryList()
Return the list of keys of all entries in this folder.
Definition kwallet.cpp:419
virtual int readMap(const QString &key, QMap< QString, QString > &value)
Read the map entry key from the current folder.
Definition kwallet.cpp:581
virtual EntryType entryType(const QString &key)
Determine the type of the entry key in this folder.
Definition kwallet.cpp:781
virtual int readPassword(const QString &key, QString &value)
Read the password entry key from the current folder.
Definition kwallet.cpp:637
static Wallet * openWallet(const QString &name, WId w, OpenType ot=Synchronous)
Open the wallet name.
Definition kwallet.cpp:238
virtual int writePassword(const QString &key, const QString &value)
Write key = value as a password to the current folder.
Definition kwallet.cpp:734
virtual bool setFolder(const QString &f)
Set the current working folder to f.
Definition kwallet.cpp:469
void walletOpened(bool success)
Emitted when a wallet is opened in asynchronous mode.
virtual int writeMap(const QString &key, const QMap< QString, QString > &value)
Write key = value as a map to the current folder.
Definition kwallet.cpp:713
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
virtual bool event(QEvent *e) override
const char * constData() const const
void exit(int returnCode)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QJsonObject object() const const
QByteArray toJson(JsonFormat format) const const
iterator insert(QLatin1StringView key, const QJsonValue &value)
QStringList keys() const const
QJsonValue value(QLatin1StringView key) const const
QJsonValue fromVariant(const QVariant &variant)
QString toString() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual void timerEvent(QTimerEvent *event)
QString fromStdString(const std::string &str)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QByteArray toLatin1() const const
QByteArray toUtf8() const const
SkipEmptyParts
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
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.