File indexing completed on 2024-05-12 16:01:50

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_IMPORT_EXPORT_MANAGER_H
0008 #define KIS_IMPORT_EXPORT_MANAGER_H
0009 
0010 #include <QObject>
0011 #include <QMap>
0012 #include <QByteArray>
0013 #include <QUrl>
0014 
0015 #include "KisImportExportFilter.h"
0016 
0017 #include "kritaui_export.h"
0018 
0019 class KisDocument;
0020 class KoProgressUpdater;
0021 
0022 template <class T>
0023 class QFuture;
0024 
0025 /**
0026  *  @brief The class managing all the filters.
0027  *
0028  *  This class manages all filters for a %Calligra application. Normally
0029  *  you will not have to use it, since KisMainWindow takes care of loading
0030  *  and saving documents.
0031  *
0032  *  @ref KisFilter
0033  *
0034  *  @author Kalle Dalheimer <kalle@kde.org>
0035  *  @author Torben Weis <weis@kde.org>
0036  *  @author Werner Trobin <trobin@kde.org>
0037  */
0038 class KRITAUI_EXPORT KisImportExportManager : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     /**
0043      * This enum is used to distinguish the import/export cases
0044      */
0045     enum Direction { Import = 1,  Export = 2 };
0046 
0047     /**
0048      * Create a filter manager for a document
0049      */
0050     explicit KisImportExportManager(KisDocument *document);
0051 
0052 public:
0053 
0054     ~KisImportExportManager() override;
0055 
0056     /**
0057      * Imports the specified document and returns the resultant filename
0058      * (most likely some file in /tmp).
0059      * @p path can be either a URL or a filename.
0060      * @p documentMimeType gives importDocument a hint about what type
0061      * the document may be. It can be left empty.
0062      *
0063      * @return  status signals the success/error of the conversion.
0064      * If the QString which is returned isEmpty() and the status is OK,
0065      * then we imported the file directly into the document.
0066      */
0067     KisImportExportErrorCode importDocument(const QString &location, const QString &mimeType);
0068 
0069     /**
0070      * @brief Exports the given file/document to the specified URL/mimetype.
0071      *
0072      * If @p mimeType is empty, then the closest matching Calligra part is searched
0073      * and when the method returns @p mimeType contains this mimetype.
0074      * Oh, well, export is a C++ keyword ;)
0075      */
0076     KisImportExportErrorCode exportDocument(const QString &location, const QString& realLocation, const QByteArray &mimeType, bool showWarnings = true, KisPropertiesConfigurationSP exportConfiguration = 0, bool isAdvancedExporting = false );
0077 
0078     QFuture<KisImportExportErrorCode> exportDocumentAsyc(const QString &location, const QString& realLocation, const QByteArray &mimeType, KisImportExportErrorCode &status, bool showWarnings = true, KisPropertiesConfigurationSP exportConfiguration = 0,bool isAdvancedExporting= false);
0079 
0080     ///@name Static API
0081     //@{
0082     /**
0083      * Suitable for passing to KoFileDialog::setMimeTypeFilters. The default mime
0084      * gets set by the "users" of this method, as we do not have enough
0085      * information here.
0086      * Optionally, @p extraNativeMimeTypes are added after the native mimetype.
0087      */
0088     static QStringList supportedMimeTypes(Direction direction);
0089 
0090     /**
0091      * @brief filterForMimeType loads the relevant import/export plugin and returns it. The caller
0092      * is responsible for deleting it!
0093      * @param mimetype the mimetype we want to import/export. If there's more than one plugin, the one
0094      * with the highest weight as defined in the json description will be taken
0095      * @param direction import or export
0096      * @return a pointer to the filter plugin or 0 if none could be found
0097      */
0098     static KisImportExportFilter *filterForMimeType(const QString &mimetype, Direction direction);
0099 
0100     /**
0101      * Fill necessary information for the export filter into the properties, e.g. if the image has
0102      * transparency or has sRGB profile.
0103      */
0104     static void fillStaticExportConfigurationProperties(KisPropertiesConfigurationSP exportConfiguration, KisImageSP image);
0105 
0106     /**
0107      * Get if the filter manager is batch mode (true)
0108      * or in interactive mode (true)
0109      */
0110     bool batchMode(void) const;
0111 
0112     void setUpdater(KoUpdaterPtr updater);
0113 
0114     static QString askForAudioFileName(const QString &defaultDir, QWidget *parent);
0115 
0116     static QString getUriForAdditionalFile(const QString &defaultUri, QWidget *parent);
0117 
0118 
0119 private:
0120 
0121     struct ConversionResult;
0122     ConversionResult convert(Direction direction, const QString &location, const QString& realLocation, const QString &mimeType, bool showWarnings, KisPropertiesConfigurationSP exportConfiguration, bool isAsync, bool isAdvancedExporting= false);
0123 
0124 
0125     void fillStaticExportConfigurationProperties(KisPropertiesConfigurationSP exportConfiguration);
0126     bool askUserAboutExportConfiguration(QSharedPointer<KisImportExportFilter> filter, KisPropertiesConfigurationSP exportConfiguration, const QByteArray &from, const QByteArray &to, bool batchMode, const bool showWarnings, bool *alsoAsKra, bool isAdvancedExporting = false);
0127 
0128     KisImportExportErrorCode doImport(const QString &location, QSharedPointer<KisImportExportFilter> filter);
0129 
0130     KisImportExportErrorCode doExport(const QString &location, QSharedPointer<KisImportExportFilter> filter, KisPropertiesConfigurationSP exportConfiguration, const QString alsoAsKraLocation);
0131     KisImportExportErrorCode doExportImpl(const QString &location, QSharedPointer<KisImportExportFilter> filter, KisPropertiesConfigurationSP exportConfiguration);
0132 
0133     QString getAlsoAsKraLocation(const QString location) const;
0134 
0135     // Private API
0136     KisImportExportManager(const KisImportExportManager& rhs);
0137     KisImportExportManager &operator=(const KisImportExportManager& rhs);
0138 
0139     KisDocument *m_document;
0140 
0141     /// A static cache for the availability checks of filters
0142     static QStringList m_importMimeTypes;
0143     static QStringList m_exportMimeTypes;
0144 
0145     class Private;
0146     Private * const d;
0147 };
0148 
0149 #endif  // __KO_FILTER_MANAGER_H__