]> Git trees. - libqmvoc.git/commitdiff
A class for writing delimited text
authorPeter Hedlund <peter@peterandlinda.com>
Wed, 28 Feb 2007 19:16:12 +0000 (19:16 +0000)
committerPeter Hedlund <peter@peterandlinda.com>
Wed, 28 Feb 2007 19:16:12 +0000 (19:16 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=638079

kdeeducore/CMakeLists.txt
kdeeducore/keduvoccsvwriter.cpp [new file with mode: 0644]
kdeeducore/keduvoccsvwriter.h [new file with mode: 0644]
kdeeducore/keduvocdocument.cpp

index 594d2699c1c956fd80864cf952ae430bc2f5fd49..2a0afbc96c3f2938a61e4af91bac2eb17690203e 100644 (file)
@@ -3,6 +3,7 @@
 
 set(kdeeducore_LIB_SRCS
    keduvoccsvreader.cpp
+   keduvoccsvwriter.cpp
    keduvocdocument.cpp
    keduvocexpression.cpp
    keduvocgrammar.cpp
@@ -36,6 +37,7 @@ install(TARGETS kdeeducore  DESTINATION ${LIB_INSTALL_DIR} )
 install(FILES
    libkdeedu_core_export.h
    keduvoccsvreader.h
+   keduvoccsvwriter.h
    keduvocdocument.h
    keduvocexpression.h
    keduvocgrammar.h
diff --git a/kdeeducore/keduvoccsvwriter.cpp b/kdeeducore/keduvoccsvwriter.cpp
new file mode 100644 (file)
index 0000000..9be56eb
--- /dev/null
@@ -0,0 +1,76 @@
+/***************************************************************************
+                   export a KEduVocDocument to a delimited text file
+    -----------------------------------------------------------------------
+    copyright           : (C) 1999-2001 Ewald Arnold
+                          (C) 2001 The KDE-EDU team
+                          (C) 2007 Peter Hedlund <peter.hedlund@kdemail.net>
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <QTextStream>
+
+#include <KLocale>
+
+#include "keduvoccsvwriter.h"
+#include "keduvocdocument.h"
+#include "keduvocexpression.h"
+
+KEduVocCsvWriter::KEduVocCsvWriter(QFile *file)
+{
+  // the file must be already open
+  m_outputFile = file;
+}
+
+
+bool KEduVocCsvWriter::writeDoc(KEduVocDocument *doc, const QString &generator)
+{
+  Q_UNUSED(generator);
+
+  m_doc = doc;
+
+  QString separator = ",";
+
+  QTextStream outputStream;
+  outputStream.setDevice(m_outputFile);
+  outputStream.setCodec("UTF-8");
+
+  outputStream << i18n("! Title:")  << separator << m_doc->title() << "\n";
+  outputStream << i18n("! Author:") << separator << m_doc->author() << "\n";
+
+  KEduVocExpression *expression;
+  int idCount = m_doc->identifierCount();
+  QString currentRow;
+
+  for (int e = 0; e < m_doc->entryCount(); e++)
+  {
+    expression = m_doc->entry(e);
+    currentRow = "";
+    bool sep = false;
+
+    for (int i = 0; i < idCount; i++) {
+      if (!sep)
+        sep = true;
+      else
+        currentRow += separator;
+
+      if (i == 0)
+        currentRow += expression->original();
+      else
+        currentRow += expression->translation(i);
+    }
+
+    if (!currentRow.isEmpty())
+      outputStream << currentRow << "\n";
+  }
+
+  return true;
+}
+
diff --git a/kdeeducore/keduvoccsvwriter.h b/kdeeducore/keduvoccsvwriter.h
new file mode 100644 (file)
index 0000000..dcfb566
--- /dev/null
@@ -0,0 +1,39 @@
+/***************************************************************************
+                    export a KEduVocDocument to a delimited text file
+    -----------------------------------------------------------------------
+    copyright           : (C) 1999-2001 Ewald Arnold
+                          (C) 2001 The KDE-EDU team
+                          (C) 2007 Peter Hedlund <peter.hedlund@kdemail.net>
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef KEDUVOCCSVWRITER_H
+#define KEDUVOCCSVWRITER_H
+
+#include <libkdeedu_core_export.h>
+
+#include <QFile>
+
+class KEduVocDocument;
+
+class KDEEDUCORE_EXPORT KEduVocCsvWriter
+{
+public:
+  KEduVocCsvWriter(QFile *file);
+
+  bool writeDoc(KEduVocDocument *doc, const QString &generator);
+
+private:
+  QFile *m_outputFile;
+  KEduVocDocument *m_doc;
+};
+
+#endif
index 3fa26c100990d0647bd63db00b0673780f4b0ebb..419d0464bd02d4a73f68d5103270f62538dc2ec2 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "keduvockvtmlwriter.h"
 #include "keduvoccsvreader.h"
+#include "keduvoccsvwriter.h"
 #include "keduvockvtmlreader.h"
 #include "keduvocwqlreader.h"
 #include "keduvocpaukerreader.h"
@@ -270,7 +271,7 @@ bool KEduVocDocument::open(const KUrl& url, bool /*append*/)
 
 bool KEduVocDocument::saveAs(QObject *parent, const KUrl & url, FileType ft, const QString & generator)
 {
-  connect( this, SIGNAL(progressChanged(KEduVocDocument*,int)), parent, SLOT(slotProgress(KEduVocDocument*,int)) );
+  connect(this, SIGNAL(progressChanged(KEduVocDocument*, int)), parent, SLOT(slotProgress(KEduVocDocument*, int)));
 
   KUrl tmp (url);
 
@@ -288,9 +289,9 @@ bool KEduVocDocument::saveAs(QObject *parent, const KUrl & url, FileType ft, con
   }
 
   bool saved = false;
+
   while (!saved)
   {
-
     QFile f(tmp.path());
 
     if (!f.open(QIODevice::WriteOnly))
@@ -308,8 +309,8 @@ bool KEduVocDocument::saveAs(QObject *parent, const KUrl & url, FileType ft, con
       break;
 
       case csv: {
-        QTextStream os( &f );                       // serialize using f
-        //TODO saved = saveToCsv(os, title);
+        KEduVocCsvWriter csvWriter(&f);
+        saved = csvWriter.writeDoc(this, generator);
       }
       break;
 
@@ -322,8 +323,6 @@ bool KEduVocDocument::saveAs(QObject *parent, const KUrl & url, FileType ft, con
     QApplication::restoreOverrideCursor();
 
     if (!saved) {
-      // TODO new writers provide an explicite error message
-      // the two messages should be merged
       QString msg = i18n("Could not save \"%1\"\nDo you want to try again?", tmp.path());
       int result = KMessageBox::warningContinueCancel(0, msg, i18n("Error Saving File"), KGuiItem(i18n("&Retry")));
       if (result == KMessageBox::Cancel)