summaryrefslogtreecommitdiffstats
path: root/framework/domain/peoplemodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/domain/peoplemodel.cpp')
-rw-r--r--framework/domain/peoplemodel.cpp101
1 files changed, 101 insertions, 0 deletions
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