Cppcheck - HTML report - digiKam-nl-search-more-criteria-rev-e2466e0973

# Cppcheck report - digiKam-nl-search-more-criteria-rev-e2466e0973: ../../core/utilities/import/views/cameranamehelper.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
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2009-08-01
     * Description : camera name helper class
     *
     * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
     *
     * SPDX-License-Identifier: GPL-2.0-or-later
     *
     * ============================================================ */

    #include "cameranamehelper.h"

    // Qt includes

    #include <QRegularExpression>

    // KDE includes

    #include <klocalizedstring.h>

    namespace
    {
    static const KLocalizedString STR_AUTO_DETECTED(ki18nc("in camera model string", "auto-detected"));

    /*
     * The constructors used here are written after many experiments.
     * REGEXP_MODES expression was failing to give exact match results if the pattern option
     * InvertedGreedinessOption was set later in extractCameraNameToken function with QRegularExpression::setPatternOption()
     * Maybe a bug in Qt.
     * */
    static QRegularExpression REGEXP_CAMERA_NAME(
        QRegularExpression::anchoredPattern(QLatin1String("(.*)\\s*\\((.*)\\)\\s*")),
        QRegularExpression::InvertedGreedinessOption | QRegularExpression::CaseInsensitiveOption);

    static QRegularExpression REGEXP_MODES(
        QRegularExpression::anchoredPattern(QLatin1String("(ptp|normal|mtp)(\\s+mode)?")),
        QRegularExpression::InvertedGreedinessOption | QRegularExpression::CaseInsensitiveOption);
    }

    namespace Digikam
    {

    QString CameraNameHelper::createCameraName(const QString& vendor, const QString& product,
                                               const QString& mode,   bool autoDetected)
    {
        if (vendor.isEmpty())
        {
            return QString();
        }

        QString tmp;
        QString _vendor  = vendor.simplified();
        QString _product = product.simplified();
        QString _mode    = mode.simplified().remove(QLatin1Char('(')).remove(QLatin1Char(')'));
        tmp              = QString::fromUtf8("%1 %2").arg(_vendor).arg(_product);

        if      (!mode.isEmpty() && mode != STR_AUTO_DETECTED.toString())
        {
            tmp.append(QLatin1String(" ("));
            tmp.append(_mode);
            tmp.append(autoDetected ? QString::fromUtf8(", %1)").arg(STR_AUTO_DETECTED.toString())
                                    : QLatin1String(")"));
        }
        else if (autoDetected)
        {
            tmp.append(QString::fromUtf8(" (%1)").arg(STR_AUTO_DETECTED.toString()));
        }

        return tmp.simplified();
    }

    QString CameraNameHelper::cameraName(const QString& name)
    {
        return parseAndFormatCameraName(name, false, false);
    }

    QString CameraNameHelper::cameraNameAutoDetected(const QString& name)
    {
        return parseAndFormatCameraName(name, true, true);
    }

    QString CameraNameHelper::parseAndFormatCameraName(const QString& cameraName,<--- Shadow argument
                                                       bool parseMode, bool autoDetected)
    {
        QString vendorAndProduct = extractCameraNameToken(cameraName, VendorAndProduct);

        if (vendorAndProduct.isEmpty())
        {
            return QString();
        }

        QString mode = parseMode ? extractCameraNameToken(cameraName, Mode)
                                 : QString();

        QString tmp = createCameraName(vendorAndProduct, QString(), mode, autoDetected);

        return (tmp.isEmpty() ? cameraName.simplified()
                              : tmp);
    }

    QString CameraNameHelper::extractCameraNameToken(const QString& cameraName, Token tokenID)<--- Shadow argument
    {
        static QRegularExpression REGEXP_AUTODETECTED(QString::fromUtf8("(%1|, %1)").arg(STR_AUTO_DETECTED.toString()));
        REGEXP_AUTODETECTED.setPatternOptions(QRegularExpression::InvertedGreedinessOption);

        QRegularExpressionMatch expMatch = REGEXP_CAMERA_NAME.match(cameraName.simplified());

        if (expMatch.hasMatch())
        {
            QString vendorProduct  = expMatch.captured(1).simplified();
            QString tmpMode        = expMatch.captured(2).simplified();
            QString clearedTmpMode = tmpMode;
            QString mode;
            clearedTmpMode.remove(REGEXP_AUTODETECTED);

            if (!tmpMode.isEmpty() && clearedTmpMode.isEmpty())
            {
                mode = tmpMode;
            }
            else
            {
                mode = REGEXP_MODES.match(clearedTmpMode).hasMatch() ? clearedTmpMode
                                                               : QLatin1String("");
            }

            if (tokenID == VendorAndProduct)
            {
                return (mode.isEmpty() ? cameraName.simplified()
                                       : vendorProduct);
            }
            else
            {
                return mode;
            }
        }

        return ((tokenID == VendorAndProduct) ? cameraName.simplified()
                                              : QLatin1String(""));
    }

    bool CameraNameHelper::sameDevices(const QString& deviceA, const QString& deviceB)
    {
        if (deviceA.isEmpty() || deviceB.isEmpty())
        {
            return false;
        }

        if (deviceA == deviceB)
        {
            return true;
        }

        // We need to parse the names a little bit. First check if the vendor and name match

        QString vendorAndProductA = extractCameraNameToken(deviceA, VendorAndProduct);
        QString vendorAndProductB = extractCameraNameToken(deviceB, VendorAndProduct);
        QString cameraNameA       = createCameraName(vendorAndProductA);
        QString cameraNameB       = createCameraName(vendorAndProductB);

        // try to clean up the string, if not possible, return false

        if (cameraNameA != cameraNameB)
        {
            return false;
        }

        // is the extracted mode known and equal?

        QString modeA                 = extractCameraNameToken(deviceA, Mode);
        QString modeB                 = extractCameraNameToken(deviceB, Mode);
        QRegularExpressionMatch match = REGEXP_MODES.match(modeA);
        bool isModeAValid             = match.hasMatch();

        modeA                         = isModeAValid ? match.captured(1).simplified().toLower()
                                                     : QLatin1String("");

        match                         = REGEXP_MODES.match(modeB);
        bool isModeBValid             = match.hasMatch();

        modeB                         = isModeBValid ? match.captured(1).simplified().toLower()
                                                     : QLatin1String("");

        if ((isModeAValid != isModeBValid) || (modeA != modeB))
        {
            return false;
        }

        return true;
    }

    } // namespace Digikam