diff --git a/kcms/notifications/kcm.h b/kcms/notifications/kcm.h --- a/kcms/notifications/kcm.h +++ b/kcms/notifications/kcm.h @@ -23,18 +23,19 @@ #include #include +#include class QAction; class SourcesModel; class FilterProxyModel; namespace NotificationManager { -class Settings; class DoNotDisturbSettings; class NotificationSettings; class JobSettings; class BadgeSettings; +class BehaviorSettings; } class KCMNotifications : public KQuickAddons::ManagedConfigModule @@ -44,11 +45,11 @@ Q_PROPERTY(SourcesModel *sourcesModel READ sourcesModel CONSTANT) Q_PROPERTY(FilterProxyModel *filteredModel READ filteredModel CONSTANT) - Q_PROPERTY(NotificationManager::Settings *settings READ settings CONSTANT) Q_PROPERTY(NotificationManager::DoNotDisturbSettings *dndSettings READ dndSettings CONSTANT) Q_PROPERTY(NotificationManager::NotificationSettings *notificationSettings READ notificationSettings CONSTANT) Q_PROPERTY(NotificationManager::JobSettings *jobSettings READ jobSettings CONSTANT) Q_PROPERTY(NotificationManager::BadgeSettings *badgeSettings READ badgeSettings CONSTANT) + Q_PROPERTY(NotificationManager::BehaviorSettings *behaviorSettings READ behaviorSettings NOTIFY behaviorSettingsChanged) Q_PROPERTY(QKeySequence toggleDoNotDisturbShortcut READ toggleDoNotDisturbShortcut @@ -67,11 +68,12 @@ SourcesModel *sourcesModel() const; FilterProxyModel *filteredModel() const; - NotificationManager::Settings *settings() const; NotificationManager::DoNotDisturbSettings *dndSettings() const; NotificationManager::NotificationSettings *notificationSettings() const; NotificationManager::JobSettings *jobSettings() const; NotificationManager::BadgeSettings *badgeSettings() const; + NotificationManager::BehaviorSettings *behaviorSettings() const; + Q_SIGNAL void behaviorSettingsChanged(); QKeySequence toggleDoNotDisturbShortcut() const; void setToggleDoNotDisturbShortcut(const QKeySequence &shortcut); @@ -88,6 +90,8 @@ Q_INVOKABLE void configureEvents(const QString ¬ifyRcName, const QString &eventId, QQuickItem *ctx = nullptr); + Q_INVOKABLE void setBehaviorSettingsToLoad(QString identifier); + public Q_SLOTS: void load() override; void save() override; @@ -100,22 +104,25 @@ private: void processPendingDeletions(); + bool isSaveNeeded() const override; + void createConnections(NotificationManager::BehaviorSettings* settings); SourcesModel *m_sourcesModel; FilterProxyModel *m_filteredModel; - NotificationManager::Settings *m_settings; NotificationManager::DoNotDisturbSettings *m_dndSettings; NotificationManager::NotificationSettings *m_notificationSettings; NotificationManager::JobSettings *m_jobSettings; NotificationManager::BadgeSettings *m_badgeSettings; + QHash m_behaviorSettingsList; + NotificationManager::BehaviorSettings *m_currentBehavior = nullptr; QAction *m_toggleDoNotDisturbAction; QKeySequence m_toggleDoNotDisturbShortcut; bool m_toggleDoNotDisturbShortcutDirty = false; + bool m_firstLoad = true; QString m_initialDesktopEntry; QString m_initialNotifyRcName; QString m_initialEventId; - }; diff --git a/kcms/notifications/kcm.cpp b/kcms/notifications/kcm.cpp --- a/kcms/notifications/kcm.cpp +++ b/kcms/notifications/kcm.cpp @@ -44,19 +44,18 @@ #include "sourcesmodel.h" #include "filterproxymodel.h" -#include #include #include #include #include +#include K_PLUGIN_FACTORY_WITH_JSON(KCMNotificationsFactory, "kcm_notifications.json", registerPlugin();) KCMNotifications::KCMNotifications(QObject *parent, const QVariantList &args) : KQuickAddons::ManagedConfigModule(parent, args) , m_sourcesModel(new SourcesModel(this)) , m_filteredModel(new FilterProxyModel(this)) - , m_settings(new NotificationManager::Settings(this)) , m_dndSettings(new NotificationManager::DoNotDisturbSettings(this)) , m_notificationSettings(new NotificationManager::NotificationSettings(this)) , m_jobSettings(new NotificationManager::JobSettings(this)) @@ -73,6 +72,7 @@ qmlRegisterType(); qmlRegisterType(); qmlRegisterType(); + qmlRegisterType(); qmlProtectModule(uri, 1); KAboutData *about = new KAboutData(QStringLiteral("kcm_notifications"), i18n("Notifications"), @@ -111,6 +111,8 @@ setInitialDesktopEntry(parser.value(desktopEntryOption)); setInitialNotifyRcName(parser.value(notifyRcNameOption)); setInitialEventId(parser.value(eventIdOption)); + + connect(this, &KCMNotifications::toggleDoNotDisturbShortcutChanged, this, &KCMNotifications::settingsChanged); } KCMNotifications::~KCMNotifications() @@ -128,11 +130,6 @@ return m_filteredModel; } -NotificationManager::Settings *KCMNotifications::settings() const -{ - return m_settings; -} - NotificationManager::DoNotDisturbSettings *KCMNotifications::dndSettings() const { return m_dndSettings; @@ -153,6 +150,11 @@ return m_badgeSettings; } +NotificationManager::BehaviorSettings *KCMNotifications::behaviorSettings() const +{ + return m_currentBehavior; +} + QKeySequence KCMNotifications::toggleDoNotDisturbShortcut() const { return m_toggleDoNotDisturbShortcut; @@ -167,7 +169,6 @@ m_toggleDoNotDisturbShortcut = shortcut; m_toggleDoNotDisturbShortcutDirty = true; emit toggleDoNotDisturbShortcutChanged(); - setNeedsSave(true); } QString KCMNotifications::initialDesktopEntry() const @@ -251,10 +252,32 @@ dialog->show(); } +void KCMNotifications::setBehaviorSettingsToLoad(QString identifier) +{ + m_currentBehavior = m_behaviorSettingsList[identifier]; + emit behaviorSettingsChanged(); +} + void KCMNotifications::load() { ManagedConfigModule::load(); - m_settings->load(); + + if (m_firstLoad) { + m_firstLoad = false; + m_sourcesModel->load(); + for (auto &settings : m_sourcesModel->settingsList()) { + auto toAdd = new NotificationManager::BehaviorSettings(settings.first, settings.second, this); + m_behaviorSettingsList[settings.second] = toAdd; + createConnections(toAdd); + } + } + if (!m_currentBehavior) { + setBehaviorSettingsToLoad(m_behaviorSettingsList.begin().key()); + } + + for (auto *behaviorSettings : m_behaviorSettingsList) { + behaviorSettings->load(); + } const QKeySequence toggleDoNotDisturbShortcut = KGlobalAccel::self()->globalShortcut( m_toggleDoNotDisturbAction->property("componentName").toString(), @@ -266,30 +289,46 @@ } m_toggleDoNotDisturbShortcutDirty = false; - setNeedsSave(false); } void KCMNotifications::save() { ManagedConfigModule::save(); - m_settings->save(); + for (auto *behaviorSettings : m_behaviorSettingsList) { + behaviorSettings->save(); + } if (m_toggleDoNotDisturbShortcutDirty) { // KeySequenceItem will already have checked whether the shortcut is available KGlobalAccel::self()->setShortcut(m_toggleDoNotDisturbAction, {m_toggleDoNotDisturbShortcut}, KGlobalAccel::NoAutoloading); } - - setNeedsSave(false); } void KCMNotifications::defaults() { ManagedConfigModule::defaults(); - m_settings->defaults(); setToggleDoNotDisturbShortcut(QKeySequence()); } +bool KCMNotifications::isSaveNeeded() const +{ + bool needSave = false; + for (auto *behaviorSettings : m_behaviorSettingsList) { + needSave |= behaviorSettings->isSaveNeeded(); + } + + return needSave || m_toggleDoNotDisturbShortcutDirty; +} + +void KCMNotifications::createConnections(NotificationManager::BehaviorSettings *settings) +{ + connect(settings, &NotificationManager::BehaviorSettings::ShowPopupsChanged, this, &KCMNotifications::settingsChanged); + connect(settings, &NotificationManager::BehaviorSettings::ShowPopupsInDndModeChanged, this, &KCMNotifications::settingsChanged); + connect(settings, &NotificationManager::BehaviorSettings::ShowInHistoryChanged, this, &KCMNotifications::settingsChanged); + connect(settings, &NotificationManager::BehaviorSettings::ShowBadgesChanged, this, &KCMNotifications::settingsChanged); +} + #include "kcm.moc" diff --git a/kcms/notifications/package/contents/ui/ApplicationConfiguration.qml b/kcms/notifications/package/contents/ui/ApplicationConfiguration.qml --- a/kcms/notifications/package/contents/ui/ApplicationConfiguration.qml +++ b/kcms/notifications/package/contents/ui/ApplicationConfiguration.qml @@ -42,27 +42,11 @@ readonly property string desktopEntry: rootIndex ? kcm.sourcesModel.data(rootIndex, Private.SourcesModel.DesktopEntryRole) || "" : "" readonly property string notifyRcName: rootIndex ? kcm.sourcesModel.data(rootIndex, Private.SourcesModel.NotifyRcNameRole) || "" : "" - function behavior() { - if (configColumn.desktopEntry) { - return kcm.settings.applicationBehavior(configColumn.desktopEntry); - } else if (configColumn.notifyRcName) { - return kcm.settings.serviceBehavior(configColumn.notifyRcName); - } - return -1; - } - - function setBehavior(flag, enable) { - var newBehavior = behavior(); - if (enable) { - newBehavior |= flag; + onAppDisplayNameChanged: { + if (desktopEntry !== "") { + kcm.setBehaviorSettingsToLoad(desktopEntry) } else { - newBehavior &= ~flag; - } - - if (configColumn.desktopEntry) { - return kcm.settings.setApplicationBehavior(configColumn.desktopEntry, newBehavior); - } else if (configColumn.notifyRcName) { - return kcm.settings.setServiceBehavior(configColumn.notifyRcName, newBehavior); + kcm.setBehaviorSettingsToLoad(notifyRcName) } } @@ -96,32 +80,34 @@ QtControls.CheckBox { id: showPopupsCheck text: i18n("Show popups") - checked: configColumn.behavior() & NotificationManager.Settings.ShowPopups - onClicked: configColumn.setBehavior(NotificationManager.Settings.ShowPopups, checked) + enabled: !kcm.behaviorSettings.isImmutable("ShowPopups") + checked: kcm.behaviorSettings.showPopups + onClicked: kcm.behaviorSettings.showPopups = checked } RowLayout { // just for indentation QtControls.CheckBox { Layout.leftMargin: mirrored ? 0 : indicator.width Layout.rightMargin: mirrored ? indicator.width : 0 text: i18n("Show in do not disturb mode") - enabled: showPopupsCheck.checked - checked: configColumn.behavior() & NotificationManager.Settings.ShowPopupsInDoNotDisturbMode - onClicked: configColumn.setBehavior(NotificationManager.Settings.ShowPopupsInDoNotDisturbMode, checked) + enabled: showPopupsCheck.checked && !kcm.behaviorSettings.isImmutable("ShowPopupsInDndMode") + checked: kcm.behaviorSettings.showPopupsInDndMode + onClicked: kcm.behaviorSettings.showPopupsInDndMode = checked } } QtControls.CheckBox { text: i18n("Show in history") - checked: configColumn.behavior() & NotificationManager.Settings.ShowInHistory - onClicked: configColumn.setBehavior(NotificationManager.Settings.ShowInHistory, checked) + enabled: !kcm.behaviorSettings.isImmutable("ShowInHistory") + checked: kcm.behaviorSettings.showInHistory + onClicked: kcm.behaviorSettings.showInHistory = checked } QtControls.CheckBox { text: i18n("Show notification badges") - enabled: !!configColumn.desktopEntry && configColumn.desktopEntry !== configColumn.otherAppsId - checked: configColumn.behavior() & NotificationManager.Settings.ShowBadges - onClicked: configColumn.setBehavior(NotificationManager.Settings.ShowBadges, checked) + enabled: !!configColumn.desktopEntry && configColumn.desktopEntry !== configColumn.otherAppsId && !kcm.behaviorSettings.isImmutable("ShowBadges") + checked: kcm.behaviorSettings.showBadges + onClicked: kcm.behaviorSettings.showBadges = checked } Kirigami.Separator { diff --git a/kcms/notifications/package/contents/ui/SourcesPage.qml b/kcms/notifications/package/contents/ui/SourcesPage.qml --- a/kcms/notifications/package/contents/ui/SourcesPage.qml +++ b/kcms/notifications/package/contents/ui/SourcesPage.qml @@ -32,8 +32,6 @@ title: i18n("Application Settings") Component.onCompleted: { - kcm.sourcesModel.load(); - var idx = kcm.sourcesModel.persistentIndexForDesktopEntry(kcm.initialDesktopEntry); if (!idx.valid) { idx = kcm.sourcesModel.persistentIndexForNotifyRcName(kcm.initialNotifyRcName); diff --git a/kcms/notifications/package/contents/ui/main.qml b/kcms/notifications/package/contents/ui/main.qml --- a/kcms/notifications/package/contents/ui/main.qml +++ b/kcms/notifications/package/contents/ui/main.qml @@ -48,12 +48,6 @@ kcm.push("SourcesPage.qml"); } - Binding { - target: kcm - property: "needsSave" - value: kcm.settings.dirty // TODO or other stuff - } - Kirigami.FormLayout { Kirigami.InlineMessage { Kirigami.FormData.isSection: true diff --git a/kcms/notifications/sourcesmodel.h b/kcms/notifications/sourcesmodel.h --- a/kcms/notifications/sourcesmodel.h +++ b/kcms/notifications/sourcesmodel.h @@ -94,6 +94,9 @@ Q_INVOKABLE void load(); + // List of config settings in the form of Application, DesktopEntry of Services, NotifyrcName to load. + QVector> settingsList() const; + private: QVector m_data; diff --git a/kcms/notifications/sourcesmodel.cpp b/kcms/notifications/sourcesmodel.cpp --- a/kcms/notifications/sourcesmodel.cpp +++ b/kcms/notifications/sourcesmodel.cpp @@ -373,3 +373,16 @@ endResetModel(); } + +QVector> SourcesModel::settingsList() const +{ + QVector> res; + for (auto &source : qAsConst(m_data)) { + if (source.desktopEntry.isEmpty()) { + res << qMakePair(QStringLiteral("Services"), source.notifyRcName); + } else { + res << qMakePair(QStringLiteral("Applications"), source.desktopEntry); + } + } + return res; +}