20 #include "todopurger.h"
21 #include "todopurger_p.h"
22 #include "fetchjobcalendar.h"
25 #include <kcalcore/todo.h>
28 using namespace Akonadi;
32 , m_currentChangeId(-1)
34 , m_calendarOwnership(true)
39 void TodoPurger::Private::onCalendarLoaded(
bool success,
const QString &message)
44 m_lastError = message;
45 if (m_calendarOwnership)
47 emit q->todosPurged(
false, 0, 0);
51 void TodoPurger::Private::onItemsDeleted(
int changeId,
const QVector<Entity::Id> &deletedItems,
52 IncidenceChanger::ResultCode result,
const QString &message)
54 if (changeId != m_currentChangeId)
57 m_lastError = message;
58 if (m_calendarOwnership)
60 emit q->todosPurged(result == IncidenceChanger::ResultCodeSuccess, deletedItems.count(), m_ignoredItems);
63 void TodoPurger::Private::deleteTodos()
66 q->setIncidenceChager(
new IncidenceChanger(
this));
67 m_changer->setShowDialogsOnError(
false);
68 m_changer->setHistoryEnabled(
false);
71 const bool oldShowdialogs = m_changer->showDialogsOnError();
72 const bool oldGroupware = m_changer->groupwareCommunication();
73 m_changer->setShowDialogsOnError(
false);
74 m_changer->setGroupwareCommunication(
false);
76 m_changer->startAtomicOperation(i18n(
"Purging completed to-dos"));
77 Akonadi::Item::List items = m_calendar->items();
78 Akonadi::Item::List toDelete;
80 foreach(
const Akonadi::Item &item, items) {
81 KCalCore::Todo::Ptr todo = CalendarUtils::incidence(item).dynamicCast<KCalCore::Todo>();
83 if (!todo || !todo->isCompleted()) {
87 if (treeIsDeletable(todo)) {
88 toDelete.append(item);
94 if (toDelete.isEmpty()) {
95 if (m_calendarOwnership)
97 emit q->todosPurged(
true, 0, m_ignoredItems);
99 m_currentChangeId = m_changer->deleteIncidences(toDelete);
100 Q_ASSERT(m_currentChangeId > 0);
103 m_changer->endAtomicOperation();
105 m_changer->setShowDialogsOnError(oldShowdialogs);
106 m_changer->setGroupwareCommunication(oldGroupware);
109 bool TodoPurger::Private::treeIsDeletable(
const KCalCore::Todo::Ptr &todo)
113 if (!todo->isCompleted() || todo->isReadOnly())
116 KCalCore::Incidence::List childs = m_calendar->childIncidences(todo->uid());
117 if (childs.isEmpty())
120 foreach(
const KCalCore::Incidence::Ptr &child, childs) {
121 KCalCore::Todo::Ptr childTodo = child.dynamicCast<KCalCore::Todo>();
126 if (!treeIsDeletable(childTodo))
134 TodoPurger::TodoPurger(QObject *parent)
136 , d(new Private(this))
140 TodoPurger::~TodoPurger()
147 d->m_changer = changer;
148 d->m_currentChangeId = -1;
150 connect(changer, SIGNAL(deleteFinished(
int,QVector<Akonadi::Item::Id>,Akonadi::IncidenceChanger::ResultCode,QString)),
151 d, SLOT(onItemsDeleted(
int,QVector<Akonadi::Item::Id>,Akonadi::IncidenceChanger::ResultCode,QString)));
156 d->m_calendar = calendar;
157 d->m_calendarOwnership = calendar.isNull();
162 d->m_lastError.clear();
168 connect(d->m_calendar.data(), SIGNAL(loadFinished(
bool,QString)), d, SLOT(onCalendarLoaded(
bool,QString)));
174 return d->m_lastError;
void purgeCompletedTodos()
Deletes completed to-dos.
Class to delete completed to-dos.
QString lastError() const
Use this after receiving the an unsuccessful todosPurged() signal to get a i18n error message...
void setCalendar(const CalendarBase::Ptr &calendar)
Sets the calendar to be used for retrieving the to-do hierarchy.
A KCalCore::Calendar that uses a one time IncidenceFetchJob to populate itself.
void setIncidenceChager(IncidenceChanger *changer)
Sets an IncidenceChanger.