summaryrefslogtreecommitdiffstats
path: root/framework/domain/outboxmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/domain/outboxmodel.cpp')
-rw-r--r--framework/domain/outboxmodel.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/framework/domain/outboxmodel.cpp b/framework/domain/outboxmodel.cpp
new file mode 100644
index 00000000..b3533976
--- /dev/null
+++ b/framework/domain/outboxmodel.cpp
@@ -0,0 +1,114 @@
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#include "outboxmodel.h"
22
23#include <QFile>
24#include <QDateTime>
25
26#include <sink/standardqueries.h>
27
28
29OutboxModel::OutboxModel(QObject *parent)
30 : QSortFilterProxyModel()
31{
32 setDynamicSortFilter(true);
33 sort(0, Qt::DescendingOrder);
34
35 using namespace Sink::ApplicationDomain;
36 auto query = Sink::StandardQueries::outboxMails();
37 query.liveQuery = true;
38 query.request<Mail::Subject>();
39 query.request<Mail::Sender>();
40 query.request<Mail::SenderName>();
41 query.request<Mail::Date>();
42 query.request<Mail::Unread>();
43 query.request<Mail::Important>();
44 query.request<Mail::Draft>();
45 query.request<Mail::Folder>();
46 runQuery(query);
47}
48
49OutboxModel::~OutboxModel()
50{
51
52}
53
54QHash< int, QByteArray > OutboxModel::roleNames() const
55{
56 QHash<int, QByteArray> roles;
57
58 roles[Subject] = "subject";
59 roles[Sender] = "sender";
60 roles[SenderName] = "senderName";
61 roles[Date] = "date";
62 roles[Unread] = "unread";
63 roles[Important] = "important";
64 roles[Draft] = "draft";
65 roles[Id] = "id";
66 roles[MimeMessage] = "mimeMessage";
67 roles[DomainObject] = "domainObject";
68
69 return roles;
70}
71
72QVariant OutboxModel::data(const QModelIndex &idx, int role) const
73{
74 auto srcIdx = mapToSource(idx);
75 auto mail = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>();
76 switch (role) {
77 case Subject:
78 return mail->getSubject();
79 case Sender:
80 return mail->getSender();
81 case SenderName:
82 return mail->getSenderName();
83 case Date:
84 return mail->getDate();
85 case Unread:
86 return mail->getUnread();
87 case Important:
88 return mail->getImportant();
89 case Draft:
90 return mail->getDraft();
91 case Id:
92 return mail->identifier();
93 case DomainObject:
94 return QVariant::fromValue(mail);
95 case MimeMessage: {
96 return mail->getMimeMessage();
97 }
98 }
99 return QSortFilterProxyModel::data(idx, role);
100}
101
102bool OutboxModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
103{
104 const auto leftDate = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>()->getDate();
105 const auto rightDate = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>()->getDate();
106 return leftDate < rightDate;
107}
108
109void OutboxModel::runQuery(const Sink::Query &query)
110{
111 m_model = Sink::Store::loadModel<Sink::ApplicationDomain::Mail>(query);
112 setSourceModel(m_model.data());
113}
114