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;
45 QVector<Akonadi::Item::Id> children;
48 explicit MultiChange(
const Akonadi::Item &parent = Akonadi::Item() )
54 return parent.isValid() && !children.isEmpty();
58 namespace CalendarSupport {
60 class CalendarUtilsPrivate
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 );
70 bool purgeCompletedSubTodos(
const KCalCore::Todo::Ptr &
todo,
bool &allPurged );
73 Akonadi::ETMCalendar::Ptr mCalendar;
74 Akonadi::IncidenceChanger *mChanger;
75 MultiChange mMultiChange;
84 CalendarUtilsPrivate::CalendarUtilsPrivate(
const Akonadi::ETMCalendar::Ptr &calendar,
CalendarUtils *qq )
85 : mCalendar( calendar ),
86 mChanger( new Akonadi::IncidenceChanger( qq ) ),
90 Q_ASSERT( mCalendar );
93 SIGNAL(modifyFinished(
int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)),
94 SLOT(handleChangeFinish(
int,Akonadi::Item,Akonadi::IncidenceChanger::ResultCode,QString)) );
97 void CalendarUtilsPrivate::handleChangeFinish(
int,
98 const Akonadi::Item &item,
99 Akonadi::IncidenceChanger::ResultCode resultCode,
100 const QString &errorString )
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;
109 if ( !mMultiChange.inProgress() ) {
110 const Akonadi::Item parent = mMultiChange.parent;
111 const bool success = mMultiChange.success;
114 mMultiChange = MultiChange();
115 Q_ASSERT( !mMultiChange.inProgress() );
118 kDebug() <<
"MultiChange finished";
119 emit q->actionFinished( parent );
121 kDebug() <<
"MultiChange failed";
122 emit q->actionFailed( parent, QString() );
127 kDebug() <<
"Change finished";
128 emit q->actionFinished( item );
130 kDebug() <<
"Change failed";
131 emit q->actionFailed( Akonadi::Item(), errorString );
136 bool CalendarUtilsPrivate::purgeCompletedSubTodos(
const KCalCore::Todo::Ptr &
todo,
bool &allPurged )
142 bool deleteThisTodo =
true;
143 Akonadi::Item::List subTodos = mCalendar->childItems( todo->uid() );
144 foreach (
const Akonadi::Item &item, subTodos ) {
146 deleteThisTodo &= purgeCompletedSubTodos( item.payload<KCalCore::Todo::Ptr>(), allPurged );
150 if ( deleteThisTodo ) {
151 if ( todo->isCompleted() ) {
152 if ( !mChanger->deleteIncidence( mCalendar->item( todo ), 0 ) ) {
156 deleteThisTodo =
false;
159 if ( todo->isCompleted() ) {
163 return deleteThisTodo;
168 CalendarUtils::CalendarUtils(
const Akonadi::ETMCalendar::Ptr &calendar,
QObject *parent )
170 d_ptr( new CalendarUtilsPrivate( calendar, this ) )
172 Q_ASSERT( calendar );
189 Q_ASSERT( item.isValid() );
191 if ( d->mMultiChange.inProgress() && !d->mMultiChange.children.contains( item.id() ) ) {
196 if ( !inc || inc->relatedTo().isEmpty() ) {
200 Incidence::Ptr oldInc( inc->clone() );
201 inc->setRelatedTo( QString() );
202 return d->mChanger->modifyIncidence( item, oldInc );
208 Q_ASSERT( item.isValid() );
211 if ( d->mMultiChange.inProgress() ) {
216 const Akonadi::Item::List subIncs = d->mCalendar->childItems( item.id() );
218 if ( !inc || subIncs.isEmpty() ) {
222 d->mMultiChange = MultiChange( item );
223 bool allStarted =
true;
224 foreach (
const Akonadi::Item &subInc, subIncs ) {
225 d->mMultiChange.children.append( subInc.id() );
229 Q_ASSERT( allStarted );
241 bool allDeleted =
true;
244 KCalCore::Todo::List rootTodos;
246 foreach (
const KCalCore::Todo::Ptr &todo, todos ) {
247 if ( todo && todo->relatedTo().isEmpty() ) {
248 rootTodos.append( todo );
253 foreach (
const KCalCore::Todo::Ptr &todo, rootTodos ) {
254 d->purgeCompletedSubTodos( todo, allDeleted );
259 KMessageBox::information(
262 "Unable to purge to-dos with uncompleted children." ),
263 i18nc(
"@title:window",
"Delete To-do" ),
264 QLatin1String(
"UncompletedChildrenPurgeTodos") );
268 #include "calendarutils.moc"
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)