summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/contactcontroller.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/contactcontroller.cpp')
-rw-r--r--framework/src/domain/contactcontroller.cpp42
1 files changed, 38 insertions, 4 deletions
diff --git a/framework/src/domain/contactcontroller.cpp b/framework/src/domain/contactcontroller.cpp
index effe96e8..e27c4a3b 100644
--- a/framework/src/domain/contactcontroller.cpp
+++ b/framework/src/domain/contactcontroller.cpp
@@ -19,6 +19,8 @@
19#include "contactcontroller.h" 19#include "contactcontroller.h"
20 20
21#include <sink/applicationdomaintype.h> 21#include <sink/applicationdomaintype.h>
22#include <sink/store.h>
23#include <sink/log.h>
22#include <KContacts/VCardConverter> 24#include <KContacts/VCardConverter>
23 25
24class MailsController : public Kube::ListPropertyController 26class MailsController : public Kube::ListPropertyController
@@ -64,22 +66,54 @@ ContactController::ContactController()
64 updateSaveAction(); 66 updateSaveAction();
65} 67}
66 68
67void ContactController::save() { 69void ContactController::save()
68 qWarning() << "Saving is not implemented"; 70{
71 using namespace Sink;
72 using namespace Sink::ApplicationDomain;
73 Query query;
74 query.containsFilter<SinkResource::Capabilities>(ResourceCapabilities::Contact::storage);
75 auto job = Store::fetchAll<SinkResource>(query)
76 .then([=](const QList<SinkResource::Ptr> &resources) {
77 if (!resources.isEmpty()) {
78 auto resourceId = resources[0]->identifier();
79 KContacts::Addressee addressee;
80 addressee.setGivenName(getFirstName());
81 addressee.setFamilyName(getLastName());
82 addressee.setFormattedName(getFirstName() + " " + getLastName());
83 KContacts::VCardConverter converter;
84 const auto vcard = converter.createVCard(addressee, KContacts::VCardConverter::v3_0);
85
86 Contact contact(resourceId);
87 contact.setVcard(vcard);
88
89 return Store::create(contact);
90 }
91 SinkWarning() << "Failed to find a resource for the contact";
92 return KAsync::error<void>(0, "Failed to find a contact resource.");
93 })
94 .then([&] (const KAsync::Error &error) {
95 SinkLog() << "Failed to save the contact: " << error;
96 emit done();
97 });
98 run(job);
69} 99}
70 100
71void ContactController::updateSaveAction() 101void ContactController::updateSaveAction()
72{ 102{
73 saveAction()->setEnabled(!getName().isEmpty()); 103 saveAction()->setEnabled(!getFirstName().isEmpty());
74} 104}
75 105
76void ContactController::loadContact(const QVariant &contact) 106void ContactController::loadContact(const QVariant &contact)
77{ 107{
78 if (auto c = contact.value<Sink::ApplicationDomain::Contact::Ptr>()) { 108 if (auto c = contact.value<Sink::ApplicationDomain::Contact::Ptr>()) {
79 setName(c->getFn());
80 const auto &vcard = c->getVcard(); 109 const auto &vcard = c->getVcard();
81 KContacts::VCardConverter converter; 110 KContacts::VCardConverter converter;
82 const auto addressee = converter.parseVCard(vcard); 111 const auto addressee = converter.parseVCard(vcard);
112
113 setName(c->getFn());
114 setFirstName(addressee.givenName());
115 setLastName(addressee.familyName());
116
83 static_cast<MailsController*>(mailsController())->set(addressee.emails()); 117 static_cast<MailsController*>(mailsController())->set(addressee.emails());
84 118
85 QStringList numbers; 119 QStringList numbers;