digiKam
wstalker.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2018-07-08
7  * Description : Base class for web service talkers.
8  *
9  * Copyright (C) 2018 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * ============================================================ */
22 
23 #ifndef DIGIKAM_WS_TALKER_H
24 #define DIGIKAM_WS_TALKER_H
25 
26 // Qt includes
27 
28 #include <QtGlobal>
29 #include <QList>
30 #include <QPair>
31 #include <QString>
32 #include <QUrl>
33 #include <QWidget>
34 #include <QSettings>
35 #include <QNetworkReply>
36 #include <QNetworkAccessManager>
37 #include <QMap>
38 
39 // Local includes
40 
41 #include "o0settingsstore.h"
42 #include "wsitem.h"
43 #include "wswizard.h"
44 
45 using namespace Digikam;
46 
48 {
49 
50 class WSTalker : public QObject
51 {
52  Q_OBJECT
53 
54 public:
55 
56  enum State
57  {
58  DEFAULT = 0,
62  ADDPHOTO
63  };
64 
65 public:
66 
67  explicit WSTalker(QWidget* const parent);
68  ~WSTalker();
69 
70  /*
71  * Get ID of an existent user account saved when he logged in before,
72  * knowing user name.
73  */
74  QString getUserID(const QString& userName);
75 
76  /*
77  * Link user account (login).
78  */
79  virtual void link();
80 
81  /*
82  * Unlink user account (logout).
83  */
84  virtual void unlink();
85 
86  /*
87  * Return true if account is linked.
88  */
89  virtual bool linked() const;
90 
91  /*
92  * This method load account that user chooses to login. If it exists and doesn't expire yet,
93  * then obtain the saved token and pass the authentication process. Otherwise, relogin.
94  */
95  virtual void authenticate();
96 
97  /*
98  * Force user to login on web service login page.
99  */
100  void reauthenticate();
101 
102  /*
103  * Abort any network request realizing at the moment.
104  */
105  void cancel();
106 
107 protected:
108 
109  /*
110  * Return a map of all information stored in the previous login of userName account.
111  */
112  QMap<QString, QVariant> getUserAccountInfo(const QString& userName);
113 
114  /*
115  * Save all necessary information of user account to disk. That information will be retrieved
116  * by getUserAccountInfo(userName) when needed.
117  */
118  void saveUserAccount(const QString& userName,
119  const QString& userID,
120  long long int expire,
121  const QString& accessToken,
122  const QString& refreshToken = QString());
123 
124  /*
125  * Remove all information of user account that was stored by saveUserAccount(...)
126  * TODO: this method should be called when user uninstalls digiKam.
127  */
128  void removeUserAccount(const QString& userName);
129 
130  /*
131  * Save as removeUserAccount(userName), but for all accounts.
132  */
133  void removeAllAccounts();
134 
135  /*
136  * A wrapper method of getUserAccountInfo(userName), but perform further verification
137  * on account's validation and further operation in case that account is expired.
138  */
139  bool loadUserAccount(const QString& userName);
140 
141  /*
142  * This method can be (and must be) reimplemented in derived class. Indeed, it will hard code
143  * (at runtime) O2's settings (i.e accessToken, refreshToken, expired date and value of linked state).
144  * It forces O2 to link to another account according to user's selection. Otherwise, O2 will
145  * "remember" account from previous login and always link to that account, if an obligated reauthenticate
146  * (unlink and then link) is not realized.
147  */
148  virtual void resetTalker(const QString& expire, const QString& accessToken, const QString& refreshToken);
149 
150  /*
151  * Sort list of albums by ascending order of titles.
152  */
153  virtual void sortAlbumsList(QList<WSAlbum>& albumsList);
154 
155  /*
156  * These methods are reimplemented in derived class and used to parse response of network requests
157  * for user's information or APIs of web service. They will be called asynchronously when responses
158  * for net request are received.
159  */
160  virtual void parseResponseGetLoggedInUser(const QByteArray& data);
161  virtual void parseResponseListAlbums(const QByteArray& data);
162  virtual void parseResponseCreateAlbum(const QByteArray& data);
163  virtual void parseResponseAddPhoto(const QByteArray& data);
164 
165  /*
166  * This method is called when authentication is complete. It should be reimplemented in derived class
167  * and call saveUserAccount(...) inside. Here, we implement a minimised version so that derived class
168  * can call it if needed.
169  */
170  virtual void authenticationDone(int errCode, const QString& errMsg);
171 
172 public:
173 
174  /*
175  * These methods are reimplemented in derived class, and will be used to make network requests
176  * for user's information or APIs of web service.
177  */
178  virtual void getLoggedInUser();
179  virtual void listAlbums(long long userID = 0);
180  virtual void createNewAlbum();
181  virtual void addPhoto(const QString& imgPath, const QString& albumID, const QString& caption);
182 
183 Q_SIGNALS:
184 
185  void signalBusy(bool val);
186  void signalOpenBrowser(const QUrl& url);
189  void signalCreateAlbumDone(int errCode, const QString& errMsg, const QString& newAlbumId);
190  void signalListAlbumsDone(int errCode, const QString& errMsg, const QList <WSAlbum>& albumsList);
191  void signalAddPhotoDone(int errCode, const QString& errMsg);
192 
193 protected Q_SLOTS:
194 
195  /*
196  * Slots for signals from O2 authentication flow
197  */
198  void slotFinished(QNetworkReply* reply);
199  void slotOpenBrowser(const QUrl& url);
200  void slotCloseBrowser();
201  virtual void slotLinkingFailed();
202  virtual void slotLinkingSucceeded();
203 
204  /*
205  * This is a particular slot, only used in case that digiKam will intercept O2 authentication flow,
206  * catch all navigation from web service, and the final url whose fragment contains accessToken
207  * and other necessary information. digiKam then parses the response to get accessToken and join back to
208  * O2's authentication flow by calling this method.
209  *
210  * Facebook is a web service where this approach is used,
211  * because the callback url is not http://127.0.0.1/ // krazy:exclude=insecurenet
212  */
213  virtual void slotResponseTokenReceived(const QMap<QString, QString>& rep);
214 
215 protected:
216 
217  QNetworkAccessManager* m_netMngr;
218  QNetworkReply* m_reply;
219 
221 
222  QSettings* m_settings;
223  O0SettingsStore* m_store;
224 
225  QString m_userName;
226 
228 };
229 
230 } // namespace DigikamGenericUnifiedPlugin
231 
232 #endif // DIGIKAM_WS_TALKER_H
Definition: wstalker.h:51
State m_state
Definition: wstalker.h:220
O0SettingsStore * m_store
Definition: wstalker.h:223
void signalCreateAlbumDone(int errCode, const QString &errMsg, const QString &newAlbumId)
void signalAddPhotoDone(int errCode, const QString &errMsg)
QNetworkReply * m_reply
Definition: wstalker.h:218
void signalOpenBrowser(const QUrl &url)
QSettings * m_settings
Definition: wstalker.h:222
WSWizard * m_wizard
Definition: wstalker.h:227
State
Definition: wstalker.h:57
@ CREATEALBUM
Definition: wstalker.h:61
@ GETUSER
Definition: wstalker.h:59
@ LISTALBUMS
Definition: wstalker.h:60
void signalListAlbumsDone(int errCode, const QString &errMsg, const QList< WSAlbum > &albumsList)
QNetworkAccessManager * m_netMngr
Definition: wstalker.h:217
QString m_userName
Definition: wstalker.h:225
Definition: wswizard.h:47
Definition: piwigotalker.h:48
Definition: wsauthentication.cpp:56
Definition: datefolderview.cpp:43
#define DEFAULT(c)
Definition: thememanager_p.cpp:781