File indexing completed on 2025-01-05 03:53:35

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-02-12
0007  * Description : a tool to export images to IPFS web service
0008  *
0009  * SPDX-FileCopyrightText: 2018      by Amar Lakshya <amar dot lakshya at xaviers dot edu dot in>
0010  * SPDX-FileCopyrightText: 2018-2020 by Caulier Gilles <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_IPFS_TALKER_H
0017 #define DIGIKAM_IPFS_TALKER_H
0018 
0019 // Qt includes
0020 
0021 #include <QObject>
0022 #include <QString>
0023 #include <QTimerEvent>
0024 
0025 namespace DigikamGenericIpfsPlugin
0026 {
0027 
0028 enum class IpfsTalkerActionType
0029 {
0030     IMG_UPLOAD, // Action: upload Result: image
0031 };
0032 
0033 struct IpfsTalkerAction
0034 {
0035     IpfsTalkerActionType type;
0036 
0037     struct
0038     {
0039         QString imgpath;
0040         QString title;
0041         QString description;
0042     } upload;
0043 };
0044 
0045 struct IpfsTalkerResult
0046 {
0047     const IpfsTalkerAction* action;
0048 
0049     struct IPFSImage
0050     {
0051         QString    name;
0052         QString    url;
0053         uint       size;
0054     } image;
0055 };
0056 
0057 /* Main class, handles the client side of the globalupload.io API
0058  */
0059 class IpfsTalker : public QObject
0060 {
0061 Q_OBJECT
0062 
0063 public:
0064 
0065     explicit IpfsTalker(QObject* const parent = nullptr);
0066     ~IpfsTalker()                           override;
0067 
0068     unsigned int workQueueLength();
0069     void queueWork(const IpfsTalkerAction& action);
0070     void cancelAllWork();
0071 
0072 Q_SIGNALS:
0073 
0074     /* Emitted on progress changes.
0075      */
0076     void progress(unsigned int percent, const IpfsTalkerAction& action);
0077     void success(const IpfsTalkerResult& result);
0078     void error(const QString& msg, const IpfsTalkerAction& action);
0079 
0080     /* Emitted when the status changes.
0081      */
0082     void busy(bool b);
0083 
0084 public Q_SLOTS:
0085 
0086     /* Connected to the current QNetworkReply.
0087      */
0088     void uploadProgress(qint64 sent, qint64 total);
0089     void replyFinished();
0090 
0091 protected:
0092 
0093     void timerEvent(QTimerEvent* event)     override;
0094 
0095 private:
0096 
0097     /* Starts m_work_timer if m_work_queue not empty.
0098      */
0099     void startWorkTimer();
0100 
0101     /* Stops m_work_timer if running.
0102      */
0103     void stopWorkTimer();
0104 
0105     /* Start working on the first item of m_work_queue
0106      * by sending a request.
0107      */
0108     void doWork();
0109 
0110 private:
0111 
0112     class Private;
0113     Private* const d;
0114 };
0115 
0116 } // namespace DigikamGenericIpfsPlugin
0117 
0118 #endif // DIGIKAM_IPFS_TALKER_H