From 4a8945d38d97b21354f4534d1d351ef143c2a0de Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Mon, 30 Jun 2014 09:41:17 +0300 Subject: [PATCH] Rewrite type handling to be simpler, get rid of OO hierarchy --- .gitignore | 1 + harbour-weechatrelay.pro | 32 +--- rpm/harbour-weechatrelay.spec | 4 +- rpm/harbour-weechatrelay.yaml | 4 +- src/weechatproto/array.cpp | 5 - src/weechatproto/array.h | 12 -- src/weechatproto/buffer.cpp | 28 ---- src/weechatproto/buffer.h | 17 -- src/weechatproto/char.cpp | 24 --- src/weechatproto/char.h | 20 --- src/weechatproto/hashtable.cpp | 10 -- src/weechatproto/hashtable.h | 17 -- src/weechatproto/hdata.cpp | 5 - src/weechatproto/hdata.h | 12 -- src/weechatproto/info.cpp | 5 - src/weechatproto/info.h | 12 -- src/weechatproto/infolist.cpp | 5 - src/weechatproto/infolist.h | 12 -- src/weechatproto/integer.cpp | 17 -- src/weechatproto/integer.h | 17 -- src/weechatproto/long.cpp | 50 ------ src/weechatproto/long.h | 19 --- src/weechatproto/pointer.cpp | 28 ---- src/weechatproto/pointer.h | 17 -- src/weechatproto/protocoltype.cpp | 19 --- src/weechatproto/protocoltype.h | 22 --- .../protocoltypeoverwriteexception.cpp | 6 - .../protocoltypeoverwriteexception.h | 13 -- src/weechatproto/string.cpp | 28 ---- src/weechatproto/string.h | 17 -- src/weechatproto/time.cpp | 25 --- src/weechatproto/time.h | 19 --- src/weechatprotocolhandler.cpp | 157 ++++++++++++++++++ src/weechatprotocolhandler.h | 13 +- 34 files changed, 173 insertions(+), 519 deletions(-) delete mode 100644 src/weechatproto/array.cpp delete mode 100644 src/weechatproto/array.h delete mode 100644 src/weechatproto/buffer.cpp delete mode 100644 src/weechatproto/buffer.h delete mode 100644 src/weechatproto/char.cpp delete mode 100644 src/weechatproto/char.h delete mode 100644 src/weechatproto/hashtable.cpp delete mode 100644 src/weechatproto/hashtable.h delete mode 100644 src/weechatproto/hdata.cpp delete mode 100644 src/weechatproto/hdata.h delete mode 100644 src/weechatproto/info.cpp delete mode 100644 src/weechatproto/info.h delete mode 100644 src/weechatproto/infolist.cpp delete mode 100644 src/weechatproto/infolist.h delete mode 100644 src/weechatproto/integer.cpp delete mode 100644 src/weechatproto/integer.h delete mode 100644 src/weechatproto/long.cpp delete mode 100644 src/weechatproto/long.h delete mode 100644 src/weechatproto/pointer.cpp delete mode 100644 src/weechatproto/pointer.h delete mode 100644 src/weechatproto/protocoltype.cpp delete mode 100644 src/weechatproto/protocoltype.h delete mode 100644 src/weechatproto/protocoltypeoverwriteexception.cpp delete mode 100644 src/weechatproto/protocoltypeoverwriteexception.h delete mode 100644 src/weechatproto/string.cpp delete mode 100644 src/weechatproto/string.h delete mode 100644 src/weechatproto/time.cpp delete mode 100644 src/weechatproto/time.h diff --git a/.gitignore b/.gitignore index a2bd457..6d384c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ harbour-weechatrelay.pro.user +*pro.user* diff --git a/harbour-weechatrelay.pro b/harbour-weechatrelay.pro index 854a8ff..c96e943 100644 --- a/harbour-weechatrelay.pro +++ b/harbour-weechatrelay.pro @@ -16,21 +16,7 @@ SOURCES += src/harbour-weechatrelay.cpp \ src/connectionhandler.cpp \ src/weechatprotocolhandler.cpp \ src/protocolhandler.cpp \ - src/qsslcertificateinfo.cpp \ - src/weechatproto/protocoltype.cpp \ - src/weechatproto/hashtable.cpp \ - src/weechatproto/hdata.cpp \ - src/weechatproto/info.cpp \ - src/weechatproto/infolist.cpp \ - src/weechatproto/array.cpp \ - src/weechatproto/time.cpp \ - src/weechatproto/pointer.cpp \ - src/weechatproto/buffer.cpp \ - src/weechatproto/string.cpp \ - src/weechatproto/long.cpp \ - src/weechatproto/integer.cpp \ - src/weechatproto/char.cpp \ - src/weechatproto/protocoltypeoverwriteexception.cpp + src/qsslcertificateinfo.cpp OTHER_FILES += qml/harbour-weechatrelay.qml \ rpm/harbour-weechatrelay.spec \ @@ -55,21 +41,7 @@ HEADERS += \ src/protocolhandler.h \ src/weechatprotocolhandler.h \ src/connectresolver.h \ - src/qsslcertificateinfo.h \ - src/weechatproto/protocoltype.h \ - src/weechatproto/hashtable.h \ - src/weechatproto/hdata.h \ - src/weechatproto/info.h \ - src/weechatproto/infolist.h \ - src/weechatproto/array.h \ - src/weechatproto/time.h \ - src/weechatproto/pointer.h \ - src/weechatproto/buffer.h \ - src/weechatproto/string.h \ - src/weechatproto/long.h \ - src/weechatproto/integer.h \ - src/weechatproto/char.h \ - src/weechatproto/protocoltypeoverwriteexception.h + src/qsslcertificateinfo.h QT += network diff --git a/rpm/harbour-weechatrelay.spec b/rpm/harbour-weechatrelay.spec index e2a05e7..a84fbe8 100644 --- a/rpm/harbour-weechatrelay.spec +++ b/rpm/harbour-weechatrelay.spec @@ -17,7 +17,7 @@ Version: 0.1 Release: 1 Group: Qt/Qt License: MIT Expat licence -URL: http://example.org/ +URL: https://bitbucket.org/vincit/harbour-weechatrelay Source0: %{name}-%{version}.tar.bz2 Source100: harbour-weechatrelay.yaml Requires: sailfishsilica-qt5 >= 0.10.9 @@ -28,7 +28,7 @@ BuildRequires: pkgconfig(sailfishapp) >= 0.0.10 BuildRequires: desktop-file-utils %description -WeeCRApp (WeeChat Relay App) is a WeeChat relay client app for Sailfish OS. +WeeCRApp (WeeChat Relay App) is a WeeChat relay client for Sailfish OS. %prep %setup -q -n %{name}-%{version} diff --git a/rpm/harbour-weechatrelay.yaml b/rpm/harbour-weechatrelay.yaml index 29f241a..4bf36b6 100644 --- a/rpm/harbour-weechatrelay.yaml +++ b/rpm/harbour-weechatrelay.yaml @@ -3,11 +3,11 @@ Summary: WeeCRApp Version: 0.1 Release: 1 Group: Qt/Qt -URL: http://example.org/ +URL: https://bitbucket.org/vincit/harbour-weechatrelay License: MIT Expat licence Sources: - '%{name}-%{version}.tar.bz2' -Description: "WeeCRApp (WeeChat Relay App) is a WeeChat relay client app for Sailfish +Description: "WeeCRApp (WeeChat Relay App) is a WeeChat relay client for Sailfish OS." Configure: none Builder: qtc5 diff --git a/src/weechatproto/array.cpp b/src/weechatproto/array.cpp deleted file mode 100644 index 0973e48..0000000 --- a/src/weechatproto/array.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "array.h" - -Array::Array() -{ -} diff --git a/src/weechatproto/array.h b/src/weechatproto/array.h deleted file mode 100644 index 9ae60f3..0000000 --- a/src/weechatproto/array.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef ARRAY_H -#define ARRAY_H - -#include "protocoltype.h" - -class Array : public ProtocolType -{ -public: - Array(); -}; - -#endif // ARRAY_H diff --git a/src/weechatproto/buffer.cpp b/src/weechatproto/buffer.cpp deleted file mode 100644 index d9be1ce..0000000 --- a/src/weechatproto/buffer.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "buffer.h" - -Buffer::Buffer(): data(0) -{ -} - -void Buffer::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - // Buffer is the same as string, but just bytes - quint32 length; - *data >> length; - - this->data = new QByteArray(); - - quint8 current; - for (quint32 i = 0; i < length; ++i) - { - *data >> current; - this->data->append(static_cast(current)); - } -} - -const QByteArray* Buffer::getBytes() const -{ - return this->data; -} diff --git a/src/weechatproto/buffer.h b/src/weechatproto/buffer.h deleted file mode 100644 index 1a2b26d..0000000 --- a/src/weechatproto/buffer.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef BUFFER_H -#define BUFFER_H - -#include "protocoltype.h" - -class Buffer : public ProtocolType -{ -public: - Buffer(); - void readFrom(QDataStream* data); - const QByteArray* getBytes() const; - -private: - QByteArray* data; -}; - -#endif // BUFFER_H diff --git a/src/weechatproto/char.cpp b/src/weechatproto/char.cpp deleted file mode 100644 index 9a68574..0000000 --- a/src/weechatproto/char.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "char.h" - -Char::Char(): data(' ') -{ -} - -void Char::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - quint8 c; - *data >> c; - this->data = static_cast(c); -} - -char Char::getChar() const -{ - return this->data; -} - -std::size_t Char::hash() const -{ - return data * 2654435761 % 2^32; -} diff --git a/src/weechatproto/char.h b/src/weechatproto/char.h deleted file mode 100644 index 760ca46..0000000 --- a/src/weechatproto/char.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CHAR_H -#define CHAR_H - -#include "protocoltype.h" - -class Char : public ProtocolType -{ -public: - Char(); - - void readFrom(QDataStream* data); - char getChar() const; - - std::size_t hash() const; - -private: - char data; -}; - -#endif // CHAR_H diff --git a/src/weechatproto/hashtable.cpp b/src/weechatproto/hashtable.cpp deleted file mode 100644 index 3374c20..0000000 --- a/src/weechatproto/hashtable.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "hashtable.h" - -HashTable::HashTable() -{ -} - -void HashTable::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); -} diff --git a/src/weechatproto/hashtable.h b/src/weechatproto/hashtable.h deleted file mode 100644 index f3cd849..0000000 --- a/src/weechatproto/hashtable.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef HASHTABLE_H -#define HASHTABLE_H - -#include "protocoltype.h" - -#include - -class HashTable : public ProtocolType -{ -public: - HashTable(); - void readFrom(QDataStream* data); - -private: -}; - -#endif // HASHTABLE_H diff --git a/src/weechatproto/hdata.cpp b/src/weechatproto/hdata.cpp deleted file mode 100644 index 3ab8245..0000000 --- a/src/weechatproto/hdata.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "hdata.h" - -HData::HData() -{ -} diff --git a/src/weechatproto/hdata.h b/src/weechatproto/hdata.h deleted file mode 100644 index 08d0e9c..0000000 --- a/src/weechatproto/hdata.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef HDATA_H -#define HDATA_H - -#include "protocoltype.h" - -class HData : public ProtocolType -{ -public: - HData(); -}; - -#endif // HDATA_H diff --git a/src/weechatproto/info.cpp b/src/weechatproto/info.cpp deleted file mode 100644 index dd6bd83..0000000 --- a/src/weechatproto/info.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "info.h" - -Info::Info() -{ -} diff --git a/src/weechatproto/info.h b/src/weechatproto/info.h deleted file mode 100644 index 1598f9d..0000000 --- a/src/weechatproto/info.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef INFO_H -#define INFO_H - -#include "protocoltype.h" - -class Info : public ProtocolType -{ -public: - Info(); -}; - -#endif // INFO_H diff --git a/src/weechatproto/infolist.cpp b/src/weechatproto/infolist.cpp deleted file mode 100644 index 78d6ab0..0000000 --- a/src/weechatproto/infolist.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "infolist.h" - -InfoList::InfoList() -{ -} diff --git a/src/weechatproto/infolist.h b/src/weechatproto/infolist.h deleted file mode 100644 index ae5aa48..0000000 --- a/src/weechatproto/infolist.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef INFOLIST_H -#define INFOLIST_H - -#include "protocoltype.h" - -class InfoList : public ProtocolType -{ -public: - InfoList(); -}; - -#endif // INFOLIST_H diff --git a/src/weechatproto/integer.cpp b/src/weechatproto/integer.cpp deleted file mode 100644 index 6b2df3e..0000000 --- a/src/weechatproto/integer.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "integer.h" - -Integer::Integer(): data(0) -{ -} - -void Integer::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - *data >> this->data; -} - -qint32 Integer::getInt() const -{ - return this->data; -} diff --git a/src/weechatproto/integer.h b/src/weechatproto/integer.h deleted file mode 100644 index b2726be..0000000 --- a/src/weechatproto/integer.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef INTEGER_H -#define INTEGER_H - -#include "protocoltype.h" - -class Integer : public ProtocolType -{ -public: - Integer(); - void readFrom(QDataStream* data); - qint32 getInt() const; - -private: - qint32 data; -}; - -#endif // INTEGER_H diff --git a/src/weechatproto/long.cpp b/src/weechatproto/long.cpp deleted file mode 100644 index 0b072f0..0000000 --- a/src/weechatproto/long.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "long.h" - -Long::Long(): data(0) -{ -} - -void Long::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - // Long is stored as a string, with one byte showing the length - quint8 length; - *data >> length; - - quint8 current; - qint8 magnitude = 1; - for (quint8 i = 0; i < length; ++i) - { - *data >> current; - - if (i == 0) - { - if (current == 45) // ASCII '-' - { - magnitude = -1; - } - } - else - { - this->data *= 10; - } - - this->data += magnitude * static_cast(current - '0'); - } -} - -qint64 Long::getLong() const -{ - return this->data; -} - -std::size_t Long::hash() const -{ - return data * 2654435761 % 2^32; -} - -bool Long::operator==(const Long& other) const -{ - return (other.getLong() == this->data); -} diff --git a/src/weechatproto/long.h b/src/weechatproto/long.h deleted file mode 100644 index 6bb4d2a..0000000 --- a/src/weechatproto/long.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef LONG_H -#define LONG_H - -#include "protocoltype.h" - -class Long : public ProtocolType -{ -public: - Long(); - void readFrom(QDataStream* data); - qint64 getLong() const; - std::size_t hash() const; - bool operator==(const Long& other) const; - -private: - qint64 data; -}; - -#endif // LONG_H diff --git a/src/weechatproto/pointer.cpp b/src/weechatproto/pointer.cpp deleted file mode 100644 index a19c198..0000000 --- a/src/weechatproto/pointer.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "pointer.h" - -Pointer::Pointer(): data(0) -{ -} - -void Pointer::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - // Pointers are stored as length (1 byte) + data as hex string - quint8 length; - *data >> length; - - this->data = new QString(); - - quint8 current; - for (quint8 i = 0; i < length; ++i) - { - *data >> current; - this->data->append(static_cast(current)); - } -} - -const QString* Pointer::getPointer() const -{ - return this->data; -} diff --git a/src/weechatproto/pointer.h b/src/weechatproto/pointer.h deleted file mode 100644 index 5c09d3e..0000000 --- a/src/weechatproto/pointer.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef POINTER_H -#define POINTER_H - -#include "protocoltype.h" - -class Pointer : public ProtocolType -{ -public: - Pointer(); - void readFrom(QDataStream* data); - const QString* getPointer() const; - -private: - QString* data; -}; - -#endif // POINTER_H diff --git a/src/weechatproto/protocoltype.cpp b/src/weechatproto/protocoltype.cpp deleted file mode 100644 index 1e66f01..0000000 --- a/src/weechatproto/protocoltype.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "protocoltype.h" -#include "protocoltypeoverwriteexception.h" - -ProtocolType::ProtocolType(): isRead(false) -{ -} - -// Mark this object as read, so that the values can't -// be overwritten. A second call to this method will throw -// an exception. -void ProtocolType::markRead() -{ - if (isRead) - { - throw new ProtocolTypeOverWriteException("Cannot overwrite ProtocolType."); - } - - isRead = true; -} diff --git a/src/weechatproto/protocoltype.h b/src/weechatproto/protocoltype.h deleted file mode 100644 index f85d5fc..0000000 --- a/src/weechatproto/protocoltype.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef PROTOCOLTYPE_H -#define PROTOCOLTYPE_H - -#include -#include - -class ProtocolType -{ -public: - ProtocolType(); - - virtual void readFrom(QDataStream* data) = 0; - void markRead(); - - // For hashtable - virtual std::size_t hash() const = 0; - -private: - bool isRead; -}; - -#endif // PROTOCOLTYPE_H diff --git a/src/weechatproto/protocoltypeoverwriteexception.cpp b/src/weechatproto/protocoltypeoverwriteexception.cpp deleted file mode 100644 index c50ec19..0000000 --- a/src/weechatproto/protocoltypeoverwriteexception.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "protocoltypeoverwriteexception.h" - -ProtocolTypeOverWriteException::ProtocolTypeOverWriteException(const std::string& reason): - std::runtime_error(reason) -{ -} diff --git a/src/weechatproto/protocoltypeoverwriteexception.h b/src/weechatproto/protocoltypeoverwriteexception.h deleted file mode 100644 index afe2e6c..0000000 --- a/src/weechatproto/protocoltypeoverwriteexception.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef PROTOCOLTYPEOVERWRITEEXCEPTION_H -#define PROTOCOLTYPEOVERWRITEEXCEPTION_H - -#include -#include - -class ProtocolTypeOverWriteException : public std::runtime_error -{ -public: - explicit ProtocolTypeOverWriteException(const std::string& reason); -}; - -#endif // PROTOCOLTYPEOVERWRITEEXCEPTION_H diff --git a/src/weechatproto/string.cpp b/src/weechatproto/string.cpp deleted file mode 100644 index 8407dd7..0000000 --- a/src/weechatproto/string.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "string.h" - -String::String(): data(0) -{ -} - -void String::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - // Strings are stored as length (4 bytes) + data (no \0) - quint32 length; - *data >> length; - - this->data = new QString(); - - quint8 current; - for (quint32 i = 0; i < length; ++i) - { - *data >> current; - this->data->append(static_cast(current)); - } -} - -const QString* String::getString() const -{ - return this->data; -} diff --git a/src/weechatproto/string.h b/src/weechatproto/string.h deleted file mode 100644 index 651bf93..0000000 --- a/src/weechatproto/string.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef STRING_H -#define STRING_H - -#include "protocoltype.h" - -class String : public ProtocolType -{ -public: - String(); - void readFrom(QDataStream* data); - const QString* getString() const; - -private: - QString* data; -}; - -#endif // STRING_H diff --git a/src/weechatproto/time.cpp b/src/weechatproto/time.cpp deleted file mode 100644 index 92c1afb..0000000 --- a/src/weechatproto/time.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "time.h" -#include "long.h" - -Time::Time(): data(0) -{ -} - -void Time::readFrom(QDataStream* data) -{ - ProtocolType::markRead(); - - // Time is stored as length (1 byte) + data (unix time) - // It's the same format as a Long so we will read it that way - Long* timelong = new Long(); - timelong->readFrom(data); - quint64 timestamp = timelong->getLong(); - - this->data = new QDateTime(); - this->data->fromTime_t(timestamp); -} - -const QDateTime* Time::getTime() const -{ - return this->data; -} diff --git a/src/weechatproto/time.h b/src/weechatproto/time.h deleted file mode 100644 index 9b9c1a4..0000000 --- a/src/weechatproto/time.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TIME_H -#define TIME_H - -#include "protocoltype.h" - -#include - -class Time : public ProtocolType -{ -public: - Time(); - void readFrom(QDataStream* data); - const QDateTime* getTime() const; - -private: - QDateTime* data; -}; - -#endif // TIME_H diff --git a/src/weechatprotocolhandler.cpp b/src/weechatprotocolhandler.cpp index f7a1172..88f6369 100644 --- a/src/weechatprotocolhandler.cpp +++ b/src/weechatprotocolhandler.cpp @@ -93,7 +93,164 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data) delete idData; } + +// EVENTS + void WeeChatProtocolHandler::handleBufferLineAdded(QDataStream* data) { } + +void WeeChatProtocolHandler::handleUnknown(QDataStream *data) +{ + +} + + +// TYPES + +char WeeChatProtocolHandler::handleChar(QDataStream* data) +{ + quint8 c; + *data >> c; + return static_cast(c); +} + +qint32 WeeChatProtocolHandler::handleInt(QDataStream* data) +{ + qint32 ret; + *data >> ret; + return ret; +} + +qint64 WeeChatProtocolHandler::handleLong(QDataStream* data) +{ + // Long is stored as a string, with one byte showing the length + qint64 ret = 0; + quint8 length; + *data >> length; + + quint8 current; + qint8 magnitude = 1; + for (quint8 i = 0; i < length; ++i) + { + *data >> current; + + if (i == 0) + { + if (current == 45) // ASCII '-' + { + magnitude = -1; + } + } + else + { + ret *= 10; + } + + ret += magnitude * static_cast(current - '0'); + } + + return ret; +} + +QString WeeChatProtocolHandler::handleString(QDataStream* data) +{ + // Strings are stored as length (4 bytes) + data (no \0) + QString ret; + quint32 length; + *data >> length; + + quint8 current; + for (quint32 i = 0; i < length; ++i) + { + *data >> current; + ret.append(static_cast(current)); + } + + return ret; +} + +QByteArray WeeChatProtocolHandler::handleBuffer(QDataStream* data) +{ + // Buffer is the same as string, but just bytes + QByteArray ret; + quint32 length; + *data >> length; + + quint8 current; + for (quint32 i = 0; i < length; ++i) + { + *data >> current; + ret.append(static_cast(current)); + } + + return ret; +} + +QString WeeChatProtocolHandler::handlePointer(QDataStream* data) +{ + // Pointers are stored as length (1 byte) + data as hex string + QString ret; + quint8 length; + *data >> length; + + quint8 current; + for (quint8 i = 0; i < length; ++i) + { + *data >> current; + ret.append(static_cast(current)); + } + + return ret; +} + +QDateTime WeeChatProtocolHandler::handleTime(QDataStream* data) +{ + // Time is stored as length (1 byte) + data (unix time) + // It's the same format as a Long so we will read it that way + QDateTime ret; + quint64 timestamp = static_cast(handleLong(data)); + return ret.fromMSecsSinceEpoch(timestamp); +} + +QHash WeeChatProtocolHandler::handleHashTable(QDataStream* data) +{ + QHash ret; + + // The hashtable first contains the types for the key and value, these + // are currently always 'str' + QString keyType = handleString(data); + QString valueType = handleString(data); + + Q_ASSERT_X(keyType == "str" && valueType == "str", + "HashTable::readFrom", "HashTable uses type that isn't string."); + + // Next is the length of the table as a 4-byte int + qint32 length = 0; + *data >> length; + + for (qint32 i = 0; i < length; ++i) + { + QString key = handleString(data); + QString value = handleString(data); + + ret.insert(key, value); + } + + return ret; +} + +QList WeeChatProtocolHandler::handleStringArray(QDataStream* data) +{ + QList ret; + + // Array consists of type (string), number of elements (int 4 bytes) and data + QString type = handleString(data); + qint32 length = handleInt(data); +} + +QList WeeChatProtocolHandler::handleIntegerArray(QDataStream* data) +{ + +} diff --git a/src/weechatprotocolhandler.h b/src/weechatprotocolhandler.h index 07c6307..af6aadd 100644 --- a/src/weechatprotocolhandler.h +++ b/src/weechatprotocolhandler.h @@ -9,6 +9,7 @@ #include #include +#include #include "protocolhandler.h" class WeeChatProtocolHandler : public ProtocolHandler @@ -40,10 +41,14 @@ protected: char handleChar(QDataStream* data); qint32 handleInt(QDataStream* data); qint64 handleLong(QDataStream* data); - QString* handleString(QDataStream* data); - QByteArray* handleBuffer(QDataStream* data); - QString* handlePointer(QDataStream* data); - QDateTime* handleTime(QDataStream* data); + QString handleString(QDataStream* data); + QByteArray handleBuffer(QDataStream* data); + QString handlePointer(QDataStream* data); + QDateTime handleTime(QDataStream* data); + QHash handleHashTable(QDataStream* data); + // TODO: Hdata, Info, Infolist + QList handleStringArray(QDataStream* data); + QList handleIntegerArray(QDataStream* data); bool readingBody;