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

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "customsoundsmanager.h"
0008 #include "ruqola_custom_sounds_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 
0012 CustomSoundsManager::CustomSoundsManager(QObject *parent)
0013     : QObject{parent}
0014 {
0015 }
0016 
0017 CustomSoundsManager::~CustomSoundsManager() = default;
0018 
0019 void CustomSoundsManager::initializeDefaultSounds()
0020 {
0021     QVector<CustomSoundInfo> listSounds;
0022     {
0023         CustomSoundInfo info;
0024         info.setExtension(QStringLiteral("mp3"));
0025         info.setIdentifier(QStringLiteral("chime"));
0026         info.setName(QStringLiteral("Chime"));
0027         listSounds.append(std::move(info));
0028     }
0029     {
0030         CustomSoundInfo info;
0031         info.setExtension(QStringLiteral("mp3"));
0032         info.setIdentifier(QStringLiteral("door"));
0033         info.setName(QStringLiteral("Door"));
0034         listSounds.append(std::move(info));
0035     }
0036     {
0037         CustomSoundInfo info;
0038         info.setExtension(QStringLiteral("mp3"));
0039         info.setIdentifier(QStringLiteral("beep"));
0040         info.setName(QStringLiteral("Beep"));
0041         listSounds.append(std::move(info));
0042     }
0043     {
0044         CustomSoundInfo info;
0045         info.setExtension(QStringLiteral("mp3"));
0046         info.setIdentifier(QStringLiteral("chelle"));
0047         info.setName(QStringLiteral("Chelle"));
0048         listSounds.append(std::move(info));
0049     }
0050     {
0051         CustomSoundInfo info;
0052         info.setExtension(QStringLiteral("mp3"));
0053         info.setIdentifier(QStringLiteral("ding"));
0054         info.setName(QStringLiteral("Ding"));
0055         listSounds.append(std::move(info));
0056     }
0057     {
0058         CustomSoundInfo info;
0059         info.setExtension(QStringLiteral("mp3"));
0060         info.setIdentifier(QStringLiteral("droplet"));
0061         info.setName(QStringLiteral("Droplet"));
0062         listSounds.append(std::move(info));
0063     }
0064     {
0065         CustomSoundInfo info;
0066         info.setExtension(QStringLiteral("mp3"));
0067         info.setIdentifier(QStringLiteral("highbell"));
0068         info.setName(QStringLiteral("Highbell"));
0069         listSounds.append(std::move(info));
0070     }
0071     {
0072         CustomSoundInfo info;
0073         info.setExtension(QStringLiteral("mp3"));
0074         info.setIdentifier(QStringLiteral("seasons"));
0075         info.setName(QStringLiteral("Seasons"));
0076         listSounds.append(std::move(info));
0077     }
0078     setCustomSoundsInfo(listSounds);
0079     // TODO download it in customsound repo I think.
0080 }
0081 
0082 const QVector<CustomSoundInfo> &CustomSoundsManager::customSoundsInfo() const
0083 {
0084     return mCustomSoundsInfo;
0085 }
0086 
0087 void CustomSoundsManager::setCustomSoundsInfo(const QVector<CustomSoundInfo> &newCustomSoundsInfo)
0088 {
0089     mCustomSoundsInfo = newCustomSoundsInfo;
0090     qCDebug(RUQOLA_CUSTOMSOUNDS_LOG) << " Assign Custom Sounds count: " << mCustomSoundsInfo.count();
0091 }
0092 
0093 void CustomSoundsManager::parseCustomSounds(const QJsonArray &replyArray)
0094 {
0095     mCustomSoundsInfo.clear();
0096     for (int i = 0, total = replyArray.count(); i < total; ++i) {
0097         CustomSoundInfo info;
0098         info.parseCustomSoundInfo(replyArray.at(i).toObject());
0099         if (info.isValid()) {
0100             mCustomSoundsInfo.append(info);
0101         }
0102     }
0103     qCDebug(RUQOLA_CUSTOMSOUNDS_LOG) << " Parse Custom Sounds count: " << mCustomSoundsInfo.count();
0104 }
0105 
0106 void CustomSoundsManager::deleteCustomSounds(const QJsonArray &replyArray)
0107 {
0108     const auto count{replyArray.count()};
0109     for (auto i = 0; i < count; ++i) {
0110         const QJsonObject obj = replyArray.at(i).toObject();
0111         const QJsonObject emojiData = obj.value(QStringLiteral("soundData")).toObject();
0112         const QString identifier = emojiData.value(QStringLiteral("_id")).toString();
0113         if (!identifier.isEmpty()) {
0114             for (int j = 0, total = mCustomSoundsInfo.count(); j < total; ++j) {
0115                 if (mCustomSoundsInfo.at(j).identifier() == identifier) {
0116                     mCustomSoundsInfo.removeAt(j);
0117                     Q_EMIT customSoundRemoved(identifier);
0118                     break;
0119                 }
0120             }
0121         }
0122     }
0123     qCDebug(RUQOLA_CUSTOMSOUNDS_LOG) << " Delete Custom Sounds count: " << mCustomSoundsInfo.count();
0124 }
0125 
0126 void CustomSoundsManager::updateCustomSounds(const QJsonArray &replyArray)
0127 {
0128     const auto count{replyArray.count()};
0129     for (auto i = 0; i < count; ++i) {
0130         const QJsonObject obj = replyArray.at(i).toObject();
0131         const QJsonObject emojiData = obj.value(QStringLiteral("soundData")).toObject();
0132         const QString identifier = emojiData.value(QStringLiteral("_id")).toString();
0133         if (!identifier.isEmpty()) {
0134             bool soundIdentifierFound = false;
0135             for (int j = 0, total = mCustomSoundsInfo.count(); j < total; ++j) {
0136                 if (mCustomSoundsInfo.at(j).identifier() == identifier) {
0137                     soundIdentifierFound = true;
0138                     mCustomSoundsInfo.removeAt(j);
0139                     CustomSoundInfo sound;
0140                     sound.parseCustomSoundInfo(emojiData);
0141                     if (sound.isValid()) {
0142                         mCustomSoundsInfo.append(sound);
0143                         Q_EMIT customSoundUpdated(identifier);
0144                     }
0145                     break;
0146                 }
0147             }
0148             if (!soundIdentifierFound) {
0149                 CustomSoundInfo sound;
0150                 sound.parseCustomSoundInfo(emojiData);
0151                 if (sound.isValid()) {
0152                     mCustomSoundsInfo.append(std::move(sound));
0153                     Q_EMIT customSoundAdded(identifier);
0154                 }
0155             }
0156         }
0157     }
0158     qCDebug(RUQOLA_CUSTOMSOUNDS_LOG) << " Update Custom Sounds count: " << mCustomSoundsInfo.count();
0159 }
0160 
0161 #include "moc_customsoundsmanager.cpp"