diff options
Diffstat (limited to 'framework/src/domain/folderlistmodel.cpp')
-rw-r--r-- | framework/src/domain/folderlistmodel.cpp | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/framework/src/domain/folderlistmodel.cpp b/framework/src/domain/folderlistmodel.cpp new file mode 100644 index 00000000..14405beb --- /dev/null +++ b/framework/src/domain/folderlistmodel.cpp | |||
@@ -0,0 +1,145 @@ | |||
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 "folderlistmodel.h" | ||
22 | #include <sink/store.h> | ||
23 | #include <sink/log.h> | ||
24 | #include <settings/settings.h> | ||
25 | |||
26 | using namespace Sink; | ||
27 | using namespace Sink::ApplicationDomain; | ||
28 | |||
29 | FolderListModel::FolderListModel(QObject *parent) : QSortFilterProxyModel() | ||
30 | { | ||
31 | setDynamicSortFilter(true); | ||
32 | sort(0, Qt::AscendingOrder); | ||
33 | |||
34 | Query query; | ||
35 | query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus); | ||
36 | query.request<Folder::Name>().request<Folder::Icon>().request<Folder::Parent>().request<Folder::SpecialPurpose>(); | ||
37 | query.requestTree<Folder::Parent>(); | ||
38 | query.setId("foldertree"); | ||
39 | runQuery(query); | ||
40 | } | ||
41 | |||
42 | FolderListModel::~FolderListModel() | ||
43 | { | ||
44 | |||
45 | } | ||
46 | |||
47 | QHash< int, QByteArray > FolderListModel::roleNames() const | ||
48 | { | ||
49 | QHash<int, QByteArray> roles; | ||
50 | |||
51 | roles[Name] = "name"; | ||
52 | roles[Icon] = "icon"; | ||
53 | roles[Id] = "id"; | ||
54 | roles[DomainObject] = "domainObject"; | ||
55 | roles[Status] = "status"; | ||
56 | |||
57 | return roles; | ||
58 | } | ||
59 | |||
60 | QVariant FolderListModel::data(const QModelIndex &idx, int role) const | ||
61 | { | ||
62 | auto srcIdx = mapToSource(idx); | ||
63 | auto folder = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Folder::Ptr>(); | ||
64 | switch (role) { | ||
65 | case Name: | ||
66 | return folder->getName(); | ||
67 | case Icon: | ||
68 | return folder->getIcon(); | ||
69 | case Id: | ||
70 | return folder->identifier(); | ||
71 | case DomainObject: | ||
72 | return QVariant::fromValue(folder); | ||
73 | case Status: { | ||
74 | switch (srcIdx.data(Sink::Store::StatusRole).toInt()) { | ||
75 | case Sink::ApplicationDomain::SyncStatus::SyncInProgress: | ||
76 | return InProgressStatus; | ||
77 | case Sink::ApplicationDomain::SyncStatus::SyncError: | ||
78 | return ErrorStatus; | ||
79 | case Sink::ApplicationDomain::SyncStatus::SyncSuccess: | ||
80 | return SuccessStatus; | ||
81 | } | ||
82 | return NoStatus; | ||
83 | } | ||
84 | } | ||
85 | return QSortFilterProxyModel::data(idx, role); | ||
86 | } | ||
87 | |||
88 | void FolderListModel::runQuery(const Query &query) | ||
89 | { | ||
90 | mModel = Store::loadModel<Folder>(query); | ||
91 | setSourceModel(mModel.data()); | ||
92 | } | ||
93 | |||
94 | void FolderListModel::setAccountId(const QVariant &accountId) | ||
95 | { | ||
96 | const auto account = accountId.toString().toUtf8(); | ||
97 | |||
98 | //Get all folders of an account | ||
99 | auto query = Query(); | ||
100 | query.resourceFilter<SinkResource::Account>(account); | ||
101 | query.setFlags(Sink::Query::LiveQuery | Sink::Query::UpdateStatus); | ||
102 | query.requestTree<Folder::Parent>(); | ||
103 | query.request<Folder::Name>() | ||
104 | .request<Folder::Icon>() | ||
105 | .request<Folder::Parent>() | ||
106 | .request<Folder::SpecialPurpose>(); | ||
107 | query.requestTree<Folder::Parent>(); | ||
108 | query.setId("foldertree" + account); | ||
109 | runQuery(query); | ||
110 | } | ||
111 | |||
112 | static int getPriority(const Sink::ApplicationDomain::Folder &folder) | ||
113 | { | ||
114 | auto specialPurpose = folder.getSpecialPurpose(); | ||
115 | if (specialPurpose.contains(Sink::ApplicationDomain::SpecialPurpose::Mail::inbox)) { | ||
116 | return 5; | ||
117 | } else if (specialPurpose.contains(Sink::ApplicationDomain::SpecialPurpose::Mail::drafts)) { | ||
118 | return 6; | ||
119 | } else if (specialPurpose.contains(Sink::ApplicationDomain::SpecialPurpose::Mail::sent)) { | ||
120 | return 7; | ||
121 | } else if (specialPurpose.contains(Sink::ApplicationDomain::SpecialPurpose::Mail::trash)) { | ||
122 | return 8; | ||
123 | } else if (!specialPurpose.isEmpty()) { | ||
124 | return 9; | ||
125 | } | ||
126 | return 10; | ||
127 | } | ||
128 | |||
129 | bool FolderListModel::lessThan(const QModelIndex &left, const QModelIndex &right) const | ||
130 | { | ||
131 | const auto leftFolder = left.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Folder::Ptr>(); | ||
132 | const auto rightFolder = right.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Folder::Ptr>(); | ||
133 | const auto leftPriority = getPriority(*leftFolder); | ||
134 | const auto rightPriority = getPriority(*rightFolder); | ||
135 | if (leftPriority == rightPriority) { | ||
136 | return leftFolder->getName() < rightFolder->getName(); | ||
137 | } | ||
138 | return leftPriority < rightPriority; | ||
139 | } | ||
140 | |||
141 | QVariant FolderListModel::accountId() const | ||
142 | { | ||
143 | return QVariant(); | ||
144 | } | ||
145 | |||