summaryrefslogtreecommitdiffstats
path: root/framework/src/domainobjectcontroller.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-05 16:17:51 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-08 16:26:38 +0100
commit0dc0b4be6e9ba07774efedee102a23e89eef8e85 (patch)
tree2d6c3a9388c5dde159ffbc72fdd6d0bd285741ed /framework/src/domainobjectcontroller.cpp
parent772eeaa715551cbb4693ab221ea8fc6dad7e1bb6 (diff)
downloadkube-0dc0b4be6e9ba07774efedee102a23e89eef8e85.tar.gz
kube-0dc0b4be6e9ba07774efedee102a23e89eef8e85.zip
Composer test
Diffstat (limited to 'framework/src/domainobjectcontroller.cpp')
-rw-r--r--framework/src/domainobjectcontroller.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/framework/src/domainobjectcontroller.cpp b/framework/src/domainobjectcontroller.cpp
new file mode 100644
index 00000000..eb1fd417
--- /dev/null
+++ b/framework/src/domainobjectcontroller.cpp
@@ -0,0 +1,109 @@
1/*
2 Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "domainobjectcontroller.h"
20
21#include <sink/store.h>
22#include <sink/applicationdomaintype.h>
23#include <kmime/kmime_message.h>
24#include <mailtemplates.h>
25
26using namespace Kube;
27
28DomainObjectController::DomainObjectController(QObject *parent)
29 : QObject(parent)
30{
31
32}
33
34QStringList toStringList(const QVariantList &list)
35{
36 QStringList s;
37 for (const auto &e : list) {
38 s << e.toString();
39 }
40 return s;
41}
42
43void DomainObjectController::create(const QVariantMap &object)
44{
45 using namespace Sink::ApplicationDomain;
46 qWarning() << "Creating " << object;
47 auto type = object["type"].toString();
48 if (type == getTypeName<Mail>()) {
49 auto toAddresses = toStringList(object["to"].toList());
50 auto ccAddresses = toStringList(object["cc"].toList());
51 auto bccAddresses = toStringList(object["bcc"].toList());
52
53 KMime::Types::Mailbox mb;
54 mb.fromUnicodeString("identity@example.org");
55 auto msg = MailTemplates::createMessage({},
56 toAddresses,
57 ccAddresses,
58 bccAddresses,
59 mb,
60 object["subject"].toString(),
61 object["body"].toString(),
62 {},
63 {},
64 {},
65 {});
66
67 Sink::Query query;
68 query.containsFilter<SinkResource::Capabilities>(ResourceCapabilities::Mail::storage);
69 query.filter<SinkResource::Account>("account1");
70 Sink::Store::fetchAll<SinkResource>(query)
71 .then([=](const QList<SinkResource::Ptr> &resources) {
72 if (!resources.isEmpty()) {
73 auto resourceId = resources[0]->identifier();
74 qWarning() << "Using resource " << resourceId << " from account " << resources[0]->getAccount();
75 auto mail = ApplicationDomainType::createEntity<Mail>(resourceId);
76 mail.setMimeMessage(msg->encodedContent(true));
77 Sink::Store::create<Mail>(mail).exec();
78 monitor(QVariant::fromValue(Mail::Ptr::create(mail)));
79 } else {
80 qWarning() << "No resources found.";
81 }
82 }).exec();
83 }
84
85}
86
87void DomainObjectController::monitor(const QVariant &object)
88{
89 using namespace Sink::ApplicationDomain;
90 qWarning() << "Monitoring " << object;
91 auto mail = object.value<Mail::Ptr>();
92 Q_ASSERT(mail);
93 Sink::Query query{*mail};
94 query.setFlags(Sink::Query::LiveQuery);
95 mModel = Sink::Store::loadModel<Mail>(query);
96 QObject::connect(mModel.data(), &QAbstractItemModel::rowsInserted, [=](const QModelIndex &index, int start, int end) {
97 for (int i = start; i <= end; i++) {
98 auto mail = mModel->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).value<Mail::Ptr>();
99 mCurrentObject = QVariant::fromValue(mail);
100 emit currentObjectChanged();
101 break;
102 }
103 });
104}
105
106QVariant DomainObjectController::currentObject() const
107{
108 return mCurrentObject;
109}