Akonadi Calendar

todopurger.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Sérgio Martins <iamsergio@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "todopurger.h"
8#include "calendarutils.h"
9#include "fetchjobcalendar.h"
10#include "todopurger_p.h"
11
12#include <KCalendarCore/Todo>
13
14#include <KLocalizedString>
15using namespace Akonadi;
16
17TodoPurgerPrivate::TodoPurgerPrivate(TodoPurger *q)
18 : q(q)
19{
20}
21
22void TodoPurgerPrivate::onCalendarLoaded(bool success, const QString &message)
23{
24 if (success) {
25 deleteTodos();
26 } else {
27 m_lastError = message;
28 if (m_calendarOwnership) {
29 m_calendar.clear();
30 }
31 Q_EMIT q->todosPurged(false, 0, 0);
32 }
33}
34
35void TodoPurgerPrivate::onItemsDeleted(int changeId, const QList<Item::Id> &deletedItems, IncidenceChanger::ResultCode result, const QString &message)
36{
37 if (changeId != m_currentChangeId) {
38 return; // Not ours.
39 }
40
41 m_lastError = message;
42 if (m_calendarOwnership) {
43 m_calendar.clear();
44 }
45 Q_EMIT q->todosPurged(result == IncidenceChanger::ResultCodeSuccess, deletedItems.count(), m_ignoredItems);
46}
47
48void TodoPurgerPrivate::deleteTodos()
49{
50 if (!m_changer) {
51 q->setIncidenceChager(new IncidenceChanger(this));
52 m_changer->setShowDialogsOnError(false);
53 m_changer->setHistoryEnabled(false);
54 }
55
56 const bool oldShowdialogs = m_changer->showDialogsOnError();
57 const bool oldGroupware = m_changer->groupwareCommunication();
58 m_changer->setShowDialogsOnError(false);
59 m_changer->setGroupwareCommunication(false);
60
61 m_changer->startAtomicOperation(i18n("Purging completed to-dos"));
62 const Akonadi::Item::List items = m_calendar->items();
63 Akonadi::Item::List toDelete;
64 m_ignoredItems = 0;
65 for (const Akonadi::Item &item : items) {
67
68 if (!todo || !todo->isCompleted()) {
69 continue;
70 }
71
72 if (treeIsDeletable(todo)) {
73 toDelete.append(item);
74 } else {
75 m_ignoredItems++;
76 }
77 }
78
79 if (toDelete.isEmpty()) {
80 if (m_calendarOwnership) {
81 m_calendar.clear();
82 }
83 Q_EMIT q->todosPurged(true, 0, m_ignoredItems);
84 } else {
85 m_currentChangeId = m_changer->deleteIncidences(toDelete);
86 Q_ASSERT(m_currentChangeId > 0);
87 }
88
89 m_changer->endAtomicOperation();
90
91 m_changer->setShowDialogsOnError(oldShowdialogs);
92 m_changer->setGroupwareCommunication(oldGroupware);
93}
94
95bool TodoPurgerPrivate::treeIsDeletable(const KCalendarCore::Todo::Ptr &todo)
96{
97 Q_ASSERT(todo);
98
99 if (!todo->isCompleted() || todo->isReadOnly()) {
100 return false;
101 }
102
103 const KCalendarCore::Incidence::List children = m_calendar->childIncidences(todo->uid());
104 if (children.isEmpty()) {
105 return true;
106 }
107
108 for (const KCalendarCore::Incidence::Ptr &child : children) {
110
111 if (!childTodo) {
112 return false; // This never happens
113 }
114
115 if (!treeIsDeletable(childTodo)) {
116 return false;
117 }
118 }
119
120 return true;
121}
122
123TodoPurger::TodoPurger(QObject *parent)
124 : QObject(parent)
125 , d(new TodoPurgerPrivate(this))
126{
127}
128
129TodoPurger::~TodoPurger() = default;
130
131void TodoPurger::setIncidenceChager(IncidenceChanger *changer)
132{
133 d->m_changer = changer;
134 d->m_currentChangeId = -1;
135 if (changer) {
136 connect(changer, &IncidenceChanger::deleteFinished, d.get(), &TodoPurgerPrivate::onItemsDeleted);
137 }
138}
139
141{
142 d->m_calendar = calendar;
143 d->m_calendarOwnership = calendar.isNull();
144}
145
147{
148 d->m_lastError.clear();
149
150 if (d->m_calendar) {
151 d->deleteTodos();
152 } else {
153 d->m_calendar = FetchJobCalendar::Ptr(new FetchJobCalendar(this));
154 // clang-format off
155 connect(d->m_calendar.data(), SIGNAL(loadFinished(bool,QString)), d.get(), SLOT(onCalendarLoaded(bool,QString)));
156 // clang-format on
157 }
158}
159
161{
162 return d->m_lastError;
163}
164
165#include "moc_todopurger_p.cpp"
166
167#include "moc_todopurger.cpp"
A KCalendarCore::Calendar that uses a one time IncidenceFetchJob to populate itself.
Class to delete completed to-dos.
Definition todopurger.h:28
QString lastError() const
Use this after receiving the an unsuccessful todosPurged() signal to get a i18n error message.
void setIncidenceChager(IncidenceChanger *changer)
Sets an IncidenceChanger.
void setCalendar(const CalendarBase::Ptr &calendar)
Sets the calendar to be used for retrieving the to-do hierarchy.
void purgeCompletedTodos()
Deletes completed to-dos.
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Todo::Ptr todo(const Akonadi::Item &item)
Returns the todo from an Akonadi item, or a null pointer if the item has no such payload.
FreeBusyManager::Singleton.
void append(QList< T > &&value)
qsizetype count() const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QSharedPointer< X > dynamicCast() const const
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.