• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi

pastehelper.cpp
00001 /*
00002     Copyright (c) 2008 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "pastehelper_p.h"
00021 
00022 #include "collectioncopyjob.h"
00023 #include "collectionmovejob.h"
00024 #include "item.h"
00025 #include "itemcreatejob.h"
00026 #include "itemcopyjob.h"
00027 #include "itemmodifyjob.h"
00028 #include "itemmovejob.h"
00029 #include "linkjob.h"
00030 #include "transactionsequence.h"
00031 #include "session.h"
00032 
00033 #include <KDebug>
00034 #include <KUrl>
00035 
00036 #include <QtCore/QByteArray>
00037 #include <QtCore/QMimeData>
00038 #include <QtCore/QStringList>
00039 
00040 using namespace Akonadi;
00041 
00042 bool PasteHelper::canPaste( const QMimeData * mimeData, const Collection & collection )
00043 {
00044   if ( !mimeData || !collection.isValid() )
00045     return false;
00046 
00047   // check that the target collection has the rights to
00048   // create the pasted items resp. collections
00049   Collection::Rights neededRights = Collection::ReadOnly;
00050   if ( KUrl::List::canDecode( mimeData ) ) {
00051     const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
00052     foreach ( const KUrl &url, urls ) {
00053       if ( url.hasQueryItem( QLatin1String( "item" ) ) ) {
00054         neededRights |= Collection::CanCreateItem;
00055       } else if ( url.hasQueryItem( QLatin1String( "collection" ) ) ) {
00056         neededRights |= Collection::CanCreateCollection;
00057       }
00058     }
00059 
00060     if ( (collection.rights() & neededRights) == 0 )
00061       return false;
00062 
00063     // check that the target collection supports the mime types of the
00064     // items/collections that shall be pasted
00065     bool supportsMimeTypes = true;
00066     foreach ( const KUrl &url, urls ) {
00067       // collections do not provide mimetype information, so ignore this check
00068       if ( url.hasQueryItem( QLatin1String( "collection" ) ) )
00069         continue;
00070 
00071       const QString mimeType = url.queryItemValue( QLatin1String( "type" ) );
00072       if ( !collection.contentMimeTypes().contains( mimeType ) ) {
00073         supportsMimeTypes = false;
00074         break;
00075       }
00076     }
00077 
00078     if ( !supportsMimeTypes )
00079       return false;
00080 
00081     return true;
00082   }
00083 
00084   return false;
00085 }
00086 
00087 KJob* PasteHelper::paste(const QMimeData * mimeData, const Collection & collection, bool copy, Session *session )
00088 {
00089   if ( !canPaste( mimeData, collection ) )
00090     return 0;
00091 
00092   // we try to drop data not coming with the akonadi:// url
00093   // find a type the target collection supports
00094   foreach ( const QString &type, mimeData->formats() ) {
00095     if ( !collection.contentMimeTypes().contains( type ) )
00096       continue;
00097 
00098     QByteArray item = mimeData->data( type );
00099     // HACK for some unknown reason the data is sometimes 0-terminated...
00100     if ( !item.isEmpty() && item.at( item.size() - 1 ) == 0 )
00101       item.resize( item.size() - 1 );
00102 
00103     Item it;
00104     it.setMimeType( type );
00105     it.setPayloadFromData( item );
00106 
00107     ItemCreateJob *job = new ItemCreateJob( it, collection );
00108     return job;
00109   }
00110 
00111   if ( !KUrl::List::canDecode( mimeData ) )
00112     return 0;
00113 
00114   // data contains an url list
00115   return pasteUriList( mimeData, collection, copy ? Qt::CopyAction : Qt::MoveAction, session );
00116 }
00117 
00118 KJob* PasteHelper::pasteUriList( const QMimeData* mimeData, const Collection &destination, Qt::DropAction action, Session *session )
00119 {
00120   if ( !KUrl::List::canDecode( mimeData ) )
00121     return 0;
00122 
00123   if ( !canPaste( mimeData, destination ) )
00124     return 0;
00125 
00126   const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
00127   Collection::List collections;
00128   Item::List items;
00129   foreach ( const KUrl &url, urls ) {
00130     const Collection collection = Collection::fromUrl( url );
00131     if ( collection.isValid() )
00132       collections.append( collection );
00133     const Item item = Item::fromUrl( url );
00134     if ( item.isValid() )
00135       items.append( item );
00136     // TODO: handle non Akonadi URLs?
00137   }
00138 
00139   TransactionSequence *transaction = new TransactionSequence( session );
00140 
00141   //FIXME: The below code disables transactions in otder to avoid data loss due to nested
00142   //transactions (copy and colcopy in the server doesn't see the items retrieved into the cache and copies empty payloads).
00143   //Remove once this is fixed properly, see the other FIXME comments.
00144   transaction->setProperty( "transactionsDisabled", true );
00145   
00146   switch ( action ) {
00147     case Qt::CopyAction:
00148       if ( !items.isEmpty() )
00149         new ItemCopyJob( items, destination, transaction );
00150       foreach ( const Collection &col, collections ) // FIXME: remove once we have a batch job for collections as well
00151         new CollectionCopyJob( col, destination, transaction );
00152       break;
00153     case Qt::MoveAction:
00154       if ( !items.isEmpty() )
00155         new ItemMoveJob( items, destination, transaction );
00156       foreach ( const Collection &col, collections ) // FIXME: remove once we have a batch job for collections as well
00157         new CollectionMoveJob( col, destination, transaction );
00158       break;
00159     case Qt::LinkAction:
00160       new LinkJob( destination, items, transaction );
00161       break;
00162     default:
00163       Q_ASSERT( "WTF?!" == false );
00164       return 0;
00165   }
00166   return transaction;
00167 }

akonadi

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal