File indexing completed on 2024-05-05 04:22:01

0001 /* SPDX-FileCopyrightText: 2003-2019 The KPhotoAlbum Development Team
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #ifndef REQUESTQUEUE_H
0006 #define REQUESTQUEUE_H
0007 
0008 #include "ImageRequest.h"
0009 #include "enums.h"
0010 
0011 #include <QQueue>
0012 #include <QSet>
0013 
0014 namespace ImageManager
0015 {
0016 class ImageClientInterface;
0017 
0018 // RequestQueue for ImageRequests. Non-synchronized, locking has to be
0019 // provided by the user.
0020 class RequestQueue
0021 {
0022 
0023 public:
0024     RequestQueue();
0025 
0026     // Add a new request to the input queue in the right priority level.
0027     // @return 'true', if this is not a request already pending.
0028     bool addRequest(ImageRequest *request);
0029 
0030     // Return the next needed ImageRequest from the queue or nullptr if there
0031     // is none. The ownership is returned back to the caller so it has to
0032     // delete it.
0033     ImageRequest *popNext();
0034 
0035     // Remove all pending requests from the given client.
0036     void cancelRequests(ImageClientInterface *client, StopAction action);
0037 
0038     bool isRequestStillValid(ImageRequest *request);
0039     void removeRequest(ImageRequest *);
0040 
0041 private:
0042     // A Reference to a ImageRequest with value semantic.
0043     // This only stores the pointer to an ImageRequest object but behaves
0044     // regarding the less-than and equals-operator like the object.
0045     // This allows to store ImageRequests with value-semantic in a Set.
0046     class ImageRequestReference
0047     {
0048     public:
0049         ImageRequestReference()
0050             : m_ptr(nullptr)
0051         {
0052         }
0053         explicit ImageRequestReference(const ImageRequest *ptr)
0054             : m_ptr(ptr)
0055         {
0056         }
0057 
0058         bool operator<(const ImageRequestReference &other) const
0059         {
0060             return *m_ptr < *other.m_ptr;
0061         }
0062         bool operator==(const ImageRequestReference &other) const
0063         {
0064             return *m_ptr == *other.m_ptr;
0065         }
0066         operator const ImageRequest &() const
0067         {
0068             return *m_ptr;
0069         }
0070 
0071     private:
0072         const ImageRequest *m_ptr;
0073     };
0074 
0075     typedef QList<QQueue<ImageRequest *>> QueueType;
0076 
0077     /** @short Priotized list of queues (= 1 priority queue) of image requests
0078      * that are waiting for processing
0079      */
0080     QueueType m_queues;
0081 
0082     /**
0083      * Set of unique requests currently pending; used to discard the exact
0084      * same requests.
0085      * TODO(hzeller): seems, that the unique-pending requests tried to be
0086      * handled in different places in kpa but sometimes in a snakeoil
0087      * way (it compares pointers instead of the content -> clean up that).
0088      */
0089     QSet<ImageRequestReference> m_uniquePending;
0090 
0091     // All active requests that have a client
0092     QSet<ImageRequest *> m_activeRequests;
0093 };
0094 
0095 }
0096 
0097 #endif /* REQUESTQUEUE_H */
0098 
0099 // vi:expandtab:tabstop=4 shiftwidth=4: