1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2014-09-30
7  * Description : a tool to export items to Piwigo web service
8  *
9  * Copyright (C) 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com>
10  * Copyright (C) 2006      by Colin Guthrie <kde at colin dot guthr dot ie>
11  * Copyright (C) 2006-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
12  * Copyright (C) 2008      by Andrea Diamantini <adjam7 at gmail dot com>
13  * Copyright (C) 2010-2019 by Frederic Coiffier <frederic dot coiffier at free dot com>
14  *
15  * This program is free software; you can redistribute it
16  * and/or modify it under the terms of the GNU General
17  * Public License as published by the Free Software Foundation;
18  * either version 2, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * ============================================================ */
26 
27 #ifndef DIGIKAM_PIWIGO_TALKER_H
28 #define DIGIKAM_PIWIGO_TALKER_H
29 
30 // Qt includes
31 
32 #include <QObject>
33 #include <QList>
34 #include <QDateTime>
35 #include <QTextStream>
36 #include <QFile>
37 #include <QUrl>
38 #include <QNetworkReply>
39 #include <QNetworkAccessManager>
40 
41 // Local includes
42 
43 #include "dinfointerface.h"
44 
45 using namespace Digikam;
46 
47 template <class T> class QList;
48 
49 namespace DigikamGenericPiwigoPlugin
50 {
51 
52 class PiwigoAlbum;
53 
54 class PiwigoTalker : public QObject
55 {
56     Q_OBJECT
57 
58 public:
59 
60     enum State
61     {
62         GE_LOGOUT = -1,
63         GE_LOGIN  = 0,
64         GE_GETVERSION,
65         GE_LISTALBUMS,
66         GE_CHECKPHOTOEXIST,
67         GE_GETINFO,
68         GE_SETINFO,
69         GE_ADDPHOTOCHUNK,
70         GE_ADDPHOTOSUMMARY
71     };
72 
73     enum
74     {
75         CHUNK_MAX_SIZE = 512*1024,
76         PIWIGO_VER_2_4 = 204
77     };
78 
79 public:
80 
81     explicit PiwigoTalker(DInfoInterface* const iface,
82                           QWidget* const parent);
83     ~PiwigoTalker() override;
84 
85 public:
86 
87     bool loggedIn() const;
88 
89     void login(const QUrl& url, const QString& name, const QString& passwd);
90     void listAlbums();
91     void listPhotos(const QString& albumName);
92 
93 /* TODO Implement this function
94     void createAlbum(const QString& parentAlbumName,
95                      const QString& albumName,
96                      const QString& albumTitle,
97                      const QString& albumCaption);
98 */
99 
100     bool addPhoto(int   albumId,
101                   const QString& photoPath,
102                   bool  rescale = false,
103                   int   maxWidth = 1600,
104                   int   maxHeight = 1600,
105                   int   quality = 95);
106 
107     void cancel();
108 
109     static QString getAuthToken();
110 
111 Q_SIGNALS:
112 
113     void signalProgressInfo(const QString& msg);
114     void signalError(const QString& msg);
115     void signalLoginFailed(const QString& msg);
116     void signalBusy(bool val);
117     void signalAlbums(const QList<PiwigoAlbum>& albumList);
118     void signalAddPhotoSucceeded();
119     void signalAddPhotoFailed(const QString& msg);
120 
121 private:
122 
123     void parseResponseLogin(const QByteArray& data);
124     void parseResponseGetVersion(const QByteArray& data);
125     void parseResponseListAlbums(const QByteArray& data);
126     void parseResponseDoesPhotoExist(const QByteArray& data);
127     void parseResponseGetInfo(const QByteArray& data);
128     void parseResponseSetInfo(const QByteArray& data);
129 
130     void addNextChunk();
131     void parseResponseAddPhotoChunk(const QByteArray& data);
132     void addPhotoSummary();
133     void parseResponseAddPhotoSummary(const QByteArray& data);
134 
135     QByteArray computeMD5Sum(const QString& filepath);
136     void deleteTemporaryFile();
137 
138 private Q_SLOTS:
139 
140     void slotFinished(QNetworkReply* reply);
141 
142 private:
143 
144     class Private;
145     Private* const d;
146 
147     static QString s_authToken;
148 };
149 
150 } // namespace DigikamGenericPiwigoPlugin
151 
152 #endif // PIWIGOTALKER_H
153