Akonadi Calendar

todopurger.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Sérgio Martins <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "todopurger.h"
8 #include "fetchjobcalendar.h"
9 #include "todopurger_p.h"
10 #include "utils_p.h"
11 
12 #include <kcalendarcore/todo.h>
13 
14 #include <KLocalizedString>
15 using namespace Akonadi;
16 
17 TodoPurger::Private::Private(TodoPurger *q)
18  : q(q)
19 {
20 }
21 
22 void TodoPurger::Private::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 
35 void TodoPurger::Private::onItemsDeleted(int changeId, const QVector<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 
48 void TodoPurger::Private::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) {
66  KCalendarCore::Todo::Ptr todo = CalendarUtils::incidence(item).dynamicCast<KCalendarCore::Todo>();
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 
95 bool TodoPurger::Private::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 childs = m_calendar->childIncidences(todo->uid());
104  if (childs.isEmpty()) {
105  return true;
106  }
107 
108  for (const KCalendarCore::Incidence::Ptr &child : childs) {
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 
123 TodoPurger::TodoPurger(QObject *parent)
124  : QObject(parent)
125  , d(new Private(this))
126 {
127 }
128 
129 TodoPurger::~TodoPurger()
130 {
131  delete d;
132 }
133 
134 void TodoPurger::setIncidenceChager(IncidenceChanger *changer)
135 {
136  d->m_changer = changer;
137  d->m_currentChangeId = -1;
138  if (changer) {
139  connect(changer, &IncidenceChanger::deleteFinished, d, &Private::onItemsDeleted);
140  }
141 }
142 
144 {
145  d->m_calendar = calendar;
146  d->m_calendarOwnership = calendar.isNull();
147 }
148 
150 {
151  d->m_lastError.clear();
152 
153  if (d->m_calendar) {
154  d->deleteTodos();
155  } else {
156  d->m_calendar = FetchJobCalendar::Ptr(new FetchJobCalendar(this));
157  connect(d->m_calendar.data(), SIGNAL(loadFinished(bool, QString)), d, SLOT(onCalendarLoaded(bool, QString)));
158  }
159 }
160 
162 {
163  return d->m_lastError;
164 }
void append(const T &value)
void purgeCompletedTodos()
Deletes completed to-dos.
Definition: todopurger.cpp:149
Class to delete completed to-dos.
Definition: todopurger.h:24
void clear()
QString lastError() const
Use this after receiving the an unsuccessful todosPurged() signal to get a i18n error message...
Definition: todopurger.cpp:161
QString i18n(const char *text, const TYPE &arg...)
QSharedPointer< X > dynamicCast() const const
void setCalendar(const CalendarBase::Ptr &calendar)
Sets the calendar to be used for retrieving the to-do hierarchy.
Definition: todopurger.cpp:143
bool isEmpty() const const
FreeBusyManager::Singleton.
int count(const T &value) const const
A KCalendarCore::Calendar that uses a one time IncidenceFetchJob to populate itself.
bool isNull() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void setIncidenceChager(IncidenceChanger *changer)
Sets an IncidenceChanger.
Definition: todopurger.cpp:134
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jun 19 2021 23:12:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.