• Skip to content
  • Skip to link menu
KDE 4.1 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   if ( KUrl::List::canDecode( mimeData ) )
00046     return true;
00047 
00048   foreach ( const QString &format, mimeData->formats() )
00049     if ( collection.contentMimeTypes().contains( format ) )
00050       return true;
00051 
00052   return false;
00053 }
00054 
00055 KJob* PasteHelper::paste(const QMimeData * mimeData, const Collection & collection, bool copy)
00056 {
00057   if ( !canPaste( mimeData, collection ) )
00058     return 0;
00059 
00060   // we try to drop data not coming with the akonadi:// url
00061   // find a type the target collection supports
00062   foreach ( const QString &type, mimeData->formats() ) {
00063     if ( !collection.contentMimeTypes().contains( type ) )
00064       continue;
00065 
00066     QByteArray item = mimeData->data( type );
00067     // HACK for some unknown reason the data is sometimes 0-terminated...
00068     if ( !item.isEmpty() && item.at( item.size() - 1 ) == 0 )
00069       item.resize( item.size() - 1 );
00070 
00071     Item it;
00072     it.setMimeType( type );
00073     it.setPayloadFromData( item );
00074 
00075     ItemCreateJob *job = new ItemCreateJob( it, collection );
00076     return job;
00077   }
00078 
00079   if ( !KUrl::List::canDecode( mimeData ) )
00080     return 0;
00081 
00082   // data contains an url list
00083   TransactionSequence *transaction = new TransactionSequence();
00084   KUrl::List urls = KUrl::List::fromMimeData( mimeData );
00085   foreach ( const KUrl &url, urls ) {
00086     if ( Collection::fromUrl( url ).isValid() ) {
00087       Collection col = Collection::fromUrl( url );
00088       if ( !copy ) {
00089         col.setParent( collection );
00090         CollectionModifyJob *job = new CollectionModifyJob( col, transaction );
00091       } else {
00092         new CollectionCopyJob( col, collection, transaction );
00093       }
00094     } else if ( Item::fromUrl( url ).isValid() ) {
00095       // TODO Extract mimetype from url and check if collection accepts it
00096       const Item item = Item::fromUrl( url );
00097       if ( !copy ) {
00098         new ItemMoveJob( item, collection, transaction );
00099       } else  {
00100         new ItemCopyJob( item, collection, transaction );
00101       }
00102     } else {
00103       // non-akonadi URL
00104       // TODO
00105       kDebug() << "Implement me!";
00106     }
00107   }
00108   return transaction;
00109 }
00110 

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
  • 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