1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36
37 #include "qgeofiletilecachemapbox.h"
38 #include <QtLocation/private/qgeotilespec_p.h>
39 #include <QDir>
40
41 QT_BEGIN_NAMESPACE
42
QGeoFileTileCacheMapbox(const QList<QGeoMapType> & mapTypes,int scaleFactor,const QString & directory,QObject * parent)43 QGeoFileTileCacheMapbox::QGeoFileTileCacheMapbox(const QList<QGeoMapType> &mapTypes, int scaleFactor, const QString &directory, QObject *parent)
44 :QGeoFileTileCache(directory, parent), m_mapTypes(mapTypes)
45 {
46 m_scaleFactor = qBound(1, scaleFactor, 2);
47 for (int i=0; i < mapTypes.size(); i++)
48 m_mapNameToId.insert(mapTypes[i].name(), i+1);
49 }
50
~QGeoFileTileCacheMapbox()51 QGeoFileTileCacheMapbox::~QGeoFileTileCacheMapbox()
52 {
53
54 }
55
tileSpecToFilename(const QGeoTileSpec & spec,const QString & format,const QString & directory) const56 QString QGeoFileTileCacheMapbox::tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory) const
57 {
58 QString filename = spec.plugin();
59 filename += QLatin1String("-");
60 filename += m_mapTypes[spec.mapId()-1].name();
61 filename += QLatin1String("-");
62 filename += QString::number(spec.zoom());
63 filename += QLatin1String("-");
64 filename += QString::number(spec.x());
65 filename += QLatin1String("-");
66 filename += QString::number(spec.y());
67
68 //Append version if real version number to ensure backwards compatibility and eviction of old tiles
69 if (spec.version() != -1) {
70 filename += QLatin1String("-");
71 filename += QString::number(spec.version());
72 }
73
74 filename += QLatin1String("-@");
75 filename += QString::number(m_scaleFactor);
76 filename += QLatin1Char('x');
77
78 filename += QLatin1String(".");
79 filename += format;
80
81 QDir dir = QDir(directory);
82
83 return dir.filePath(filename);
84 }
85
filenameToTileSpec(const QString & filename) const86 QGeoTileSpec QGeoFileTileCacheMapbox::filenameToTileSpec(const QString &filename) const
87 {
88 QStringList parts = filename.split('.');
89
90 if (parts.length() != 3) // 3 because the map name has always a dot in it.
91 return QGeoTileSpec();
92
93 QString name = parts.at(0) + QChar('.') + parts.at(1);
94 QStringList fields = name.split('-');
95
96 int length = fields.length();
97 if (length != 6 && length != 7) {
98 return QGeoTileSpec();
99 } else {
100 int scaleIdx = fields.last().indexOf("@");
101 if (scaleIdx < 0 || fields.last().size() <= (scaleIdx + 2))
102 return QGeoTileSpec();
103 int scaleFactor = fields.last()[scaleIdx + 1].digitValue();
104 if (scaleFactor != m_scaleFactor)
105 return QGeoTileSpec();
106 }
107
108 QList<int> numbers;
109
110 bool ok = false;
111 for (int i = 2; i < length-1; ++i) { // skipping -@_X
112 ok = false;
113 int value = fields.at(i).toInt(&ok);
114 if (!ok)
115 return QGeoTileSpec();
116 numbers.append(value);
117 }
118
119 //File name without version, append default
120 if (numbers.length() < 4)
121 numbers.append(-1);
122
123 return QGeoTileSpec(fields.at(0),
124 m_mapNameToId[fields.at(1)],
125 numbers.at(0),
126 numbers.at(1),
127 numbers.at(2),
128 numbers.at(3));
129 }
130
131 QT_END_NAMESPACE
132