File indexing completed on 2024-11-10 04:40:29

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 #include "job.h"
0011 
0012 namespace Akonadi
0013 {
0014 class Collection;
0015 class Item;
0016 class ItemCreateJobPrivate;
0017 
0018 /**
0019  * @short Job that creates a new item in the Akonadi storage.
0020  *
0021  * This job creates a new item with all the set properties in the
0022  * given target collection.
0023  *
0024  * Note that items can not be created in the root collection (Collection::root())
0025  * and the collection must have Collection::contentMimeTypes() that match the mimetype
0026  * of the item being created.
0027  *
0028  * Example:
0029  *
0030  * @code
0031  *
0032  * // Create a contact item in the root collection
0033  *
0034  * KContacts::Addressee addr;
0035  * addr.setNameFromString( "Joe Jr. Miller" );
0036  *
0037  * Akonadi::Item item;
0038  * item.setMimeType( "text/directory" );
0039  * item.setPayload<KContacts::Addressee>( addr );
0040  *
0041  * Akonadi::Collection collection = getCollection();
0042  *
0043  * Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, collection );
0044  * connect( job, SIGNAL(result(KJob*)), SLOT(jobFinished(KJob*)) );
0045  *
0046  * ...
0047  *
0048  * MyClass::jobFinished( KJob *job )
0049  * {
0050  *   if ( job->error() )
0051  *     qDebug() << "Error occurred";
0052  *   else
0053  *     qDebug() << "Contact item created successfully";
0054  * }
0055  *
0056  * @endcode
0057  *
0058  * @author Volker Krause <vkrause@kde.org>
0059  */
0060 class AKONADICORE_EXPORT ItemCreateJob : public Job
0061 {
0062     Q_OBJECT
0063 
0064 public:
0065     /**
0066      * Creates a new item create job.
0067      *
0068      * @param item The item to create.
0069      *             @note It must have a mime type set.
0070      * @param collection The parent collection where the new item shall be located in.
0071      * @param parent The parent object.
0072      */
0073     ItemCreateJob(const Item &item, const Collection &collection, QObject *parent = nullptr);
0074 
0075     /**
0076      * Destroys the item create job.
0077      */
0078     ~ItemCreateJob() override;
0079 
0080     /**
0081      * Returns the created item with the new unique id, or an invalid item if the job failed.
0082      */
0083     [[nodiscard]] Item item() const;
0084 
0085     enum MergeOption {
0086         NoMerge = 0, ///< Don't merge
0087         RID = 1, ///< Merge by remote id
0088         GID = 2, ///< Merge by GID
0089         Silent = 4 ///< Only return the id of the merged/created item.
0090     };
0091     Q_DECLARE_FLAGS(MergeOptions, MergeOption)
0092 
0093     /**
0094      * Merge this item into an existing one if available.
0095      *
0096      * If an item with same GID and/or remote ID as the created item exists in
0097      * specified collection (depending on the provided options), the new item will
0098      * be merged into the existing one and the merged item will be returned
0099      * (unless the Silent option is used).
0100      *
0101      * If no matching item is found a new item is created.
0102      *
0103      * If the item does not have a GID or RID, this option will be
0104      * ignored and a new item will be created.
0105      *
0106      * By default, merging is disabled.
0107      *
0108      * @param options Merge options.
0109      * @since 4.14
0110      */
0111     void setMerge(MergeOptions options);
0112 
0113 protected:
0114     void doStart() override;
0115     bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response) override;
0116 
0117 private:
0118     Q_DECLARE_PRIVATE(ItemCreateJob)
0119 };
0120 
0121 Q_DECLARE_OPERATORS_FOR_FLAGS(ItemCreateJob::MergeOptions)
0122 
0123 }