File indexing completed on 2024-05-19 05:38:22

0001 /*
0002     SPDX-FileCopyrightText: 2020 Cyril Rossi <cyril.rossi@enioka.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "notificationsdata.h"
0008 
0009 #include <libnotificationmanager/badgesettings.h>
0010 #include <libnotificationmanager/behaviorsettings.h>
0011 #include <libnotificationmanager/donotdisturbsettings.h>
0012 #include <libnotificationmanager/jobsettings.h>
0013 #include <libnotificationmanager/notificationsettings.h>
0014 
0015 NotificationsData::NotificationsData(QObject *parent)
0016     : KCModuleData(parent)
0017     , m_dndSettings(new NotificationManager::DoNotDisturbSettings(this))
0018     , m_notificationSettings(new NotificationManager::NotificationSettings(this))
0019     , m_jobSettings(new NotificationManager::JobSettings(this))
0020     , m_badgeSettings(new NotificationManager::BadgeSettings(this))
0021 {
0022     autoRegisterSkeletons();
0023     readBehaviorSettings();
0024 }
0025 
0026 NotificationManager::DoNotDisturbSettings *NotificationsData::dndSettings() const
0027 {
0028     return m_dndSettings;
0029 }
0030 
0031 NotificationManager::NotificationSettings *NotificationsData::notificationSettings() const
0032 {
0033     return m_notificationSettings;
0034 }
0035 
0036 NotificationManager::JobSettings *NotificationsData::jobSettings() const
0037 {
0038     return m_jobSettings;
0039 }
0040 
0041 NotificationManager::BadgeSettings *NotificationsData::badgeSettings() const
0042 {
0043     return m_badgeSettings;
0044 }
0045 
0046 NotificationManager::BehaviorSettings *NotificationsData::behaviorSettings(int index) const
0047 {
0048     return m_behaviorSettingsList.value(index);
0049 }
0050 
0051 void NotificationsData::insertBehaviorSettings(int index, NotificationManager::BehaviorSettings *settings)
0052 {
0053     m_behaviorSettingsList[index] = settings;
0054 }
0055 
0056 void NotificationsData::loadBehaviorSettings()
0057 {
0058     for (auto *behaviorSettings : std::as_const(m_behaviorSettingsList)) {
0059         behaviorSettings->load();
0060     }
0061 }
0062 
0063 void NotificationsData::saveBehaviorSettings()
0064 {
0065     for (auto *behaviorSettings : std::as_const(m_behaviorSettingsList)) {
0066         behaviorSettings->save();
0067     }
0068 }
0069 
0070 void NotificationsData::defaultsBehaviorSettings()
0071 {
0072     for (auto *behaviorSettings : std::as_const(m_behaviorSettingsList)) {
0073         behaviorSettings->setDefaults();
0074     }
0075 }
0076 
0077 bool NotificationsData::isSaveNeededBehaviorSettings() const
0078 {
0079     bool needSave = std::any_of(m_behaviorSettingsList.cbegin(), m_behaviorSettingsList.cend(), [](const NotificationManager::BehaviorSettings *settings) {
0080         return settings->isSaveNeeded();
0081     });
0082     return needSave;
0083 }
0084 
0085 bool NotificationsData::isDefaultsBehaviorSettings() const
0086 {
0087     bool notDefault = std::any_of(m_behaviorSettingsList.cbegin(), m_behaviorSettingsList.cend(), [](const NotificationManager::BehaviorSettings *settings) {
0088         return !settings->isDefaults();
0089     });
0090     return !notDefault;
0091 }
0092 
0093 void NotificationsData::readBehaviorSettings()
0094 {
0095     KConfig config("plasmanotifyrc", KConfig::SimpleConfig);
0096 
0097     for (auto groupEntry : {QStringLiteral("Applications"), QStringLiteral("Services")}) {
0098         KConfigGroup group(&config, groupEntry);
0099         for (const QString &desktopEntry : group.groupList()) {
0100             m_behaviorSettingsList.insert(m_behaviorSettingsList.count(), new NotificationManager::BehaviorSettings(groupEntry, desktopEntry, this));
0101         }
0102     }
0103 }
0104 
0105 bool NotificationsData::isDefaults() const
0106 {
0107     return KCModuleData::isDefaults() && isDefaultsBehaviorSettings();
0108 }