File indexing completed on 2024-05-12 05:02:10

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "notificationoptions.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonObject>
0010 
0011 NotificationOptions::NotificationOptions() = default;
0012 
0013 void NotificationOptions::updateNotificationOptions(const QJsonObject &obj)
0014 {
0015     parseNotificationOptions(obj);
0016 }
0017 
0018 void NotificationOptions::parseNotificationOptions(const QJsonObject &obj)
0019 {
0020     // qDebug() << " parseNotificationOptions " << obj;
0021     mHideUnreadStatus = obj.value(QLatin1String("hideUnreadStatus")).toBool();
0022     mHideMentionStatus = obj.value(QLatin1String("hideMentionStatus")).toBool();
0023     mDisableNotifications = obj.value(QLatin1String("disableNotifications")).toBool();
0024 
0025     mAudioNotificationValue = obj.value(QLatin1String("audioNotificationValue")).toString();
0026 
0027     //"desktopNotificationDuration":0,"desktopNotifications":"mentions"
0028     mDesktopNotifications =
0029         NotificationValue{obj.value(QLatin1String("desktopNotifications")).toString(), obj.value(QLatin1String("desktopPrefOrigin")).toString()};
0030     //"mobilePushNotifications":"nothing"
0031     mMobilePushNotification =
0032         NotificationValue{obj.value(QLatin1String("mobilePushNotifications")).toString(), obj.value(QLatin1String("mobilePrefOrigin")).toString()};
0033     //"emailNotifications":"default"
0034     mEmailNotifications = NotificationValue{obj.value(QLatin1String("emailNotifications")).toString(), obj.value(QLatin1String("emailPrefOrigin")).toString()};
0035     //"unreadAlert":"nothing"
0036     mUnreadTrayIconAlert = obj.value(QLatin1String("unreadAlert")).toString();
0037     mMuteGroupMentions = obj.value(QLatin1String("muteGroupMentions")).toBool();
0038 }
0039 
0040 QString NotificationOptions::audioNotificationValue() const
0041 {
0042     return mAudioNotificationValue;
0043 }
0044 
0045 void NotificationOptions::setAudioNotificationValue(const QString &audioNotificationValue)
0046 {
0047     mAudioNotificationValue = audioNotificationValue;
0048 }
0049 
0050 QJsonObject NotificationOptions::serialize(const NotificationOptions &options)
0051 {
0052     QJsonObject obj;
0053     obj[QLatin1String("audioNotificationValue")] = options.audioNotificationValue();
0054     obj[QLatin1String("disableNotifications")] = options.disableNotifications();
0055     obj[QLatin1String("desktopNotifications")] = options.desktopNotifications().currentValue();
0056     obj[QLatin1String("mobilePushNotifications")] = options.mobilePushNotification().currentValue();
0057     obj[QLatin1String("emailNotifications")] = options.emailNotifications().currentValue();
0058     obj[QLatin1String("unreadAlert")] = options.unreadTrayIconAlert();
0059     obj[QLatin1String("hideUnreadStatus")] = options.hideUnreadStatus();
0060     obj[QLatin1String("muteGroupMentions")] = options.muteGroupMentions();
0061     obj[QLatin1String("hideMentionStatus")] = options.hideMentionStatus();
0062     return obj;
0063 }
0064 
0065 NotificationOptions NotificationOptions::deserialize(const QJsonObject &o)
0066 {
0067     qCWarning(RUQOLA_LOG) << "Not implemented yet";
0068     NotificationOptions options;
0069     options.setAudioNotificationValue(o[QLatin1String("audioNotificationValue")].toString());
0070     options.setDisableNotifications(o[QLatin1String("disableNotifications")].toBool());
0071     options.setUnreadTrayIconAlert(o[QLatin1String("unreadAlert")].toString());
0072     options.setHideUnreadStatus(o[QLatin1String("hideUnreadStatus")].toBool());
0073     options.setMuteGroupMentions(o[QLatin1String("muteGroupMentions")].toBool());
0074     options.setHideMentionStatus(o[QLatin1String("hideMentionStatus")].toBool());
0075     options.setDesktopNotifications(NotificationValue(o[QLatin1String("desktopNotifications")].toString(), QString()));
0076     options.setMobilePushNotification(NotificationValue(o[QLatin1String("mobilePushNotifications")].toString(), QString()));
0077     options.setEmailNotifications(NotificationValue(o[QLatin1String("emailNotifications")].toString(), QString()));
0078     return options;
0079 }
0080 
0081 bool NotificationOptions::hideUnreadStatus() const
0082 {
0083     return mHideUnreadStatus;
0084 }
0085 
0086 void NotificationOptions::setHideUnreadStatus(bool value)
0087 {
0088     mHideUnreadStatus = value;
0089 }
0090 
0091 bool NotificationOptions::disableNotifications() const
0092 {
0093     return mDisableNotifications;
0094 }
0095 
0096 void NotificationOptions::setDisableNotifications(bool disableNotifications)
0097 {
0098     mDisableNotifications = disableNotifications;
0099 }
0100 
0101 QString NotificationOptions::unreadTrayIconAlert() const
0102 {
0103     return mUnreadTrayIconAlert;
0104 }
0105 
0106 void NotificationOptions::setUnreadTrayIconAlert(const QString &unreadTrayIconAlert)
0107 {
0108     mUnreadTrayIconAlert = unreadTrayIconAlert;
0109 }
0110 
0111 NotificationOptions::NotificationValue NotificationOptions::emailNotifications() const
0112 {
0113     return mEmailNotifications;
0114 }
0115 
0116 void NotificationOptions::setEmailNotifications(const NotificationValue &emailNotifications)
0117 {
0118     mEmailNotifications = emailNotifications;
0119 }
0120 
0121 NotificationOptions::NotificationValue NotificationOptions::mobilePushNotification() const
0122 {
0123     return mMobilePushNotification;
0124 }
0125 
0126 void NotificationOptions::setMobilePushNotification(const NotificationValue &mobilePushNotification)
0127 {
0128     mMobilePushNotification = mobilePushNotification;
0129 }
0130 
0131 NotificationOptions::NotificationValue NotificationOptions::desktopNotifications() const
0132 {
0133     return mDesktopNotifications;
0134 }
0135 
0136 void NotificationOptions::setDesktopNotifications(const NotificationValue &desktopNotifications)
0137 {
0138     mDesktopNotifications = desktopNotifications;
0139 }
0140 
0141 bool NotificationOptions::operator==(const NotificationOptions &other) const
0142 {
0143     return (mDesktopNotifications == other.desktopNotifications()) && (mMobilePushNotification == other.mobilePushNotification())
0144         && (mEmailNotifications == other.emailNotifications()) && (mUnreadTrayIconAlert == other.unreadTrayIconAlert())
0145         && (mDisableNotifications == other.disableNotifications()) && (mHideUnreadStatus == other.hideUnreadStatus())
0146         && (mAudioNotificationValue == other.audioNotificationValue()) && (mMuteGroupMentions == other.muteGroupMentions())
0147         && (mHideMentionStatus == other.hideMentionStatus());
0148 }
0149 
0150 bool NotificationOptions::operator!=(const NotificationOptions &other) const
0151 {
0152     return !operator==(other);
0153 }
0154 
0155 bool NotificationOptions::muteGroupMentions() const
0156 {
0157     return mMuteGroupMentions;
0158 }
0159 
0160 void NotificationOptions::setMuteGroupMentions(bool muteGroupMentions)
0161 {
0162     mMuteGroupMentions = muteGroupMentions;
0163 }
0164 
0165 bool NotificationOptions::hideMentionStatus() const
0166 {
0167     return mHideMentionStatus;
0168 }
0169 
0170 void NotificationOptions::setHideMentionStatus(bool newHideMentionStatus)
0171 {
0172     mHideMentionStatus = newHideMentionStatus;
0173 }
0174 
0175 QDebug operator<<(QDebug d, const NotificationOptions &t)
0176 {
0177     d.space() << "mAudioNotificationValue:" << t.audioNotificationValue();
0178     d.space() << "mDesktopNotifications:" << t.desktopNotifications();
0179     d.space() << "mMobilePushNotification:" << t.mobilePushNotification();
0180     d.space() << "mEmailNotifications:" << t.emailNotifications();
0181     d.space() << "mDisableNotifications:" << t.disableNotifications();
0182     d.space() << "hideUnreadStatus:" << t.hideUnreadStatus();
0183     d.space() << "unreadTrayIconAlert:" << t.unreadTrayIconAlert();
0184     d.space() << "mMuteGroupMentions:" << t.muteGroupMentions();
0185     d.space() << "mHideMentionStatus:" << t.hideMentionStatus();
0186     return d;
0187 }
0188 
0189 QDebug operator<<(QDebug d, const NotificationOptions::NotificationValue &t)
0190 {
0191     d << " value " << t.value;
0192     d << " preferenceOrigin " << t.preferenceOrigin;
0193     return d;
0194 }
0195 
0196 bool NotificationOptions::NotificationValue::operator==(const NotificationValue &other) const
0197 {
0198     return other.preferenceOrigin == preferenceOrigin && other.value == value;
0199 }
0200 
0201 QString NotificationOptions::NotificationValue::currentValue() const
0202 {
0203     if (preferenceOrigin == QLatin1String("subscription") && !value.isEmpty()) {
0204         return value;
0205     }
0206     // Keep compatibility
0207     if (preferenceOrigin.isEmpty() && !value.isEmpty()) {
0208         return value;
0209     }
0210     if (preferenceOrigin.isEmpty() && value.isEmpty()) {
0211         return QString();
0212     }
0213     return QStringLiteral("default");
0214 }
0215 
0216 bool NotificationOptions::NotificationValue::isEmpty() const
0217 {
0218     return preferenceOrigin.isEmpty() && value.isEmpty();
0219 }
0220 
0221 #include "moc_notificationoptions.cpp"