1 /* ============================================================
2 *
3 * This file is a part of kipi-plugins project
4 *
5 *
6 * Date        : 2014-09-30
7 * Description : a plugin to export to a remote Piwigo server.
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-2018 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-2014 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 PIWIGOTALKER_H
28 #define PIWIGOTALKER_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 // Libkipi includes
42 
43 #include <KIPI/Interface>
44 
45 using namespace KIPI;
46 
47 template <class T> class QList;
48 
49 namespace KIPIPiwigoExportPlugin
50 {
51 
52 class GAlbum;
53 class GPhoto;
54 
55 class PiwigoTalker : public QObject
56 {
57     Q_OBJECT
58 
59 public:
60 
61     enum State
62     {
63         GE_LOGOUT = -1,
64         GE_LOGIN  = 0,
65         GE_GETVERSION,
66         GE_LISTALBUMS,
67         GE_CHECKPHOTOEXIST,
68         GE_GETINFO,
69         GE_SETINFO,
70         GE_ADDPHOTOCHUNK,
71         GE_ADDPHOTOSUMMARY
72     };
73 
74     enum
75     {
76         CHUNK_MAX_SIZE = 512*1024,
77         PIWIGO_VER_2_4 = 24
78     };
79 
80 public:
81 
82     PiwigoTalker(QWidget* const parent);
83     ~PiwigoTalker();
84 
getAuthToken()85     static QString getAuthToken()
86     {
87         return s_authToken;
88     };
89 
90     bool loggedIn() const;
91 
92     void login(const QUrl& url, const QString& name, const QString& passwd);
93     void listAlbums();
94     void listPhotos(const QString& albumName);
95 
96     /* TODO Implement this function
97     void createAlbum(const QString& parentAlbumName,
98                      const QString& albumName,
99                      const QString& albumTitle,
100                      const QString& albumCaption);*/
101 
102     bool addPhoto(int albumId,
103                   const QString& photoPath,
104                   bool  rescale = false, int maxWidth = 1600, int maxHeight = 1600, int quality = 95);
105 
106     void cancel();
107 
108 Q_SIGNALS:
109 
110     void signalProgressInfo(const QString& msg);
111     void signalError(const QString& msg);
112     void signalLoginFailed(const QString& msg);
113     void signalBusy(bool val);
114     void signalAlbums(const QList<GAlbum>& albumList);
115     void signalAddPhotoSucceeded();
116     void signalAddPhotoFailed(const QString& msg);
117 
118 private:
119 
120     void parseResponseLogin(const QByteArray& data);
121     void parseResponseGetVersion(const QByteArray& data);
122     void parseResponseListAlbums(const QByteArray& data);
123     void parseResponseDoesPhotoExist(const QByteArray& data);
124     void parseResponseGetInfo(const QByteArray& data);
125     void parseResponseSetInfo(const QByteArray& data);
126 
127     void addNextChunk();
128     void parseResponseAddPhotoChunk(const QByteArray& data);
129     void addPhotoSummary();
130     void parseResponseAddPhotoSummary(const QByteArray& data);
131 
132     QByteArray computeMD5Sum(const QString& filepath);
133     void deleteTemporaryFile();
134 
135 private Q_SLOTS:
136 
137     void slotFinished(QNetworkReply* reply);
138 
139 private:
140 
141     QWidget*               m_parent;
142     State                  m_state;
143     QString                m_cookie;
144     QUrl                   m_url;
145     QNetworkAccessManager* m_netMngr;
146     QNetworkReply*         m_reply;
147     bool                   m_loggedIn;
148     QByteArray             m_talker_buffer;
149     uint                   m_chunkId;
150     uint                   m_nbOfChunks;
151     int                    m_version;
152 
153     QByteArray             m_md5sum;
154     QString                m_path;
155     QString                m_tmpPath;    // If set, contains a temporary file which must be deleted
156     int                    m_albumId;
157     int                    m_photoId;    // Filled when the photo already exist
158     QString                m_comment;    // Synchronized with Piwigo comment
159     QString                m_title;      // Synchronized with Piwigo name
160     QString                m_author;     // Synchronized with Piwigo author
161     QDateTime              m_date;       // Synchronized with Piwigo date
162     Interface*             m_iface;
163 
164     static QString         s_authToken;
165 };
166 
167 } // namespace KIPIPiwigoExportPlugin
168 
169 #endif /* PIWIGOTALKER_H */
170