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 ++ views/people/qml/People.qml | 3 +-- views/people/qml/PersonComposer.qml | 28 ++++++++++++++++++- 7 files changed, 73 insertions(+), 25 deletions(-) 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(); diff --git a/views/people/qml/People.qml b/views/people/qml/People.qml index 718d3c08..ad723427 100644 --- a/views/people/qml/People.qml +++ b/views/people/qml/People.qml @@ -51,8 +51,7 @@ FocusScope { leftMargin: Kube.Units.smallSpacing } text: qsTr("New Contact") - //visible: stack.depth == 1 - visible: false + visible: stack.depth == 1 onClicked: { stack.push(personComposer) diff --git a/views/people/qml/PersonComposer.qml b/views/people/qml/PersonComposer.qml index f56532f8..b409c3d6 100644 --- a/views/people/qml/PersonComposer.qml +++ b/views/people/qml/PersonComposer.qml @@ -194,7 +194,7 @@ Flickable { } } - Column{ + Column { id: address width: root.width - Kube.Units.largeSpacing @@ -226,6 +226,32 @@ Flickable { backgroundColor: "white" } } + + Column{ + width: root.width - Kube.Units.largeSpacing + spacing: Kube.Units.smallSpacing + + Kube.Label { + text: "Addressbook" + } + Kube.ComboBox { + width: Kube.Units.gridUnit * 20 + + model: Kube.EntityModel { + id: addressbookModel + type: "addressbook" + //TODO + //accountId: "" + roles: ["name", "color"] + } + textRole: "name" + Layout.fillWidth: true + onCurrentIndexChanged: { + contactController.addressbook = addressbookModel.data(currentIndex).object + } + } + } + Item { width: parent.width height: Kube.Units.largeSpacing -- cgit v1.2.3