/* * Copyright (C) 2015 Christian Mollekopf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "propertymapper.h" #include "applicationdomaintype.h" #include #include #include "mail_generated.h" #include "contact_generated.h" template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { return fbb.CreateString(property.toString().toStdString()).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { return fbb.CreateString(property.value().value.toStdString()).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { const auto ba = property.toByteArray(); const auto s = fbb.CreateString(ba.constData(), ba.size()); return s.o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { QByteArray ba; QDataStream ds(&ba, QIODevice::WriteOnly); ds << property.toDateTime(); return fbb.CreateString(ba.toStdString()).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { const auto list = property.value(); std::vector> vector; for (const auto &value : list) { auto offset = fbb.CreateString(value.toStdString()); vector.push_back(offset); } return fbb.CreateVector(vector).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { const auto value = property.value(); return Sink::ApplicationDomain::Buffer::CreateMailContactDirect(fbb, value.name.toUtf8().constData(), value.emailAddress.toUtf8().constData()).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { const auto list = property.value>(); std::vector> vector; for (const auto &value : list) { auto offset = Sink::ApplicationDomain::Buffer::CreateMailContactDirect(fbb, value.name.toUtf8().constData(), value.emailAddress.toUtf8().constData()).o; vector.push_back(offset); } return fbb.CreateVector(vector).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { const auto list = property.value>(); std::vector> vector; for (const auto &value : list) { auto offset = Sink::ApplicationDomain::Buffer::CreateContactEmailDirect(fbb, value.type, value.email.toUtf8().constData()).o; vector.push_back(offset); } return fbb.CreateVector(vector).o; } return 0; } QString propertyToString(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QString::fromStdString(property->str()); } return QString(); } template <> QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QString::fromStdString(property->str()); } return QVariant(); } template <> QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QVariant::fromValue(Sink::ApplicationDomain::Reference{QString::fromStdString(property->str()).toUtf8()}); } return QVariant(); } template <> QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QByteArray(property->c_str(), property->Length()); } return QVariant(); } template <> QVariant propertyToVariant(const flatbuffers::Vector *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QByteArray(reinterpret_cast(property->Data()), property->Length()); } return QVariant(); } template <> QVariant propertyToVariant(const flatbuffers::Vector> *property) { if (property) { QByteArrayList list; for (auto it = property->begin(); it != property->end();) { // We have to copy the memory, otherwise it would become eventually invalid list << QString::fromStdString((*it)->str()).toUtf8(); it.operator++(); } return QVariant::fromValue(list); } return QVariant(); } template <> QVariant propertyToVariant(const Sink::ApplicationDomain::Buffer::MailContact *property) { if (property) { return QVariant::fromValue(Sink::ApplicationDomain::Mail::Contact{propertyToString(property->name()), propertyToString(property->email())}); } return QVariant(); } template <> QVariant propertyToVariant>(const flatbuffers::Vector> *property) { if (property) { QList list; for (auto it = property->begin(); it != property->end();) { // We have to copy the memory, otherwise it would become eventually invalid list << Sink::ApplicationDomain::Mail::Contact{propertyToString(it->name()), propertyToString(it->email())}; it.operator++(); } return QVariant::fromValue(list); } return QVariant(); } template <> QVariant propertyToVariant>(const flatbuffers::Vector> *property) { if (property) { QList list; for (auto it = property->begin(); it != property->end();) { list << Sink::ApplicationDomain::Contact::Email{static_cast(it->type()), propertyToString(it->email())}; it.operator++(); } return QVariant::fromValue(list); } return QVariant(); } template <> QVariant propertyToVariant(uint8_t property) { return static_cast(property); } template <> QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { auto ba = QByteArray::fromRawData(property->c_str(), property->size()); QDateTime dt; QDataStream ds(&ba, QIODevice::ReadOnly); ds >> dt; return dt; } return QVariant(); }