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

# Cppcheck report - digiKam-nl-search-more-criteria-rev-e2466e0973: ../../core/libs/widgets/text/donlinetranslator_tts.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
    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
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2018-02-22
     * Description : A text translator using web-services - TTS support.
     *
     * SPDX-FileCopyrightText: 2018-2022 by Hennadii Chernyshchyk <genaloner at gmail dot com>
     * SPDX-FileCopyrightText: 2021-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
     *
     * SPDX-License-Identifier: GPL-2.0-or-later
     *
     * ============================================================ */

    #include "donlinetranslator_p.h"

    namespace Digikam
    {

    DOnlineTts::DOnlineTts(QObject* const parent)
        : QObject(parent),
          d      (new Private)
    {
    }

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

    void DOnlineTts::generateUrls(const QString& text,
                                  DOnlineTranslator::Engine engine,
                                  DOnlineTranslator::Language lang,
                                  Voice voice,<--- Shadow argument
                                  Emotion emotion)<--- Shadow argument
    {
        // Get speech

        QString unparsedText = text;

        switch (engine)
        {
            case DOnlineTranslator::Google:
            {
                if (voice != NoVoice)
                {
                    setError(UnsupportedVoice, i18nc("@info", "Selected engine %1 does not support voice settings",
                                                     QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Engine>().valueToKey(engine))));
                    return;
                }

                if (emotion != NoEmotion)
                {
                    setError(UnsupportedEmotion, i18nc("@info", "Selected engine %1 does not support emotion settings",
                                                       QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Engine>().valueToKey(engine))));
                    return;
                }

                const QString langString = languageApiCode(engine, lang);

                if (langString.isNull())
                {
                    return;
                }

                // Google has a limit of characters per tts request. If the query is larger, then it should be split into several

                while (!unparsedText.isEmpty())
                {
                    const int splitIndex = DOnlineTranslator::getSplitIndex(unparsedText, d->s_googleTtsLimit); // Split the part by special symbol

                    // Generate URL API for adding it to the playlist

                    QUrl apiUrl(QStringLiteral("https://translate.googleapis.com/translate_tts"));
                    const QString query = QStringLiteral("ie=UTF-8&client=gtx&tl=%1&q=%2")
                                        .arg(langString, QString::fromUtf8(QUrl::toPercentEncoding(unparsedText.left(splitIndex))));

    #if defined(Q_OS_LINUX)

                    apiUrl.setQuery(query);

    #elif defined(Q_OS_WIN)

                    apiUrl.setQuery(query, QUrl::DecodedMode);

    #endif
                    d->media.append(apiUrl);

                    // Remove the said part from the next saying

                    unparsedText = unparsedText.mid(splitIndex);
                }

                break;
            }

            case DOnlineTranslator::Yandex:
            {
                const QString langString = languageApiCode(engine, lang);

                if (langString.isNull())
                {
                    return;
                }

                const QString voiceString = voiceApiCode(engine, voice);

                if (voiceString.isNull())
                {
                    return;
                }

                const QString emotionString = emotionApiCode(engine, emotion);

                if (emotionString.isNull())
                {
                    return;
                }

                // Yandex has a limit of characters per tts request. If the query is larger, then it should be split into several

                while (!unparsedText.isEmpty())
                {
                    const int splitIndex = DOnlineTranslator::getSplitIndex(unparsedText, d->s_yandexTtsLimit); // Split the part by special symbol

                    // Generate URL API for adding to the playlist
                    QUrl apiUrl(QStringLiteral("https://tts.voicetech.yandex.net/tts"));
                    const QString query = QStringLiteral("text=%1&lang=%2&speaker=%3&emotion=%4&format=mp3")
                                            .arg(QString::fromUtf8(QUrl::toPercentEncoding(unparsedText.left(splitIndex))), langString, voiceString, emotionString);

    #if defined(Q_OS_LINUX)

                    apiUrl.setQuery(query);

    #elif defined(Q_OS_WIN)

                    apiUrl.setQuery(query, QUrl::DecodedMode);

    #endif

                    d->media.append(apiUrl);

                    // Remove the said part from the next saying

                    unparsedText = unparsedText.mid(splitIndex);
                }

                break;
            }

            case DOnlineTranslator::Bing:
            case DOnlineTranslator::LibreTranslate:
            case DOnlineTranslator::Lingva:
            {
                // NOTE:
                // Lingva returns audio in strange format, use placeholder, until we'll figure it out
                //
                // Example: https://lingva.ml/api/v1/audio/en/Hello%20World!
                // Will return json with uint bytes array, according to documentation
                // See: https://github.com/TheDavidDelta/lingva-translate#public-apis
                setError(UnsupportedEngine, i18nc("@info", "%1 engine does not support TTS",
                                                  QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Engine>().valueToKey(engine))));
                break;
            }
        }
    }

    QList<QUrl> DOnlineTts::media() const
    {
        return d->media;
    }

    QString DOnlineTts::errorString() const
    {
        return d->errorString;
    }

    DOnlineTts::TtsError DOnlineTts::error() const
    {
        return d->error;
    }

    QString DOnlineTts::voiceCode(Voice voice)<--- Shadow argument
    {
        return Private::s_voiceCodes.value(voice);
    }

    QString DOnlineTts::emotionCode(Emotion emotion)<--- Shadow argument
    {
        return Private::s_emotionCodes.value(emotion);
    }

    DOnlineTts::Emotion DOnlineTts::emotion(const QString& emotionCode)<--- Shadow argument
    {
        return Private::s_emotionCodes.key(emotionCode, NoEmotion);
    }

    DOnlineTts::Voice DOnlineTts::voice(const QString& voiceCode)<--- Shadow argument
    {
        return Private::s_voiceCodes.key(voiceCode, NoVoice);
    }

    void DOnlineTts::setError(TtsError error, const QString& errorString)<--- Shadow argument<--- Shadow argument
    {
        d->error       = error;
        d->errorString = errorString;
    }

    // Returns engine-specific language code for tts
    QString DOnlineTts::languageApiCode(DOnlineTranslator::Engine engine, DOnlineTranslator::Language lang)
    {
        switch (engine)
        {
            case DOnlineTranslator::Google:
            case DOnlineTranslator::Lingva: // Lingva is a frontend to Google Translate
            {
                if (lang != DOnlineTranslator::Auto)
                {
                    return DOnlineTranslator::languageApiCode(engine, lang); // Google use the same codes for tts (except 'auto')
                }

                break;
            }

            case DOnlineTranslator::Yandex:
            {
                switch (lang)
                {
                    case DOnlineTranslator::Russian:
                    {
                        return QStringLiteral("ru_RU");
                    }

                    case DOnlineTranslator::Tatar:
                    {
                        return QStringLiteral("tr_TR");
                    }

                    case DOnlineTranslator::English:
                    {
                        return QStringLiteral("en_GB");
                    }

                    default:
                    {
                        break;
                    }
                }

                break;
            }

            default:
            {
                break;
            }
        }

        setError(UnsupportedLanguage, i18nc("@info", "Selected language %1 is not supported for %2",
                                            QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Language>().valueToKey(lang)),
                                            QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Engine>().valueToKey(engine))));

        return QString();
    }

    QString DOnlineTts::voiceApiCode(DOnlineTranslator::Engine engine, Voice voice)<--- Shadow argument
    {
        if (engine == DOnlineTranslator::Yandex)
        {
            if (voice == NoVoice)
            {
                return voiceCode(Zahar);
            }

            return voiceCode(voice);
        }

        setError(UnsupportedVoice, i18nc("@info", "Selected voice %1 is not supported for %2",
                                         QString::fromUtf8(QMetaEnum::fromType<Voice>().valueToKey(voice)),
                                         QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Engine>().valueToKey(engine))));

        return QString();
    }

    QString DOnlineTts::emotionApiCode(DOnlineTranslator::Engine engine, Emotion emotion)<--- Shadow argument
    {
        if (engine == DOnlineTranslator::Yandex)
        {
            if (emotion == NoEmotion)
            {
                return emotionCode(Neutral);
            }

            return emotionCode(emotion);
        }

        setError(UnsupportedEmotion, i18nc("@info", "Selected emotion %1 is not supported for %2",
                                           QString::fromUtf8(QMetaEnum::fromType<Emotion>().valueToKey(emotion)),
                                           QString::fromUtf8(QMetaEnum::fromType<DOnlineTranslator::Engine>().valueToKey(engine))));

        return QString();
    }

    } // namespace Digikam