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

calendarsupport

  • sources
  • kde-4.14
  • 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() const
54  {
55  return parent.isValid() && !children.isEmpty();
56  }
57 };
58 
59 namespace CalendarSupport {
60 
61 class CalendarUtilsPrivate
62 {
63  public:
65  CalendarUtilsPrivate( const Akonadi::ETMCalendar::Ptr &calendar, CalendarUtils *qq );
66  void handleChangeFinish( int changeId,
67  const Akonadi::Item &item,
68  Akonadi::IncidenceChanger::ResultCode resultCode,
69  const QString &errorString );
70 
71  bool purgeCompletedSubTodos( const KCalCore::Todo::Ptr &todo, bool &allPurged );
72 
74  Akonadi::ETMCalendar::Ptr mCalendar;
75  Akonadi::IncidenceChanger *mChanger;
76  MultiChange mMultiChange;
77 
78  private:
79  CalendarUtils *const q_ptr;
80  Q_DECLARE_PUBLIC( CalendarUtils )
81 };
82 
83 }
84 
85 CalendarUtilsPrivate::CalendarUtilsPrivate( const Akonadi::ETMCalendar::Ptr &calendar, CalendarUtils *qq )
86  : mCalendar( calendar ),
87  mChanger( new Akonadi::IncidenceChanger( qq ) ),
88  q_ptr( qq )
89 {
90  Q_Q( CalendarUtils );
91  Q_ASSERT( mCalendar );
92 
93  q->connect( mChanger,
94  SIGNAL(modifyFinished(int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)),
95  SLOT(handleChangeFinish(int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)) );
96 }
97 
98 void CalendarUtilsPrivate::handleChangeFinish( int,
99  const Akonadi::Item &item,
100  Akonadi::IncidenceChanger::ResultCode resultCode,
101  const QString &errorString )
102 {
103  Q_Q( CalendarUtils );
104  const bool success = resultCode == Akonadi::IncidenceChanger::ResultCodeSuccess;
105  if ( mMultiChange.inProgress() ) {
106  mMultiChange.children.remove( mMultiChange.children.indexOf( item.id() ) );
107  mMultiChange.success = mMultiChange.success && success;
108 
109  // Are we still in progress?
110  if ( !mMultiChange.inProgress() ) {
111  const Akonadi::Item parent = mMultiChange.parent;
112  const bool success = mMultiChange.success;
113 
114  // Reset the multi change.
115  mMultiChange = MultiChange();
116  Q_ASSERT( !mMultiChange.inProgress() );
117 
118  if ( success ) {
119  kDebug() << "MultiChange finished";
120  emit q->actionFinished( parent );
121  } else {
122  kDebug() << "MultiChange failed";
123  emit q->actionFailed( parent, QString() );
124  }
125  }
126  } else {
127  if ( success ) {
128  kDebug() << "Change finished";
129  emit q->actionFinished( item );
130  } else {
131  kDebug() << "Change failed";
132  emit q->actionFailed( Akonadi::Item(), errorString );
133  }
134  }
135 }
136 
137 bool CalendarUtilsPrivate::purgeCompletedSubTodos( const KCalCore::Todo::Ptr &todo, bool &allPurged )
138 {
139  if ( !todo ) {
140  return true;
141  }
142 
143  bool deleteThisTodo = true;
144  Akonadi::Item::List subTodos = mCalendar->childItems( todo->uid() );
145  foreach ( const Akonadi::Item &item, subTodos ) {
146  if ( CalendarSupport::hasTodo( item ) ) {
147  deleteThisTodo &= purgeCompletedSubTodos( item.payload<KCalCore::Todo::Ptr>(), allPurged );
148  }
149  }
150 
151  if ( deleteThisTodo ) {
152  if ( todo->isCompleted() ) {
153  if ( !mChanger->deleteIncidence( mCalendar->item( todo ), 0 ) ) {
154  allPurged = false;
155  }
156  } else {
157  deleteThisTodo = false;
158  }
159  } else {
160  if ( todo->isCompleted() ) {
161  allPurged = false;
162  }
163  }
164  return deleteThisTodo;
165 }
166 
168 
169 CalendarUtils::CalendarUtils( const Akonadi::ETMCalendar::Ptr &calendar, QObject *parent )
170  : QObject( parent ),
171  d_ptr( new CalendarUtilsPrivate( calendar, this ) )
172 {
173  Q_ASSERT( calendar );
174 }
175 
176 CalendarUtils::~CalendarUtils()
177 {
178  delete d_ptr;
179 }
180 
181 Akonadi::ETMCalendar::Ptr CalendarUtils::calendar() const
182 {
183  Q_D( const CalendarUtils );
184  return d->mCalendar;
185 }
186 
187 bool CalendarUtils::makeIndependent( const Akonadi::Item &item )
188 {
189  Q_D( CalendarUtils );
190  Q_ASSERT( item.isValid() );
191 
192  if ( d->mMultiChange.inProgress() && !d->mMultiChange.children.contains( item.id() ) ) {
193  return false;
194  }
195 
196  const Incidence::Ptr inc = CalendarSupport::incidence( item );
197  if ( !inc || inc->relatedTo().isEmpty() ) {
198  return false;
199  }
200 
201  Incidence::Ptr oldInc( inc->clone() );
202  inc->setRelatedTo( QString() );
203  return d->mChanger->modifyIncidence( item, oldInc );
204 }
205 
206 bool CalendarUtils::makeChildrenIndependent( const Akonadi::Item &item )
207 {
208  Q_D( CalendarUtils );
209  Q_ASSERT( item.isValid() );
210 
211 
212  if ( d->mMultiChange.inProgress() ) {
213  return false;
214  }
215 
216  const Incidence::Ptr inc = CalendarSupport::incidence( item );
217  const Akonadi::Item::List subIncs = d->mCalendar->childItems( item.id() );
218 
219  if ( !inc || subIncs.isEmpty() ) {
220  return false;
221  }
222 
223  d->mMultiChange = MultiChange( item );
224  bool allStarted = true;
225  foreach ( const Akonadi::Item &subInc, subIncs ) {
226  d->mMultiChange.children.append( subInc.id() );
227  allStarted = allStarted && makeIndependent( subInc );
228  }
229 
230  Q_ASSERT( allStarted ); // OKay, maybe we should not assert here, but one or
231  // changes could have been started, so just returning
232  // false isn't suitable either.
233 
234  return true;
235 }
236 
238 
239 void CalendarUtils::purgeCompletedTodos()
240 {
241  Q_D( CalendarUtils );
242  bool allDeleted = true;
243 // startMultiModify( i18n( "Purging completed to-dos" ) );
244  KCalCore::Todo::List todos = calendar()->rawTodos();
245  KCalCore::Todo::List rootTodos;
246 
247  foreach ( const KCalCore::Todo::Ptr &todo, todos ) {
248  if ( todo && todo->relatedTo().isEmpty() ) { // top level todo //REVIEW(AKONADI_PORT)
249  rootTodos.append( todo );
250  }
251  }
252 
253  // now that we have a list of all root todos, check them and their children
254  foreach ( const KCalCore::Todo::Ptr &todo, rootTodos ) {
255  d->purgeCompletedSubTodos( todo, allDeleted );
256  }
257 
258 // endMultiModify();
259  if ( !allDeleted ) {
260  KMessageBox::information(
261  0,
262  i18nc( "@info",
263  "Unable to purge to-dos with uncompleted children." ),
264  i18nc( "@title:window", "Delete To-do" ),
265  QLatin1String("UncompletedChildrenPurgeTodos") );
266  }
267 }
268 
269 #include "moc_calendarutils.cpp"
CalendarSupport::CalendarUtils::calendar
Akonadi::ETMCalendar::Ptr calendar() const
Returns the Caledar on which this utils class is operating.
Definition: calendarutils.cpp:181
CalendarSupport::CalendarUtils::makeIndependent
bool makeIndependent(const Akonadi::Item &item)
Makes the incidence from.
Definition: calendarutils.cpp:187
CalendarSupport::CalendarUtils::purgeCompletedTodos
void purgeCompletedTodos()
Todo specific methods.
Definition: calendarutils.cpp:239
CalendarSupport::CalendarUtils::makeChildrenIndependent
bool makeChildrenIndependent(const Akonadi::Item &item)
Makes all children of the incindence from.
Definition: calendarutils.cpp:206
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:78
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:124
utils.h
calendarutils.h
QObject
QString
CalendarSupport::CalendarUtils
Some calendar/Incidence related utilitly methods.
Definition: calendarutils.h:45
QVector< Akonadi::Item::Id >
QLatin1String
CalendarSupport::hasTodo
CALENDARSUPPORT_EXPORT bool hasTodo(const Akonadi::Item &item)
returns whether an Akonadi item contains a todo
Definition: utils.cpp:160
CalendarSupport::todos
CALENDARSUPPORT_EXPORT KCalCore::Todo::List todos(const QMimeData *mimeData, const KDateTime::Spec &timeSpec)
Definition: utils.cpp:360
CalendarSupport::CalendarUtils::~CalendarUtils
~CalendarUtils()
Definition: calendarutils.cpp:176
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:15 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
  • pimprint

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