File indexing completed on 2024-05-19 15:58:37

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "autotranslatelanguages.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 
0012 AutotranslateLanguages::AutotranslateLanguages() = default;
0013 
0014 QVector<AutotranslateLanguage> AutotranslateLanguages::autotranslateLanguage() const
0015 {
0016     return mAutotranslateLanguages;
0017 }
0018 
0019 void AutotranslateLanguages::setAutotranslateLanguages(const QVector<AutotranslateLanguage> &autotranslateLanguage)
0020 {
0021     mAutotranslateLanguages = autotranslateLanguage;
0022 }
0023 
0024 bool AutotranslateLanguages::isEmpty() const
0025 {
0026     return mAutotranslateLanguages.isEmpty();
0027 }
0028 
0029 int AutotranslateLanguages::count() const
0030 {
0031     return mAutotranslateLanguages.count();
0032 }
0033 
0034 void AutotranslateLanguages::clear()
0035 {
0036     mAutotranslateLanguages.clear();
0037 }
0038 
0039 AutotranslateLanguage AutotranslateLanguages::at(int index) const
0040 {
0041     if (index < 0 || index > mAutotranslateLanguages.count()) {
0042         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0043         return {};
0044     }
0045     return mAutotranslateLanguages.at(index);
0046 }
0047 
0048 void AutotranslateLanguages::parseLanguages(const QJsonObject &obj)
0049 {
0050     clear();
0051     // qDebug() << " obj " << obj;
0052     const QJsonArray array = obj[QLatin1String("languages")].toArray();
0053     for (const QJsonValue &current : array) {
0054         const QJsonObject languageObject = current.toObject();
0055         AutotranslateLanguage lang;
0056         lang.setLanguage(languageObject.value(QLatin1String("language")).toString());
0057         lang.setDisplayLanguage(languageObject.value(QLatin1String("name")).toString());
0058         mAutotranslateLanguages.append(std::move(lang));
0059     }
0060 }
0061 
0062 QDebug operator<<(QDebug d, const AutotranslateLanguages &t)
0063 {
0064     const QVector<AutotranslateLanguage> autotranslateLanguages = t.autotranslateLanguage();
0065     for (const AutotranslateLanguage &lang : autotranslateLanguages) {
0066         d << "autotranslate :" << lang << "\n";
0067     }
0068     return d;
0069 }