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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
pastehelper.cpp
1 /*
2  Copyright (c) 2008 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "pastehelper_p.h"
21 
22 #include "collectioncopyjob.h"
23 #include "collectionmovejob.h"
24 #include "item.h"
25 #include "itemcreatejob.h"
26 #include "itemcopyjob.h"
27 #include "itemmodifyjob.h"
28 #include "itemmovejob.h"
29 #include "linkjob.h"
30 #include "transactionsequence.h"
31 #include "session.h"
32 
33 #include <KDebug>
34 #include <KUrl>
35 
36 #include <QtCore/QByteArray>
37 #include <QtCore/QMimeData>
38 #include <QtCore/QStringList>
39 
40 using namespace Akonadi;
41 
42 bool PasteHelper::canPaste( const QMimeData * mimeData, const Collection & collection )
43 {
44  if ( !mimeData || !collection.isValid() ) {
45  return false;
46  }
47 
48  // check that the target collection has the rights to
49  // create the pasted items resp. collections
50  Collection::Rights neededRights = Collection::ReadOnly;
51  if ( KUrl::List::canDecode( mimeData ) ) {
52  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
53  foreach ( const KUrl &url, urls ) {
54  if ( url.hasQueryItem( QLatin1String( "item" ) ) ) {
55  neededRights |= Collection::CanCreateItem;
56  } else if ( url.hasQueryItem( QLatin1String( "collection" ) ) ) {
57  neededRights |= Collection::CanCreateCollection;
58  }
59  }
60 
61  if ( ( collection.rights() & neededRights ) == 0 ) {
62  return false;
63  }
64 
65  // check that the target collection supports the mime types of the
66  // items/collections that shall be pasted
67  bool supportsMimeTypes = true;
68  foreach ( const KUrl &url, urls ) {
69  // collections do not provide mimetype information, so ignore this check
70  if ( url.hasQueryItem( QLatin1String( "collection" ) ) ) {
71  continue;
72  }
73 
74  const QString mimeType = url.queryItemValue( QLatin1String( "type" ) );
75  if ( !collection.contentMimeTypes().contains( mimeType ) ) {
76  supportsMimeTypes = false;
77  break;
78  }
79  }
80 
81  if ( !supportsMimeTypes ) {
82  return false;
83  }
84 
85  return true;
86  }
87 
88  return false;
89 }
90 
91 KJob* PasteHelper::paste(const QMimeData * mimeData, const Collection & collection, bool copy, Session *session )
92 {
93  if ( !canPaste( mimeData, collection ) ) {
94  return 0;
95  }
96 
97  // we try to drop data not coming with the akonadi:// url
98  // find a type the target collection supports
99  foreach ( const QString &type, mimeData->formats() ) {
100  if ( !collection.contentMimeTypes().contains( type ) ) {
101  continue;
102  }
103 
104  QByteArray item = mimeData->data( type );
105  // HACK for some unknown reason the data is sometimes 0-terminated...
106  if ( !item.isEmpty() && item.at( item.size() - 1 ) == 0 ) {
107  item.resize( item.size() - 1 );
108  }
109 
110  Item it;
111  it.setMimeType( type );
112  it.setPayloadFromData( item );
113 
114  ItemCreateJob *job = new ItemCreateJob( it, collection );
115  return job;
116  }
117 
118  if ( !KUrl::List::canDecode( mimeData ) ) {
119  return 0;
120  }
121 
122  // data contains an url list
123  return pasteUriList( mimeData, collection, copy ? Qt::CopyAction : Qt::MoveAction, session );
124 }
125 
126 KJob* PasteHelper::pasteUriList( const QMimeData* mimeData, const Collection &destination, Qt::DropAction action, Session *session )
127 {
128  if ( !KUrl::List::canDecode( mimeData ) ) {
129  return 0;
130  }
131 
132  if ( !canPaste( mimeData, destination ) ) {
133  return 0;
134  }
135 
136  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
137  Collection::List collections;
138  Item::List items;
139  foreach ( const KUrl &url, urls ) {
140  const Collection collection = Collection::fromUrl( url );
141  if ( collection.isValid() ) {
142  collections.append( collection );
143  }
144  const Item item = Item::fromUrl( url );
145  if ( item.isValid() ) {
146  items.append( item );
147  }
148  // TODO: handle non Akonadi URLs?
149  }
150 
151  TransactionSequence *transaction = new TransactionSequence( session );
152 
153  //FIXME: The below code disables transactions in otder to avoid data loss due to nested
154  //transactions (copy and colcopy in the server doesn't see the items retrieved into the cache and copies empty payloads).
155  //Remove once this is fixed properly, see the other FIXME comments.
156  transaction->setProperty( "transactionsDisabled", true );
157 
158  switch ( action ) {
159  case Qt::CopyAction:
160  if ( !items.isEmpty() ) {
161  new ItemCopyJob( items, destination, transaction );
162  }
163  foreach ( const Collection &col, collections ) { // FIXME: remove once we have a batch job for collections as well
164  new CollectionCopyJob( col, destination, transaction );
165  }
166  break;
167  case Qt::MoveAction:
168  if ( !items.isEmpty() ) {
169  new ItemMoveJob( items, destination, transaction );
170  }
171  foreach ( const Collection &col, collections ) { // FIXME: remove once we have a batch job for collections as well
172  new CollectionMoveJob( col, destination, transaction );
173  }
174  break;
175  case Qt::LinkAction:
176  new LinkJob( destination, items, transaction );
177  break;
178  default:
179  Q_ASSERT(false); // WTF?!
180  return 0;
181  }
182  return transaction;
183 }
Akonadi::CollectionMoveJob
Job that moves a collection in the Akonadi storage to a new parent collection.
Definition: collectionmovejob.h:50
Akonadi::ItemCopyJob
Job that copies a set of items to a target collection in the Akonadi storage.
Definition: itemcopyjob.h:60
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::PasteHelper::paste
KJob * paste(const QMimeData *mimeData, const Collection &collection, bool copy=true, Session *session=0)
Paste/drop the given mime data into the given collection.
Definition: pastehelper.cpp:91
Akonadi::Collection::CanCreateCollection
Can create new subcollections in this collection.
Definition: collection.h:92
Akonadi::PasteHelper::pasteUriList
KJob * pasteUriList(const QMimeData *mimeData, const Collection &collection, Qt::DropAction action, Session *session=0)
URI list paste/drop.
Definition: pastehelper.cpp:126
Akonadi::LinkJob
Job that links items inside the Akonadi storage.
Definition: linkjob.h:64
Akonadi::Collection::ReadOnly
Can only read items or subcollection of this collection.
Definition: collection.h:87
Akonadi::Collection::CanCreateItem
Can create new items in this collection.
Definition: collection.h:89
Akonadi::Session
A communication session with the Akonadi storage.
Definition: session.h:59
Akonadi::ItemCreateJob
Job that creates a new item in the Akonadi storage.
Definition: itemcreatejob.h:73
Akonadi::CollectionCopyJob
Job that copies a collection into another collection in the Akonadi storage.
Definition: collectioncopyjob.h:57
Akonadi::Collection::rights
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:99
Akonadi::PasteHelper::canPaste
bool canPaste(const QMimeData *mimeData, const Collection &collection)
Check whether the given mime data can be pasted into the given collection.
Definition: pastehelper.cpp:42
Akonadi::ItemMoveJob
Job that moves an item into a different collection in the Akonadi storage.
Definition: itemmovejob.h:48
Akonadi::TransactionSequence
Base class for jobs that need to run a sequence of sub-jobs in a transaction.
Definition: transactionsequence.h:69
Akonadi::Collection::contentMimeTypes
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
Definition: collection.cpp:115
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::Collection::fromUrl
static Collection fromUrl(const KUrl &url)
Creates a collection from the given url.
Definition: collection.cpp:172
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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