~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
    Copyright (c) 2011 Christian Mollekopf <chrigi_1@fastmail.fm>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "trashjob.h"

#include "collection.h"
#include "entitydeletedattribute.h"
#include "item.h"
#include "job_p.h"
#include "trashsettings.h"

#include <KLocale>

#include <akonadi/itemdeletejob.h>
#include <akonadi/collectiondeletejob.h>
#include <akonadi/itemmovejob.h>
#include <akonadi/collectionmovejob.h>
#include <akonadi/itemmodifyjob.h>
#include <akonadi/collectionmodifyjob.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/collectionfetchscope.h>
#include <akonadi/itemfetchjob.h>
#include <akonadi/collectionfetchjob.h>

#include <QHash>

using namespace Akonadi;

class TrashJob::TrashJobPrivate : public JobPrivate
{
  public:
    TrashJobPrivate( TrashJob *parent )
        : JobPrivate( parent ),
        mKeepTrashInCollection( false ),
        mSetRestoreCollection( false ),
        mDeleteIfInTrash( false ) {
    }
//4.
    void selectResult( KJob *job );
//3.
    //Helper functions to recursivly set the attribute on deleted collections
    void setAttribute( const Akonadi::Collection::List & );
    void setAttribute( const Akonadi::Item::List & );
    //Set attributes after ensuring that move job was successful
    void setAttribute( KJob *job );

//2.
    //called after parent of the trashed item was fetched (needed to see in which resource the item is in)
    void parentCollectionReceived( const Akonadi::Collection::List & );


//1.
    //called after initial fetch of trashed items
    void itemsReceived( const Akonadi::Item::List & );
    //called after initial fetch of trashed collection
    void collectionsReceived( const Akonadi::Collection::List & );


    Q_DECLARE_PUBLIC( TrashJob )

    Item::List mItems;
    Collection mCollection;
    Collection mRestoreCollection;
    Collection mTrashCollection;
    bool mKeepTrashInCollection;
    bool mSetRestoreCollection; //only set restore collection when moved to trash collection (not in place)
    bool mDeleteIfInTrash;
    QHash<Collection, Item::List> mCollectionItems; //list of trashed items sorted according to parent collection
    QHash<Entity::Id, Collection> mParentCollections; //fetched parent collcetion of items (containing the resource name)

};

void TrashJob::TrashJobPrivate::selectResult( KJob *job )
{
  Q_Q( TrashJob );
  if ( job->error() ) {
    kWarning() << job->objectName();
    kWarning() << job->errorString();
    return; // KCompositeJob takes care of errors
  }

  if ( !q->hasSubjobs() || ( q->subjobs().contains( static_cast<KJob*>( q->sender() ) ) && q->subjobs().size() == 1 ) ) {
    q->emitResult();
  }
}

void TrashJob::TrashJobPrivate::setAttribute( const Akonadi::Collection::List &list )
{
  Q_Q( TrashJob );
  QListIterator<Collection> i( list );
  while ( i.hasNext() ) {
    const Collection &col = i.next();
    EntityDeletedAttribute *eda = new EntityDeletedAttribute();
    if ( mSetRestoreCollection ) {
      Q_ASSERT( mRestoreCollection.isValid() );
      eda->setRestoreCollection( mRestoreCollection );
    }

    Collection modCol( col.id() ); //really only modify attribute (forget old remote ids, etc.), otherwise we have an error because of the move
    modCol.addAttribute( eda );

    CollectionModifyJob *job = new CollectionModifyJob( modCol, q );
    q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );

    ItemFetchJob *itemFetchJob = new ItemFetchJob( col, q );
    //TODO not sure if it is guaranteed that itemsReceived is always before result (otherwise the result is emitted before the attributes are set)
    q->connect( itemFetchJob, SIGNAL(itemsReceived(Akonadi::Item::List)), SLOT(setAttribute(Akonadi::Item::List)) );
    q->connect( itemFetchJob, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
  }
}

void TrashJob::TrashJobPrivate::setAttribute( const Akonadi::Item::List &list )
{
  Q_Q( TrashJob );
  Item::List items = list;
  QMutableListIterator<Item> i( items );
  while ( i.hasNext() ) {
    const Item &item = i.next();
    EntityDeletedAttribute *eda = new EntityDeletedAttribute();
    if ( mSetRestoreCollection ) {
      //When deleting a collection, we want to restore the deleted collection's items restored to the deleted collection's parent, not the items parent
      if ( mRestoreCollection.isValid() ) {
        eda->setRestoreCollection( mRestoreCollection );
      } else {
        Q_ASSERT( mParentCollections.contains( item.parentCollection().id() ) );
        eda->setRestoreCollection( mParentCollections.value( item.parentCollection().id() ) );
      }
    }

    Item modItem( item.id() ); //really only modify attribute (forget old remote ids, etc.)
    modItem.addAttribute( eda );
    ItemModifyJob *job = new ItemModifyJob( modItem, q );
    job->setIgnorePayload( true );
    q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
  }

  //For some reason it is not possible to apply this change to multiple items at once
  /*ItemModifyJob *job = new ItemModifyJob(items, q);
  q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );*/
}

void TrashJob::TrashJobPrivate::setAttribute( KJob* job )
{
  Q_Q( TrashJob );
  if ( job->error() ) {
    kWarning() << job->objectName();
    kWarning() << job->errorString();
    q->setError( Job::Unknown );
    q->setErrorText( i18n( "Move to trash collection failed, aborting trash operation" ) );
    return;
  }

  //For Items
  const QVariant var = job->property( "MovedItems" );
  if ( var.isValid() ) {
    int id = var.toInt();
    Q_ASSERT( id >= 0 );
    setAttribute( mCollectionItems.value( Collection( id ) ) );
    return;
  }

  //For a collection
  Q_ASSERT( mCollection.isValid() );
  setAttribute( Collection::List() << mCollection );
  //Set the attribute on all subcollections and items
  CollectionFetchJob *colFetchJob = new CollectionFetchJob( mCollection, CollectionFetchJob::Recursive, q );
  q->connect( colFetchJob, SIGNAL(collectionsReceived(Akonadi::Collection::List)), SLOT(setAttribute(Akonadi::Collection::List)) );
  q->connect( colFetchJob, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
}

void TrashJob::TrashJobPrivate::parentCollectionReceived( const Akonadi::Collection::List &collections )
{
  Q_Q( TrashJob );
  Q_ASSERT( collections.size() == 1 );
  const Collection &parentCollection = collections.first();

  //store attribute
  Q_ASSERT( !parentCollection.resource().isEmpty() );
  Collection trashCollection = mTrashCollection;
  if ( !mTrashCollection.isValid() ) {
    trashCollection = TrashSettings::getTrashCollection( parentCollection.resource() );
  }
  if ( !mKeepTrashInCollection && trashCollection.isValid() ) { //Only set the restore collection if the item is moved to trash
    mSetRestoreCollection = true;
  }

  mParentCollections.insert( parentCollection.id(), parentCollection );

  if ( trashCollection.isValid() ) {  //Move the items to the correct collection if available
    ItemMoveJob *job = new ItemMoveJob( mCollectionItems.value( parentCollection ), trashCollection, q );
    job->setProperty( "MovedItems", parentCollection.id() );
    q->connect( job, SIGNAL(result(KJob*)), SLOT(setAttribute(KJob*)) ); //Wait until the move finished to set the attirbute
    q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
  } else {
    setAttribute( mCollectionItems.value( parentCollection ) );
  }
}

void TrashJob::TrashJobPrivate::itemsReceived( const Akonadi::Item::List &items )
{
  Q_Q( TrashJob );
  if ( items.isEmpty() ) {
    q->setError( Job::Unknown );
    q->setErrorText( i18n( "Invalid items passed" ) );
    q->emitResult();
    return;
  }

  Item::List toDelete;

  QListIterator<Item> i( items );
  while ( i.hasNext() ) {
    const Item &item = i.next();
    if ( item.hasAttribute<EntityDeletedAttribute>() ) {
      toDelete.append( item );
      continue;
    }
    Q_ASSERT( item.parentCollection().isValid() );
    mCollectionItems[item.parentCollection()].append( item ); //Sort by parent col ( = restore collection)
  }

  foreach( const Collection &col, mCollectionItems.keys() ) { //krazy:exclude=foreach
    CollectionFetchJob *job = new CollectionFetchJob( col, Akonadi::CollectionFetchJob::Base, q );
    q->connect( job, SIGNAL(collectionsReceived(Akonadi::Collection::List)),
                SLOT(parentCollectionReceived(Akonadi::Collection::List)) );
  }

  if ( mDeleteIfInTrash && !toDelete.isEmpty() ) {
    ItemDeleteJob *job = new ItemDeleteJob( toDelete, q );
    q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
  } else if ( mCollectionItems.isEmpty() ) { //No job started, so we abort the job
    kWarning() << "Nothing to do";
    q->emitResult();
  }

}

void TrashJob::TrashJobPrivate::collectionsReceived( const Akonadi::Collection::List &collections )
{
  Q_Q( TrashJob );
  if ( collections.isEmpty() ) {
    q->setError( Job::Unknown );
    q->setErrorText( i18n( "Invalid collection passed" ) );
    q->emitResult();
    return;
  }
  Q_ASSERT( collections.size() == 1 );
  mCollection = collections.first();

  if ( mCollection.hasAttribute<EntityDeletedAttribute>() ) {//marked as deleted
    if ( mDeleteIfInTrash ) {
      CollectionDeleteJob *job = new CollectionDeleteJob( mCollection, q );
      q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
    } else {
      kWarning() << "Nothing to do";
      q->emitResult();
    }
    return;
  }

  Collection trashCollection = mTrashCollection;
  if ( !mTrashCollection.isValid() ) {
    trashCollection = TrashSettings::getTrashCollection( mCollection.resource() );
  }
  if ( !mKeepTrashInCollection && trashCollection.isValid() ) { //only set the restore collection if the item is moved to trash
    mSetRestoreCollection = true;
    Q_ASSERT( mCollection.parentCollection().isValid() );
    mRestoreCollection = mCollection.parentCollection();
    mRestoreCollection.setResource( mCollection.resource() ); //The parent collection doesn't contain the resource, so we have to set it manually
  }

  if ( trashCollection.isValid() ) {
    CollectionMoveJob *job = new CollectionMoveJob( mCollection, trashCollection, q );
    q->connect( job, SIGNAL(result(KJob*)), SLOT(setAttribute(KJob*)) );
    q->connect( job, SIGNAL(result(KJob*)), SLOT(selectResult(KJob*)) );
  } else {
    setAttribute( Collection::List() << mCollection );
  }

}




TrashJob::TrashJob( const Item & item, QObject * parent )
    : Job( new TrashJobPrivate( this ), parent )
{
  Q_D( TrashJob );
  d->mItems << item;
}

TrashJob::TrashJob( const Item::List& items, QObject* parent )
    : Job( new TrashJobPrivate( this ), parent )
{
  Q_D( TrashJob );
  d->mItems = items;
}

TrashJob::TrashJob( const Collection& collection, QObject* parent )
    : Job( new TrashJobPrivate( this ), parent )
{
  Q_D( TrashJob );
  d->mCollection = collection;
}

TrashJob::~TrashJob()
{
}

Item::List TrashJob::items() const
{
  Q_D( const TrashJob );
  return d->mItems;
}

void TrashJob::setTrashCollection( const Akonadi::Collection &collection )
{
  Q_D( TrashJob );
  d->mTrashCollection = collection;
}

void TrashJob::keepTrashInCollection( bool enable )
{
  Q_D( TrashJob );
  d->mKeepTrashInCollection = enable;
}

void TrashJob::deleteIfInTrash( bool enable )
{
  Q_D( TrashJob );
  d->mDeleteIfInTrash = enable;
}

void TrashJob::doStart()
{
  Q_D( TrashJob );

  //Fetch items first to ensure that the EntityDeletedAttribute is available
  if ( !d->mItems.isEmpty() ) {
    ItemFetchJob *job = new ItemFetchJob( d->mItems, this );
    job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); //so we have access to the resource
    //job->fetchScope().setCacheOnly(true);
    job->fetchScope().fetchAttribute<EntityDeletedAttribute>( true );
    connect( job, SIGNAL(itemsReceived(Akonadi::Item::List)), this, SLOT(itemsReceived(Akonadi::Item::List)) );

  } else if ( d->mCollection.isValid() ) {
    CollectionFetchJob *job = new CollectionFetchJob( d->mCollection, CollectionFetchJob::Base, this );
    job->fetchScope().setAncestorRetrieval( Akonadi::CollectionFetchScope::Parent );
    connect( job, SIGNAL(collectionsReceived(Akonadi::Collection::List)), this, SLOT(collectionsReceived(Akonadi::Collection::List)) );

  } else {
    kWarning() << "No valid collection or empty itemlist";
    setError( Job::Unknown );
    setErrorText( i18n( "No valid collection or empty itemlist" ) );
    emitResult();
  }
}

#include "trashjob.moc"