• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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.h"
00021 
00022 #include "collectioncopyjob.h"
00023 #include "collectionmodifyjob.h"
00024 #include "item.h"
00025 #include "itemcreatejob.h"
00026 #include "itemcopyjob.h"
00027 #include "itemmodifyjob.h"
00028 #include "itemmovejob.h"
00029 #include "transactionsequence.h"
00030 
00031 #include <KDebug>
00032 #include <KUrl>
00033 
00034 #include <QtCore/QByteArray>
00035 #include <QtCore/QMimeData>
00036 #include <QtCore/QStringList>
00037 
00038 using namespace Akonadi;
00039 
00040 bool PasteHelper::canPaste(const QMimeData * mimeData, const Collection & collection)
00041 {
00042   if ( !mimeData || !collection.isValid() )
00043     return false;
00044 
00045   // TODO check acls depending on mimetype
00046   if ( (collection.rights() & (Collection::CanCreateItem | Collection::CanCreateCollection) ) == 0 )
00047     return false;
00048 
00049   if ( KUrl::List::canDecode( mimeData ) )
00050     return true;
00051 
00052   foreach ( const QString &format, mimeData->formats() )
00053     if ( collection.contentMimeTypes().contains( format ) )
00054       return true;
00055 
00056   return false;
00057 }
00058 
00059 KJob* PasteHelper::paste(const QMimeData * mimeData, const Collection & collection, bool copy)
00060 {
00061   if ( !canPaste( mimeData, collection ) )
00062     return 0;
00063 
00064   // we try to drop data not coming with the akonadi:// url
00065   // find a type the target collection supports
00066   foreach ( const QString &type, mimeData->formats() ) {
00067     if ( !collection.contentMimeTypes().contains( type ) )
00068       continue;
00069 
00070     QByteArray item = mimeData->data( type );
00071     // HACK for some unknown reason the data is sometimes 0-terminated...
00072     if ( !item.isEmpty() && item.at( item.size() - 1 ) == 0 )
00073       item.resize( item.size() - 1 );
00074 
00075     Item it;
00076     it.setMimeType( type );
00077     it.setPayloadFromData( item );
00078 
00079     ItemCreateJob *job = new ItemCreateJob( it, collection );
00080     return job;
00081   }
00082 
00083   if ( !KUrl::List::canDecode( mimeData ) )
00084     return 0;
00085 
00086   // data contains an url list
00087   TransactionSequence *transaction = new TransactionSequence();
00088   KUrl::List urls = KUrl::List::fromMimeData( mimeData );
00089   foreach ( const KUrl &url, urls ) {
00090     if ( Collection::fromUrl( url ).isValid() ) {
00091       Collection col = Collection::fromUrl( url );
00092       if ( !copy ) {
00093         col.setParent( collection );
00094         CollectionModifyJob *job = new CollectionModifyJob( col, transaction );
00095       } else {
00096         new CollectionCopyJob( col, collection, transaction );
00097       }
00098     } else if ( Item::fromUrl( url ).isValid() ) {
00099       // TODO Extract mimetype from url and check if collection accepts it
00100       const Item item = Item::fromUrl( url );
00101       if ( !copy ) {
00102         new ItemMoveJob( item, collection, transaction );
00103       } else  {
00104         new ItemCopyJob( item, collection, transaction );
00105       }
00106     } else {
00107       // non-akonadi URL
00108       // TODO
00109       kDebug() << "Implement me!";
00110     }
00111   }
00112   return transaction;
00113 }
00114 

akonadi

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
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