File indexing completed on 2024-04-28 04:58:10
0001 // /* This file is part of the KDE project 0002 // SPDX-FileCopyrightText: 2023 Stefano Crocco <stefano.crocco@alice.it> 0003 // 0004 // SPDX-License-Identifier: LGPL-2.0-or-later 0005 // */ 0006 0007 #ifndef ASYNCSELECTORINTERFACE_H 0008 #define ASYNCSELECTORINTERFACE_H 0009 0010 #include <QtGlobal> 0011 #include <QSharedDataPointer> 0012 #include <QString> 0013 #include <QtPlugin> 0014 0015 #if QT_VERSION_MAJOR < 6 0016 #include <KParts/SelectorInterface> 0017 #endif 0018 0019 #include <libkonq_export.h> 0020 0021 #include <functional> 0022 0023 /** 0024 * @brief Alternative to KParts::SelectorInterface which provides an asynchronous API 0025 * 0026 * This interface closely mimics the API of KParts::SelectorInterface 0027 */ 0028 class LIBKONQ_EXPORT AsyncSelectorInterface 0029 { 0030 public: 0031 0032 /** 0033 * @brief Destructor 0034 */ 0035 virtual ~AsyncSelectorInterface(); 0036 0037 #if QT_VERSION_MAJOR < 6 0038 typedef KParts::SelectorInterface::Element Element; 0039 // typedef KParts::SelectorInterface::QueryMethod QueryMethod; 0040 // typedef KParts::SelectorInterface::QueryMethods QueryMethods; 0041 #else 0042 class Element; 0043 class ElementPrivate; 0044 #endif 0045 enum QueryMethod { 0046 None = 0x00, /*!< Querying is not possible. */ 0047 EntireContent = 0x01, /*!< Query or can query the entire content. */ 0048 SelectedContent = 0x02, /*!< Query or can query only the user selected content, if any. */ 0049 }; 0050 /** 0051 * Stores a combination of #QueryMethod values. 0052 */ 0053 Q_DECLARE_FLAGS(QueryMethods, QueryMethod) 0054 0055 /** 0056 * Returns the supported query methods. 0057 * 0058 * By default this function returns None. 0059 * 0060 * @see QueryMethod 0061 */ 0062 virtual QueryMethods supportedAsyncQueryMethods() const; 0063 0064 /** 0065 * @brief A function taking a single KParts::SelectorInterface::Element as argument and without return value 0066 */ 0067 typedef const std::function<void (const Element &)> SingleElementSelectorCallback; 0068 0069 /** 0070 * @brief A function taking a `QList<Element>` as argument and without return value 0071 */ 0072 typedef const std::function<void (const QList<Element> &)> MultipleElementSelectorCallback; 0073 0074 /** 0075 * @brief Passes to the given callback the first (in document order) element in this fragment matching 0076 * the given CSS selector and querying method 0077 * 0078 * If no element satisfying the given query is found, including the case where the requested query method is not supported, 0079 * the callback must be called with an invalid element. 0080 * @see KParts::SelectorInterface::querySelector() 0081 * @note This function is _asynchronous_ 0082 * @param query the query containing the string 0083 * @param method the method to use for the query 0084 * @param callback the function to call with the found element 0085 */ 0086 virtual void querySelectorAsync(const QString &query, QueryMethod method, SingleElementSelectorCallback& callback) = 0; 0087 0088 /** 0089 * @brief Passes to the given callback all the elements in this fragment matching the given CSS selector and querying method 0090 * 0091 * If no element satisfying the given query is found, including the case where the requested query method is not supported, 0092 * the callback must be called with an empty list. 0093 * @see KParts::SelectorInterface::querySelector() 0094 * @note This function is _asynchronous_ 0095 * @param query the query containing the string 0096 * @param method the method to use for the query 0097 * @param callback the function to call with the found elements 0098 */ 0099 virtual void querySelectorAllAsync(const QString &query, QueryMethod method, MultipleElementSelectorCallback& callback) = 0; 0100 #if QT_VERSION_MAJOR > 5 0101 0102 //Code for this class copied from kparts/selectorinterface.h (KF 5.110) written by David Faure <faure@kde.org> 0103 class LIBKONQ_EXPORT Element 0104 { 0105 public: 0106 /** 0107 * Constructor 0108 */ 0109 Element(); 0110 0111 /** 0112 * Copy constructor 0113 */ 0114 Element(const Element &other); 0115 0116 /** 0117 * Destructor 0118 */ 0119 ~Element(); 0120 0121 /** 0122 * Returns true if the element is null ; otherwise returns false. 0123 */ 0124 bool isNull() const; 0125 0126 /** 0127 * Sets the tag name of this element. 0128 */ 0129 void setTagName(const QString &tag); 0130 0131 /** 0132 * Returns the tag name of this element. 0133 */ 0134 QString tagName() const; 0135 0136 /** 0137 * Adds an attribute with the given name and value. 0138 * If an attribute with the same name exists, its value is replaced by value. 0139 */ 0140 void setAttribute(const QString &name, const QString &value); 0141 0142 /** 0143 * Returns the list of attributes in this element. 0144 */ 0145 QStringList attributeNames() const; 0146 0147 /** 0148 * Returns the attribute with the given name. If the attribute does not exist, defaultValue is returned. 0149 */ 0150 QString attribute(const QString &name, const QString &defaultValue = QString()) const; 0151 0152 /** 0153 * Returns true if the attribute with the given @p name exists. 0154 */ 0155 bool hasAttribute(const QString &name) const; 0156 0157 // No namespace support yet, could be added with attributeNS, setAttributeNS 0158 0159 /** 0160 * Swaps the contents of @p other with the contents of this. 0161 */ 0162 void swap(Element &other) 0163 { 0164 d.swap(other.d); 0165 } 0166 0167 /** 0168 * Assignment operator 0169 */ 0170 Element &operator=(const Element &other) 0171 { 0172 if (this != &other) { 0173 Element copy(other); 0174 swap(copy); 0175 } 0176 return *this; 0177 } 0178 0179 private: 0180 QSharedDataPointer<ElementPrivate> d; 0181 }; 0182 #endif 0183 }; 0184 0185 #if QT_VERSION_MAJOR > 5 0186 inline void qSwap(AsyncSelectorInterface::Element &lhs, AsyncSelectorInterface::Element &rhs) 0187 { 0188 lhs.swap(rhs); 0189 } 0190 0191 Q_DECLARE_TYPEINFO(AsyncSelectorInterface::Element, Q_MOVABLE_TYPE); 0192 #endif 0193 0194 Q_DECLARE_INTERFACE(AsyncSelectorInterface, "org.kde.libkonq.AsyncSelectorInterface") 0195 0196 #endif // ASYNCSELECTORINTERFACE_H