• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
  • calendar
todopurger.cpp
1 /*
2  Copyright (C) 2013 Sérgio Martins <iamsergio@gmail.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 
20 #include "todopurger.h"
21 #include "todopurger_p.h"
22 #include "fetchjobcalendar.h"
23 #include "utils_p.h"
24 
25 #include <kcalcore/todo.h>
26 
27 #include <KLocale>
28 using namespace Akonadi;
29 
30 TodoPurger::Private::Private(TodoPurger *q)
31  : m_changer(0)
32  , m_currentChangeId(-1)
33  , m_ignoredItems(0)
34  , m_calendarOwnership(true)
35  , q(q)
36 {
37 }
38 
39 void TodoPurger::Private::onCalendarLoaded(bool success, const QString &message)
40 {
41  if (success) {
42  deleteTodos();
43  } else {
44  m_lastError = message;
45  if (m_calendarOwnership)
46  m_calendar.clear();
47  emit q->todosPurged(false, 0, 0);
48  }
49 }
50 
51 void TodoPurger::Private::onItemsDeleted(int changeId, const QVector<Entity::Id> &deletedItems,
52  IncidenceChanger::ResultCode result, const QString &message)
53 {
54  if (changeId != m_currentChangeId)
55  return; // Not ours.
56 
57  m_lastError = message;
58  if (m_calendarOwnership)
59  m_calendar.clear();
60  emit q->todosPurged(result == IncidenceChanger::ResultCodeSuccess, deletedItems.count(), m_ignoredItems);
61 }
62 
63 void TodoPurger::Private::deleteTodos()
64 {
65  if (!m_changer) {
66  q->setIncidenceChager(new IncidenceChanger(this));
67  m_changer->setShowDialogsOnError(false);
68  m_changer->setHistoryEnabled(false);
69  }
70 
71  const bool oldShowdialogs = m_changer->showDialogsOnError();
72  const bool oldGroupware = m_changer->groupwareCommunication();
73  m_changer->setShowDialogsOnError(false);
74  m_changer->setGroupwareCommunication(false);
75 
76  m_changer->startAtomicOperation(i18n("Purging completed to-dos"));
77  Akonadi::Item::List items = m_calendar->items();
78  Akonadi::Item::List toDelete;
79  m_ignoredItems = 0;
80  foreach(const Akonadi::Item &item, items) {
81  KCalCore::Todo::Ptr todo = CalendarUtils::incidence(item).dynamicCast<KCalCore::Todo>();
82 
83  if (!todo || !todo->isCompleted()) {
84  continue;
85  }
86 
87  if (treeIsDeletable(todo)) {
88  toDelete.append(item);
89  } else {
90  m_ignoredItems++;
91  }
92  }
93 
94  if (toDelete.isEmpty()) {
95  if (m_calendarOwnership)
96  m_calendar.clear();
97  emit q->todosPurged(true, 0, m_ignoredItems);
98  } else {
99  m_currentChangeId = m_changer->deleteIncidences(toDelete);
100  Q_ASSERT(m_currentChangeId > 0);
101  }
102 
103  m_changer->endAtomicOperation();
104 
105  m_changer->setShowDialogsOnError(oldShowdialogs);
106  m_changer->setGroupwareCommunication(oldGroupware);
107 }
108 
109 bool TodoPurger::Private::treeIsDeletable(const KCalCore::Todo::Ptr &todo)
110 {
111  Q_ASSERT(todo);
112 
113  if (!todo->isCompleted() || todo->isReadOnly())
114  return false;
115 
116  KCalCore::Incidence::List childs = m_calendar->childIncidences(todo->uid());
117  if (childs.isEmpty())
118  return true;
119 
120  foreach(const KCalCore::Incidence::Ptr &child, childs) {
121  KCalCore::Todo::Ptr childTodo = child.dynamicCast<KCalCore::Todo>();
122 
123  if (!childTodo)
124  return false; // This never happens
125 
126  if (!treeIsDeletable(childTodo))
127  return false;
128  }
129 
130  return true;
131 }
132 
133 
134 TodoPurger::TodoPurger(QObject *parent)
135  : QObject(parent)
136  , d(new Private(this))
137 {
138 }
139 
140 TodoPurger::~TodoPurger()
141 {
142  delete d;
143 }
144 
145 void TodoPurger::setIncidenceChager(IncidenceChanger *changer)
146 {
147  d->m_changer = changer;
148  d->m_currentChangeId = -1;
149  if (changer)
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)));
152 }
153 
154 void TodoPurger::setCalendar(const CalendarBase::Ptr &calendar)
155 {
156  d->m_calendar = calendar;
157  d->m_calendarOwnership = calendar.isNull();
158 }
159 
160 void TodoPurger::purgeCompletedTodos()
161 {
162  d->m_lastError.clear();
163 
164  if (d->m_calendar) {
165  d->deleteTodos();
166  } else {
167  d->m_calendar = FetchJobCalendar::Ptr(new FetchJobCalendar(this));
168  connect(d->m_calendar.data(), SIGNAL(loadFinished(bool,QString)), d, SLOT(onCalendarLoaded(bool,QString)));
169  }
170 }
171 
172 QString TodoPurger::lastError() const
173 {
174  return d->m_lastError;
175 }
Akonadi::TodoPurger::purgeCompletedTodos
void purgeCompletedTodos()
Deletes completed to-dos.
Definition: todopurger.cpp:160
Akonadi::TodoPurger
Class to delete completed to-dos.
Definition: todopurger.h:38
Akonadi::TodoPurger::lastError
QString lastError() const
Use this after receiving the an unsuccessful todosPurged() signal to get a i18n error message...
Definition: todopurger.cpp:172
Akonadi::TodoPurger::setCalendar
void setCalendar(const CalendarBase::Ptr &calendar)
Sets the calendar to be used for retrieving the to-do hierarchy.
Definition: todopurger.cpp:154
Akonadi::FetchJobCalendar
A KCalCore::Calendar that uses a one time IncidenceFetchJob to populate itself.
Definition: fetchjobcalendar.h:45
Akonadi::TodoPurger::setIncidenceChager
void setIncidenceChager(IncidenceChanger *changer)
Sets an IncidenceChanger.
Definition: todopurger.cpp:145
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal