[office/tellico] /: Allow the DBUS interface to export to stdout

Robby Stephenson null at kde.org
Thu Mar 14 14:59:46 GMT 2024


Git commit eb223f81f53a041ca36d2ed8a23caad29a892499 by Robby Stephenson.
Committed on 14/03/2024 at 14:59.
Pushed by rstephenson into branch 'master'.

Allow the DBUS interface to export to stdout

Using a file name of '--' will result in the text being piped to stdout.
With a running instance of Tellico, the dbus command would be:
  qdbus-qt5 org.kde.tellico /Tellico org.kde.tellico.exportXML --

CCBUG: 479961

M  +5    -1    doc/advanced.docbook
M  +22   -9    src/core/filehandler.cpp
M  +2    -0    src/core/filehandler.h
M  +5    -1    src/dbusinterface.cpp

https://invent.kde.org/office/tellico/-/commit/eb223f81f53a041ca36d2ed8a23caad29a892499

diff --git a/doc/advanced.docbook b/doc/advanced.docbook
index 71a52bba..d901d5c6 100644
--- a/doc/advanced.docbook
+++ b/doc/advanced.docbook
@@ -62,7 +62,11 @@ bool showEntry(int id)
 </programlisting>
 
 <para>
-For the four import commands, the first argument is the file to import, and the second is the import action. Three actions are available: <emphasis>replace</emphasis>, <emphasis>append</emphasis>, and <emphasis>merge</emphasis>. Four file formats are supported for importing: Tellico &XML; files, Bibtex files, MODS files, and RIS files.
+For the four import commands, the first argument is the file to import, and the second is the import action. Three actions are available: <emphasis>replace</emphasis>, <emphasis>append</emphasis>, and <emphasis>merge</emphasis>. Four file formats are supported for importing: Tellico &XML; files, Bibtex files, MODS files, and RIS files. Metadata from <link linkend="importing-pdf">&PDF;</link> files can also be imported.
+</para>
+
+<para>
+For any of the commands to export text, a file name of <filename>--</filename> will pipe to the standard output.
 </para>
 
 <para>
diff --git a/src/core/filehandler.cpp b/src/core/filehandler.cpp
index 40f96168..1a871cb9 100644
--- a/src/core/filehandler.cpp
+++ b/src/core/filehandler.cpp
@@ -41,6 +41,7 @@
 #include <QUrl>
 #include <QDomDocument>
 #include <QFile>
+#include <QDir>
 #include <QTextStream>
 #include <QTemporaryFile>
 #include <QSaveFile>
@@ -224,6 +225,14 @@ bool FileHandler::writeTextURL(const QUrl& url_, const QString& text_, bool enco
   }
 
   if(url_.isLocalFile()) {
+    // push to stdout _if_ file name is '--' AND is same as current path
+    // as is used in dbusinterface
+    if(url_.fileName() == QLatin1String("--") &&
+       url_.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash).path() == QDir::currentPath()) {
+      QTextStream ts(stdout);
+      writeTextStream(ts, text_, encodeUTF8_);
+      return true;
+    }
     QSaveFile f(url_.toLocalFile());
     f.open(QIODevice::WriteOnly);
     if(f.error() != QFile::NoError) {
@@ -264,23 +273,27 @@ bool FileHandler::writeTextURL(const QUrl& url_, const QString& text_, bool enco
 
 bool FileHandler::writeTextFile(QSaveFile& file_, const QString& text_, bool encodeUTF8_) {
   QTextStream ts(&file_);
+  writeTextStream(ts, text_, encodeUTF8_);
+  file_.flush();
+  const bool success = file_.commit();
+  if(!success) {
+    myLog() << "Failed to write text file:" << file_.error();
+  }
+  return success;
+}
+
+void FileHandler::writeTextStream(QTextStream& ts_, const QString& text_, bool encodeUTF8_) {
   if(encodeUTF8_) {
 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
-    ts.setCodec("UTF-8");
+    ts_.setCodec("UTF-8");
 #else
-    ts.setEncoding(QStringConverter::Utf8);
+    ts_.setEncoding(QStringConverter::Utf8);
 #endif
   }
   // KDE Bug 380832. If string is longer than MAX_TEXT_CHUNK_WRITE_SIZE characters, split into chunks.
   for(int i = 0; i < text_.length(); i += MAX_TEXT_CHUNK_WRITE_SIZE) {
-    ts << text_.mid(i, MAX_TEXT_CHUNK_WRITE_SIZE);
+    ts_ << text_.mid(i, MAX_TEXT_CHUNK_WRITE_SIZE);
   }
-  file_.flush();
-  const bool success = file_.commit();
-  if(!success) {
-    myLog() << "Failed to write text file:" << file_.error();
-  }
-  return success;
 }
 
 bool FileHandler::writeDataURL(const QUrl& url_, const QByteArray& data_, bool force_, bool quiet_) {
diff --git a/src/core/filehandler.h b/src/core/filehandler.h
index dee4afea..b1c4e32c 100644
--- a/src/core/filehandler.h
+++ b/src/core/filehandler.h
@@ -37,6 +37,7 @@ namespace KIO {
 class QDomDocument;
 class QIODevice;
 class QSaveFile;
+class QTextStream;
 
 namespace Tellico {
   class ImageFactory;
@@ -167,6 +168,7 @@ private:
    * @return A boolean indicating success
    */
   static bool writeTextFile(QSaveFile& file, const QString& text, bool encodeUTF8);
+  static void writeTextStream(QTextStream& ts, const QString& text, bool encodeUTF8);
   /**
    * Writes data to a file.
    *
diff --git a/src/dbusinterface.cpp b/src/dbusinterface.cpp
index a89fae52..8b904a35 100644
--- a/src/dbusinterface.cpp
+++ b/src/dbusinterface.cpp
@@ -88,7 +88,11 @@ bool ApplicationInterface::importFile(Tellico::Import::Format format, const QStr
 
 bool ApplicationInterface::exportCollection(Tellico::Export::Format format, const QString& file, bool filtered) {
   const QUrl url = QUrl::fromUserInput(file, QDir::currentPath(), QUrl::AssumeLocalFile);
-  myLog() << "Exporting collection to" << url.toDisplayString(QUrl::PreferLocalFile);
+  if(file == QLatin1String("--")) {
+    myLog() << "Exporting collection to stdout";
+  } else {
+    myLog() << "Exporting collection to" << url.toDisplayString(QUrl::PreferLocalFile);
+  }
   return m_mainWindow->exportCollection(format, url, filtered);
 }
 



More information about the kde-doc-english mailing list