File indexing completed on 2024-06-23 05:06:50

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Tobias Koenig <tokoe@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 <qglobal.h>
0011 
0012 #include <memory>
0013 
0014 namespace Akonadi
0015 {
0016 class Item;
0017 class ItemFetchScope;
0018 class ItemMonitorPrivate;
0019 
0020 /**
0021  * @short A convenience class to monitor a single item for changes.
0022  *
0023  * This class can be used as a base class for classes that want to show
0024  * a single item to the user and keep track of status changes of the item
0025  * without having to using a Monitor object themself.
0026  *
0027  * Example:
0028  *
0029  * @code
0030  *
0031  * // A label that shows the name of a contact item
0032  *
0033  * class ContactLabel : public QLabel, public Akonadi::ItemMonitor
0034  * {
0035  *   public:
0036  *     ContactLabel( QWidget *parent = nullptr )
0037  *       : QLabel( parent )
0038  *     {
0039  *       setText( "No Name" );
0040  *     }
0041  *
0042  *   protected:
0043  *     virtual void itemChanged( const Akonadi::Item &item )
0044  *     {
0045  *       if ( item.mimeType() != "text/directory" )
0046  *         return;
0047  *
0048  *       const KContacts::Addressee addr = item.payload<KContacts::Addressee>();
0049  *       setText( addr.fullName() );
0050  *     }
0051  *
0052  *     virtual void itemRemoved()
0053  *     {
0054  *       setText( "No Name" );
0055  *     }
0056  * };
0057  *
0058  * ...
0059  *
0060  * ContactLabel *label = new ContactLabel( this );
0061  *
0062  * const Akonadi::Item item = fetchJob->items().at(0);
0063  * label->setItem( item );
0064  *
0065  * @endcode
0066  *
0067  * @author Tobias Koenig <tokoe@kde.org>
0068  */
0069 class AKONADICORE_EXPORT ItemMonitor
0070 {
0071 public:
0072     /**
0073      * Creates a new item monitor.
0074      */
0075     ItemMonitor();
0076 
0077     /**
0078      * Destroys the item monitor.
0079      */
0080     virtual ~ItemMonitor();
0081 
0082     /**
0083      * Sets the @p item that shall be monitored.
0084      */
0085     void setItem(const Item &item);
0086 
0087     /**
0088      * Returns the currently monitored item.
0089      */
0090     Item item() const;
0091 
0092 protected:
0093     /**
0094      * This method is called whenever the monitored item has changed.
0095      *
0096      * @param item The changed item.
0097      */
0098     virtual void itemChanged(const Item &item);
0099 
0100     /**
0101      * This method is called whenever the monitored item has been removed.
0102      */
0103     virtual void itemRemoved();
0104 
0105     /**
0106      * Sets the item fetch scope.
0107      *
0108      * Controls how much of an item's data is fetched from the server, e.g.
0109      * whether to fetch the full item payload or only meta data.
0110      *
0111      * @param fetchScope The new scope for item fetch operations.
0112      *
0113      * @see fetchScope()
0114      */
0115     void setFetchScope(const ItemFetchScope &fetchScope);
0116 
0117     /**
0118      * Returns the item fetch scope.
0119      *
0120      * Since this returns a reference it can be used to conveniently modify the
0121      * current scope in-place, i.e. by calling a method on the returned reference
0122      * without storing it in a local variable. See the ItemFetchScope documentation
0123      * for an example.
0124      *
0125      * @return a reference to the current item fetch scope
0126      *
0127      * @see setFetchScope() for replacing the current item fetch scope
0128      */
0129     ItemFetchScope &fetchScope();
0130 
0131 private:
0132     /// @cond PRIVATE
0133     friend class ItemMonitorPrivate;
0134     std::unique_ptr<ItemMonitorPrivate> const d;
0135     /// @endcond
0136 
0137     Q_DISABLE_COPY(ItemMonitor)
0138 };
0139 
0140 }