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,
56 virtual ~PasteHelperJob();
59 void onDragSourceCollectionFetched(KJob *job);
63 void runItemsActions();
64 void runCollectionsActions();
67 Qt::DropAction mAction;
68 Akonadi::Item::List mItems;
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()) {
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;
102 if (dragSourceCollection.
isValid()) {
105 setAutomaticCommittingEnabled(
false);
108 CollectionFetchJob::Base,
110 QObject::connect(fetch, SIGNAL(finished(KJob*)),
111 this, SLOT(onDragSourceCollectionFetched(KJob*)));
117PasteHelperJob::~PasteHelperJob()
121void PasteHelperJob::onDragSourceCollectionFetched(KJob *job)
124 kDebug() << fetch->error() << fetch->
collections().count();
125 if (fetch->error() || fetch->
collections().count() != 1) {
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);
147 new UnlinkJob(sourceCollection, mItems,
this);
148 if (mDestCollection.isVirtual()) {
149 new LinkJob(mDestCollection, mItems,
this);
155 new LinkJob(mDestCollection, mItems,
this);
160 runCollectionsActions();
169void PasteHelperJob::runActions()
172 runCollectionsActions();
175void PasteHelperJob::runItemsActions()
177 if (mItems.isEmpty()) {
189 new LinkJob(mDestCollection, mItems,
this);
196void PasteHelperJob::runCollectionsActions()
198 if (mCollections.isEmpty()) {
204 foreach (
const Collection &col, mCollections) {
209 foreach (
const Collection &col, mCollections) {
223bool PasteHelper::canPaste(
const QMimeData *mimeData,
const Collection &collection)
225 if (!mimeData || !collection.isValid()) {
231 Collection::Rights neededRights = Collection::ReadOnly;
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"))) {
236 neededRights |= Collection::CanCreateItem;
237 }
else if (url.hasQueryItem(QLatin1String(
"collection"))) {
238 neededRights |= Collection::CanCreateCollection;
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) {
272KJob *PasteHelper::paste(
const QMimeData *mimeData,
const Collection &collection,
bool copy,
Session *session)
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);
307KJob *PasteHelper::pasteUriList(
const QMimeData *mimeData,
const Collection &destination, Qt::DropAction action,
Session *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) {
321 const Collection collection = Collection::fromUrl(url);
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 copies a collection into another collection in the Akonadi storage.
Job that fetches collections from the Akonadi storage.
Collection::List collections() const
Returns the list of fetched collection.
Job that moves a collection in the Akonadi storage to a new parent 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.
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
Collection parentCollection() const
Returns the parent collection of this object.
bool isValid() const
Returns whether the entity is valid.
Id id() const
Returns the unique identifier of the entity.
Job that copies a set of items to a target collection in the Akonadi storage.
Job that creates a new item in the Akonadi storage.
Job that moves an item into a different collection in the Akonadi storage.
Job that links items inside 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.
Job that unlinks items inside the Akonadi storage.
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.