/* * 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 "mail_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()) { const auto blob = property.value(); auto s = blob.value + (blob.isExternal ? ":ext" : ":int"); return fbb.CreateString(s.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()) { return fbb.CreateString(property.toByteArray().toStdString()).o; } return 0; } template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { return fbb.CreateString(property.toDateTime().toString().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; } QString propertyToString(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QString::fromStdString(property->c_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->c_str()); } return QVariant(); } template <> QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid auto s = QString::fromStdString(property->c_str()); auto ext = s.endsWith(":ext"); s.chop(4); auto blob = Sink::ApplicationDomain::BLOB{s}; blob.isExternal = ext; return QVariant::fromValue(blob); } 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->c_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 QString::fromStdString(property->c_str()).toUtf8(); } 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)->c_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(uint8_t property) { return static_cast(property); } template <> QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid return QDateTime::fromString(QString::fromStdString(property->c_str())); } return QVariant(); }