[kde-doc-english] [kwallet-query] /: Support to overwrite maps entries.

Andrea Scarpino scarpino at kde.org
Sat Jun 13 18:04:05 UTC 2015


Git commit 1e78059834e1e2464160d07bd8adaed6437f14e4 by Andrea Scarpino.
Committed on 13/06/2015 at 18:03.
Pushed by scarpino into branch 'master'.

Support to overwrite maps entries.

-w, --write-password takes in input a well-formed JSON object.

M  +2    -2    doc/man-kwallet-query.1.docbook
M  +25   -13   src/querydriver.cpp
M  +1    -1    src/querydriver.h

http://commits.kde.org/kwallet-query/1e78059834e1e2464160d07bd8adaed6437f14e4

diff --git a/doc/man-kwallet-query.1.docbook b/doc/man-kwallet-query.1.docbook
index 3b70926..8f694c6 100644
--- a/doc/man-kwallet-query.1.docbook
+++ b/doc/man-kwallet-query.1.docbook
@@ -58,7 +58,7 @@
 <listitem>
 <simpara>
   Read the contents of the given <emphasis>Entry</emphasis> from the <emphasis role="strong">Folder</emphasis> section of the
-  <emphasis>wallet</emphasis> and output it on the standard output.
+  <emphasis>wallet</emphasis> and output it on the standard output. Maps are exported as JSON object.
 </simpara>
 </listitem>
 </varlistentry>
@@ -69,7 +69,7 @@
 <listitem>
 <simpara>
   Write secrets to the given <emphasis>Entry</emphasis> under the <emphasis role="strong">Folder</emphasis> section of the given
-  <emphasis>wallet</emphasis>. The secrets are read from the standard input.
+  <emphasis>wallet</emphasis>. The secrets are read from the standard input. Maps take in input a well-formed JSON object.
   <emphasis role="strong">IMPORTANT</emphasis> previous wallet entry value will be overwritten by this option, so
   be careful when using it!
 </simpara>
diff --git a/src/querydriver.cpp b/src/querydriver.cpp
index 8f7b9c1..f7bf566 100644
--- a/src/querydriver.cpp
+++ b/src/querydriver.cpp
@@ -88,7 +88,7 @@ void QueryDriver::walletOpened(bool success) {
                 readValue();
                 break;
             case Write:
-                writePasswordValue();
+                writeValue();
                 break;
             default:
                 Q_ASSERT(0);
@@ -152,27 +152,39 @@ void QueryDriver::readPasswordValue() {
     }
 }
 
-void QueryDriver::writePasswordValue() {
+void QueryDriver::writeValue() {
     if (verbose) qDebug() << "writing" << entryName << "to" << entryFolder << "to" << walletName;
     theWallet->setFolder(entryFolder);
 
-    Wallet::EntryType kind = theWallet->entryType(entryName);
-    if (kind != Wallet::Password) {
-        std::cout << i18n("You can only write password values. Maps are not supported.").toStdString() << std::endl;
-        exit(4);
-    }
-
     QString passwordContents;
     for (std::string line; std::getline(std::cin, line); ) {
         if (!passwordContents.isEmpty()) passwordContents += '\n';
         passwordContents += QString::fromStdString(line);
         if (!std::cin) break;
     }
-    if (verbose) qDebug() << "  about to write " << passwordContents;
-    int rc = theWallet->writePassword(entryName, passwordContents);
-    if (rc != 0) {
-        std::cout << i18n("Failed to write entry %1 value to %2 wallet", entryName, walletName).toStdString() << std::endl;
-        exit(4);
+    Wallet::EntryType kind = theWallet->entryType(entryName);
+    if (kind == Wallet::Password) {
+        if (verbose) qDebug() << "about to write" << passwordContents;
+        int rc = theWallet->writePassword(entryName, passwordContents);
+        if (rc != 0) {
+            std::cout << i18n("Failed to write entry %1 value to %2 wallet", entryName, walletName).toStdString() << std::endl;
+            exit(4);
+        }
+    } else if (kind == Wallet::Map) {
+        const QJsonDocument json = QJsonDocument::fromJson(passwordContents.toLatin1());
+        if (!json.isNull()) {
+            QJsonObject values = json.object();
+            QMap<QString, QString> map;
+            for (auto e : values.keys()) {
+                map.insert(e, values.value(e).toString());
+            }
+            if (verbose) qDebug() << "about to write" << map;
+            int rc = theWallet->writeMap(entryName, map);
+            if (rc != 0) {
+                std::cout << i18n("Failed to write entry %1 value to %2 wallet", entryName, walletName).toStdString() << std::endl;
+                exit(4);
+            }
+        }
     }
     quit();
 }
diff --git a/src/querydriver.h b/src/querydriver.h
index 3f502b2..f5b8b43 100644
--- a/src/querydriver.h
+++ b/src/querydriver.h
@@ -50,7 +50,7 @@ private:
     void readValue();
     void readMapValue();
     void readPasswordValue();
-    void writePasswordValue();
+    void writeValue();
 
 private Q_SLOTS:
     void walletOpened(bool);


More information about the kde-doc-english mailing list