3 commits - kolabformat/errorhandler.cpp kolabformat/errorhandler.h kolabformatV2/distributionlist.cpp kolabformatV2/distributionlist.h tests/CMakeLists.txt tests/debugstreamtest.cpp tests/debugstreamtest.h tests/legacyformattest.cpp tests/legacyformattest.h tests/testfiles

Christian Mollekopf mollekopf at kolabsys.com
Thu Apr 11 19:26:30 CEST 2013


 kolabformat/errorhandler.cpp                    |   36 ++++++++++++
 kolabformat/errorhandler.h                      |   67 ++++++++++--------------
 kolabformatV2/distributionlist.cpp              |   26 +++++++--
 kolabformatV2/distributionlist.h                |    1 
 tests/CMakeLists.txt                            |    2 
 tests/debugstreamtest.cpp                       |   56 ++++++++++++++++++++
 tests/debugstreamtest.h                         |   32 +++++++++++
 tests/legacyformattest.cpp                      |   58 ++++++++++++++++++++
 tests/legacyformattest.h                        |   31 +++++++++++
 tests/testfiles/v2/contacts/distlistWithUID.xml |   21 +++++++
 10 files changed, 285 insertions(+), 45 deletions(-)

New commits:
commit a220c403e31f9e102d28d8cc5dfa94ae090cba4c
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Thu Apr 11 19:26:13 2013 +0200

    Support for uid's in kolabv2

diff --git a/kolabformatV2/distributionlist.cpp b/kolabformatV2/distributionlist.cpp
index 34379ef..6fe6873 100644
--- a/kolabformatV2/distributionlist.cpp
+++ b/kolabformatV2/distributionlist.cpp
@@ -78,6 +78,8 @@ void KolabV2::DistributionList::loadDistrListMember( const QDomElement& element
         member.displayName = e.text();
       else if ( tagName == "smtp-address" )
         member.email = e.text();
+      else if ( tagName == "uid" )
+        member.uid = e.text();
     }
   }
   mDistrListMembers.append( member );
@@ -90,8 +92,12 @@ void DistributionList::saveDistrListMembers( QDomElement& element ) const
     QDomElement e = element.ownerDocument().createElement( "member" );
     element.appendChild( e );
     const Member& m = *it;
-    writeString( e, "display-name", m.displayName );
-    writeString( e, "smtp-address", m.email );
+    if (!m.uid.isEmpty()) {
+      writeString( e, "uid", m.uid );
+    } else {
+      writeString( e, "display-name", m.displayName );
+      writeString( e, "smtp-address", m.email );
+    }
   }
 }
 
@@ -192,8 +198,13 @@ void DistributionList::setFields( const KABC::ContactGroup* contactGroup )
 
     mDistrListMembers.append( m );
   }
-  if (contactGroup->contactReferenceCount() > 0) {
-    kWarning() << "Tried to save contact references, which should have been resolved already";
+  for ( uint index = 0; index < contactGroup->contactReferenceCount(); ++index ) {
+    const KABC::ContactGroup::ContactReference& data = contactGroup->contactReference( index );
+
+    Member m;
+    m.uid = data.uid();
+
+    mDistrListMembers.append( m );
   }
   if (contactGroup->contactGroupReferenceCount() > 0) {
     kWarning() << "Tried to save contact group references, which should have been resolved already";
@@ -209,8 +220,11 @@ void DistributionList::saveTo( KABC::ContactGroup* contactGroup )
 
   QList<Member>::ConstIterator mit = mDistrListMembers.constBegin();
   for ( ; mit != mDistrListMembers.constEnd(); ++mit ) {
-    const KABC::ContactGroup::Data data( (*mit).displayName, (*mit).email );
-    contactGroup->append( data );
+    if (!(*mit).uid.isEmpty()) {
+      contactGroup->append(KABC::ContactGroup::ContactReference( (*mit).uid ));
+    } else {
+      contactGroup->append(KABC::ContactGroup::Data( (*mit).displayName, (*mit).email ));
+    }
   }
 }
 
diff --git a/kolabformatV2/distributionlist.h b/kolabformatV2/distributionlist.h
index c0a42e0..f646540 100644
--- a/kolabformatV2/distributionlist.h
+++ b/kolabformatV2/distributionlist.h
@@ -86,6 +86,7 @@ private:
   struct Member {
     QString displayName;
     QString email;
+    QString uid;
   };
   QList<Member> mDistrListMembers;
 };
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 6e53353..ef7a37f 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -24,6 +24,7 @@ addTest(timezonetest)
 addTest(mimeobjecttest)
 addTest(xmlobjecttest)
 addTest(debugstreamtest)
+addTest(legacyformattest)
 
 if(PHP_BINDINGS)
     find_path(PHP_KOLABFORMAT_PATH NAMES kolabformat.php PATHS /usr/local/lib/php/modules /usr/lib/php/modules PATH_SUFFIXES lib/php/modules lib64/php/modules)
diff --git a/tests/legacyformattest.cpp b/tests/legacyformattest.cpp
new file mode 100644
index 0000000..dfb0a61
--- /dev/null
+++ b/tests/legacyformattest.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012  Christian Mollekopf <mollekopf at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "legacyformattest.h"
+#include "kolabformat/xmlobject.h"
+#include "kolabformat/errorhandler.h"
+#include "testutils.h"
+
+#include <QTest>
+#include <fstream>
+#include <sstream>
+
+void V2Test::testReadDistlistUID()
+{
+    std::ifstream t((TESTFILEDIR.toStdString()+"v2/contacts/distlistWithUID.xml").c_str());
+    std::stringstream buffer;
+    buffer << t.rdbuf();
+    
+    Kolab::XMLObject xo;
+    const Kolab::DistList distlist = xo.readDistlist(buffer.str(), Kolab::KolabV2);
+    foreach (const Kolab::ContactReference &contact, distlist.members()) {
+        QVERIFY(!contact.uid().empty());
+    }
+    QVERIFY(!Kolab::ErrorHandler::errorOccured());
+}
+
+void V2Test::testWriteDistlistUID()
+{
+    Kolab::DistList distlist;
+    distlist.setUid("uid");
+    distlist.setName("name");
+    std::vector<Kolab::ContactReference> members;
+    members.push_back(Kolab::ContactReference(Kolab::ContactReference::UidReference, "memberuid", "membername"));
+    distlist.setMembers(members);
+    
+    Kolab::XMLObject xo;
+    const std::string xml = xo.writeDistlist(distlist, Kolab::KolabV2);
+    QVERIFY(QString::fromStdString(xml).contains("memberuid"));
+    QVERIFY(!Kolab::ErrorHandler::errorOccured());
+}
+
+QTEST_MAIN( V2Test )
+
+#include "legacyformattest.moc"
\ No newline at end of file
diff --git a/tests/legacyformattest.h b/tests/legacyformattest.h
new file mode 100644
index 0000000..42c6de3
--- /dev/null
+++ b/tests/legacyformattest.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012  Christian Mollekopf <mollekopf at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef V2TEST_H
+#define V2TEST_H
+
+#include <QObject>
+
+class V2Test: public QObject
+{
+    Q_OBJECT
+private slots:
+    void testReadDistlistUID();
+    void testWriteDistlistUID();
+};
+
+#endif // V2TEST_H
diff --git a/tests/testfiles/v2/contacts/distlistWithUID.xml b/tests/testfiles/v2/contacts/distlistWithUID.xml
new file mode 100644
index 0000000..74a41b9
--- /dev/null
+++ b/tests/testfiles/v2/contacts/distlistWithUID.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<distribution-list version="1.0">
+  <uid>ebb1774429a2e03afafb31f233e23b42</uid>
+  <body></body>
+  <categories></categories>
+  <creation-date>2010-11-25T18:02:32Z</creation-date>
+  <last-modification-date>2011-07-23T09:06:38Z</last-modification-date>
+  <sensitivity>public</sensitivity>
+  <product-id>Horde::Kolab</product-id>
+  <display-name>Another lista</display-name>
+  <member>
+    <display-name>Another  User</display-name>
+    <smtp-address>other at debian-vm.local</smtp-address>
+    <uid>a2cfdc52365ef429042413bf7717dc85</uid>
+  </member>
+  <member>
+    <display-name>Sample A. User Jr.</display-name>
+    <smtp-address>sample at debian-vm.local</smtp-address>
+    <uid>f538c7e9ad5a63e4452b7db3bc291231</uid>
+  </member>
+</distribution-list>
\ No newline at end of file


commit 1ac387f8e8f9606b9892cfe6b7a69b33a9557abb
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Thu Apr 11 19:12:21 2013 +0200

    qdebug serializer for std::string

diff --git a/kolabformat/errorhandler.cpp b/kolabformat/errorhandler.cpp
index f53eb81..91021f4 100644
--- a/kolabformat/errorhandler.cpp
+++ b/kolabformat/errorhandler.cpp
@@ -26,6 +26,12 @@
 
 #include <kolabformat.h>
 
+QDebug operator<<(QDebug dbg, const std::string &s)
+{
+    dbg.nospace() << QString::fromStdString(s);
+    return dbg.space();
+}
+
 namespace Kolab {
 
 DebugStream::DebugStream()
diff --git a/kolabformat/errorhandler.h b/kolabformat/errorhandler.h
index bf32d85..2a3d34e 100644
--- a/kolabformat/errorhandler.h
+++ b/kolabformat/errorhandler.h
@@ -134,4 +134,7 @@ private:
 #define Critical() Kolab::ErrorHandler::debugStream(Kolab::ErrorHandler::Critical, __LINE__, __FILE__)
 
 }
+
+QDebug operator<<(QDebug dbg, const std::string &s);
+
 #endif // ERRORHANDLER_H


commit eed6b316aa83ca005cfd7520602ea1b752afe645
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Thu Apr 11 16:02:29 2013 +0200

    Use qDebug based debugstream to reuse qDebug serializers.

diff --git a/kolabformat/errorhandler.cpp b/kolabformat/errorhandler.cpp
index 110486e..f53eb81 100644
--- a/kolabformat/errorhandler.cpp
+++ b/kolabformat/errorhandler.cpp
@@ -28,6 +28,22 @@
 
 namespace Kolab {
 
+DebugStream::DebugStream()
+:   QIODevice()
+{
+    open(WriteOnly);
+}
+
+DebugStream::~DebugStream(){}
+
+qint64 DebugStream::writeData(const char *data, qint64 len) {
+    const QByteArray buf = QByteArray::fromRawData(data, len);
+//         qt_message_output(QtDebugMsg, buf.trimmed().constData());
+    ErrorHandler::instance().addError(m_severity, buf, m_location);
+    return len;
+}
+
+
 QMutex mutex;
     
 void logMessage(const QString &message, const QString &file, int line, ErrorHandler::Severity s)
@@ -35,6 +51,20 @@ void logMessage(const QString &message, const QString &file, int line, ErrorHand
     ErrorHandler::instance().addError(s, message, file+" "+QString::number(line));
 }
 
+ErrorHandler::ErrorHandler()
+:   m_worstError(Debug),
+    m_debugStream(new DebugStream)
+{
+
+};
+
+QDebug ErrorHandler::debugStream(ErrorHandler::Severity severity, int line, const char* file)
+{
+    QMutexLocker locker(&mutex);
+    ErrorHandler::instance().m_debugStream->m_location = QString(QString(file) + "(" + QString::number(line)+")");
+    ErrorHandler::instance().m_debugStream->m_severity = severity;
+    return QDebug(ErrorHandler::instance().m_debugStream.data());
+}
 
 void ErrorHandler::addError(ErrorHandler::Severity s, const QString& message, const QString &location)
 {
diff --git a/kolabformat/errorhandler.h b/kolabformat/errorhandler.h
index 2ce1694..bf32d85 100644
--- a/kolabformat/errorhandler.h
+++ b/kolabformat/errorhandler.h
@@ -23,9 +23,11 @@
 
 #include <QString>
 #include <QList>
+#include <QDebug>
 
 namespace Kolab {
 
+class DebugStream;
 /**
  * Kolab Error Handler
  * 
@@ -87,14 +89,20 @@ public:
         return false;
     }
     
+    /**
+     * Returns a debug stream to which logs errors
+     */
+    static QDebug debugStream(Severity, int line, const char* file);
+
 private:
-    ErrorHandler():m_worstError(Debug) {};
+    ErrorHandler();
     ErrorHandler(const ErrorHandler &);
     ErrorHandler & operator= (const ErrorHandler &);
     
     Severity m_worstError;
     QString m_worstErrorMessage;
     QList <Err> m_errorQueue;
+    QScopedPointer<DebugStream> m_debugStream;
 };
 
 void logMessage(const QString &,const QString &, int, ErrorHandler::Severity s);
@@ -105,47 +113,25 @@ void logMessage(const QString &,const QString &, int, ErrorHandler::Severity s);
 #define CRITICAL(message) logMessage(message,__FILE__, __LINE__, ErrorHandler::Critical);
 
 
-/**
- * Drop in replacement for qWarning()
- * 
- * Note that this is not only for debug builds, its a fundamental part to detect errors.
- * 
- * TODO: It should be possible to replace this with a kWarning/qWarning error handler
- */
-struct KolabLogger {
-    bool isLast;
-    QString m_message;
+class DebugStream: public QIODevice
+{
+public:
     QString m_location;
     ErrorHandler::Severity m_severity;
-    KolabLogger(ErrorHandler::Severity s, int line, const QString &file): isLast(true), m_message(), m_location(file + "(" + QString::number(line)+")"), m_severity(s) {};
-    KolabLogger(ErrorHandler::Severity s, const QString &message, const QString &location): isLast(true), m_message(message), m_location(location), m_severity(s) {};
-    ~KolabLogger(){if (isLast) ErrorHandler::instance().addError(m_severity, m_message, m_location);}
-    QString maybeSpace() const { if (!m_message.isEmpty()) return " "; return ""; }
-    KolabLogger operator<<(const QString &message) {
-        isLast = false;
-        return KolabLogger(m_severity, m_message+maybeSpace()+message, m_location);
-    }
-    KolabLogger operator<<(const QByteArray &message) {
-        isLast = false;
-        return KolabLogger(m_severity, m_message+maybeSpace()+message, m_location);
-    }
-    KolabLogger operator<<(const int &n) {
-        isLast = false;
-        return KolabLogger(m_severity, m_message+maybeSpace()+QString::number(n), m_location);
-    }
-    KolabLogger operator<<(const double &n) {
-        isLast = false;
-        return KolabLogger(m_severity, m_message+maybeSpace()+QString::number(n), m_location);
-    }
-    inline KolabLogger operator<<(const char* t) {
-        isLast = false;
-        return KolabLogger(m_severity, m_message+maybeSpace()+QString::fromAscii(t), m_location);
-    }
+    DebugStream();
+    virtual ~DebugStream();
+    bool isSequential() const { return true; }
+    qint64 readData(char *, qint64) { return 0; /* eof */ }
+    qint64 readLineData(char *, qint64) { return 0; /* eof */ }
+    qint64 writeData(const char *data, qint64 len);
+private:
+    Q_DISABLE_COPY(DebugStream)
 };
-#define Debug() Kolab::KolabLogger(Kolab::ErrorHandler::Debug, __LINE__, __FILE__)
-#define Warning() Kolab::KolabLogger(Kolab::ErrorHandler::Warning, __LINE__, __FILE__)
-#define Error() Kolab::KolabLogger(Kolab::ErrorHandler::Error, __LINE__, __FILE__)
-#define Critical() Kolab::KolabLogger(Kolab::ErrorHandler::Critical, __LINE__, __FILE__)
+
+#define Debug() Kolab::ErrorHandler::debugStream(Kolab::ErrorHandler::Debug, __LINE__, __FILE__)
+#define Warning() Kolab::ErrorHandler::debugStream(Kolab::ErrorHandler::Warning, __LINE__, __FILE__)
+#define Error() Kolab::ErrorHandler::debugStream(Kolab::ErrorHandler::Error, __LINE__, __FILE__)
+#define Critical() Kolab::ErrorHandler::debugStream(Kolab::ErrorHandler::Critical, __LINE__, __FILE__)
 
 }
 #endif // ERRORHANDLER_H
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 186942e..6e53353 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -23,6 +23,7 @@ addTest(kolabobjecttest)
 addTest(timezonetest)
 addTest(mimeobjecttest)
 addTest(xmlobjecttest)
+addTest(debugstreamtest)
 
 if(PHP_BINDINGS)
     find_path(PHP_KOLABFORMAT_PATH NAMES kolabformat.php PATHS /usr/local/lib/php/modules /usr/lib/php/modules PATH_SUFFIXES lib/php/modules lib64/php/modules)
diff --git a/tests/debugstreamtest.cpp b/tests/debugstreamtest.cpp
new file mode 100644
index 0000000..3edbe08
--- /dev/null
+++ b/tests/debugstreamtest.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2012  Christian Mollekopf <mollekopf at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "debugstreamtest.h"
+
+#include "kolabformat/errorhandler.h"
+
+#include <QTest>
+
+void DebugStreamTest::testDebugstream()
+{
+    Error() << "test1";
+    Error() << "test2" << "bla" << 3 << QMap<QString, int>();
+    QCOMPARE(Kolab::ErrorHandler::instance().getErrors().size(), 2);
+    QVERIFY(Kolab::ErrorHandler::instance().getErrors().first().message.contains("test1"));
+    QCOMPARE(Kolab::ErrorHandler::instance().getErrors().first().severity, Kolab::ErrorHandler::Error);
+    QVERIFY(Kolab::ErrorHandler::instance().getErrors().last().message.contains("bla"));
+}
+
+void DebugStreamTest::testDebugNotLogged()
+{
+    Kolab::ErrorHandler::instance().clear();
+    Debug() << "test1";
+    QCOMPARE(Kolab::ErrorHandler::instance().getErrors().size(), 0);
+}
+
+void DebugStreamTest::testHasError()
+{
+    Debug() << "test1";
+    QCOMPARE(Kolab::ErrorHandler::errorOccured(), false);
+    Warning() << "test1";
+    QCOMPARE(Kolab::ErrorHandler::errorOccured(), false);
+    Error() << "test1";
+    QCOMPARE(Kolab::ErrorHandler::errorOccured(), true);
+    Kolab::ErrorHandler::clearErrors();
+    QCOMPARE(Kolab::ErrorHandler::errorOccured(), false);
+}
+
+
+QTEST_MAIN( DebugStreamTest )
+
+#include "debugstreamtest.moc"
\ No newline at end of file
diff --git a/tests/debugstreamtest.h b/tests/debugstreamtest.h
new file mode 100644
index 0000000..f94495b
--- /dev/null
+++ b/tests/debugstreamtest.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012  Christian Mollekopf <mollekopf at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DEBUGSTREAMTEST_H
+#define DEBUGSTREAMTEST_H
+
+#include <QObject>
+
+class DebugStreamTest: public QObject
+{
+    Q_OBJECT
+private slots:
+    void testDebugstream();
+    void testDebugNotLogged();
+    void testHasError();
+};
+
+#endif // DEBUGSTREAMTEST_H





More information about the commits mailing list