00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00048
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
00064
00065 bool supportsMimeTypes = true;
00066 foreach ( const KUrl &url, urls ) {
00067
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
00093
00094 foreach ( const QString &type, mimeData->formats() ) {
00095 if ( !collection.contentMimeTypes().contains( type ) )
00096 continue;
00097
00098 QByteArray item = mimeData->data( type );
00099
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
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
00137 }
00138
00139 TransactionSequence *transaction = new TransactionSequence( session );
00140 switch ( action ) {
00141 case Qt::CopyAction:
00142 if ( !items.isEmpty() )
00143 new ItemCopyJob( items, destination, transaction );
00144 foreach ( const Collection &col, collections )
00145 new CollectionCopyJob( col, destination, transaction );
00146 break;
00147 case Qt::MoveAction:
00148 if ( !items.isEmpty() )
00149 new ItemMoveJob( items, destination, transaction );
00150 foreach ( const Collection &col, collections )
00151 new CollectionMoveJob( col, destination, transaction );
00152 break;
00153 case Qt::LinkAction:
00154 new LinkJob( destination, items, transaction );
00155 break;
00156 default:
00157 Q_ASSERT( "WTF?!" == false );
00158 return 0;
00159 }
00160 return transaction;
00161 }