File indexing completed on 2025-01-05 04:55:03
0001 /* 0002 Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com> 0003 0004 This library is free software; you can redistribute it and/or modify it 0005 under the terms of the GNU Library General Public License as published by 0006 the Free Software Foundation; either version 2 of the License, or (at your 0007 option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, but WITHOUT 0010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 0012 License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to the 0016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0017 02110-1301, USA. 0018 */ 0019 0020 #include "entitycontroller.h" 0021 0022 #include <sink/store.h> 0023 #include <sink/log.h> 0024 0025 #include <QVariantMap> 0026 0027 using namespace Sink; 0028 using namespace Sink::ApplicationDomain; 0029 0030 static QByteArrayList toByteArrayList(const QVariantList &list) 0031 { 0032 QByteArrayList s; 0033 for (const auto &e : list) { 0034 s << e.toByteArray(); 0035 } 0036 return s; 0037 } 0038 0039 EntityController::EntityController(QObject *parent) 0040 : QObject(parent) 0041 { 0042 0043 } 0044 0045 static KAsync::Job<void> createCalendar(const QByteArray &resourceId, const QVariantMap &object) 0046 { 0047 using Sink::ApplicationDomain::Calendar; 0048 0049 auto calendar = ApplicationDomainType::createEntity<Calendar>(resourceId); 0050 calendar.setName(object["name"].toString()); 0051 if (object.contains("color")) { 0052 calendar.setColor(object["color"].toByteArray()); 0053 } 0054 calendar.setEnabled(object["enabled"].toBool()); 0055 if (object.contains("contentTypes")) { 0056 calendar.setContentTypes(toByteArrayList(object["contentTypes"].toList())); 0057 } else { 0058 calendar.setContentTypes({"event", "todo"}); 0059 } 0060 return Sink::Store::create(calendar); 0061 } 0062 0063 static KAsync::Job<QByteArray> findResource(const QVariantMap &map) 0064 { 0065 if (map.contains("resource")) { 0066 return KAsync::value(map.value("resource").toByteArray()); 0067 } 0068 0069 Query query; 0070 //FIXME A resource that can store a type normally also provides it. Doesn't work for the mailtransportresource though. 0071 //Resource should probably just provide $type.store for all types. 0072 query.containsFilter<SinkResource::Capabilities>(map["type"].toByteArray()); 0073 query.filter<SinkResource::Account>(map["account"].toByteArray()); 0074 return Store::fetchOne<SinkResource>(query) 0075 .then([=](const SinkResource &resource) { 0076 return KAsync::value(resource.identifier()); 0077 }); 0078 } 0079 0080 void EntityController::create(const QVariantMap &map) 0081 { 0082 qDebug() << "Create entity " << map; 0083 auto job = findResource(map) 0084 .then([=](const QByteArray &resource) { 0085 if (map["type"].toString() == "calendar") { 0086 return createCalendar(resource, map["entity"].toMap()); 0087 } 0088 return KAsync::error("Not handled"); 0089 }); 0090 job.exec(); 0091 } 0092 0093 void EntityController::remove(const QString &type, const QVariant &entity) 0094 { 0095 if (type == "calendar") { 0096 Sink::Store::remove<Sink::ApplicationDomain::Calendar>(*entity.value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>()).exec(); 0097 } else if (type == "folder") { 0098 Sink::Store::remove<Sink::ApplicationDomain::Folder>(*entity.value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>()).exec(); 0099 } else { 0100 //Not implemented 0101 Q_ASSERT(false); 0102 } 0103 }