31 #include <KCalCore/Incidence>
32 #include <Akonadi/Calendar/ETMCalendar>
33 #include <Akonadi/Calendar/IncidenceChanger>
35 #include <KLocalizedString>
36 #include <KMessageBox>
38 using namespace CalendarSupport;
39 using namespace KCalCore;
48 explicit MultiChange(
const Akonadi::Item &parent = Akonadi::Item() )
53 bool inProgress()
const
55 return parent.isValid() && !children.isEmpty();
59 namespace CalendarSupport {
61 class CalendarUtilsPrivate
65 CalendarUtilsPrivate(
const Akonadi::ETMCalendar::Ptr &calendar,
CalendarUtils *qq );
66 void handleChangeFinish(
int changeId,
67 const Akonadi::Item &item,
68 Akonadi::IncidenceChanger::ResultCode resultCode,
71 bool purgeCompletedSubTodos(
const KCalCore::Todo::Ptr &
todo,
bool &allPurged );
74 Akonadi::ETMCalendar::Ptr mCalendar;
75 Akonadi::IncidenceChanger *mChanger;
76 MultiChange mMultiChange;
85 CalendarUtilsPrivate::CalendarUtilsPrivate(
const Akonadi::ETMCalendar::Ptr &calendar,
CalendarUtils *qq )
86 : mCalendar( calendar ),
87 mChanger( new Akonadi::IncidenceChanger( qq ) ),
91 Q_ASSERT( mCalendar );
94 SIGNAL(modifyFinished(
int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,
QString)),
95 SLOT(handleChangeFinish(
int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,
QString)) );
98 void CalendarUtilsPrivate::handleChangeFinish(
int,
99 const Akonadi::Item &item,
100 Akonadi::IncidenceChanger::ResultCode resultCode,
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;
110 if ( !mMultiChange.inProgress() ) {
111 const Akonadi::Item parent = mMultiChange.parent;
112 const bool success = mMultiChange.success;
115 mMultiChange = MultiChange();
116 Q_ASSERT( !mMultiChange.inProgress() );
119 kDebug() <<
"MultiChange finished";
120 emit q->actionFinished( parent );
122 kDebug() <<
"MultiChange failed";
123 emit q->actionFailed( parent,
QString() );
128 kDebug() <<
"Change finished";
129 emit q->actionFinished( item );
131 kDebug() <<
"Change failed";
132 emit q->actionFailed( Akonadi::Item(), errorString );
137 bool CalendarUtilsPrivate::purgeCompletedSubTodos(
const KCalCore::Todo::Ptr &
todo,
bool &allPurged )
143 bool deleteThisTodo =
true;
144 Akonadi::Item::List subTodos = mCalendar->childItems( todo->uid() );
145 foreach (
const Akonadi::Item &item, subTodos ) {
147 deleteThisTodo &= purgeCompletedSubTodos( item.payload<KCalCore::Todo::Ptr>(), allPurged );
151 if ( deleteThisTodo ) {
152 if ( todo->isCompleted() ) {
153 if ( !mChanger->deleteIncidence( mCalendar->item( todo ), 0 ) ) {
157 deleteThisTodo =
false;
160 if ( todo->isCompleted() ) {
164 return deleteThisTodo;
169 CalendarUtils::CalendarUtils(
const Akonadi::ETMCalendar::Ptr &calendar,
QObject *parent )
171 d_ptr( new CalendarUtilsPrivate( calendar, this ) )
173 Q_ASSERT( calendar );
190 Q_ASSERT( item.isValid() );
192 if ( d->mMultiChange.inProgress() && !d->mMultiChange.children.contains( item.id() ) ) {
197 if ( !inc || inc->relatedTo().isEmpty() ) {
201 Incidence::Ptr oldInc( inc->clone() );
202 inc->setRelatedTo(
QString() );
203 return d->mChanger->modifyIncidence( item, oldInc );
209 Q_ASSERT( item.isValid() );
212 if ( d->mMultiChange.inProgress() ) {
217 const Akonadi::Item::List subIncs = d->mCalendar->childItems( item.id() );
219 if ( !inc || subIncs.isEmpty() ) {
223 d->mMultiChange = MultiChange( item );
224 bool allStarted =
true;
225 foreach (
const Akonadi::Item &subInc, subIncs ) {
226 d->mMultiChange.children.append( subInc.id() );
230 Q_ASSERT( allStarted );
242 bool allDeleted =
true;
245 KCalCore::Todo::List rootTodos;
247 foreach (
const KCalCore::Todo::Ptr &todo, todos ) {
248 if ( todo && todo->relatedTo().isEmpty() ) {
249 rootTodos.append( todo );
254 foreach (
const KCalCore::Todo::Ptr &todo, rootTodos ) {
255 d->purgeCompletedSubTodos( todo, allDeleted );
260 KMessageBox::information(
263 "Unable to purge to-dos with uncompleted children." ),
264 i18nc(
"@title:window",
"Delete To-do" ),
269 #include "moc_calendarutils.cpp"
Akonadi::ETMCalendar::Ptr calendar() const
Returns the Caledar on which this utils class is operating.
bool makeIndependent(const Akonadi::Item &item)
Makes the incidence from.
void purgeCompletedTodos()
Todo specific methods.
bool makeChildrenIndependent(const Akonadi::Item &item)
Makes all children of the incindence from.
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 ...
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 ...
Some calendar/Incidence related utilitly methods.
CALENDARSUPPORT_EXPORT bool hasTodo(const Akonadi::Item &item)
returns whether an Akonadi item contains a todo
CALENDARSUPPORT_EXPORT KCalCore::Todo::List todos(const QMimeData *mimeData, const KDateTime::Spec &timeSpec)