1 /****************************************************************************
2 **
3 ** Copyright (C) 2013-2018 Esri <contracts@esri.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "geotiledmapreply_esri.h"
41 
42 #include <QtLocation/private/qgeotilespec_p.h>
43 
44 QT_BEGIN_NAMESPACE
45 
46 static const unsigned char pngSignature[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00};
47 static const unsigned char jpegSignature[] = {0xFF, 0xD8, 0xFF, 0x00};
48 static const unsigned char gifSignature[] = {0x47, 0x49, 0x46, 0x38, 0x00};
49 
GeoTiledMapReplyEsri(QNetworkReply * reply,const QGeoTileSpec & spec,QObject * parent)50 GeoTiledMapReplyEsri::GeoTiledMapReplyEsri(QNetworkReply *reply, const QGeoTileSpec &spec,
51                                            QObject *parent) :
52     QGeoTiledMapReply(spec, parent)
53 {
54     if (!reply) {
55         setError(UnknownError, QStringLiteral("Null reply"));
56         return;
57     }
58     connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished()));
59     connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
60             this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
61     connect(this, &QGeoTiledMapReply::aborted, reply, &QNetworkReply::abort);
62     connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
63 }
64 
~GeoTiledMapReplyEsri()65 GeoTiledMapReplyEsri::~GeoTiledMapReplyEsri()
66 {
67 }
68 
networkReplyFinished()69 void GeoTiledMapReplyEsri::networkReplyFinished()
70 {
71     QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
72     reply->deleteLater();
73 
74     if (reply->error() != QNetworkReply::NoError)
75         return;
76 
77     QByteArray const& imageData = reply->readAll();
78 
79     bool validFormat = true;
80     if (imageData.startsWith(reinterpret_cast<const char*>(pngSignature)))
81         setMapImageFormat(QStringLiteral("png"));
82     else if (imageData.startsWith(reinterpret_cast<const char*>(jpegSignature)))
83         setMapImageFormat(QStringLiteral("jpg"));
84     else if (imageData.startsWith(reinterpret_cast<const char*>(gifSignature)))
85         setMapImageFormat(QStringLiteral("gif"));
86     else
87         validFormat = false;
88 
89     if (validFormat)
90         setMapImageData(imageData);
91 
92     setFinished(true);
93 }
94 
networkReplyError(QNetworkReply::NetworkError error)95 void GeoTiledMapReplyEsri::networkReplyError(QNetworkReply::NetworkError error)
96 {
97     QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
98     reply->deleteLater();
99     if (error == QNetworkReply::OperationCanceledError)
100         setFinished(true);
101     else
102         setError(QGeoTiledMapReply::CommunicationError, reply->errorString());
103 }
104 
105 QT_END_NAMESPACE
106