summaryrefslogtreecommitdiffstats
path: root/framework
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-03-10 21:50:08 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-03-10 21:50:48 +0100
commite52a02bb77c5292a016d2c14e0730d44d25d4000 (patch)
treebb67d615bd8d699ca0ff2c5996253acf46d5389a /framework
parentca120180460c7eeefaec9b1f31e5ad8aee32df2d (diff)
downloadkube-e52a02bb77c5292a016d2c14e0730d44d25d4000.tar.gz
kube-e52a02bb77c5292a016d2c14e0730d44d25d4000.zip
Simple peoplemodel for addressbook
Currently just queries for a flat list of contacts. Read-only.
Diffstat (limited to 'framework')
-rw-r--r--framework/domain/CMakeLists.txt1
-rw-r--r--framework/domain/contactcontroller.cpp33
-rw-r--r--framework/domain/contactcontroller.h12
-rw-r--r--framework/domain/mailplugin.cpp2
-rw-r--r--framework/domain/peoplemodel.cpp101
-rw-r--r--framework/domain/peoplemodel.h63
6 files changed, 192 insertions, 20 deletions
diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt
index 9abc54b0..b3f7acc5 100644
--- a/framework/domain/CMakeLists.txt
+++ b/framework/domain/CMakeLists.txt
@@ -25,6 +25,7 @@ set(mailplugin_SRCS
25 foldercontroller.cpp 25 foldercontroller.cpp
26 mouseproxy.cpp 26 mouseproxy.cpp
27 contactcontroller.cpp 27 contactcontroller.cpp
28 peoplemodel.cpp
28) 29)
29find_package(KF5 REQUIRED COMPONENTS Package) 30find_package(KF5 REQUIRED COMPONENTS Package)
30 31
diff --git a/framework/domain/contactcontroller.cpp b/framework/domain/contactcontroller.cpp
index 0be90e77..a1a805c9 100644
--- a/framework/domain/contactcontroller.cpp
+++ b/framework/domain/contactcontroller.cpp
@@ -18,16 +18,17 @@
18 18
19#include "contactcontroller.h" 19#include "contactcontroller.h"
20 20
21#include <sink/applicationdomaintype.h>
22
21ContactController::ContactController() 23ContactController::ContactController()
22 : Kube::Controller(), 24 : Kube::Controller(),
23 action_save{new Kube::ControllerAction{this, &ContactController::save}} 25 action_save{new Kube::ControllerAction{this, &ContactController::save}}
24{ 26{
25 loadContact("test");
26 updateSaveAction(); 27 updateSaveAction();
27} 28}
28 29
29void ContactController::save() { 30void ContactController::save() {
30 //TODO 31 qWarning() << "Saving is not implemented";
31} 32}
32 33
33void ContactController::updateSaveAction() 34void ContactController::updateSaveAction()
@@ -37,23 +38,31 @@ void ContactController::updateSaveAction()
37 38
38void ContactController::loadContact(const QVariant &contact) 39void ContactController::loadContact(const QVariant &contact)
39{ 40{
40 setName("Anita Rosenzweig"); 41 if (auto c = contact.value<Sink::ApplicationDomain::Contact::Ptr>()) {
41 m_emails << "rosenzweig@kolabnow.com" << "wolfi@kolabnow.com"; 42 setName(c->getFn());
43 QStringList emails;
44 for (const auto &e : c->getEmails()) {
45 emails << e;
46 }
47 setEmails(emails);
48 }
42} 49}
43 50
44void ContactController::removeEmail(const QString &email) 51QVariant ContactController::contact() const
45{ 52{
46 m_emails.removeOne(email); 53 return QVariant{};
47 emit emailsChanged();
48} 54}
49 55
50void ContactController::addEmail(const QString &email) 56void ContactController::removeEmail(const QString &email)
51{ 57{
52 m_emails << email; 58 auto emails = getEmails();
53 emit emailsChanged(); 59 emails.removeAll(email);
60 setEmails(emails);
54} 61}
55 62
56QStringList ContactController::emails() const 63void ContactController::addEmail(const QString &email)
57{ 64{
58 return m_emails; 65 auto emails = getEmails();
66 emails.append(email);
67 setEmails(emails);
59} 68}
diff --git a/framework/domain/contactcontroller.h b/framework/domain/contactcontroller.h
index d9bca563..d1973d9c 100644
--- a/framework/domain/contactcontroller.h
+++ b/framework/domain/contactcontroller.h
@@ -29,28 +29,24 @@ class ContactController : public Kube::Controller
29{ 29{
30 Q_OBJECT 30 Q_OBJECT
31 31
32 Q_PROPERTY(QStringList emails READ emails NOTIFY emailsChanged) 32 // Input properties
33 Q_PROPERTY(QVariant contact READ contact WRITE loadContact)
33 34
34 //Interface properties 35 //Interface properties
35 KUBE_CONTROLLER_PROPERTY(QString, Name, name) 36 KUBE_CONTROLLER_PROPERTY(QString, Name, name)
37 KUBE_CONTROLLER_PROPERTY(QStringList, Emails, emails)
36 38
37 KUBE_CONTROLLER_ACTION(save) 39 KUBE_CONTROLLER_ACTION(save)
38 40
39public: 41public:
40 explicit ContactController(); 42 explicit ContactController();
41 43
42 QStringList emails() const;
43
44 Q_INVOKABLE void loadContact(const QVariant &contact); 44 Q_INVOKABLE void loadContact(const QVariant &contact);
45 Q_INVOKABLE void removeEmail(const QString &email); 45 Q_INVOKABLE void removeEmail(const QString &email);
46 Q_INVOKABLE void addEmail(const QString &email); 46 Q_INVOKABLE void addEmail(const QString &email);
47 47
48signals: 48 QVariant contact() const;
49 void emailsChanged();
50 49
51private slots: 50private slots:
52 void updateSaveAction(); 51 void updateSaveAction();
53
54private:
55 QStringList m_emails;
56}; 52};
diff --git a/framework/domain/mailplugin.cpp b/framework/domain/mailplugin.cpp
index fba6c458..12b9a7ce 100644
--- a/framework/domain/mailplugin.cpp
+++ b/framework/domain/mailplugin.cpp
@@ -31,6 +31,7 @@
31#include "foldercontroller.h" 31#include "foldercontroller.h"
32#include "mouseproxy.h" 32#include "mouseproxy.h"
33#include "contactcontroller.h" 33#include "contactcontroller.h"
34#include "peoplemodel.h"
34 35
35#include <QtQml> 36#include <QtQml>
36 37
@@ -49,4 +50,5 @@ void MailPlugin::registerTypes (const char *uri)
49 qmlRegisterType<FolderController>(uri, 1, 0, "FolderController"); 50 qmlRegisterType<FolderController>(uri, 1, 0, "FolderController");
50 qmlRegisterType<MouseProxy>(uri, 1, 0, "MouseProxy"); 51 qmlRegisterType<MouseProxy>(uri, 1, 0, "MouseProxy");
51 qmlRegisterType<ContactController>(uri, 1, 0,"ContactController"); 52 qmlRegisterType<ContactController>(uri, 1, 0,"ContactController");
53 qmlRegisterType<PeopleModel>(uri, 1, 0,"PeopleModel");
52} 54}
diff --git a/framework/domain/peoplemodel.cpp b/framework/domain/peoplemodel.cpp
new file mode 100644
index 00000000..7cc2f3fa
--- /dev/null
+++ b/framework/domain/peoplemodel.cpp
@@ -0,0 +1,101 @@
1/*
2 Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net>
3 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
9
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19*/
20#include "peoplemodel.h"
21
22#include <sink/standardqueries.h>
23#include <sink/store.h>
24
25PeopleModel::PeopleModel(QObject *parent)
26 : QSortFilterProxyModel()
27{
28 using namespace Sink::ApplicationDomain;
29
30 setDynamicSortFilter(true);
31 sort(0, Qt::DescendingOrder);
32 Sink::Query query;
33 query.setFlags(Sink::Query::LiveQuery);
34 query.request<Contact::Fn>();
35 query.request<Contact::Emails>();
36 query.request<Contact::Addressbook>();
37 query.request<Contact::Vcard>();
38 runQuery(query);
39}
40
41PeopleModel::~PeopleModel()
42{
43
44}
45
46QHash< int, QByteArray > PeopleModel::roleNames() const
47{
48 static QHash<int, QByteArray> roles = {
49 {Name, "name"},
50 {Emails, "emails"},
51 {Addressbook, "addressbook"},
52 {Type, "type"},
53 {DomainObject, "domainObject"}
54 };
55 return roles;
56}
57
58QVariant PeopleModel::data(const QModelIndex &idx, int role) const
59{
60 auto srcIdx = mapToSource(idx);
61 auto contact = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>();
62 switch (role) {
63 case Name:
64 return contact->getFn();
65 case Emails:
66 return QVariant::fromValue(contact->getEmails());
67 case Addressbook:
68 return contact->getAddressbook();
69 case Type:
70 return "contact";
71 case DomainObject:
72 return QVariant::fromValue(contact);
73 }
74 return QSortFilterProxyModel::data(idx, role);
75}
76
77bool PeopleModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
78{
79 const auto leftName = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>()->getFn();
80 const auto rightName = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>()->getFn();
81 return leftName < rightName;
82}
83
84void PeopleModel::runQuery(const Sink::Query &query)
85{
86 mModel = Sink::Store::loadModel<Sink::ApplicationDomain::Contact>(query);
87 setSourceModel(mModel.data());
88}
89
90void PeopleModel::setAddressbook(const QVariant &parentFolder)
91{
92 //TODO filter query by addressbook
93 qWarning() << "The addressbook filter is not yet implemented";
94}
95
96QVariant PeopleModel::addressbook() const
97{
98 return QVariant();
99}
100
101
diff --git a/framework/domain/peoplemodel.h b/framework/domain/peoplemodel.h
new file mode 100644
index 00000000..419d59a6
--- /dev/null
+++ b/framework/domain/peoplemodel.h
@@ -0,0 +1,63 @@
1/*
2 Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net>
3 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
9
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19*/
20
21#pragma once
22
23#include <QSortFilterProxyModel>
24#include <QSharedPointer>
25
26namespace Sink {
27 class Query;
28};
29
30/**
31 * A model that mixes addressbooks, contact groups and contacts
32 */
33class PeopleModel : public QSortFilterProxyModel
34{
35 Q_OBJECT
36 Q_PROPERTY (QVariant addressbook READ addressbook WRITE setAddressbook)
37
38public:
39 PeopleModel(QObject *parent = Q_NULLPTR);
40 ~PeopleModel();
41
42 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
43
44 bool lessThan(const QModelIndex &left, const QModelIndex &right) const Q_DECL_OVERRIDE;
45
46 enum Roles {
47 Name = Qt::UserRole + 1,
48 Type,
49 Emails,
50 Addressbook,
51 DomainObject
52 };
53
54 QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
55
56 void runQuery(const Sink::Query &query);
57
58 void setAddressbook(const QVariant &parentFolder);
59 QVariant addressbook() const;
60
61private:
62 QSharedPointer<QAbstractItemModel> mModel;
63};