File indexing completed on 2024-05-12 16:25:36

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "commands.h"
0008 #include "downloadappslanguages/downloadappslanguagesmanager.h"
0009 #include "ruqola_commands_debug.h"
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 
0013 Commands::Commands() = default;
0014 
0015 bool Commands::isEmpty() const
0016 {
0017     return mCommands.isEmpty();
0018 }
0019 
0020 void Commands::clear()
0021 {
0022     mCommands.clear();
0023 }
0024 
0025 int Commands::count() const
0026 {
0027     return mCommands.count();
0028 }
0029 
0030 Command Commands::at(int index) const
0031 {
0032     if (index < 0 || index > mCommands.count()) {
0033         qCWarning(RUQOLA_COMMANDS_LOG) << "Invalid index " << index;
0034         return {};
0035     }
0036     return mCommands.at(index);
0037 }
0038 
0039 void Commands::parseMoreCommands(const QJsonObject &commandsObj)
0040 {
0041     const int commandsCount = commandsObj[QLatin1String("count")].toInt();
0042     mOffset = commandsObj[QLatin1String("offset")].toInt();
0043     mTotal = commandsObj[QLatin1String("total")].toInt();
0044     parseListCommands(commandsObj);
0045     mCommandsCount += commandsCount;
0046 }
0047 
0048 void Commands::parseListCommands(const QJsonObject &commandsObj)
0049 {
0050     const QJsonArray commandsArray = commandsObj[QLatin1String("commands")].toArray();
0051     mCommands.reserve(mCommands.count() + commandsArray.count());
0052     const QString lang = QLocale().name();
0053     for (const QJsonValue &current : commandsArray) {
0054         if (current.type() == QJsonValue::Object) {
0055             const QJsonObject commandsObj = current.toObject();
0056             Command m;
0057             m.parseCommand(commandsObj);
0058             if (mDownloadManager) {
0059                 const QString description = mDownloadManager->translatedString(lang, m.description());
0060                 if (!description.isEmpty()) {
0061                     m.setDescription(description);
0062                 }
0063                 const QString parameters = mDownloadManager->translatedString(lang, m.params());
0064                 if (!parameters.isEmpty()) {
0065                     m.setParams(parameters);
0066                 }
0067             }
0068             mCommands.append(std::move(m));
0069         } else {
0070             qCWarning(RUQOLA_COMMANDS_LOG) << "Problem when parsing commands" << current.type();
0071         }
0072     }
0073 }
0074 
0075 DownloadAppsLanguagesManager *Commands::downloadManager() const
0076 {
0077     return mDownloadManager;
0078 }
0079 
0080 void Commands::setDownloadManager(DownloadAppsLanguagesManager *downloadManager)
0081 {
0082     mDownloadManager = downloadManager;
0083 }
0084 
0085 int Commands::commandsCount() const
0086 {
0087     return mCommandsCount;
0088 }
0089 
0090 void Commands::setCommandsCount(int commandsCount)
0091 {
0092     mCommandsCount = commandsCount;
0093 }
0094 
0095 QVector<Command> Commands::commands() const
0096 {
0097     return mCommands;
0098 }
0099 
0100 void Commands::setCommands(const QVector<Command> &commands)
0101 {
0102     mCommands = commands;
0103 }
0104 
0105 void Commands::parseCommands(const QJsonObject &commandsObj)
0106 {
0107     mCommandsCount = commandsObj[QLatin1String("count")].toInt();
0108     mOffset = commandsObj[QLatin1String("offset")].toInt();
0109     mTotal = commandsObj[QLatin1String("total")].toInt();
0110     mCommands.clear();
0111     parseListCommands(commandsObj);
0112 }
0113 
0114 int Commands::offset() const
0115 {
0116     return mOffset;
0117 }
0118 
0119 void Commands::setOffset(int offset)
0120 {
0121     mOffset = offset;
0122 }
0123 
0124 int Commands::total() const
0125 {
0126     return mTotal;
0127 }
0128 
0129 void Commands::setTotal(int total)
0130 {
0131     mTotal = total;
0132 }
0133 
0134 QDebug operator<<(QDebug d, const Commands &t)
0135 {
0136     d.space() << "total" << t.total();
0137     d.space() << "offset" << t.offset();
0138     d.space() << "commandsCount" << t.commandsCount() << "\n";
0139     for (int i = 0, total = t.commands().count(); i < total; ++i) {
0140         d.space() << t.commands().at(i) << "\n";
0141     }
0142     return d;
0143 }