From 581b5a7f16a1c399958742a3b103f47ef9518662 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 2 Aug 2018 23:15:01 +0200 Subject: Addressbook support for the contact composer --- framework/qml/ComboBox.qml | 7 +++++ framework/src/domain/contactcontroller.cpp | 43 +++++++++++++++--------------- framework/src/domain/contactcontroller.h | 2 ++ framework/src/entitymodel.cpp | 13 +++++++++ framework/src/entitymodel.h | 2 ++ 5 files changed, 45 insertions(+), 22 deletions(-) (limited to 'framework') diff --git a/framework/qml/ComboBox.qml b/framework/qml/ComboBox.qml index 71c9bcc3..64a1c7b3 100644 --- a/framework/qml/ComboBox.qml +++ b/framework/qml/ComboBox.qml @@ -32,6 +32,13 @@ T.ComboBox { spacing: Kube.Units.largeSpacing padding: Kube.Units.smallSpacing + //Autoselect the first item + onCountChanged: { + if (currentIndex < 0) { + currentIndex = 0 + } + } + contentItem: Kube.Label { leftPadding: Kube.Units.smallSpacing rightPadding: Kube.Units.largeSpacing diff --git a/framework/src/domain/contactcontroller.cpp b/framework/src/domain/contactcontroller.cpp index 6bcaac62..0b46b34d 100644 --- a/framework/src/domain/contactcontroller.cpp +++ b/framework/src/domain/contactcontroller.cpp @@ -70,31 +70,30 @@ void ContactController::save() { using namespace Sink; using namespace Sink::ApplicationDomain; - Query query; - query.containsFilter(ResourceCapabilities::Contact::storage); - auto job = Store::fetchAll(query) - .then([=](const QList &resources) { - if (!resources.isEmpty()) { - auto resourceId = resources[0]->identifier(); - KContacts::Addressee addressee; - addressee.setGivenName(getFirstName()); - addressee.setFamilyName(getLastName()); - addressee.setFormattedName(getFirstName() + " " + getLastName()); - KContacts::VCardConverter converter; - const auto vcard = converter.createVCard(addressee, KContacts::VCardConverter::v3_0); - - Contact contact(resourceId); - contact.setVcard(vcard); - - return Store::create(contact); - } - SinkWarning() << "Failed to find a resource for the contact"; - return KAsync::error(0, "Failed to find a contact resource."); - }) + + const auto addressbook = getAddressbook(); + if (!addressbook) { + qWarning() << "No addressbook selected"; + } + + KContacts::Addressee addressee; + addressee.setGivenName(getFirstName()); + addressee.setFamilyName(getLastName()); + addressee.setFormattedName(getFirstName() + " " + getLastName()); + KContacts::VCardConverter converter; + + Contact contact(addressbook->resourceInstanceIdentifier()); + contact.setVcard(converter.createVCard(addressee, KContacts::VCardConverter::v3_0)); + contact.setAddressbook(*addressbook); + + auto job = Store::create(contact) .then([&] (const KAsync::Error &error) { - SinkLog() << "Failed to save the contact: " << error; + if (error) { + SinkWarning() << "Failed to save the contact: " << error; + } emit done(); }); + run(job); } diff --git a/framework/src/domain/contactcontroller.h b/framework/src/domain/contactcontroller.h index 61e37de8..ea853048 100644 --- a/framework/src/domain/contactcontroller.h +++ b/framework/src/domain/contactcontroller.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "controller.h" @@ -44,6 +45,7 @@ class KUBE_EXPORT ContactController : public Kube::Controller KUBE_CONTROLLER_PROPERTY(QString, Company, company) KUBE_CONTROLLER_PROPERTY(QString, JobTitle, jobTitle) KUBE_CONTROLLER_PROPERTY(QByteArray, ImageData, imageData) + KUBE_CONTROLLER_PROPERTY(Sink::ApplicationDomain::ApplicationDomainType::Ptr, Addressbook, addressbook) KUBE_CONTROLLER_LISTCONTROLLER(mails) KUBE_CONTROLLER_LISTCONTROLLER(phones) diff --git a/framework/src/entitymodel.cpp b/framework/src/entitymodel.cpp index b2d7a9ec..5a79fdf2 100644 --- a/framework/src/entitymodel.cpp +++ b/framework/src/entitymodel.cpp @@ -60,8 +60,11 @@ void EntityModel::runQuery(const Query &query) { if (mType == "calendar") { mModel = Store::loadModel(query); + } else if (mType == "addressbook") { + mModel = Store::loadModel(query); } else { qWarning() << "Type not supported " << mType; + Q_ASSERT(false); } setSourceModel(mModel.data()); } @@ -139,3 +142,13 @@ QVariantMap EntityModel::filter() const { return {}; } + + +QVariantMap EntityModel::data(int row) const +{ + QVariantMap map; + for (const auto &r : mRoleNames.keys()) { + map.insert(mRoleNames.value(r), data(index(row, 0), r)); + } + return map; +} diff --git a/framework/src/entitymodel.h b/framework/src/entitymodel.h index b8a0417e..add66d78 100644 --- a/framework/src/entitymodel.h +++ b/framework/src/entitymodel.h @@ -63,6 +63,8 @@ public: void setFilter(const QVariantMap &); QVariantMap filter() const; + Q_INVOKABLE QVariantMap data(int row) const; + private: void runQuery(const Sink::Query &query); void updateQuery(); -- cgit v1.2.3