diff options
Diffstat (limited to 'framework/src/domain/outboxmodel.cpp')
-rw-r--r-- | framework/src/domain/outboxmodel.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/framework/src/domain/outboxmodel.cpp b/framework/src/domain/outboxmodel.cpp new file mode 100644 index 00000000..237648b1 --- /dev/null +++ b/framework/src/domain/outboxmodel.cpp | |||
@@ -0,0 +1,141 @@ | |||
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 | #include <QString> | ||
26 | |||
27 | #include <sink/standardqueries.h> | ||
28 | #include <sink/notifier.h> | ||
29 | #include <sink/notification.h> | ||
30 | |||
31 | |||
32 | OutboxModel::OutboxModel(QObject *parent) | ||
33 | : QSortFilterProxyModel(), | ||
34 | mNotifier(new Sink::Notifier{Sink::Query{}.containsFilter<Sink::ApplicationDomain::SinkResource::Capabilities>(Sink::ApplicationDomain::ResourceCapabilities::Mail::transport)}), | ||
35 | mStatus(NoStatus) | ||
36 | { | ||
37 | setDynamicSortFilter(true); | ||
38 | sort(0, Qt::DescendingOrder); | ||
39 | |||
40 | using namespace Sink::ApplicationDomain; | ||
41 | auto query = Sink::StandardQueries::outboxMails(); | ||
42 | query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus); | ||
43 | query.request<Mail::Subject>(); | ||
44 | query.request<Mail::Date>(); | ||
45 | query.request<Mail::Folder>(); | ||
46 | runQuery(query); | ||
47 | connect(this, &QAbstractItemModel::rowsInserted, this, &OutboxModel::countChanged); | ||
48 | connect(this, &QAbstractItemModel::rowsRemoved, this, &OutboxModel::countChanged); | ||
49 | |||
50 | mNotifier->registerHandler([this] (const Sink::Notification &n) { | ||
51 | //TODO aggregate status from multiple resources | ||
52 | if (n.type == Sink::Notification::Status) { | ||
53 | switch (n.code) { | ||
54 | case Sink::ApplicationDomain::Status::ErrorStatus: | ||
55 | mStatus = ErrorStatus; | ||
56 | break; | ||
57 | case Sink::ApplicationDomain::Status::BusyStatus: | ||
58 | mStatus = InProgressStatus; | ||
59 | break; | ||
60 | default: | ||
61 | mStatus = NoStatus; | ||
62 | break; | ||
63 | } | ||
64 | emit statusChanged(); | ||
65 | } | ||
66 | |||
67 | }); | ||
68 | |||
69 | } | ||
70 | |||
71 | OutboxModel::~OutboxModel() | ||
72 | { | ||
73 | |||
74 | } | ||
75 | |||
76 | QHash< int, QByteArray > OutboxModel::roleNames() const | ||
77 | { | ||
78 | QHash<int, QByteArray> roles; | ||
79 | |||
80 | roles[Subject] = "subject"; | ||
81 | roles[Date] = "date"; | ||
82 | roles[Status] = "status"; | ||
83 | roles[Id] = "id"; | ||
84 | roles[MimeMessage] = "mimeMessage"; | ||
85 | roles[DomainObject] = "domainObject"; | ||
86 | |||
87 | return roles; | ||
88 | } | ||
89 | |||
90 | QVariant OutboxModel::data(const QModelIndex &idx, int role) const | ||
91 | { | ||
92 | auto srcIdx = mapToSource(idx); | ||
93 | auto mail = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>(); | ||
94 | switch (role) { | ||
95 | case Subject: | ||
96 | return mail->getSubject(); | ||
97 | case Date: | ||
98 | return mail->getDate(); | ||
99 | case Status: { | ||
100 | const auto status = srcIdx.data(Sink::Store::StatusRole).toInt(); | ||
101 | if (status == Sink::ApplicationDomain::SyncStatus::SyncInProgress) { | ||
102 | return InProgressStatus; | ||
103 | } | ||
104 | if (status == Sink::ApplicationDomain::SyncStatus::SyncError) { | ||
105 | return ErrorStatus; | ||
106 | } | ||
107 | return PendingStatus; | ||
108 | } | ||
109 | case Id: | ||
110 | return mail->identifier(); | ||
111 | case DomainObject: | ||
112 | return QVariant::fromValue(mail); | ||
113 | case MimeMessage: { | ||
114 | return mail->getMimeMessage(); | ||
115 | } | ||
116 | } | ||
117 | return QSortFilterProxyModel::data(idx, role); | ||
118 | } | ||
119 | |||
120 | bool OutboxModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | ||
121 | { | ||
122 | const auto leftDate = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>()->getDate(); | ||
123 | const auto rightDate = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Mail::Ptr>()->getDate(); | ||
124 | return leftDate < rightDate; | ||
125 | } | ||
126 | |||
127 | void OutboxModel::runQuery(const Sink::Query &query) | ||
128 | { | ||
129 | mModel = Sink::Store::loadModel<Sink::ApplicationDomain::Mail>(query); | ||
130 | setSourceModel(mModel.data()); | ||
131 | } | ||
132 | |||
133 | int OutboxModel::count() const | ||
134 | { | ||
135 | return rowCount(); | ||
136 | } | ||
137 | |||
138 | int OutboxModel::status() const | ||
139 | { | ||
140 | return mStatus; | ||
141 | } | ||