diff options
Diffstat (limited to 'framework/src/domain/mailcontroller.cpp')
-rw-r--r-- | framework/src/domain/mailcontroller.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/framework/src/domain/mailcontroller.cpp b/framework/src/domain/mailcontroller.cpp new file mode 100644 index 00000000..b912567a --- /dev/null +++ b/framework/src/domain/mailcontroller.cpp | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "mailcontroller.h" | ||
20 | |||
21 | #include <sink/store.h> | ||
22 | #include <sink/log.h> | ||
23 | #include <sink/standardqueries.h> | ||
24 | |||
25 | SINK_DEBUG_AREA("mailcontroller"); | ||
26 | |||
27 | using namespace Sink; | ||
28 | using namespace Sink::ApplicationDomain; | ||
29 | |||
30 | MailController::MailController() | ||
31 | : Kube::Controller(), | ||
32 | action_markAsRead{new Kube::ControllerAction{this, &MailController::markAsRead}}, | ||
33 | action_markAsUnread{new Kube::ControllerAction{this, &MailController::markAsUnread}}, | ||
34 | action_markAsImportant{new Kube::ControllerAction{this, &MailController::markAsImportant}}, | ||
35 | action_moveToTrash{new Kube::ControllerAction{this, &MailController::moveToTrash}}, | ||
36 | action_restoreFromTrash{new Kube::ControllerAction{this, &MailController::restoreFromTrash}}, | ||
37 | action_remove{new Kube::ControllerAction{this, &MailController::remove}}, | ||
38 | action_moveToFolder{new Kube::ControllerAction{this, &MailController::moveToFolder}} | ||
39 | { | ||
40 | QObject::connect(this, &MailController::mailChanged, &MailController::updateActions); | ||
41 | updateActions(); | ||
42 | } | ||
43 | |||
44 | void MailController::runModification(const std::function<void(ApplicationDomain::Mail &)> &f) | ||
45 | { | ||
46 | if (auto mail = getMail()) { | ||
47 | f(*mail); | ||
48 | run(Store::modify(*mail)); | ||
49 | } else if (auto mail = getThreadLeader()) { | ||
50 | f(*mail); | ||
51 | run(Store::modify(Sink::StandardQueries::completeThread(*mail), *mail)); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | void MailController::updateActions() | ||
56 | { | ||
57 | auto mail = getMail(); | ||
58 | if (!mail) { | ||
59 | mail= getThreadLeader(); | ||
60 | } | ||
61 | if (mail) { | ||
62 | action_moveToTrash->setEnabled(!mail->getTrash()); | ||
63 | action_restoreFromTrash->setEnabled(mail->getTrash()); | ||
64 | action_markAsRead->setEnabled(mail->getUnread()); | ||
65 | action_markAsUnread->setEnabled(!mail->getUnread()); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | void MailController::markAsRead() | ||
70 | { | ||
71 | runModification([] (ApplicationDomain::Mail &mail) { | ||
72 | mail.setUnread(false); | ||
73 | SinkLog() << "Mark as read " << mail.identifier(); | ||
74 | }); | ||
75 | } | ||
76 | |||
77 | void MailController::markAsUnread() | ||
78 | { | ||
79 | runModification([] (ApplicationDomain::Mail &mail) { | ||
80 | mail.setUnread(true); | ||
81 | SinkLog() << "Mark as unread " << mail.identifier(); | ||
82 | }); | ||
83 | } | ||
84 | |||
85 | void MailController::markAsImportant() | ||
86 | { | ||
87 | runModification([] (ApplicationDomain::Mail &mail) { | ||
88 | mail.setImportant(true); | ||
89 | SinkLog() << "Mark as important " << mail.identifier(); | ||
90 | }); | ||
91 | } | ||
92 | |||
93 | void MailController::moveToTrash() | ||
94 | { | ||
95 | runModification([] (ApplicationDomain::Mail &mail) { | ||
96 | mail.setTrash(true); | ||
97 | SinkLog() << "Move to trash " << mail.identifier(); | ||
98 | }); | ||
99 | } | ||
100 | |||
101 | void MailController::restoreFromTrash() | ||
102 | { | ||
103 | runModification([] (ApplicationDomain::Mail &mail) { | ||
104 | mail.setTrash(false); | ||
105 | SinkLog() << "Restore from trash " << mail.identifier(); | ||
106 | }); | ||
107 | } | ||
108 | |||
109 | void MailController::remove() | ||
110 | { | ||
111 | runModification([] (ApplicationDomain::Mail &mail) { | ||
112 | mail.setTrash(true); | ||
113 | SinkLog() << "Remove " << mail.identifier(); | ||
114 | }); | ||
115 | } | ||
116 | |||
117 | void MailController::moveToFolder() | ||
118 | { | ||
119 | runModification([&] (ApplicationDomain::Mail &mail) { | ||
120 | auto targetFolder = getTargetFolder(); | ||
121 | mail.setFolder(*targetFolder); | ||
122 | SinkLog() << "Moving to folder " << mail.identifier() << targetFolder->identifier(); | ||
123 | }); | ||
124 | } | ||
125 | |||