Cppcheck - HTML report - digiKam-master-rev-960d3ce42d

# Cppcheck report - digiKam-master-rev-960d3ce42d: ../../core/utilities/geolocation/engine/runners/ReverseGeocodingRunnerManager.cpp

      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
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2023-05-15
     * Description : geolocation engine based on Marble.
     *               (c) 2007-2022 Marble Team
     *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
     *
     * SPDX-FileCopyrightText: 2023-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
     *
     * SPDX-License-Identifier: LGPL-2.1-or-later
     *
     * ============================================================ */

    #include "ReverseGeocodingRunnerManager.h"

    // Qt includes

    #include <QList>
    #include <QThreadPool>
    #include <QTimer>

    // Local includes

    #include "MarbleModel.h"
    #include "GeoDataCoordinates.h"
    #include "GeoDataPlacemark.h"
    #include "Planet.h"
    #include "PluginManager.h"
    #include "ReverseGeocodingRunnerPlugin.h"
    #include "RunnerTask.h"
    #include "digikam_debug.h"

    namespace Marble
    {

    class Q_DECL_HIDDEN ReverseGeocodingRunnerManager::Private
    {
    public:

        Private(ReverseGeocodingRunnerManager* parent, const MarbleModel* marbleModel);

        QList<const ReverseGeocodingRunnerPlugin*> plugins(const QList<const ReverseGeocodingRunnerPlugin*>& plugins) const;<--- Shadowed function

        void addReverseGeocodingResult(const GeoDataCoordinates& coordinates, const GeoDataPlacemark& placemark);
        void cleanupReverseGeocodingTask(ReverseGeocodingTask* task);

    public:

        ReverseGeocodingRunnerManager* const q               = nullptr;
        const MarbleModel* const             m_marbleModel   = nullptr;
        const PluginManager*                 m_pluginManager = nullptr;
        QList<ReverseGeocodingTask*>         m_reverseTasks;
        QVector<GeoDataCoordinates>          m_reverseGeocodingResults;
        QString                              m_reverseGeocodingResult;
    };

    ReverseGeocodingRunnerManager::Private::Private(ReverseGeocodingRunnerManager* parent, const MarbleModel* marbleModel) :
        q(parent),
        m_marbleModel(marbleModel),
        m_pluginManager(marbleModel->pluginManager())
    {
        qRegisterMetaType<GeoDataPlacemark>("GeoDataPlacemark");
        qRegisterMetaType<GeoDataCoordinates>("GeoDataCoordinates");
    }

    QList<const ReverseGeocodingRunnerPlugin*> ReverseGeocodingRunnerManager::Private::plugins(const QList<const ReverseGeocodingRunnerPlugin*>& plugins) const
    {
        QList<const ReverseGeocodingRunnerPlugin*> result;

        for (const ReverseGeocodingRunnerPlugin* plugin : plugins)
        {
            if ((m_marbleModel && m_marbleModel->workOffline() && !plugin->canWorkOffline()))
            {
                continue;
            }

            if (!plugin->canWork())
            {
                continue;
            }

            if (m_marbleModel && !plugin->supportsCelestialBody(m_marbleModel->planet()->id()))
            {
                continue;
            }

            result << plugin;
        }

        return result;
    }

    void ReverseGeocodingRunnerManager::Private::addReverseGeocodingResult(const GeoDataCoordinates& coordinates, const GeoDataPlacemark& placemark)
    {
        if (!m_reverseGeocodingResults.contains(coordinates) && !placemark.address().isEmpty())
        {
            m_reverseGeocodingResults.push_back(coordinates);
            m_reverseGeocodingResult = placemark.address();
            Q_EMIT q->reverseGeocodingFinished(coordinates, placemark);
        }

        if (m_reverseTasks.isEmpty())
        {
            Q_EMIT q->reverseGeocodingFinished();
        }
    }

    void ReverseGeocodingRunnerManager::Private::cleanupReverseGeocodingTask(ReverseGeocodingTask* task)
    {
        m_reverseTasks.removeAll(task);
        qCDebug(DIGIKAM_GEOENGINE_LOG) << "removing task " << m_reverseTasks.size() << " " << (quintptr)task;

        if (m_reverseTasks.isEmpty())
        {
            Q_EMIT q->reverseGeocodingFinished();
        }
    }

    ReverseGeocodingRunnerManager::ReverseGeocodingRunnerManager(const MarbleModel* marbleModel, QObject* parent) :
        QObject(parent),
        d(new Private(this, marbleModel))
    {
        if (QThreadPool::globalInstance()->maxThreadCount() < 4)
        {
            QThreadPool::globalInstance()->setMaxThreadCount(4);
        }
    }

    ReverseGeocodingRunnerManager::~ReverseGeocodingRunnerManager()
    {
        delete d;
    }

    void ReverseGeocodingRunnerManager::reverseGeocoding(const GeoDataCoordinates& coordinates)
    {
        d->m_reverseTasks.clear();
        d->m_reverseGeocodingResult.clear();
        d->m_reverseGeocodingResults.removeAll(coordinates);

        QList<const ReverseGeocodingRunnerPlugin*> plugins = d->plugins(d->m_pluginManager->reverseGeocodingRunnerPlugins());<--- Shadow local variable

        for (const ReverseGeocodingRunnerPlugin* plugin : plugins)
        {
            ReverseGeocodingTask* task = new ReverseGeocodingTask(plugin->newRunner(), this, d->m_marbleModel, coordinates);
            connect(task, SIGNAL(finished(ReverseGeocodingTask*)), this, SLOT(cleanupReverseGeocodingTask(ReverseGeocodingTask*)));
            qCDebug(DIGIKAM_GEOENGINE_LOG) << "reverse task " << plugin->nameId() << " " << (quintptr)task;
            d->m_reverseTasks << task;
        }

        for (ReverseGeocodingTask* task : d->m_reverseTasks)
        {
            QThreadPool::globalInstance()->start(task);
        }

        if (plugins.isEmpty())
        {
            GeoDataPlacemark anonymous;
            anonymous.setCoordinate(coordinates);
            Q_EMIT reverseGeocodingFinished(coordinates, anonymous);
            d->cleanupReverseGeocodingTask(nullptr);
        }
    }

    QString ReverseGeocodingRunnerManager::searchReverseGeocoding(const GeoDataCoordinates& coordinates, int timeout)
    {
        QEventLoop localEventLoop;
        QTimer watchdog;
        watchdog.setSingleShot(true);
        connect(&watchdog, SIGNAL(timeout()),
                &localEventLoop, SLOT(quit()));
        connect(this, SIGNAL(reverseGeocodingFinished()),
                &localEventLoop, SLOT(quit()), Qt::QueuedConnection);

        watchdog.start(timeout);
        reverseGeocoding(coordinates);
        localEventLoop.exec();
        return d->m_reverseGeocodingResult;
    }

    } // namespace Marble

    #include "moc_ReverseGeocodingRunnerManager.cpp"