20#include "pastehelper_p.h"
22#include "collectioncopyjob.h"
23#include "collectionmovejob.h"
24#include "collectionfetchjob.h"
26#include "itemcreatejob.h"
27#include "itemcopyjob.h"
28#include "itemmodifyjob.h"
29#include "itemmovejob.h"
31#include "transactionsequence.h"
38#include <QtCore/QByteArray>
39#include <QtCore/QMimeData>
40#include <QtCore/QStringList>
41#include <QtCore/QMutexLocker>
43#include <boost/bind.hpp>
52 explicit PasteHelperJob(Qt::DropAction action,
const Akonadi::Item::List &items,
54 const Akonadi::Collection &destination,
56 virtual ~PasteHelperJob();
59 void onDragSourceCollectionFetched(KJob *job);
63 void runItemsActions();
64 void runCollectionsActions();
67 Qt::DropAction mAction;
68 Akonadi::Item::List mItems;
70 Akonadi::Collection mDestCollection;
73PasteHelperJob::PasteHelperJob(Qt::DropAction action,
const Item::List &items,
80 , mCollections(collections)
81 , mDestCollection(destination)
86 setProperty(
"transactionsDisabled",
true);
89 if (!items.isEmpty() && items.first().parentCollection().isValid()) {
91 const Collection parent = items.first().parentCollection();
92 if (std::find_if(items.constBegin(), items.constEnd(),
93 boost::bind(&Entity::operator!=, boost::bind(
static_cast<Collection (Item::*)() const
>(&Item::parentCollection), _1), parent))
96 dragSourceCollection = parent;
99 kDebug() << items.first().parentCollection().id() << dragSourceCollection.
id();
102 if (dragSourceCollection.
isValid()) {
105 setAutomaticCommittingEnabled(
false);
110 QObject::connect(fetch, SIGNAL(finished(KJob*)),
111 this, SLOT(onDragSourceCollectionFetched(KJob*)));
117PasteHelperJob::~PasteHelperJob()
121void PasteHelperJob::onDragSourceCollectionFetched(KJob *job)
123 CollectionFetchJob *fetch = qobject_cast<CollectionFetchJob*>(job);
124 kDebug() << fetch->error() << fetch->
collections().count();
125 if (fetch->error() || fetch->
collections().count() != 1) {
133 const Collection sourceCollection = fetch->
collections().first();
134 kDebug() <<
"FROM: " << sourceCollection.
id() << sourceCollection.
name() << sourceCollection.
isVirtual();
135 kDebug() <<
"DEST: " << mDestCollection.id() << mDestCollection.name() << mDestCollection.isVirtual();
136 kDebug() <<
"ACTN:" << mAction;
140 if (mDestCollection.isVirtual()) {
141 new LinkJob(mDestCollection, mItems,
this);
143 new ItemCopyJob(mItems, mDestCollection,
this);
147 new UnlinkJob(sourceCollection, mItems,
this);
148 if (mDestCollection.isVirtual()) {
149 new LinkJob(mDestCollection, mItems,
this);
151 new ItemCopyJob(mItems, mDestCollection,
this);
155 new LinkJob(mDestCollection, mItems,
this);
160 runCollectionsActions();
169void PasteHelperJob::runActions()
172 runCollectionsActions();
175void PasteHelperJob::runItemsActions()
177 if (mItems.isEmpty()) {
183 new ItemCopyJob(mItems, mDestCollection,
this);
186 new ItemMoveJob(mItems, mDestCollection,
this);
189 new LinkJob(mDestCollection, mItems,
this);
196void PasteHelperJob::runCollectionsActions()
198 if (mCollections.isEmpty()) {
204 foreach (
const Collection &col, mCollections) {
205 new CollectionCopyJob(col, mDestCollection,
this);
209 foreach (
const Collection &col, mCollections) {
210 new CollectionMoveJob(col, mDestCollection,
this);
225 if (!mimeData || !collection.isValid()) {
232 if (KUrl::List::canDecode(mimeData)) {
233 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
234 foreach (
const KUrl &url, urls) {
235 if (url.hasQueryItem(QLatin1String(
"item"))) {
237 }
else if (url.hasQueryItem(QLatin1String(
"collection"))) {
242 if ((collection.rights() & neededRights) == 0) {
248 bool supportsMimeTypes =
true;
249 foreach (
const KUrl &url, urls) {
251 if (url.hasQueryItem(QLatin1String(
"collection"))) {
255 const QString mimeType = url.queryItemValue(QLatin1String(
"type"));
256 if (!collection.contentMimeTypes().contains(mimeType)) {
257 supportsMimeTypes =
false;
262 if (!supportsMimeTypes) {
274 if (!
canPaste(mimeData, collection)) {
280 foreach (
const QString &type, mimeData->formats()) {
281 if (!collection.contentMimeTypes().contains(type)) {
285 QByteArray item = mimeData->data(type);
287 if (!item.isEmpty() && item.at(item.size() - 1) == 0) {
288 item.resize(item.size() - 1);
292 it.setMimeType(type);
293 it.setPayloadFromData(item);
299 if (!KUrl::List::canDecode(mimeData)) {
304 return pasteUriList(mimeData, collection, copy ? Qt::CopyAction : Qt::MoveAction, session);
309 if (!KUrl::List::canDecode(mimeData)) {
313 if (!
canPaste(mimeData, destination)) {
317 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
320 foreach (
const KUrl &url, urls) {
322 if (collection.isValid()) {
323 collections.append(collection);
325 Item item = Item::fromUrl(url);
326 if (url.hasQueryItem(QLatin1String(
"parent"))) {
327 item.setParentCollection(
Collection(url.queryItem(QLatin1String(
"parent")).toLongLong()));
329 if (item.isValid()) {
336 PasteHelperJob *job =
new PasteHelperJob(action, items,
337 collections, destination,
343#include "pastehelper.moc"
Job that fetches collections from the Akonadi storage.
@ Base
Only fetch the base collection.
Collection::List collections() const
Returns the list of fetched collection.
Represents a collection of PIM items.
QList< Collection > List
Describes a list of collections.
QString name() const
Returns the i18n'ed name of the collection.
static Collection fromUrl(const KUrl &url)
Creates a collection from the given url.
@ ReadOnly
Can only read items or subcollection of this collection.
@ CanCreateItem
Can create new items in this collection.
@ CanCreateCollection
Can create new subcollections in this collection.
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
bool isValid() const
Returns whether the entity is valid.
Id id() const
Returns the unique identifier of the entity.
Job that creates a new item in the Akonadi storage.
A communication session with the Akonadi storage.
Base class for jobs that need to run a sequence of sub-jobs in a transaction.
void commit()
Commits the transaction as soon as all pending sub-jobs finished successfully.
KJob * paste(const QMimeData *mimeData, const Collection &collection, bool copy=true, Session *session=0)
Paste/drop the given mime data into the given collection.
KJob * pasteUriList(const QMimeData *mimeData, const Collection &collection, Qt::DropAction action, Session *session=0)
URI list paste/drop.
bool canPaste(const QMimeData *mimeData, const Collection &collection)
Check whether the given mime data can be pasted into the given collection.
FreeBusyManager::Singleton.