digiKam
inattalker.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 : 2021-03-20
7  * Description : a tool to export images to iNaturalist web service
8  *
9  * Copyright (C) 2021-2022 by Joerg Lohse <joergmlpts at gmail dot com>
10  * Copyright (C) 2005-2009 by Vardhman Jain <vardhman at gmail dot com>
11  * Copyright (C) 2009-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
12  * Copyright (C) 2017-2019 by Maik Qualmann <metzpinguin at gmail dot com>
13  *
14  * This program is free software; you can redistribute it
15  * and/or modify it under the terms of the GNU General
16  * Public License as published by the Free Software Foundation;
17  * either version 2, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * ============================================================ */
25 
26 #ifndef DIGIKAM_INAT_TALKER_H
27 #define DIGIKAM_INAT_TALKER_H
28 
29 // Qt includes
30 
31 #include <QUrl>
32 #include <QList>
33 #include <QPair>
34 #include <QString>
35 #include <QObject>
36 #include <QNetworkReply>
37 #include <QNetworkCookie>
38 
39 // Local includes
40 
41 #include "dinfointerface.h"
42 #include "ditemslist.h"
43 #include "inattaxon.h"
44 #include "inatscore.h"
45 
46 class QProgressDialog;
47 
48 using namespace Digikam;
49 
51 {
52 
53 class INatPhotoInfo;
54 class INatPhotoSet;
55 
56 class AuthRequest;
57 class ApiTokenRequest;
58 
59 typedef QPair<QString, QList<Taxon> > AutoCompletions;
60 
61 class INatTalker : public QObject
62 {
63  Q_OBJECT
64 
65 public:
66 
71  {
73  : m_observationId(-1),
74  m_totalImages (0),
75  m_updateIds (false),
76  m_rescale (false),
77  m_maxDim (-1),
78  m_quality (-1)
79  {
80  }
81 
83  bool updId,
84  bool resize,
85  int mxDim,
86  int q, const QString& userName)
87  : m_observationId(-1),
88  m_totalImages (imgs.count()),
89  m_images (imgs),
90  m_user (userName),
91  m_updateIds (updId),
92  m_rescale (resize),
93  m_maxDim (mxDim),
94  m_quality (q)
95  {
96  }
97 
101  QString m_apiKey;
102  QString m_user;
104  bool m_rescale;
105  int m_maxDim;
107  };
108 
113  {
115  : m_observationPhotoId(-1),
116  m_photoId (-1)
117  {
118  }
119 
120  PhotoUploadResult(const PhotoUploadRequest& req, int obsPhId, int phId)
121  : m_request (req),
122  m_observationPhotoId(obsPhId),
123  m_photoId (phId)
124  {
125  }
126 
130  };
131 
133  {
134 
136  : m_observationId (-1),
137  m_latitude (0.0),
138  m_longitude (0.0),
139  m_distanceMeters (-1.0),
140  m_obscured (false),
141  m_referenceTaxon (0),
142  m_referenceLatitude (0.0),
143  m_referenceLongitude(0.0)
144  {
145  }
146 
148  double latitude,
149  double longitude,
150  double distanceMeters,
151  bool obscured,
152  uint taxon,
153  double referenceLatitude,
154  double referenceLongitude)
155  : m_observationId (id),
156  m_latitude (latitude),
157  m_longitude (longitude),
158  m_distanceMeters (distanceMeters),
159  m_obscured (obscured),
160  m_referenceTaxon (taxon),
161  m_referenceLatitude (referenceLatitude),
162  m_referenceLongitude (referenceLongitude)
163  {
164  }
165 
166  void updateObservation(int id,
167  double latitude,
168  double longitude,
169  double distanceMeters)
170  {
171  m_observationId = id;
172  m_latitude = latitude;
173  m_longitude = longitude;
174  m_distanceMeters = distanceMeters;
175  }
176 
177  bool isValid() const
178  {
179  return (m_observationId != -1);
180  }
181 
183  double m_latitude;
184  double m_longitude;
190  };
191 
192  INatTalker(QWidget* const parent,
193  const QString& serviceName,
194  DInfoInterface* const iface);
195  ~INatTalker() override;
196 
197  void unLink();
198  void cancel();
199  void removeUserName(const QString& userName);
200 
204  bool stillUploading() const;
205 
209  int apiTokenExpiresIn() const;
210 
214  bool restoreApiToken(const QString& username,
216  bool emitSignal);
217 
221  void loadUrl(const QUrl& url, int retries = 0);
222 
226  void taxonAutoCompletions(const QString& partialName);
227 
231  void userInfo(const QList<QNetworkCookie>&);
232 
236  void nearbyPlaces(double latitude, double longitude);
237 
241  void closestObservation(uint taxon, double latitude, double longitude,
242  double radius_km = 10.0,
243  const QString& origQuery = QString());
244 
248  void computerVision(const QUrl& localImage);
249 
253  void createObservation(const QByteArray&, const PhotoUploadRequest&);
254 
259  void verifyCreateObservation(const QByteArray&,
260  const PhotoUploadRequest&,
261  int page, int retries);
262 
266  void deleteObservation(int id, const QString& apiKey, int retries = 0);
267 
271  void uploadNextPhoto(const PhotoUploadRequest&);
272 
277  void verifyUploadNextPhoto(const PhotoUploadRequest&, int retries);
278 
279 public:
280 
281  QProgressDialog* m_authProgressDlg;
282 
283 Q_SIGNALS:
284 
285  void signalBusy(bool val);
286 
287  void signalLoadUrlSucceeded(const QUrl&, const QByteArray&);
291  void signalNearbyPlaces(const QStringList&);
295 
296  void signalLinkingSucceeded(const QString& login,
297  const QString& name, const QUrl& iconUrl);
298  void signalLinkingFailed(const QString& error);
299 
300 private:
301 
302  QString tmpFileName(const QString& path);
303 
304 private Q_SLOTS:
305 
306  void slotApiToken(const QString&, const QList<QNetworkCookie>&);
307  void slotFinished(QNetworkReply* reply);
308  void slotTimeout();
309 
310 private:
311 
312  class Private;
313  Private* const d;
314 
315  friend class UserRequest;
316  friend class LoadUrlRequest;
317  friend class AutoCompletionRequest;
318  friend class NearbyPlacesRequest;
319  friend class NearbyObservationRequest;
320  friend class ComputerVisionRequest;
321 };
322 
323 } // namespace DigikamGenericINatPlugin
324 
325 #endif // DIGIKAM_INAT_TALKER_H
Definition: inattalker.h:62
void signalTaxonAutoCompletions(const AutoCompletions &)
QProgressDialog * m_authProgressDlg
Definition: inattalker.h:281
void signalObservationCreated(const INatTalker::PhotoUploadRequest &)
void signalNearbyPlaces(const QStringList &)
void signalNearbyObservation(const INatTalker::NearbyObservation &)
void signalLoadUrlSucceeded(const QUrl &, const QByteArray &)
void signalLinkingSucceeded(const QString &login, const QString &name, const QUrl &iconUrl)
void signalLinkingFailed(const QString &error)
void signalPhotoUploaded(const INatTalker::PhotoUploadResult &)
void signalComputerVisionResults(const ImageScores &)
Definition: dinfointerface.h:56
Definition: inatbrowserdlg.cpp:64
QPair< QString, QList< Taxon > > AutoCompletions
Definition: inatsuggest.h:47
QPair< QString, QList< ComputerVisionScore > > ImageScores
Definition: inatscore.h:75
Definition: datefolderview.cpp:43
double m_longitude
Definition: inattalker.h:184
double m_latitude
Definition: inattalker.h:183
bool isValid() const
Definition: inattalker.h:177
double m_referenceLongitude
Definition: inattalker.h:189
double m_distanceMeters
Definition: inattalker.h:185
uint m_referenceTaxon
Definition: inattalker.h:187
void updateObservation(int id, double latitude, double longitude, double distanceMeters)
Definition: inattalker.h:166
double m_referenceLatitude
Definition: inattalker.h:188
NearbyObservation(int id, double latitude, double longitude, double distanceMeters, bool obscured, uint taxon, double referenceLatitude, double referenceLongitude)
Definition: inattalker.h:147
QList< QUrl > m_images
Definition: inattalker.h:100
PhotoUploadRequest(const QList< QUrl > &imgs, bool updId, bool resize, int mxDim, int q, const QString &userName)
Definition: inattalker.h:82
QString m_apiKey
Definition: inattalker.h:101
int m_observationPhotoId
Definition: inattalker.h:128
PhotoUploadRequest m_request
Definition: inattalker.h:127
PhotoUploadResult(const PhotoUploadRequest &req, int obsPhId, int phId)
Definition: inattalker.h:120