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

akonadi

  • 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  // check that the target collection has the rights to
48  // create the pasted items resp. collections
49  Collection::Rights neededRights = Collection::ReadOnly;
50  if ( KUrl::List::canDecode( mimeData ) ) {
51  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
52  foreach ( const KUrl &url, urls ) {
53  if ( url.hasQueryItem( QLatin1String( "item" ) ) ) {
54  neededRights |= Collection::CanCreateItem;
55  } else if ( url.hasQueryItem( QLatin1String( "collection" ) ) ) {
56  neededRights |= Collection::CanCreateCollection;
57  }
58  }
59 
60  if ( (collection.rights() & neededRights) == 0 )
61  return false;
62 
63  // check that the target collection supports the mime types of the
64  // items/collections that shall be pasted
65  bool supportsMimeTypes = true;
66  foreach ( const KUrl &url, urls ) {
67  // collections do not provide mimetype information, so ignore this check
68  if ( url.hasQueryItem( QLatin1String( "collection" ) ) )
69  continue;
70 
71  const QString mimeType = url.queryItemValue( QLatin1String( "type" ) );
72  if ( !collection.contentMimeTypes().contains( mimeType ) ) {
73  supportsMimeTypes = false;
74  break;
75  }
76  }
77 
78  if ( !supportsMimeTypes )
79  return false;
80 
81  return true;
82  }
83 
84  return false;
85 }
86 
87 KJob* PasteHelper::paste(const QMimeData * mimeData, const Collection & collection, bool copy, Session *session )
88 {
89  if ( !canPaste( mimeData, collection ) )
90  return 0;
91 
92  // we try to drop data not coming with the akonadi:// url
93  // find a type the target collection supports
94  foreach ( const QString &type, mimeData->formats() ) {
95  if ( !collection.contentMimeTypes().contains( type ) )
96  continue;
97 
98  QByteArray item = mimeData->data( type );
99  // HACK for some unknown reason the data is sometimes 0-terminated...
100  if ( !item.isEmpty() && item.at( item.size() - 1 ) == 0 )
101  item.resize( item.size() - 1 );
102 
103  Item it;
104  it.setMimeType( type );
105  it.setPayloadFromData( item );
106 
107  ItemCreateJob *job = new ItemCreateJob( it, collection );
108  return job;
109  }
110 
111  if ( !KUrl::List::canDecode( mimeData ) )
112  return 0;
113 
114  // data contains an url list
115  return pasteUriList( mimeData, collection, copy ? Qt::CopyAction : Qt::MoveAction, session );
116 }
117 
118 KJob* PasteHelper::pasteUriList( const QMimeData* mimeData, const Collection &destination, Qt::DropAction action, Session *session )
119 {
120  if ( !KUrl::List::canDecode( mimeData ) )
121  return 0;
122 
123  if ( !canPaste( mimeData, destination ) )
124  return 0;
125 
126  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
127  Collection::List collections;
128  Item::List items;
129  foreach ( const KUrl &url, urls ) {
130  const Collection collection = Collection::fromUrl( url );
131  if ( collection.isValid() )
132  collections.append( collection );
133  const Item item = Item::fromUrl( url );
134  if ( item.isValid() )
135  items.append( item );
136  // TODO: handle non Akonadi URLs?
137  }
138 
139  TransactionSequence *transaction = new TransactionSequence( session );
140 
141  //FIXME: The below code disables transactions in otder to avoid data loss due to nested
142  //transactions (copy and colcopy in the server doesn't see the items retrieved into the cache and copies empty payloads).
143  //Remove once this is fixed properly, see the other FIXME comments.
144  transaction->setProperty( "transactionsDisabled", true );
145 
146  switch ( action ) {
147  case Qt::CopyAction:
148  if ( !items.isEmpty() )
149  new ItemCopyJob( items, destination, transaction );
150  foreach ( const Collection &col, collections ) // FIXME: remove once we have a batch job for collections as well
151  new CollectionCopyJob( col, destination, transaction );
152  break;
153  case Qt::MoveAction:
154  if ( !items.isEmpty() )
155  new ItemMoveJob( items, destination, transaction );
156  foreach ( const Collection &col, collections ) // FIXME: remove once we have a batch job for collections as well
157  new CollectionMoveJob( col, destination, transaction );
158  break;
159  case Qt::LinkAction:
160  new LinkJob( destination, items, transaction );
161  break;
162  default:
163  Q_ASSERT( "WTF?!" == false );
164  return 0;
165  }
166  return transaction;
167 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:48:10 by doxygen 1.8.1.2 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-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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