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

List:       kde-commits
Subject:    [kdepimlibs/akregator_port] krss: move the property de/serialization from feedpropertiesattribute to
From:       Frank Osterfeld <frank.osterfeld () kdemail ! net>
Date:       2012-03-31 15:16:22
Message-ID: 20120331151622.35086A60F0 () git ! kde ! org
[Download RAW message or body]

Git commit a065dfbc1bb5dcc8811346904fe1d0031a69ee79 by Frank Osterfeld.
Committed on 11/10/2009 at 11:39.
Pushed by osterfeld into branch 'akregator_port'.

move the property de/serialization from feedpropertiesattribute to helper_p.h,
and use it in tagproperties, too (which didn't handle =, ; in tag attributes \
correctly, and would crash in case the attribute value is malformed). Add similar \
functions to de/serialize flat string lists and use them for \
SubscriptionLabelCollectionAttributes. Add a unit test for the serialization.

svn path=/branches/work/akonadi-ports/kdepim/; revision=1033821

M  +1    -0    krss/CMakeLists.txt
M  +22   -0    krss/dbushelper.cpp
M  +21   -0    krss/dbushelper_p.h
M  +10   -22   krss/defaulttagprovider/tagpropertiesattribute.cpp
M  +2    -4    krss/defaulttagprovider/tagpropertiesattribute.h
M  +3    -44   krss/feedpropertiescollectionattribute.cpp
A  +112  -0    krss/helper.cpp     [License: LGPL (v2+)]
A  +38   -0    krss/helper_p.h     [License: LGPL (v2+)]
M  +3    -6    krss/subscriptionlabelscollectionattribute.cpp
M  +1    -0    krss/tests/CMakeLists.txt
A  +55   -0    krss/tests/testhelper.cpp     [License: LGPL (v2+)]
A  +35   -0    krss/tests/testhelper.h     [License: LGPL (v2+)]

http://commits.kde.org/kdepimlibs/a065dfbc1bb5dcc8811346904fe1d0031a69ee79

diff --git a/krss/CMakeLists.txt b/krss/CMakeLists.txt
index ce5df5e..2ce4378 100644
--- a/krss/CMakeLists.txt
+++ b/krss/CMakeLists.txt
@@ -38,6 +38,7 @@ set(krss_LIB_SRCS
    netfeed.cpp
    feedlist.cpp
    dbushelper.cpp
+   helper.cpp
    itemmodel.cpp
    treenode.cpp
    treenodevisitor.cpp
diff --git a/krss/dbushelper.cpp b/krss/dbushelper.cpp
index eb542ec..b7737a0 100644
--- a/krss/dbushelper.cpp
+++ b/krss/dbushelper.cpp
@@ -1,3 +1,25 @@
+/*
+ * This file is part of the krss library
+ *
+ * Copyright (C) 2009 Frank Osterfeld <osterfeld@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
 #include "dbushelper_p.h"
 
 #include <QtDBus/QDBusAbstractInterface>
diff --git a/krss/dbushelper_p.h b/krss/dbushelper_p.h
index 909343f..6c32007 100644
--- a/krss/dbushelper_p.h
+++ b/krss/dbushelper_p.h
@@ -1,3 +1,24 @@
+/*
+ * This file is part of the krss library
+ *
+ * Copyright (C) 2009 Frank Osterfeld <osterfeld@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
 #ifndef KRSS_DBUSHELPER_H
 #define KRSS_DBUSHELPER_H
 
diff --git a/krss/defaulttagprovider/tagpropertiesattribute.cpp \
b/krss/defaulttagprovider/tagpropertiesattribute.cpp index 103c64c..d2b0ea0 100644
--- a/krss/defaulttagprovider/tagpropertiesattribute.cpp
+++ b/krss/defaulttagprovider/tagpropertiesattribute.cpp
@@ -16,6 +16,7 @@
 */
 
 #include "tagpropertiesattribute.h"
+#include "helper_p.h"
 
 #include <QtCore/QStringList>
 
@@ -34,59 +35,46 @@ QByteArray TagPropertiesAttribute::type() const
 TagPropertiesAttribute* TagPropertiesAttribute::clone() const
 {
     TagPropertiesAttribute *attr = new TagPropertiesAttribute();
-    attr->setLabel( m_label );
-    attr->setDescription( m_description );
-    attr->setIcon( m_icon );
+    attr->m_properties = m_properties;
     return attr;
 }
 
 QByteArray TagPropertiesAttribute::serialized() const
 {
-    QStringList props;
-    props << QLatin1String("Label=") + m_label;
-    props << QLatin1String("Description=") + m_description;
-    props << QLatin1String("Icon=") + m_icon;
-    return props.join( QLatin1String(";") ).toUtf8();
+    return encodeProperties( m_properties );
 }
 
 void TagPropertiesAttribute::deserialize( const QByteArray& data )
 {
-    if ( data.isEmpty() )
-        return;
-
-    // so ugly, am i missing something?
-    const QStringList props = QString::fromUtf8( data.constData(), data.size() \
                ).split( QLatin1Char(';') );
-    m_label = props[0].split( QLatin1Char('=') )[1];
-    m_description = props[1].split( QLatin1Char('=') )[1];
-    m_icon = props[2].split( QLatin1Char('=') )[1];
+    m_properties = decodeProperties( data );
 }
 
 QString TagPropertiesAttribute::label() const
 {
-    return m_label;
+    return m_properties.value( QLatin1String("Label") );
 }
 
 void TagPropertiesAttribute::setLabel( const QString& label )
 {
-    m_label = label;
+    m_properties.insert( QLatin1String("Label"), label );
 }
 
 QString TagPropertiesAttribute::description() const
 {
-    return m_description;
+    return m_properties.value( QLatin1String("Description") );
 }
 
 void TagPropertiesAttribute::setDescription( const QString& description )
 {
-    m_description = description;
+    m_properties.insert( QLatin1String("Description"), description );
 }
 
 QString TagPropertiesAttribute::icon() const
 {
-    return m_icon;
+    return m_properties.value( QLatin1String("Icon") );
 }
 
 void TagPropertiesAttribute::setIcon( const QString& icon )
 {
-    m_icon = icon;
+    m_properties.insert( QLatin1String("Icon"), icon );
 }
diff --git a/krss/defaulttagprovider/tagpropertiesattribute.h \
b/krss/defaulttagprovider/tagpropertiesattribute.h index 69fc4a3..7ebffaa 100644
--- a/krss/defaulttagprovider/tagpropertiesattribute.h
+++ b/krss/defaulttagprovider/tagpropertiesattribute.h
@@ -20,6 +20,7 @@
 
 #include <akonadi/attribute.h>
 
+#include <QtCore/QHash>
 #include <QtCore/QString>
 
 namespace KRss {
@@ -42,10 +43,7 @@ public:
     void setIcon( const QString& icon );
 
 private:
-
-    QString m_label;
-    QString m_description;
-    QString m_icon;
+    QHash<QString, QString> m_properties;
 };
 
 } // namespace KRss
diff --git a/krss/feedpropertiescollectionattribute.cpp \
b/krss/feedpropertiescollectionattribute.cpp index f9e8582..934fca7 100644
--- a/krss/feedpropertiescollectionattribute.cpp
+++ b/krss/feedpropertiescollectionattribute.cpp
@@ -16,6 +16,7 @@
 */
 
 #include "feedpropertiescollectionattribute.h"
+#include "helper_p.h"
 
 #include <QtCore/QStringList>
 
@@ -36,56 +37,14 @@ FeedPropertiesCollectionAttribute* \
FeedPropertiesCollectionAttribute::clone() co  return new \
FeedPropertiesCollectionAttribute( *this );  }
 
-namespace {
-    static QByteArray encode( const QString& str ) {
-        QByteArray ba = str.toUtf8();
-        ba.replace( '\\', "\\\\" );
-        ba.replace( ';', "\\;" );
-        ba.replace( '=', "\\=" );
-        return ba;
-    }
-}
-
 QByteArray FeedPropertiesCollectionAttribute::serialized() const
 {
-    QByteArray ba;
-    Q_FOREACH( const QString& i, m_properties.keys() ) {
-        if ( !ba.isEmpty() )
-            ba += ";";
-        ba += encode( i ) + "=" + encode( m_properties.value( i ) );
-    }
-    return ba;
-
+    return encodeProperties( m_properties );
 }
 
 void FeedPropertiesCollectionAttribute::deserialize( const QByteArray &data )
 {
-    QByteArray key;
-    QByteArray value;
-    bool isEscaped = false;
-    bool isKey = true;
-    for ( int i=0; i < data.size(); ++i ) {
-        const char ch = data[i];
-        if ( isEscaped ) {
-            ( isKey ? key : value ) += ch;
-            isEscaped = false;
-        } else {
-            if ( ch == '\\' )
-                isEscaped = true;
-            else if ( ch == ';' ) {
-                m_properties.insert( QString::fromUtf8( key ), QString::fromUtf8( \
                value ) );
-                key.clear();
-                value.clear();
-                isKey = true;
-            }
-            else if ( ch == '=' )
-                isKey = false;
-            else
-                ( isKey ? key : value ) += ch;
-        }
-    }
-    if ( !key.isEmpty() )
-        m_properties.insert( QString::fromUtf8( key ), QString::fromUtf8( value ) );
+    m_properties = decodeProperties( data );
 }
 
 QString FeedPropertiesCollectionAttribute::name() const
diff --git a/krss/helper.cpp b/krss/helper.cpp
new file mode 100644
index 0000000..4be3153
--- /dev/null
+++ b/krss/helper.cpp
@@ -0,0 +1,112 @@
+/*
+ * This file is part of the krss library
+ *
+ * Copyright (C) 2009 Frank Osterfeld <osterfeld@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+#include "helper_p.h"
+
+#include <QByteArray>
+#include <QHash>
+#include <QString>
+#include <QStringList>
+
+static QByteArray encodeString( const QString& str ) {
+    QByteArray ba = str.toUtf8();
+    ba.replace( '\\', "\\\\" );
+    ba.replace( ';', "\\;" );
+    ba.replace( '=', "\\=" );
+    return ba;
+}
+
+QByteArray KRss::encodeProperties( const QHash<QString, QString>& properties ) {
+    QByteArray ba;
+    Q_FOREACH( const QString& i, properties.keys() ) {
+        if ( !ba.isEmpty() )
+            ba += ";";
+        ba += encodeString( i ) + "=" + encodeString( properties.value( i ) );
+    }
+    return ba;
+}
+
+QHash<QString, QString> KRss::decodeProperties( const QByteArray& data ) {
+    QHash<QString,QString> properties;
+    QByteArray key;
+    QByteArray value;
+    bool isEscaped = false;
+    bool isKey = true;
+    for ( int i=0; i < data.size(); ++i ) {
+        const char ch = data[i];
+        if ( isEscaped ) {
+            ( isKey ? key : value ) += ch;
+            isEscaped = false;
+        } else {
+            if ( ch == '\\' )
+                isEscaped = true;
+            else if ( ch == ';' ) {
+                properties.insert( QString::fromUtf8( key ), QString::fromUtf8( \
value ) ); +                key.clear();
+                value.clear();
+                isKey = true;
+            }
+            else if ( ch == '=' )
+                isKey = false;
+            else
+                ( isKey ? key : value ) += ch;
+        }
+    }
+    if ( !key.isEmpty() )
+        properties.insert( QString::fromUtf8( key ), QString::fromUtf8( value ) );
+    return properties;
+}
+
+QByteArray KRss::encodeStringList( const QStringList& list ) {
+    QByteArray ba;
+    Q_FOREACH( const QString& i, list ) {
+        if ( !ba.isEmpty() )
+            ba += ";";
+        ba += encodeString( i );
+    }
+    return ba;
+
+}
+
+QStringList KRss::decodeStringList( const QByteArray& data ) {
+    QStringList list;
+    QByteArray str;
+    bool isEscaped = false;
+    for ( int i=0; i < data.size(); ++i ) {
+        const char ch = data[i];
+        if ( isEscaped ) {
+            str += ch;
+            isEscaped = false;
+        } else {
+            if ( ch == '\\' )
+                isEscaped = true;
+            else if ( ch == ';' ) {
+                list << QString::fromUtf8( str );
+                str.clear();
+            }
+            else
+                str += ch;
+        }
+    }
+    if ( !str.isEmpty() )
+        list << QString::fromUtf8( str );
+    return list;
+}
diff --git a/krss/helper_p.h b/krss/helper_p.h
new file mode 100644
index 0000000..f546ff3
--- /dev/null
+++ b/krss/helper_p.h
@@ -0,0 +1,38 @@
+/*
+ * This file is part of the krss library
+ *
+ * Copyright (C) 2009 Frank Osterfeld <osterfeld@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+#ifndef KRSS_HELPER_P_H
+#define KRSS_HELPER_P_H
+
+class QByteArray;
+class QString;
+class QStringList;
+
+template <typename K, typename V> class QHash;
+
+namespace KRss {
+  QByteArray encodeProperties( const QHash<QString, QString>& properties );
+  QHash<QString, QString> decodeProperties( const QByteArray& data );
+  QByteArray encodeStringList( const QStringList& list );
+  QStringList decodeStringList( const QByteArray& data );
+}
+
+#endif // KRSS_HELPER_P_H
diff --git a/krss/subscriptionlabelscollectionattribute.cpp \
b/krss/subscriptionlabelscollectionattribute.cpp index 95fbd09..0c1f4da 100644
--- a/krss/subscriptionlabelscollectionattribute.cpp
+++ b/krss/subscriptionlabelscollectionattribute.cpp
@@ -16,6 +16,7 @@
 */
 
 #include "subscriptionlabelscollectionattribute.h"
+#include "helper_p.h"
 
 using namespace KRss;
 
@@ -37,16 +38,12 @@ SubscriptionLabelsCollectionAttribute* \
SubscriptionLabelsCollectionAttribute::cl  
 QByteArray SubscriptionLabelsCollectionAttribute::serialized() const
 {
-        return m_subscriptionLabels.join( QLatin1String(";") ).toUtf8();
+        return encodeStringList( m_subscriptionLabels );
 }
 
 void SubscriptionLabelsCollectionAttribute::deserialize( const QByteArray &data )
 {
-        if ( data.isEmpty() )
-                return;
-
-        // so ugly, am i missing something?
-        m_subscriptionLabels = QString::fromUtf8( data.constData(), data.size() \
).split( QLatin1Char(';') ); +        m_subscriptionLabels = decodeStringList( data \
);  }
 
 QStringList SubscriptionLabelsCollectionAttribute::subscriptionLabels() const
diff --git a/krss/tests/CMakeLists.txt b/krss/tests/CMakeLists.txt
index 32aaf10..64a6800 100644
--- a/krss/tests/CMakeLists.txt
+++ b/krss/tests/CMakeLists.txt
@@ -38,6 +38,7 @@ add_simple_test(testitem testitem.cpp)
 add_simple_test(testcategory testcategory.cpp)
 add_simple_test(testperson testperson.cpp)
 add_simple_test(testenclosure testenclosure.cpp)
+add_simple_test(testhelper testhelper.cpp ${CMAKE_SOURCE_DIR}/krss/krss/helper.cpp)
 add_akonadi_isolated_test(tagprovidertest tagprovidertest.cpp)
 
 kde4_add_executable(itemimporter itemimporter.cpp)
diff --git a/krss/tests/testhelper.cpp b/krss/tests/testhelper.cpp
new file mode 100644
index 0000000..44e64aa
--- /dev/null
+++ b/krss/tests/testhelper.cpp
@@ -0,0 +1,55 @@
+/*
+ * This file is part of the krss library
+ *
+ * Copyright (C) 2009 Frank Osterfeld <osterfeld@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "testhelper.h"
+#include "helper_p.h"
+
+#include <QByteArray>
+#include <QHash>
+#include <QString>
+#include <QStringList>
+
+#include <qtest_kde.h>
+
+void TestHelper::testAttributeSerialization()
+{
+    QHash<QString,QString> p;
+    QCOMPARE( KRss::decodeProperties( QByteArray() ), p );
+    QCOMPARE( KRss::encodeProperties( p ), QByteArray() );
+    p.insert( QLatin1String("Key"), QLatin1String("Value") );
+    p.insert( QLatin1String("\\=;;;\\;"), QLatin1String("dsfds ; ;\\\\;\\\\\\=") );
+    p.insert( QLatin1String("\\\\=;;;\\;"), QLatin1String("dsfds ; \
;\\\\;dsf\\\\\\=") ); +    const QByteArray serialized = KRss::encodeProperties( p );
+    const QHash<QString, QString> read = KRss::decodeProperties( serialized );
+    QCOMPARE( read, p );
+    const QStringList emptyList;
+    QCOMPARE( KRss::decodeStringList( QByteArray() ), emptyList );
+    QCOMPARE( KRss::encodeStringList( emptyList ), QByteArray() );
+    const QStringList origKeys = p.keys();
+    const QByteArray serializedKeys = KRss::encodeStringList(  origKeys );
+    const QStringList readKeys = KRss::decodeStringList( serializedKeys );
+    QCOMPARE( readKeys, origKeys );
+}
+
+QTEST_KDEMAIN( TestHelper, NoGUI )
+
+#include "testhelper.moc"
diff --git a/krss/tests/testhelper.h b/krss/tests/testhelper.h
new file mode 100644
index 0000000..f33fcff
--- /dev/null
+++ b/krss/tests/testhelper.h
@@ -0,0 +1,35 @@
+/*
+ * This file is part of the kfeed library
+ *
+ * Copyright (C) 2009 Frank Osterfeld <osterfeld@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef TESTHELPER_H
+#define TESTHELPER_H
+
+#include <QObject>
+
+class TestHelper : public QObject
+{
+Q_OBJECT
+private Q_SLOTS:
+    void testAttributeSerialization();
+};
+
+#endif // TESTHELPER_H


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

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