File indexing completed on 2024-04-21 04:55:26

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #ifndef MEDIAMANAGER_H
0010 #define MEDIAMANAGER_H
0011 
0012 #include <QObject>
0013 #include <QMap>
0014 #include <QPixmap>
0015 #include <QUrl>
0016 
0017 #include "choqok_export.h"
0018 
0019 namespace KIO
0020 {
0021 class Job;
0022 }
0023 class KJob;
0024 namespace Choqok
0025 {
0026 /**
0027     @brief Media files manager!
0028     A simple and global way to fetch and cache images
0029 
0030     @author Mehrdad Momeny \<mehrdad.momeny@gmail.com\>
0031 */
0032 class CHOQOK_EXPORT MediaManager : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     enum ReturnMode {
0037         Sync = 0, Async
0038     };
0039     ~MediaManager();
0040 
0041     static MediaManager *self();
0042 
0043     /**
0044      * @brief Fetch an Image and cache it for later use.
0045      *
0046      * @param remoteUrl The URL of image to fetch
0047      * @param mode Return mode, if set to Sync and the image is not available in the cache the null pixmap will be returned.
0048      * if mode set to @ref Async and image is not available in the cache, the null pixmap will be returned
0049      * and then @ref MediaManager will fetch the image and emit @ref imageFetched() on success or
0050      * emit @ref fetchError() on error.
0051      * And if mode set to @ref Sync and image is not in the cache @ref MediaManager will not fetch it.
0052      *
0053      * @return return @ref QPixmap of requested image if exists in cache, otherwise null pixmap
0054      */
0055     QPixmap fetchImage(const QUrl &remoteUrl, ReturnMode mode = Sync);
0056 
0057     /**
0058      * @return KDE Default image
0059      */
0060     QPixmap &defaultImage();
0061 
0062     /**
0063      * @brief Parse a text for EmotIcons with kde default theme.
0064      */
0065     QString parseEmoticons(const QString &text);
0066 
0067     static QPixmap convertToGrayScale(const QPixmap &pic);
0068 
0069     /**
0070     Upload medium at @p localUrl to @p pluginId service or to last used service when @p pluginId is empty.
0071 
0072     @see mediumUploaded()
0073     @see mediumUploadFailed()
0074     */
0075     void uploadMedium(const QUrl &localUrl, const QString &pluginId = QString());
0076 
0077     /**
0078     Create and return a byte array containing a multipart/form-data to send with HTTP POST request
0079 
0080     Boundary is AaB03x
0081 
0082     @param formdata are the "form-data" parts of data.
0083                     This map knows as a list of name/value pairs
0084     @param mediaFiles are media files attached to form, each file stored in one QMap in list
0085 
0086     @note media file maps should contain these keys:
0087         name: The name of entry
0088         filename: the file name on server
0089         medium: contain the medium data loaded from disk!
0090         mediumType: type of medium file
0091     */
0092     static QByteArray createMultipartFormData(const QMap<QString, QByteArray> &formdata,
0093             const QList< QMap<QString, QByteArray> > &mediaFiles);
0094 
0095 public Q_SLOTS:
0096     /**
0097      * @brief Clear image cache
0098      */
0099     void clearImageCache();
0100 
0101 Q_SIGNALS:
0102     void fetchError(const QUrl &remoteUrl, const QString &errMsg);
0103     void imageFetched(const QUrl &remoteUrl, const QPixmap &pixmap);
0104 
0105     void mediumUploaded(const QUrl &localUrl, const QString &remoteUrl);
0106     void mediumUploadFailed(const QUrl &localUrl, const QString &errorMessage);
0107 
0108 protected Q_SLOTS:
0109     void slotImageFetched(KJob *job);
0110 
0111 protected:
0112     MediaManager();
0113 
0114 private:
0115     class Private;
0116     Private *const d;
0117     static MediaManager *mSelf;
0118 };
0119 
0120 }
0121 #endif