File indexing completed on 2024-05-12 03:54:56

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2005-2012 David Faure <faure@kde.org>
0005     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KURLMIMEDATA_H
0011 #define KURLMIMEDATA_H
0012 
0013 #include "kcoreaddons_export.h"
0014 #include <QFlags>
0015 #include <QMap>
0016 #include <QUrl>
0017 QT_BEGIN_NAMESPACE
0018 class QMimeData;
0019 QT_END_NAMESPACE
0020 
0021 /**
0022  * Utility functions for using URLs in QMimeData.
0023  * In addition to QMimeData::setUrls() and QMimeData::urls(), these functions allow to:
0024  *
0025  * - Store two sets of URLs, the KDE-specific URLs and the equivalent local-file URLs
0026  *     for compatibility with non-KDE applications
0027  * - Store KIO metadata, such as the HTTP referrer for a given URL (some websites
0028  *     require it for downloading e.g. an image)
0029  *
0030  * @since 5.0
0031  */
0032 namespace KUrlMimeData
0033 {
0034 typedef QMap<QString, QString> MetaDataMap;
0035 
0036 /**
0037  * Adds URLs and KIO metadata into the given QMimeData.
0038  *
0039  * WARNING: do not call this method multiple times on the same mimedata object,
0040  * you can add urls only once. But you can add other things, e.g. images, XML...
0041  *
0042  * @param mimeData the QMimeData instance used to drag or copy this URL
0043  */
0044 KCOREADDONS_EXPORT void setUrls(const QList<QUrl> &urls, const QList<QUrl> &mostLocalUrls, QMimeData *mimeData);
0045 
0046 /**
0047  * Export URLs through the XDG Documents Portal to allow interaction from/with sandbox code.
0048  * This implements the application/vnd.portal.filetransfer mimetype extension.
0049  * When built without dbus support or the portal isn't installed on the target system, then this
0050  * is no-op and returns false.
0051  * Remote URLs are automatically mounted into the file system using kio-fuse
0052  * @returns whether all URLS were exported through the portal
0053  */
0054 KCOREADDONS_EXPORT bool exportUrlsToPortal(QMimeData *mimeData);
0055 
0056 /**
0057  * @param metaData KIO metadata shipped in the mime data, which is used for instance to
0058  * set a correct HTTP referrer (some websites require it for downloading e.g. an image)
0059  */
0060 KCOREADDONS_EXPORT void setMetaData(const MetaDataMap &metaData, QMimeData *mimeData);
0061 
0062 /**
0063  * Return the list of mimeTypes that can be decoded by urlsFromMimeData
0064  */
0065 KCOREADDONS_EXPORT QStringList mimeDataTypes();
0066 
0067 /**
0068  * Flags to be used in urlsFromMimeData.
0069  */
0070 enum DecodeOption {
0071     /**
0072      * When the mimedata contains both KDE-style URLs (eg: desktop:/foo) and
0073      * the "most local" version of the URLs (eg: file:///home/dfaure/Desktop/foo),
0074      * decode it as the KDE-style URL. Useful in DnD code e.g. when moving icons,
0075      * and the kde-style url is used as identifier for the icons.
0076      */
0077     PreferKdeUrls = 0,
0078     /**
0079      * When the mimedata contains both KDE-style URLs (eg: desktop:/foo) and
0080      * the "most local" version of the URLs (eg: file:///home/dfaure/Desktop/foo),
0081      * decode it as local urls. Useful in paste/drop operations that end up calling KIO,
0082      * so that urls from other users work as well.
0083      */
0084     PreferLocalUrls = 1,
0085 };
0086 Q_DECLARE_FLAGS(DecodeOptions, DecodeOption)
0087 Q_DECLARE_OPERATORS_FOR_FLAGS(DecodeOptions)
0088 
0089 /**
0090  * Extract a list of urls from the contents of @p mimeData.
0091  *
0092  * Compared to QMimeData::urls(), this method has support for retrieving KDE-specific URLs
0093  * when urls() would retrieve "most local URLs" instead as well as support for the XDG Documents Portal
0094  * via the application/vnd.portal.filetransfer mimedata extension.
0095  *
0096  * Decoding will fail if @p mimeData does not contain any URLs, or if at
0097  * least one extracted URL is not valid.
0098  *
0099  * When application/vnd.portal.filetransfer is set you'll only receive URLs retrieved from the XDG Documents Portal.
0100  * When the portal is not available application/vnd.portal.filetransfer gets ignored.
0101  *
0102  * @param mimeData the mime data to extract from; cannot be 0
0103  * @param decodeOptions options for decoding
0104  * @param metaData optional pointer to a map which will hold the metadata after this call
0105  * @return the list of urls
0106  */
0107 KCOREADDONS_EXPORT QList<QUrl> urlsFromMimeData(const QMimeData *mimeData, DecodeOptions decodeOptions = PreferKdeUrls, MetaDataMap *metaData = nullptr);
0108 }
0109 
0110 #endif /* KURLMIMEDATA_H */