diff --git a/private/qtjolie-branch/sodep/sodephelpers_p.h b/private/qtjolie-branch/sodep/sodephelpers_p.h new file mode 100644 index 000000000..5c75664ae --- /dev/null +++ b/private/qtjolie-branch/sodep/sodephelpers_p.h @@ -0,0 +1,123 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2008 Kevin Ottens + * + * 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 SODEPHELPERS_P_H +#define SODEPHELPERS_P_H + +#include +#include + +inline void sodepWrite(QIODevice &io, double value) +{ + const char *in = (const char*)&value; + char out[8]; + + for (int i=0; i<8; ++i) { + out[i] = in[7-i]; + } + + io.write(out, 8); +} + +inline void sodepWrite(QIODevice &io, qint32 value) +{ + const char *in = (const char*)&value; + char out[4]; + + for (int i=0; i<4; ++i) { + out[i] = in[3-i]; + } + + io.write(out, 4); +} + +inline void sodepWrite(QIODevice &io, qint64 value) +{ + const char *in = (const char*)&value; + char out[8]; + + for (int i=0; i<8; ++i) { + out[i] = in[7-i]; + } + + io.write(out, 8); +} + +inline void sodepWrite(QIODevice &io, const QString &value) +{ + QByteArray data = value.toUtf8(); + sodepWrite(io, data.size()); + io.write(data); +} + +inline double sodepReadDouble(QIODevice &io) +{ + double d; + char *out = (char*)&d; + char in[8]; + + io.read(in, 8); + + for (int i=0; i<8; ++i) { + out[i] = in[7-i]; + } + + return d; +} + +inline qint32 sodepReadInt32(QIODevice &io) +{ + qint32 i; + char *out = (char*)&i; + char in[4]; + + io.read(in, 4); + + for (int j=0; j<4; ++j) { + out[j] = in[3-j]; + } + + return i; +} + +inline qint64 sodepReadInt64(QIODevice &io) +{ + qint64 i; + char *out = (char*)&i; + char in[8]; + + io.read(in, 8); + + for (int j=0; j<8; ++j) { + out[j] = in[7-j]; + } + + return i; +} + +inline QString sodepReadString(QIODevice &io) +{ + qint32 length = sodepReadInt32(io); + QByteArray data = io.read(length); + return QString::fromUtf8(data); +} + + +#endif diff --git a/private/qtjolie-branch/sodep/sodepmessage.cpp b/private/qtjolie-branch/sodep/sodepmessage.cpp new file mode 100644 index 000000000..a477676e3 --- /dev/null +++ b/private/qtjolie-branch/sodep/sodepmessage.cpp @@ -0,0 +1,131 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2008 Kevin Ottens + * + * 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 "sodepmessage.h" + +#include + +#include "sodephelpers_p.h" + +class SodepMessagePrivate +{ +public: + SodepMessagePrivate() : id(0) {} + + qint64 id; + QString resourcePath; + QString operationName; + SodepFault fault; + SodepValue data; +}; + +SodepMessage::SodepMessage() + : d(new SodepMessagePrivate) +{ +} + +SodepMessage::SodepMessage(const QString &resourcePath, const QString &operationName, qint64 id) + : d(new SodepMessagePrivate) +{ + d->id = id; + d->resourcePath = resourcePath; + d->operationName = operationName; +} + +SodepMessage::SodepMessage(const SodepMessage &other) + : d(new SodepMessagePrivate) +{ + *d = *other.d; +} + +SodepMessage::~SodepMessage() +{ + delete d; +} + +SodepMessage &SodepMessage::operator=(const SodepMessage &other) +{ + *d = *other.d; + + return *this; +} + +qint64 SodepMessage::id() const +{ + return d->id; +} + +QString SodepMessage::resourcePath() const +{ + return d->resourcePath; +} + +QString SodepMessage::operationName() const +{ + return d->operationName; +} + +SodepFault SodepMessage::fault() const +{ + return d->fault; +} + +void SodepMessage::setFault(const SodepFault &fault) +{ + d->fault = fault; +} + +SodepValue SodepMessage::data() const +{ + return d->data; +} + +void SodepMessage::setData(const SodepValue &data) +{ + d->data = data; +} + +bool SodepMessage::isValid() +{ + return !d->resourcePath.isEmpty() && !d->operationName.isEmpty(); +} + +void SodepMessage::writeTo(QIODevice &io) const +{ + sodepWrite(io, d->id); + sodepWrite(io, d->resourcePath); + sodepWrite(io, d->operationName); + d->fault.writeTo(io); + d->data.writeTo(io); +} + +SodepMessage SodepMessage::readFrom(QIODevice &io) +{ + SodepMessage result; + + result.d->id = sodepReadInt64(io); + result.d->resourcePath = sodepReadString(io); + result.d->operationName = sodepReadString(io); + result.d->fault = SodepFault::readFrom(io); + result.d->data = SodepValue::readFrom(io); + + return result; +} + diff --git a/private/qtjolie-branch/sodep/sodepmessage.h b/private/qtjolie-branch/sodep/sodepmessage.h new file mode 100644 index 000000000..a89e37d48 --- /dev/null +++ b/private/qtjolie-branch/sodep/sodepmessage.h @@ -0,0 +1,61 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2008 Kevin Ottens + * + * 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 SODEPMESSAGE_H +#define SODEPMESSAGE_H + +#include +#include + +class SodepMessagePrivate; + +class Q_DECL_EXPORT SodepMessage +{ +public: + SodepMessage(); + explicit SodepMessage(const QString &resourcePath, + const QString &operationName, + qint64 id = 0); + SodepMessage(const SodepMessage &other); + ~SodepMessage(); + + SodepMessage &operator=(const SodepMessage &other); + + qint64 id() const; + + QString resourcePath() const; + QString operationName() const; + + SodepFault fault() const; + void setFault(const SodepFault &fault); + + SodepValue data() const; + void setData(const SodepValue &data); + + bool isValid(); + + void writeTo(QIODevice &io) const; + static SodepMessage readFrom(QIODevice &io); + +private: + SodepMessagePrivate * const d; +}; + +#endif diff --git a/private/qtjolie-branch/sodep/sodepvalue.cpp b/private/qtjolie-branch/sodep/sodepvalue.cpp new file mode 100644 index 000000000..703e84ce8 --- /dev/null +++ b/private/qtjolie-branch/sodep/sodepvalue.cpp @@ -0,0 +1,206 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2008 Kevin Ottens + * + * 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 "sodepvalue.h" + +#include +#include + +#include "sodephelpers_p.h" + +class SodepValuePrivate +{ +public: + QVariant content; + QMap > children; +}; + +SodepValue::SodepValue() + : d(new SodepValuePrivate) +{ +} + +SodepValue::SodepValue(const QString &content) + : d(new SodepValuePrivate) +{ + d->content = content; +} + +SodepValue::SodepValue(qint32 content) + : d(new SodepValuePrivate) +{ + d->content = content; +} + +SodepValue::SodepValue(double content) + : d(new SodepValuePrivate) +{ + d->content = content; +} + +SodepValue::SodepValue(const SodepValue &other) + : d(new SodepValuePrivate) +{ + *d = *other.d; +} + +SodepValue::~SodepValue() +{ + delete d; +} + +SodepValue &SodepValue::operator=(const SodepValue &other) +{ + *d = *other.d; + + return *this; +} + +QStringList SodepValue::childrenNames() const +{ + return d->children.keys(); +} + +QList & SodepValue::children(const QString &name) +{ + return d->children[name]; +} + +const QList & SodepValue::children(const QString &name) const +{ + return d->children[name]; +} + +QString SodepValue::toString() const +{ + if (isString()) { + return d->content.toString(); + } else { + return QString(); + } +} + +qint32 SodepValue::toInt() const +{ + if (isInt()) { + return d->content.toInt(); + } else { + return 0; + } +} + +double SodepValue::toDouble() const +{ + if (isDouble()) { + return d->content.toDouble(); + } else { + return 0.0; + } +} + +bool SodepValue::isString() const +{ + return d->content.type()==QVariant::String; +} + +bool SodepValue::isInt() const +{ + return d->content.type()==QVariant::Int; +} + +bool SodepValue::isDouble() const +{ + return d->content.type()==QVariant::Double; +} + +bool SodepValue::isValid() const +{ + return isString() || isInt() || isDouble(); +} + +void SodepValue::writeTo(QIODevice &io) const +{ + if (isDouble()) { + io.putChar(3); + sodepWrite(io, toDouble()); + } else if (isInt()) { + io.putChar(2); + sodepWrite(io, toInt()); + } else if (isString()) { + io.putChar(1); + sodepWrite(io, toString()); + } else { + io.putChar(0); + } + + sodepWrite(io, d->children.size()); + + foreach (const QString &name, d->children.keys()) { + sodepWrite(io, name); + + QList values = d->children[name]; + qint32 valueCount = values.size(); + sodepWrite(io, valueCount); + for (int j=0; j values; + qint32 valueCount = sodepReadInt32(io); + for (int j=0; jchildren[name] = values; + } + + return result; +} diff --git a/private/qtjolie-branch/tests/sodepmessagetest.cpp b/private/qtjolie-branch/tests/sodepmessagetest.cpp new file mode 100644 index 000000000..f7a6da117 --- /dev/null +++ b/private/qtjolie-branch/tests/sodepmessagetest.cpp @@ -0,0 +1,114 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2008 Kevin Ottens + * + * 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 +#include + +#include +#include "sodeptesthelpers.h" + +class SodepMessageTest : public QObject +{ + Q_OBJECT + +private slots: + void shouldVerifyInitialState() + { + SodepMessage m1("/foo", "bar"); + SodepMessage m2("/pata/pata", "pon", 2); + + QCOMPARE(m1.resourcePath(), QString("/foo")); + QCOMPARE(m1.operationName(), QString("bar")); + QCOMPARE(m1.id(), qint64(0)); + + QCOMPARE(m2.resourcePath(), QString("/pata/pata")); + QCOMPARE(m2.operationName(), QString("pon")); + QCOMPARE(m2.id(), qint64(2)); + + m1 = m2; + + QCOMPARE(m1.resourcePath(), QString("/pata/pata")); + QCOMPARE(m1.operationName(), QString("pon")); + QCOMPARE(m2.id(), qint64(2)); + } + + void shouldBeSerializable_data() + { + SodepValue v(42); + QByteArray vSerial = QByteArray::fromHex("020000002A00000000"); + SodepFault f("foo"); + QByteArray fSerial = QByteArray::fromHex("0100000003")+QByteArray("foo") + + QByteArray::fromHex("0000000000"); + + QTest::addColumn("original"); + QTest::addColumn("serialized"); + + QTest::newRow("no payload message") << SodepMessage("/pata", "pon") + << QByteArray::fromHex("0000000000000000") + + QByteArray::fromHex("00000005")+QByteArray("/pata") + + QByteArray::fromHex("00000003")+QByteArray("pon") + + QByteArray::fromHex("00") + + QByteArray::fromHex("0000000000"); + + SodepMessage payload("/pata", "pon"); + payload.setFault(f); + payload.setData(v); + QTest::newRow("payload message") << payload + << QByteArray::fromHex("0000000000000000") + + QByteArray::fromHex("00000005")+QByteArray("/pata") + + QByteArray::fromHex("00000003")+QByteArray("pon") + + fSerial + + vSerial; + + SodepMessage payloadId("/pata", "pon", 42); + payloadId.setFault(f); + payloadId.setData(v); + QTest::newRow("payload and id message") << payloadId + << QByteArray::fromHex("000000000000002A") + + QByteArray::fromHex("00000005")+QByteArray("/pata") + + QByteArray::fromHex("00000003")+QByteArray("pon") + + fSerial + + vSerial; + } + + void shouldBeSerializable() + { + QBuffer buffer; + + QFETCH(SodepMessage, original); + QFETCH(QByteArray, serialized); + SodepMessage result; + + buffer.open(QIODevice::WriteOnly); + original.writeTo(buffer); + buffer.close(); + + buffer.open(QIODevice::ReadOnly); + result = SodepMessage::readFrom(buffer); + buffer.close(); + + sodepCompare(result, original); + QCOMPARE(buffer.data(), serialized); + } +}; + +QTEST_MAIN(SodepMessageTest) + +#include "sodepmessagetest.moc"