[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    branches/work/sharedkraft/src
From:       Klaas Freitag <freitag () suse ! de>
Date:       2011-01-25 17:28:26
Message-ID: 20110125172826.E302CAC8B9 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1217081 by freitag:

Proper save to an xml file

 M  +1 -0      CMakeLists.txt  
 M  +18 -48    archiveman.cpp  
 M  +1 -1      archiveman.h  
 M  +1 -1      docdigestview.cpp  
 M  +3 -3      docposition.cpp  
 M  +1 -1      doctype.cpp  
 M  +3 -2      documentsaverdb.cpp  
 A             documentsaverfile.cpp   [License: GPL (v2+)]
 A             documentsaverfile.h   [License: GPL (v2+)]
 M  +127 -4    kraftdoc.cpp  
 M  +15 -6     kraftdoc.h  
 M  +4 -0      kraftsettings.kcfg  


--- branches/work/sharedkraft/src/CMakeLists.txt #1217080:1217081
@@ -97,6 +97,7 @@
     positionviewwidget.cpp
     documentsaverbase.cpp
     documentsaverdb.cpp
+    documentsaverfile.cpp
     documentman.cpp
     docdigest.cpp
     docdigestview.cpp
--- branches/work/sharedkraft/src/archiveman.cpp #1217080:1217081
@@ -21,6 +21,7 @@
 #include <QSqlIndex>
 #include <QFile>
 #include <QTextStream>
+#include <QDir>
 
 #include <kstandarddirs.h>
 #include <kdebug.h>
@@ -32,6 +33,7 @@
 #include "dbids.h"
 #include "kraftsettings.h"
 #include "documentman.h"
+#include "documentsaverfile.h"
 
 ArchiveMan *ArchiveMan::self()
 {
@@ -76,63 +78,31 @@
   return re;
 }
 
-QDomElement ArchiveMan::xmlTextElement( QDomDocument doc, const QString& name, const \
QString& value ) +void ArchiveMan::archiveDocumentXml( KraftDoc *doc, const QString& \
archId )  {
-  QDomElement elem = doc.createElement( name );
-  QDomText t = doc.createTextNode( value );
-  elem.appendChild( t );
-  return elem;
-}
-
-QDomDocument ArchiveMan::archiveDocumentXml( KraftDoc *doc, const QString& archId )
-{
-  QDomDocument xmldoc( "kraftdocument" );
-  QDomElement root = xmldoc.createElement( "kraftdocument" );
-  // Fixme:
-  xmldoc.appendChild( root );
-  QDomElement cust = xmldoc.createElement( "client" );
-  root.appendChild( cust );
-  cust.appendChild( xmlTextElement( xmldoc, "address", doc->address() ) );
-  cust.appendChild( xmlTextElement( xmldoc, "clientId", doc->addressUid() ) );
-
-  QDomElement docElem = xmldoc.createElement( "docframe" );
-  root.appendChild( docElem );
-  docElem.appendChild( xmlTextElement( xmldoc, "docType", doc->docType() ) );
-  docElem.appendChild( xmlTextElement( xmldoc, "docDesc", doc->whiteboard() ) );
-  docElem.appendChild( xmlTextElement( xmldoc, "ident", doc->ident() ) );
-  docElem.appendChild( xmlTextElement( xmldoc, "preText", doc->preText() ) );
-  docElem.appendChild( xmlTextElement( xmldoc, "postText", doc->postText() ) );
-  docElem.appendChild( xmlTextElement( xmldoc, "projectLabel", doc->projectLabel() ) \
                );
-  docElem.appendChild( xmlTextElement( xmldoc, "salut", doc->salut() ) );
-  docElem.appendChild( xmlTextElement( xmldoc, "goodbye", doc->goodbye() ) );
-
-  docElem.appendChild( xmlTextElement( xmldoc, "date",
-                                       doc->locale()->formatDate( doc->date() ) ) );
-
-  root.appendChild( doc->positions().domElement( xmldoc ) );
-
-  QString xml = xmldoc.toString();
+  QString xml = doc->xmlDomDocument().toString();
   // kDebug() << "Resulting XML: " << xml << endl;
 
-  QString outputDir = ArchiveMan::self()->xmlBaseDir();
-  QString filename = ArchiveMan::self()->archiveFileName( doc->ident(), archId, \
"xml" ); +  QDir dir( xmlBaseDir() );
+  if( dir.exists() ) {
+    kDebug() << "Archive Dir does not exist, will try to create...";
+    if( !dir.mkpath( dir.absolutePath() ) ) {
+      kDebug() << "Creation of directory failed, aborting";
+      return;
+    }
+  }
+  QString filename  = archiveFileName( doc->ident(), archId, "xml" );
 
-  QString xmlFile = QString( "%1/%2" ).arg( outputDir ).arg( filename );
+  QString xmlFile = QString( "%1/%2" ).arg( dir.absolutePath() ).arg( filename );
 
-  kDebug() << "Storing XML to " << xmlFile << endl;
-
   if ( KraftSettings::self()->doXmlArchive() ) {
-    QFile file( xmlFile );
-    if ( file.open( QIODevice::WriteOnly ) ) {
-      QTextStream stream( &file );
-      stream << xml << "\n";
-      file.close();
+    DocumentSaverFile fs;
+    fs.setFilename( xmlFile );
+    fs.saveDocument( doc );
     } else {
-      kDebug() << "Saving failed" << endl;
+    kDebug() << "Storage of xml disabled by configuration doXmlArchive";
     }
   }
-  return xmldoc ;
-}
 
 dbID ArchiveMan::archiveDocumentDb( KraftDoc *doc )
 {
--- branches/work/sharedkraft/src/archiveman.h #1217080:1217081
@@ -43,7 +43,7 @@
     QString archiveFileName( const QString&, const QString&, const QString& ) const;
 
   protected:
-    virtual QDomDocument archiveDocumentXml( KraftDoc*,  const QString& );
+    virtual void archiveDocumentXml( KraftDoc*,  const QString& );
     virtual dbID archiveDocumentDb( KraftDoc* );
 
   private:
--- branches/work/sharedkraft/src/docdigestview.cpp #1217080:1217081
@@ -521,7 +521,7 @@
   }
 
   item->setText( 1,  clientName );
-  item->setText( 2, doc->locale()->formatDate( doc->lastModified(), \
KLocale::ShortDate ) ); +  item->setText( 2, doc->locale()->formatDateTime( \
doc->lastModified() ) ); // , KLocale::ShortDate ) );  item->setText( 3, \
doc->locale()->formatDate( doc->date(), KLocale::ShortDate ) );    item->setText( 4, \
doc->projectLabel() );  item->setText( 5, doc->ident() );
--- branches/work/sharedkraft/src/docposition.cpp #1217080:1217081
@@ -403,9 +403,9 @@
       posElem.appendChild( xmlTextElement( doc, "unit", e.einheit( am ) ) );
 
       Geld g = dp->unitPrice();
-      posElem.appendChild( xmlTextElement( doc, "unitprice", g.toString( mLocale ) ) \
                );
-
-      posElem.appendChild( xmlTextElement( doc, "sumprice", Geld( g*am).toString( \
mLocale ) ) ); +      posElem.appendChild( xmlTextElement( doc, "unitprice", \
QString::number(g.toDouble())) ); +      g = Geld( g*am );
+      posElem.appendChild( xmlTextElement( doc, "sumprice", QString::number( \
g.toDouble())));  }
   }
   return topElem;
--- branches/work/sharedkraft/src/doctype.cpp #1217080:1217081
@@ -33,7 +33,7 @@
 #include "kraftsettings.h"
 #include "kraftdb.h"
 #include "numbercycle.h"
-#include <kstandarddirs.h>
+
 /**
 @author Klaas Freitag
 */
--- branches/work/sharedkraft/src/documentsaverdb.cpp #1217080:1217081
@@ -262,7 +262,8 @@
 void DocumentSaverDB::load( const QString& id, KraftDoc *doc )
 {
     QSqlQuery q;
-    q.prepare("SELECT ident, docType, clientID, clientAddress, salut, goodbye, date, \
lastModified, language, country, pretext, posttext, docDescription, projectlabel FROM \
document WHERE docID=:docID"); +    q.prepare("SELECT ident, docType, clientID, \
clientAddress, salut, goodbye, date, lastModified," +              "language, \
country, pretext, posttext, docDescription, projectlabel FROM document WHERE \
docID=:docID");  q.bindValue(":docID", id);
     q.exec();
     kDebug() << "Loading document id " << id << endl;
@@ -281,7 +282,7 @@
         doc->setSalut(      q.value( 4    ).toString() );
         doc->setGoodbye(    q.value( 5  ).toString() );
         doc->setDate (      q.value( 6     ).toDate() );
-        doc->setLastModified( q.value( 7 ).toDate() );
+        doc->setLastModified( q.value( 7 ).toDateTime() );
         doc->setCountryLanguage( q.value( 8 ).toString(),
                                  q.value( 9 ).toString());
 
--- branches/work/sharedkraft/src/kraftdoc.cpp #1217080:1217081
@@ -28,6 +28,7 @@
 #include <kabc/stdaddressbook.h>
 #include <kabc/addresseedialog.h>
 #include <kabc/addressee.h>
+#include <kstandarddirs.h>
 
 // application specific includes
 #include "kraftdoc.h"
@@ -35,8 +36,10 @@
 #include "kraftview.h"
 #include "docposition.h"
 #include "documentsaverdb.h"
+#include "documentsaverfile.h"
 #include "defaultprovider.h"
 #include "documentman.h"
+#include "kraftsettings.h"
 
 // FIXME: Make KraftDoc inheriting DocDigest!
 
@@ -53,6 +56,7 @@
 KraftDoc::~KraftDoc()
 {
   delete mLocale;
+  delete mSaver;
 }
 
 KraftDoc& KraftDoc::operator=( KraftDoc& origDoc )
@@ -192,10 +196,10 @@
 
     DocumentSaverBase *saver = getSaver();
     if( saver ) {
-        result = saver->saveDocument( this );
         if ( isNew() ) {
-          setLastModified( QDate::currentDate() );
+          setLastModified( QDateTime::currentDateTime() );
         }
+        result = saver->saveDocument( this );
 
         // We go through the whole document and remove the positions
         // that are to delete because they now were deleted in the
@@ -329,8 +333,32 @@
     if( ! mSaver )
     {
         kDebug() << "Create new Document DB-Saver" << endl;
-        mSaver = new DocumentSaverDB();
+    DocumentSaverFile *saver = new DocumentSaverFile();
+
+    QString outputDir = KraftSettings::self()->xmlDocPath();
+    KStandardDirs stdDirs;
+
+    if ( outputDir.isEmpty() ) {
+      outputDir = stdDirs.saveLocation( "data", "kraft/docXml", true );
     }
+
+    if ( ! outputDir.endsWith( "/" ) ) outputDir += "/";
+
+    QDir dir( outputDir );
+    if( !dir.exists() ) {
+      kDebug() << "Archive Dir does not exist, will try to create...";
+      if( !dir.mkpath( dir.absolutePath() ) ) {
+        kDebug() << "Creation of directory failed, aborting";
+        return 0;
+      }
+    }
+
+    const QString xmlFile = QString( "%1%2.xml" ).arg( outputDir ).arg( "document" \
); +    kDebug() << "Saving XML-Document to " << xmlFile;
+
+    saver->setFilename( xmlFile );
+    mSaver = saver;
+  }
     return mSaver;
 }
 
@@ -390,5 +418,100 @@
   return i18n( "Unknown document part" );
 }
 
+QDomDocument KraftDoc::xmlDomDocument()
+{
+  QDomDocument xmldoc( "kraftdocument" );
+  QDomElement root = xmldoc.createElement( "kraftdocument" );
+  root.setAttribute( "xmlSchemaVersion", 1.0 );
+  // Fixme:
+  xmldoc.appendChild( root );
+  QDomElement cust = xmldoc.createElement( "client" );
+  root.appendChild( cust );
+  cust.appendChild( xmlTextElement( xmldoc, "address", address() ) );
+  cust.appendChild( xmlTextElement( xmldoc, "clientId", addressUid() ) );
 
-#include "kraftdoc.moc"
+  QDomElement meta = xmldoc.createElement( "meta" );
+  root.appendChild( meta );
+  meta.appendChild( xmlTextElement( xmldoc, "lastModified", \
QString::number(lastModified().toMSecsSinceEpoch())) ); +  meta.appendChild( \
xmlTextElement( xmldoc, "owner", qgetenv("USER")) ); +
+  QDomElement docElem = xmldoc.createElement( "docframe" );
+  root.appendChild( docElem );
+  docElem.appendChild( xmlTextElement( xmldoc, "currency",     \
locale()->currencyCode() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"docType",      docType() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"docDesc",      whiteboard() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"ident",        ident() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"preText",      preText() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"postText",     postText() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"projectLabel", projectLabel() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"salut",        salut() ) ); +  docElem.appendChild( xmlTextElement( xmldoc, \
"goodbye",      goodbye() ) ); +
+  docElem.appendChild( xmlTextElement( xmldoc, "date", date().toString( Qt::ISODate \
) ) ); +  root.appendChild( positions().domElement( xmldoc ) );
+
+  QDomElement sums = xmldoc.createElement( "sums");
+  root.appendChild( sums );
+  sums.appendChild( xmlTextElement( xmldoc, "netto", \
QString::number(nettoSum().toDouble()))); +  sums.appendChild( xmlTextElement( \
xmldoc, "brutto", QString::number(bruttoSum().toDouble()))); +  QDomElement taxes = \
xmldoc.createElement( "taxes" ); +  sums.appendChild( taxes );
+  // FIXME: Write a list of tax percentage sums
+  taxes.appendChild( xmlTextElement( xmldoc, "vat", QString::number( \
vatSum().toDouble()))); +
+  return xmldoc ;
+}
+
+QDomElement KraftDoc::xmlTextElement( QDomDocument doc, const QString& name, const \
QString& value ) +{
+  QDomElement elem = doc.createElement( name );
+  QDomText t = doc.createTextNode( encodeXml(value) );
+  elem.appendChild( t );
+  return elem;
+}
+
+QString KraftDoc::encodeXml( const QString& encodeMe )
+{
+  QString temp;
+
+  for (qint32 index(0); index < encodeMe.size(); index++) {
+    QChar character(encodeMe.at(index));
+
+    switch (character.unicode()) {
+    case '&':
+      temp += "&amp;"; break;
+
+    case '\'':
+      temp += "&apos;"; break;
+
+    case '"':
+      temp += "&quot;"; break;
+
+    case '<':
+      temp += "&lt;"; break;
+
+    case '>':
+      temp += "&gt;"; break;
+
+    default:
+      temp += character;
+      break;
+    }
+  }
+
+  return temp;
+}
+
+QString KraftDoc::decodeXml( const QString& decodeMe )
+{
+  QString temp(decodeMe);
+
+  temp.replace("&amp;", "&");
+  temp.replace("&apos;", "'");
+  temp.replace("&quot;", "\"");
+  temp.replace("&lt;", "<");
+  temp.replace("&gt;", ">");
+
+  return temp;
+}
+
--- branches/work/sharedkraft/src/kraftdoc.h #1217080:1217081
@@ -19,9 +19,10 @@
 #define KRAFTDOC_H
 
 // include files for QT
-#include <qobject.h>
-#include <qstring.h>
-#include <qdatetime.h>
+#include <QObject>
+#include <QString>
+#include <QDateTime>
+#include <QDomDocument>
 
 #include "docposition.h"
 #include "dbids.h"
@@ -83,8 +84,8 @@
   QDate date() { return mDate; }
   void setDate( QDate d ) { mDate = d; }
 
-  QDate lastModified() { return mLastModified; }
-  void setLastModified( QDate d ) { mLastModified = d; }
+  QDateTime lastModified() { return mLastModified; }
+  void setLastModified( QDateTime d ) { mLastModified = d; }
 
   QString docType() { return mDocType; }
   void setDocType( const QString& s ) {  mDocType = s; }
@@ -135,6 +136,11 @@
 
   void setCountryLanguage( const QString&, const QString& );
 
+  QDomDocument xmlDomDocument();
+
+  QString encodeXml( const QString& );
+  QString decodeXml( const QString& );
+
   public slots:
   /** calls redrawDocument() on all views connected to the document object and is 
    *  called by the view by which the document has been changed.
@@ -147,9 +153,11 @@
   void slotRemovePosition( int );
   void slotMoveUpPosition( int );
   void slotMoveDownPosition( int );
+
 private:
   /** the list of the views currently connected to the document */
   QList<KraftView*> pViewList;
+  QDomElement xmlTextElement( QDomDocument, const QString&, const QString& );
 
   DocumentSaverBase* getSaver( const QString& saverHint = QString() );
   /** the modified flag of the current document */
@@ -173,11 +181,12 @@
   KLocale *mLocale;
 
   QDate   mDate;
-  QDate   mLastModified;
+  QDateTime          mLastModified;
   DocPositionList mPositions;
   DBIdList mRemovePositions;
   DocumentSaverBase *mSaver;
   dbID    mDocID;
+
 };
 
 #endif // KraftDoc_H
--- branches/work/sharedkraft/src/kraftsettings.kcfg #1217080:1217081
@@ -15,6 +15,10 @@
     <entry name="XmlArchivePath" type="String">
       <label>Where Kraft stores the XML archive documents. If empty, \
KDEHOME/share/apps is used.</label>  </entry>
+    <entry name="XmlDocPath" type="String">
+      <label>Where Kraft stores the XML documents. If empty, KDEHOME/share/apps is \
used.</label> +    </entry>
+
   </group>
 
   <group name="WindowSizes">


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic