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

calendarsupport

  • sources
  • kde-4.12
  • kdepim
  • calendarsupport
calendarutils.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Bertjan Broeksema <broeksema@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 
18  As a special exception, permission is given to link this program
19  with any edition of Qt, and distribute the resulting executable,
20  without including the source code for Qt in the source distribution.
21 */
22 
23 // NOTE: The code of the following methods is taken from
24 // kdepim/korganizer/calendarview.cpp:
25 // - makeIndependent (was incidence_unsub)
26 // - makeChildrenIndependent
27 
28 #include "calendarutils.h"
29 #include "utils.h"
30 
31 #include <KCalCore/Incidence>
32 #include <Akonadi/Calendar/ETMCalendar>
33 #include <Akonadi/Calendar/IncidenceChanger>
34 
35 #include <KLocalizedString>
36 #include <KMessageBox>
37 
38 using namespace CalendarSupport;
39 using namespace KCalCore;
40 
42 
43 struct MultiChange {
44  Akonadi::Item parent;
45  QVector<Akonadi::Item::Id> children;
46  bool success;
47 
48  explicit MultiChange( const Akonadi::Item &parent = Akonadi::Item() )
49  : parent( parent ),
50  success( true )
51  {}
52 
53  bool inProgress() {
54  return parent.isValid() && !children.isEmpty();
55  }
56 };
57 
58 namespace CalendarSupport {
59 
60 class CalendarUtilsPrivate
61 {
62  public:
64  CalendarUtilsPrivate( const Akonadi::ETMCalendar::Ptr &calendar, CalendarUtils *qq );
65  void handleChangeFinish( int changeId,
66  const Akonadi::Item &item,
67  Akonadi::IncidenceChanger::ResultCode resultCode,
68  const QString &errorString );
69 
70  bool purgeCompletedSubTodos( const KCalCore::Todo::Ptr &todo, bool &allPurged );
71 
73  Akonadi::ETMCalendar::Ptr mCalendar;
74  Akonadi::IncidenceChanger *mChanger;
75  MultiChange mMultiChange;
76 
77  private:
78  CalendarUtils *const q_ptr;
79  Q_DECLARE_PUBLIC( CalendarUtils )
80 };
81 
82 }
83 
84 CalendarUtilsPrivate::CalendarUtilsPrivate( const Akonadi::ETMCalendar::Ptr &calendar, CalendarUtils *qq )
85  : mCalendar( calendar ),
86  mChanger( new Akonadi::IncidenceChanger( qq ) ),
87  q_ptr( qq )
88 {
89  Q_Q( CalendarUtils );
90  Q_ASSERT( mCalendar );
91 
92  q->connect( mChanger,
93  SIGNAL(modifyFinished(int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)),
94  SLOT(handleChangeFinish(int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)) );
95 }
96 
97 void CalendarUtilsPrivate::handleChangeFinish( int,
98  const Akonadi::Item &item,
99  Akonadi::IncidenceChanger::ResultCode resultCode,
100  const QString &errorString )
101 {
102  Q_Q( CalendarUtils );
103  const bool success = resultCode == Akonadi::IncidenceChanger::ResultCodeSuccess;
104  if ( mMultiChange.inProgress() ) {
105  mMultiChange.children.remove( mMultiChange.children.indexOf( item.id() ) );
106  mMultiChange.success = mMultiChange.success && success;
107 
108  // Are we still in progress?
109  if ( !mMultiChange.inProgress() ) {
110  const Akonadi::Item parent = mMultiChange.parent;
111  const bool success = mMultiChange.success;
112 
113  // Reset the multi change.
114  mMultiChange = MultiChange();
115  Q_ASSERT( !mMultiChange.inProgress() );
116 
117  if ( success ) {
118  kDebug() << "MultiChange finished";
119  emit q->actionFinished( parent );
120  } else {
121  kDebug() << "MultiChange failed";
122  emit q->actionFailed( parent, QString() );
123  }
124  }
125  } else {
126  if ( success ) {
127  kDebug() << "Change finished";
128  emit q->actionFinished( item );
129  } else {
130  kDebug() << "Change failed";
131  emit q->actionFailed( Akonadi::Item(), errorString );
132  }
133  }
134 }
135 
136 bool CalendarUtilsPrivate::purgeCompletedSubTodos( const KCalCore::Todo::Ptr &todo, bool &allPurged )
137 {
138  if ( !todo ) {
139  return true;
140  }
141 
142  bool deleteThisTodo = true;
143  Akonadi::Item::List subTodos = mCalendar->childItems( todo->uid() );
144  foreach ( const Akonadi::Item &item, subTodos ) {
145  if ( CalendarSupport::hasTodo( item ) ) {
146  deleteThisTodo &= purgeCompletedSubTodos( item.payload<KCalCore::Todo::Ptr>(), allPurged );
147  }
148  }
149 
150  if ( deleteThisTodo ) {
151  if ( todo->isCompleted() ) {
152  if ( !mChanger->deleteIncidence( mCalendar->item( todo ), 0 ) ) {
153  allPurged = false;
154  }
155  } else {
156  deleteThisTodo = false;
157  }
158  } else {
159  if ( todo->isCompleted() ) {
160  allPurged = false;
161  }
162  }
163  return deleteThisTodo;
164 }
165 
167 
168 CalendarUtils::CalendarUtils( const Akonadi::ETMCalendar::Ptr &calendar, QObject *parent )
169  : QObject( parent ),
170  d_ptr( new CalendarUtilsPrivate( calendar, this ) )
171 {
172  Q_ASSERT( calendar );
173 }
174 
175 CalendarUtils::~CalendarUtils()
176 {
177  delete d_ptr;
178 }
179 
180 Akonadi::ETMCalendar::Ptr CalendarUtils::calendar() const
181 {
182  Q_D( const CalendarUtils );
183  return d->mCalendar;
184 }
185 
186 bool CalendarUtils::makeIndependent( const Akonadi::Item &item )
187 {
188  Q_D( CalendarUtils );
189  Q_ASSERT( item.isValid() );
190 
191  if ( d->mMultiChange.inProgress() && !d->mMultiChange.children.contains( item.id() ) ) {
192  return false;
193  }
194 
195  const Incidence::Ptr inc = CalendarSupport::incidence( item );
196  if ( !inc || inc->relatedTo().isEmpty() ) {
197  return false;
198  }
199 
200  Incidence::Ptr oldInc( inc->clone() );
201  inc->setRelatedTo( QString() );
202  return d->mChanger->modifyIncidence( item, oldInc );
203 }
204 
205 bool CalendarUtils::makeChildrenIndependent( const Akonadi::Item &item )
206 {
207  Q_D( CalendarUtils );
208  Q_ASSERT( item.isValid() );
209 
210 
211  if ( d->mMultiChange.inProgress() ) {
212  return false;
213  }
214 
215  const Incidence::Ptr inc = CalendarSupport::incidence( item );
216  const Akonadi::Item::List subIncs = d->mCalendar->childItems( item.id() );
217 
218  if ( !inc || subIncs.isEmpty() ) {
219  return false;
220  }
221 
222  d->mMultiChange = MultiChange( item );
223  bool allStarted = true;
224  foreach ( const Akonadi::Item &subInc, subIncs ) {
225  d->mMultiChange.children.append( subInc.id() );
226  allStarted = allStarted && makeIndependent( subInc );
227  }
228 
229  Q_ASSERT( allStarted ); // OKay, maybe we should not assert here, but one or
230  // changes could have been started, so just returning
231  // false isn't suitable either.
232 
233  return true;
234 }
235 
237 
238 void CalendarUtils::purgeCompletedTodos()
239 {
240  Q_D( CalendarUtils );
241  bool allDeleted = true;
242 // startMultiModify( i18n( "Purging completed to-dos" ) );
243  KCalCore::Todo::List todos = calendar()->rawTodos();
244  KCalCore::Todo::List rootTodos;
245 
246  foreach ( const KCalCore::Todo::Ptr &todo, todos ) {
247  if ( todo && todo->relatedTo().isEmpty() ) { // top level todo //REVIEW(AKONADI_PORT)
248  rootTodos.append( todo );
249  }
250  }
251 
252  // now that we have a list of all root todos, check them and their children
253  foreach ( const KCalCore::Todo::Ptr &todo, rootTodos ) {
254  d->purgeCompletedSubTodos( todo, allDeleted );
255  }
256 
257 // endMultiModify();
258  if ( !allDeleted ) {
259  KMessageBox::information(
260  0,
261  i18nc( "@info",
262  "Unable to purge to-dos with uncompleted children." ),
263  i18nc( "@title:window", "Delete To-do" ),
264  QLatin1String("UncompletedChildrenPurgeTodos") );
265  }
266 }
267 
268 #include "calendarutils.moc"
CalendarSupport::CalendarUtils::calendar
Akonadi::ETMCalendar::Ptr calendar() const
Returns the Caledar on which this utils class is operating.
Definition: calendarutils.cpp:180
CalendarSupport::CalendarUtils::makeIndependent
bool makeIndependent(const Akonadi::Item &item)
Makes the incidence from.
Definition: calendarutils.cpp:186
CalendarSupport::CalendarUtils::purgeCompletedTodos
void purgeCompletedTodos()
Todo specific methods.
Definition: calendarutils.cpp:238
CalendarSupport::CalendarUtils::makeChildrenIndependent
bool makeChildrenIndependent(const Akonadi::Item &item)
Makes all children of the incindence from.
Definition: calendarutils.cpp:205
QObject
CalendarSupport::incidence
CALENDARSUPPORT_EXPORT KCalCore::Incidence::Ptr incidence(const Akonadi::Item &item)
returns the incidence from an akonadi item, or a null pointer if the item has no such payload ...
Definition: utils.cpp:75
CalendarSupport::todo
CALENDARSUPPORT_EXPORT KCalCore::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 ...
Definition: utils.cpp:121
utils.h
calendarutils.h
CalendarSupport::CalendarUtils
Some calendar/Incidence related utilitly methods.
Definition: calendarutils.h:45
CalendarSupport::hasTodo
CALENDARSUPPORT_EXPORT bool hasTodo(const Akonadi::Item &item)
returns whether an Akonadi item contains a todo
Definition: utils.cpp:157
CalendarSupport::todos
CALENDARSUPPORT_EXPORT KCalCore::Todo::List todos(const QMimeData *mimeData, const KDateTime::Spec &timeSpec)
Definition: utils.cpp:357
CalendarSupport::CalendarUtils::~CalendarUtils
~CalendarUtils()
Definition: calendarutils.cpp:175
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:54:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

calendarsupport

Skip menu "calendarsupport"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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