kolabformat/xmlobject.cpp kolabformat/xmlobject.h

Christian Mollekopf mollekopf at kolabsys.com
Wed Nov 7 12:01:22 CET 2012


 kolabformat/xmlobject.cpp |   78 ++++++++++++++++++++++++++++++++++++++--------
 kolabformat/xmlobject.h   |   20 +++++++----
 2 files changed, 78 insertions(+), 20 deletions(-)

New commits:
commit 4df3f3c1c848ca94101a4364d65be6bad3b11319
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Wed Nov 7 12:01:11 2012 +0100

    Generate a UID if missing also for the v2 format, and make the generated UID accessible.

diff --git a/kolabformat/xmlobject.cpp b/kolabformat/xmlobject.cpp
index be6cbb8..4b4b15f 100644
--- a/kolabformat/xmlobject.cpp
+++ b/kolabformat/xmlobject.cpp
@@ -22,6 +22,7 @@
 #include "conversion/kolabconversion.h"
 #include "conversion/commonconversion.h"
 #include "conversion/kabcconversion.h"
+#include <QUuid>
 
 namespace Kolab {
 
@@ -29,22 +30,33 @@ XMLObject::XMLObject()
 {
 
 }
+
+std::string XMLObject::getSerializedUID() const
+{
+    return mWrittenUID;
+}
     
 std::vector< std::string > XMLObject::getAttachments() const
 {
     return mAttachments;
 }
 
-
 std::string XMLObject::writeEvent(const Event &event, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version == KolabV2) {
         const KCalCore::Event::Ptr i = Conversion::toKCalCore(event);
+        if (i->uid().isEmpty()) {
+            i->setUid(QUuid::createUuid().toString());
+        }
+        mWrittenUID = Conversion::toStdString(i->uid().toUtf8().constData());
         //The timezone is used for created and last modified dates
         const QString &xml = KolabV2::Event::eventToXML(i, QLatin1String("UTC"));
         return Conversion::toStdString(xml);
     }
-    return Kolab::writeEvent(event, productId);
+    const std::string result = Kolab::writeEvent(event, productId);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 Event XMLObject::readEvent(const std::string& s, Version version)
@@ -63,13 +75,20 @@ Event XMLObject::readEvent(const std::string& s, Version version)
 
 std::string XMLObject::writeTodo(const Todo &event, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version == KolabV2) {
         const KCalCore::Todo::Ptr i = Conversion::toKCalCore(event);
+        if (i->uid().isEmpty()) {
+            i->setUid(QUuid::createUuid().toString());
+        }
+        mWrittenUID = Conversion::toStdString(i->uid().toUtf8().constData());
         //The timezone is used for created and last modified dates
         const QString &xml = KolabV2::Task::taskToXML(i, QLatin1String("UTC"));
         return Conversion::toStdString(xml);
     }
-    return Kolab::writeTodo(event, productId);
+    const std::string result = Kolab::writeTodo(event, false);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 Todo XMLObject::readTodo(const std::string& s, Version version)
@@ -88,13 +107,20 @@ Todo XMLObject::readTodo(const std::string& s, Version version)
 
 std::string XMLObject::writeJournal(const Journal &event, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version == KolabV2) {
         const KCalCore::Journal::Ptr i = Conversion::toKCalCore(event);
+        if (i->uid().isEmpty()) {
+            i->setUid(QUuid::createUuid().toString());
+        }
+        mWrittenUID = Conversion::toStdString(i->uid().toUtf8().constData());
         //The timezone is used for created and last modified dates
         const QString &xml = KolabV2::Journal::journalToXML(i, QLatin1String("UTC"));
         return Conversion::toStdString(xml);
     }
-    return Kolab::writeJournal(event, productId);
+    const std::string result = Kolab::writeJournal(event, false);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 Journal XMLObject::readJournal(const std::string& s, Version version)
@@ -113,11 +139,14 @@ Journal XMLObject::readJournal(const std::string& s, Version version)
 
 std::string XMLObject::writeFreebusy(const Freebusy &event, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version != KolabV3) {
         Critical() << "only v3 implementation available";
         return std::string();
     }
-    return Kolab::writeFreebusy(event, productId);
+    const std::string result = Kolab::writeFreebusy(event, productId);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 Freebusy XMLObject::readFreebusy(const std::string& s, Version version)
@@ -162,14 +191,21 @@ Contact XMLObject::readContact(const std::string& s, Version version)
 
 std::string XMLObject::writeContact(const Contact &contact, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version == KolabV2) {
         //FIXME attachment names are hardcoded for now
-        const KABC::Addressee addressee = Conversion::toKABC(contact);
+        KABC::Addressee addressee = Conversion::toKABC(contact);
+        if (addressee.uid().isEmpty()) {
+            addressee.setUid(QUuid::createUuid().toString());
+        }
+        mWrittenUID = Conversion::toStdString(addressee.uid().toUtf8().constData());
         const KolabV2::Contact contact(&addressee);
         const QByteArray xml = contact.saveXML().toUtf8();
         return std::string(xml.constData());
     }
-    return Kolab::writeContact(contact, productId);
+    const std::string result = Kolab::writeContact(contact, productId);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 DistList XMLObject::readDistlist(const std::string& s, Version version)
@@ -184,13 +220,20 @@ DistList XMLObject::readDistlist(const std::string& s, Version version)
 
 std::string XMLObject::writeDistlist(const DistList &distlist, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version == KolabV2) {
-        const KABC::ContactGroup contactGroup = Conversion::toKABC(distlist);
+        KABC::ContactGroup contactGroup = Conversion::toKABC(distlist);
+        if (contactGroup.id().isEmpty()) {
+            contactGroup.setId(QUuid::createUuid().toString());
+        }
+        mWrittenUID = Conversion::toStdString(contactGroup.id().toUtf8().constData());
         const KolabV2::DistributionList d(&contactGroup);
         const QByteArray xml = d.saveXML().toUtf8();
         return std::string(xml.constData());
     }
-    return Kolab::writeDistlist(distlist, productId);
+    const std::string result = Kolab::writeDistlist(distlist, productId);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 Note XMLObject::readNote(const std::string& s, Version version)
@@ -204,12 +247,20 @@ Note XMLObject::readNote(const std::string& s, Version version)
 
 std::string XMLObject::writeNote(const Note &note, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version == KolabV2) {
-        const KMime::Message::Ptr n = Conversion::toNote(note);
+        Note noteWithUID = note;
+        if (noteWithUID.uid().empty()) {
+            noteWithUID.setUid(Conversion::toStdString(QUuid::createUuid().toString()));
+        }
+        mWrittenUID = noteWithUID.uid();
+        const KMime::Message::Ptr n = Conversion::toNote(noteWithUID);
         const QByteArray &xml = noteToKolabXML(n);
         return std::string(xml.constData());
     }
-    return Kolab::writeNote(note, productId);
+    const std::string result = Kolab::writeNote(note, productId);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
 Configuration XMLObject::readConfiguration(const std::string& s, Version version)
@@ -234,11 +285,14 @@ Configuration XMLObject::readConfiguration(const std::string& s, Version version
 
 std::string XMLObject::writeConfiguration(const Configuration &configuration, Version version, const std::string& productId)
 {
+    mWrittenUID.clear();
     if (version != KolabV3) {
         Critical() << "only v3 implementation available";
         return std::string();
     }
-    return Kolab::writeConfiguration(configuration, productId);
+    const std::string result = Kolab::writeConfiguration(configuration, productId);
+    mWrittenUID = Kolab::getSerializedUID();
+    return result;
 }
 
     
diff --git a/kolabformat/xmlobject.h b/kolabformat/xmlobject.h
index 1496bbf..6bb4149 100644
--- a/kolabformat/xmlobject.h
+++ b/kolabformat/xmlobject.h
@@ -35,20 +35,23 @@ class KOLAB_EXPORT XMLObject
 {
 public:
     explicit XMLObject();
+
+    std::string getSerializedUID() const;
+    
     ///List of attachment names to be retrieved from the mime message (only when reading v2, for v3 attachments containing the cid: of the attachment-part are created )
     std::vector<std::string> getAttachments() const;
     
     Kolab::Event readEvent(const std::string& s, Kolab::Version version);
-    static std::string writeEvent(const Kolab::Event &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeEvent(const Kolab::Event &, Kolab::Version version, const std::string& productId = std::string());
     
     Kolab::Todo readTodo(const std::string& s, Kolab::Version version);
-    static std::string writeTodo(const Kolab::Todo &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeTodo(const Kolab::Todo &, Kolab::Version version, const std::string& productId = std::string());
 
     Kolab::Journal readJournal(const std::string& s, Kolab::Version version);
-    static std::string writeJournal(const Kolab::Journal &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeJournal(const Kolab::Journal &, Kolab::Version version, const std::string& productId = std::string());
 
     Kolab::Freebusy readFreebusy(const std::string& s, Kolab::Version version);
-    static std::string writeFreebusy(const Kolab::Freebusy &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeFreebusy(const Kolab::Freebusy &, Kolab::Version version, const std::string& productId = std::string());
 
     std::string pictureAttachmentName() const;
     std::string logoAttachmentName() const;
@@ -68,26 +71,27 @@ public:
      * ** kolab-logo.png
      * ** sound
      */
-    static std::string writeContact(const Kolab::Contact &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeContact(const Kolab::Contact &, Kolab::Version version, const std::string& productId = std::string());
     
     Kolab::DistList readDistlist(const std::string& s, Kolab::Version version);
-    static std::string writeDistlist(const Kolab::DistList &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeDistlist(const Kolab::DistList &, Kolab::Version version, const std::string& productId = std::string());
 
     /**
      * V2 notes:
      * * set the creation date from the mime date header.
      */
     Kolab::Note readNote(const std::string& s, Kolab::Version version);
-    static std::string writeNote(const Kolab::Note &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeNote(const Kolab::Note &, Kolab::Version version, const std::string& productId = std::string());
 
     Kolab::Configuration readConfiguration(const std::string& s, Kolab::Version version);
-    static std::string writeConfiguration(const Kolab::Configuration &, Kolab::Version version, const std::string& productId = std::string());
+    std::string writeConfiguration(const Kolab::Configuration &, Kolab::Version version, const std::string& productId = std::string());
     
 private:
     std::vector<std::string> mAttachments;
     std::string mLogoAttachmentName;
     std::string mSoundAttachmentName;
     std::string mPictureAttachmentName;
+    std::string mWrittenUID;
 };
 
 }





More information about the commits mailing list