summaryrefslogtreecommitdiff
path: root/src/plugins/geoservices/places_jsondb/iconhandler.cpp
blob: d324545400299212eeafe3fe735e419a65b9c1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "iconhandler.h"
#include "idreply.h"

#include <QtCore/QFile>
#include <QtCore/QUrl>
#include <QtCore/QVariantMap>

IconHandler::IconHandler(const QPlaceIcon &inputIcon, const QJsonObject &thumbnailsJson,
                         IdReply *parent)
    : QObject(parent), m_inputIcon(inputIcon), m_thumbnailsJson(thumbnailsJson),
      m_error(QPlaceReply::NoError), m_currIconIndex(0), m_reply(parent)
{
    QStringList prefixes;
    prefixes << QStringLiteral("small") << QStringLiteral("medium") << QStringLiteral("large") << QStringLiteral("fullscreen");

    QList<QUrl> uniqueInputUrls; //unique source urls that have been supplied without associated destinations
    foreach (const QString &prefix, prefixes) {
        bool ok;
        QUrl sourceUrl = convertToUrl(m_inputIcon.parameters().value(prefix + QLatin1String("SourceUrl"), QUrl()), &ok);
        if (!ok) {
            triggerDone(QPlaceReply::BadArgumentError, QString::fromLatin1("icon parameter for key: ") + prefix + QLatin1String("SourceUrl")
                        + QLatin1String(" was not a QUrl object"));
            return;
        }
        QUrl destinationUrl = convertToUrl(m_inputIcon.parameters().value(prefix + QLatin1String("Url"), QUrl()), &ok);
        if (!ok) {
            triggerDone(QPlaceReply::BadArgumentError, QString::fromLatin1("icon parameter for key: ") + prefix + QLatin1String("Url")
                        + QLatin1String(" was not a QUrl object"));
            return;
        }

        if (destinationUrl.isEmpty()) {
            if (sourceUrl.isEmpty() || uniqueInputUrls.contains(sourceUrl))
                continue;
            else
                uniqueInputUrls.append(sourceUrl);
        }

        QString destination;
        if (!destinationUrl.isEmpty())
            destination = prefix + QLatin1String("Url");

        JsonDbIcon *icon = new JsonDbIcon(this);
        icon->setSourceUrl(sourceUrl);
        icon->setDestinationUrl(destinationUrl);
        icon->setDestination(destination);
        if (m_inputIcon.parameters().contains(prefix + QLatin1String("Size")))
            icon->setSpecifiedSize(m_inputIcon.parameters().value(prefix + QLatin1String("Size")).toSize());

        m_icons.append(icon);
    }

    processIcons();
}

IconHandler::~IconHandler()
{
    qDeleteAll(m_icons);
}

QString IconHandler::errorString() const
{
    return m_errorString;
}

QPlaceReply::Error IconHandler::error() const
{
    return m_error;
}

QNetworkAccessManager *IconHandler::networkAccessManager()
{
    return m_reply->engine()->networkAccessManager();
}

void IconHandler::processIcons()
{
    if (JsonDbIcon *senderIcon = qobject_cast<JsonDbIcon *>(sender())) {
        if (senderIcon->error() != QPlaceReply::NoError) {
            triggerDone(senderIcon->error(), senderIcon->errorString());
            return;
        }
    }

    if (m_currIconIndex < m_icons.count()) {
        JsonDbIcon *icon = m_icons.at(m_currIconIndex);
        connect(icon, SIGNAL(initializationFinished()), this, SLOT(processIcons()), Qt::QueuedConnection);
        icon->initialize();
        m_currIconIndex++;
        return;
    } else {
        bool error = false;

        //try to set destinations for icons which were not already set
        QStringList specifiedDestinations;
        foreach (JsonDbIcon *icon, m_icons) {
            if (!icon->destination().isEmpty())
                specifiedDestinations.append(icon->destination());
        }

        //try to set small,medium and large destinations if they haven't already been explicitly specified
        //and there are icons with unspecified destinations. (essentially we are creating data urls if necessary)
        if (!specifiedDestinations.contains(JsonDbIcon::SmallDestination))
            trySetDestination(JsonDbIcon::SmallDestination);

        if (!specifiedDestinations.contains(JsonDbIcon::MediumDestination))
            trySetDestination(JsonDbIcon::MediumDestination);

        if (!specifiedDestinations.contains(JsonDbIcon::LargeDestination))
            trySetDestination(JsonDbIcon::LargeDestination);

        //Note that we don't try and set the destination for full screen thumbnails
        //since data urls are meant to be just for small images

        //if we have an existing place, we try to preserve existing properties
        //of the thumbnail objects, but we completely remove/replace the fields
        //that are relevant for the QtLocation API.
        m_thumbnailsJson.remove(JsonDb::Small);
        m_thumbnailsJson.remove(JsonDb::Medium);
        m_thumbnailsJson.remove(JsonDb::Large);
        m_thumbnailsJson.remove(JsonDb::Fullscreen);
        m_thumbnailsJson.remove(JsonDb::NokiaIcon);

        foreach (JsonDbIcon *icon, m_icons) {
            QJsonObject thumbnailJson;
            if (icon->error() != QPlaceReply::NoError) {
                triggerDone(icon->error(), icon->errorString());
                error = true;
                break;
            }

            if (!icon->sourceUrl().isEmpty()
                    && icon->destinationUrl().scheme().compare(QLatin1String("file")) == 0) {
                if (!icon->copy()) {
                    triggerDone(icon->error(), icon->errorString());
                    error = true;
                    break;
                }
            }

            thumbnailJson.insert(JsonDb::Url, icon->destinationUrl().toString());
            if (icon->size().isValid()) {
                thumbnailJson.insert(JsonDb::Height, icon->size().height());
                thumbnailJson.insert(JsonDb::Width, icon->size().width());
            } else {
                //size of icon could not be calculated, therefore rely on manually specified size
                if (!icon->specifiedSize().isValid()) {
                    triggerDone(QPlaceReply::BadArgumentError, QLatin1String("Size of icon could not be generated nor was it validly specified"));
                    error = true;
                    break;
                }
                thumbnailJson.insert(JsonDb::Height, icon->specifiedSize().height());
                thumbnailJson.insert(JsonDb::Width, icon->specifiedSize().width());
            }

            if (icon->destination() == JsonDbIcon::SmallDestination)
                m_thumbnailsJson.insert(JsonDb::Small, thumbnailJson);
            else if (icon->destination() == JsonDbIcon::MediumDestination)
                m_thumbnailsJson.insert(JsonDb::Medium, thumbnailJson);
            else if (icon->destination() == JsonDbIcon::LargeDestination)
                m_thumbnailsJson.insert(JsonDb::Large, thumbnailJson);
            else
                m_thumbnailsJson.insert(JsonDb::Fullscreen, thumbnailJson);
        }

        QString nokiaIcon = m_inputIcon.parameters().value(JsonDbIcon::NokiaIcon).toString();
        bool nokiaIconGenerated = m_inputIcon.parameters()
                                        .value(JsonDbIcon::NokiaIconGenerated).toBool();
        if (!nokiaIcon.isEmpty() && !nokiaIconGenerated) {
            QString localIconPath = m_reply->engine()->localDataPath() + nokiaIcon;
            if (QFile::exists(localIconPath))
                m_thumbnailsJson.insert(JsonDb::NokiaIcon, nokiaIcon);
        }

        if (!error) {
            QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection,
                                      Q_ARG(QJsonObject,m_thumbnailsJson));
        }

        qDeleteAll(m_icons);
        m_icons.clear();
    }
}

void IconHandler::trySetDestination(const QString &destination)
{
    static int threshold;
    int height;

    //assumption is that icons are squarish
    //so we can rely on height as a means to detect which size bucket
    //the icon belongs to
    if (destination == JsonDbIcon::SmallDestination) {
        threshold = (JsonDbIcon::SmallSize.height() + JsonDbIcon::MediumSize.height()) / 2;
        height = JsonDbIcon::SmallSize.height();
    } else if (destination == JsonDbIcon::MediumDestination) {
        threshold = (JsonDbIcon::MediumSize.height() + JsonDbIcon::LargeSize.height()) / 2;
        height = JsonDbIcon::MediumSize.height();
    } else if (destination == JsonDbIcon::LargeDestination) {
        threshold = JsonDbIcon::LargeSize.height() * 2;
        height = JsonDbIcon::LargeSize.height();
    } //note fullscreen thumbnails should not be set as data urls.

    JsonDbIcon *currIcon = 0;
    foreach (JsonDbIcon *icon, m_icons) {
        if (icon->destination().isEmpty()
                && icon->size().height() < threshold
                && (currIcon == 0 ||  qAbs(icon->size().height() - height) < qAbs(currIcon->size().height() - height))) {
            if (currIcon)
                currIcon->setDestination(QString());
            currIcon = icon;
            currIcon->setDestination(destination);
        }
    }

    if (currIcon)
        currIcon->setDestinationDataUrl();
}

void IconHandler::triggerDone(const QPlaceReply::Error error, const QString &errorString)
{
    m_error = error;
    m_errorString = errorString;
    QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection,
                              Q_ARG(QJsonObject,m_thumbnailsJson));
}

QUrl IconHandler::convertToUrl(const QVariant &var, bool *ok)
{
    if (ok)
        *ok = false;

    switch (var.type()) {
    case (QVariant::Url): {
        if (ok)
            *ok = true;
        QUrl url = var.toUrl();
        if (url.scheme().isEmpty())
            return QUrl::fromUserInput(url.toString());
        else
            return url;
        break;
    }
    default:
        break;
    }
    return QUrl();
}