diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-05 15:04:00 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-05 15:04:00 +0200 |
commit | 4b1798f0cdf87361869e7cf2b341acacd056c410 (patch) | |
tree | 3ff780641acdcb20b81f9b41533afd50a2525d38 /framework/src/domain/peoplemodel.cpp | |
parent | 71721aa4f3e85bea1a2fe504e86d99f80a3106a9 (diff) | |
download | kube-4b1798f0cdf87361869e7cf2b341acacd056c410.tar.gz kube-4b1798f0cdf87361869e7cf2b341acacd056c410.zip |
Moved cpp code into src directory
Diffstat (limited to 'framework/src/domain/peoplemodel.cpp')
-rw-r--r-- | framework/src/domain/peoplemodel.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/framework/src/domain/peoplemodel.cpp b/framework/src/domain/peoplemodel.cpp new file mode 100644 index 00000000..c9e7248c --- /dev/null +++ b/framework/src/domain/peoplemodel.cpp | |||
@@ -0,0 +1,127 @@ | |||
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 | |||
25 | PeopleModel::PeopleModel(QObject *parent) | ||
26 | : QSortFilterProxyModel() | ||
27 | { | ||
28 | using namespace Sink::ApplicationDomain; | ||
29 | |||
30 | setDynamicSortFilter(true); | ||
31 | sort(0, Qt::DescendingOrder); | ||
32 | setFilterCaseSensitivity(Qt::CaseInsensitive); | ||
33 | Sink::Query query; | ||
34 | query.setFlags(Sink::Query::LiveQuery); | ||
35 | query.request<Contact::Fn>(); | ||
36 | query.request<Contact::Emails>(); | ||
37 | query.request<Contact::Addressbook>(); | ||
38 | query.request<Contact::Vcard>(); | ||
39 | query.request<Contact::Firstname>(); | ||
40 | query.request<Contact::Lastname>(); | ||
41 | runQuery(query); | ||
42 | } | ||
43 | |||
44 | PeopleModel::~PeopleModel() | ||
45 | { | ||
46 | |||
47 | } | ||
48 | |||
49 | void PeopleModel::setFilter(const QString &filter) | ||
50 | { | ||
51 | setFilterWildcard(filter); | ||
52 | } | ||
53 | |||
54 | QString PeopleModel::filter() const | ||
55 | { | ||
56 | return {}; | ||
57 | } | ||
58 | |||
59 | QHash< int, QByteArray > PeopleModel::roleNames() const | ||
60 | { | ||
61 | static QHash<int, QByteArray> roles = { | ||
62 | {Name, "name"}, | ||
63 | {Emails, "emails"}, | ||
64 | {Addressbook, "addressbook"}, | ||
65 | {Type, "type"}, | ||
66 | {DomainObject, "domainObject"}, | ||
67 | {FirstName, "firstName"}, | ||
68 | {LastName, "lastName"} | ||
69 | }; | ||
70 | return roles; | ||
71 | } | ||
72 | |||
73 | static QStringList toStringList(const QList<Sink::ApplicationDomain::Contact::Email> &list) | ||
74 | { | ||
75 | QStringList out; | ||
76 | std::transform(list.constBegin(), list.constEnd(), std::back_inserter(out), [] (const Sink::ApplicationDomain::Contact::Email &s) { return s.email; }); | ||
77 | return out; | ||
78 | } | ||
79 | |||
80 | QVariant PeopleModel::data(const QModelIndex &idx, int role) const | ||
81 | { | ||
82 | auto srcIdx = mapToSource(idx); | ||
83 | auto contact = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>(); | ||
84 | switch (role) { | ||
85 | case Name: | ||
86 | return contact->getFn(); | ||
87 | case Emails: | ||
88 | return QVariant::fromValue(toStringList(contact->getEmails())); | ||
89 | case Addressbook: | ||
90 | return contact->getAddressbook(); | ||
91 | case Type: | ||
92 | return "contact"; | ||
93 | case DomainObject: | ||
94 | return QVariant::fromValue(contact); | ||
95 | case FirstName: | ||
96 | return contact->getFirstname(); | ||
97 | case LastName: | ||
98 | return contact->getLastname(); | ||
99 | } | ||
100 | return QSortFilterProxyModel::data(idx, role); | ||
101 | } | ||
102 | |||
103 | bool PeopleModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | ||
104 | { | ||
105 | const auto leftName = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>()->getFn(); | ||
106 | const auto rightName = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Contact::Ptr>()->getFn(); | ||
107 | return leftName < rightName; | ||
108 | } | ||
109 | |||
110 | void PeopleModel::runQuery(const Sink::Query &query) | ||
111 | { | ||
112 | mModel = Sink::Store::loadModel<Sink::ApplicationDomain::Contact>(query); | ||
113 | setSourceModel(mModel.data()); | ||
114 | } | ||
115 | |||
116 | void PeopleModel::setAddressbook(const QVariant &parentFolder) | ||
117 | { | ||
118 | //TODO filter query by addressbook | ||
119 | qWarning() << "The addressbook filter is not yet implemented"; | ||
120 | } | ||
121 | |||
122 | QVariant PeopleModel::addressbook() const | ||
123 | { | ||
124 | return QVariant(); | ||
125 | } | ||
126 | |||
127 | |||