File indexing completed on 2024-04-28 16:54:38

0001 /*
0002     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 
0011 #include <KSharedConfig>
0012 
0013 #include <QDateTime>
0014 #include <QScopedPointer>
0015 #include <QString>
0016 
0017 #include "notificationmanager_export.h"
0018 
0019 namespace NotificationManager
0020 {
0021 /**
0022  * @short Notification settings and state
0023  *
0024  * This class encapsulates all global settings related to notifications
0025  * as well as do not disturb mode and other state.
0026  *
0027  * This class can be used by applications to alter their behavior
0028  * depending on user's notification preferences.
0029  *
0030  * @author Kai Uwe Broulik <kde@privat.broulik.de>
0031  **/
0032 class NOTIFICATIONMANAGER_EXPORT Settings : public QObject
0033 {
0034     Q_OBJECT
0035 
0036     /**
0037      * Whether to show critical notification popups in do not disturb mode.
0038      */
0039     Q_PROPERTY(bool criticalPopupsInDoNotDisturbMode READ criticalPopupsInDoNotDisturbMode WRITE setCriticalPopupsInDoNotDisturbMode NOTIFY settingsChanged)
0040     /**
0041      * Whether to keep normal notifications always on top.
0042      */
0043     Q_PROPERTY(bool keepNormalAlwaysOnTop READ keepNormalAlwaysOnTop WRITE setKeepNormalAlwaysOnTop NOTIFY settingsChanged)
0044     /**
0045      * Whether to show popups for low priority notifications.
0046      */
0047     Q_PROPERTY(bool lowPriorityPopups READ lowPriorityPopups WRITE setLowPriorityPopups NOTIFY settingsChanged)
0048     /**
0049      * Whether to add low priority notifications to the history.
0050      */
0051     Q_PROPERTY(bool lowPriorityHistory READ lowPriorityHistory WRITE setLowPriorityHistory NOTIFY settingsChanged)
0052 
0053     /**
0054      * The notification popup position on screen.
0055      * CloseToWidget means they should be positioned closely to where the plasmoid is located on screen.
0056      */
0057     Q_PROPERTY(PopupPosition popupPosition READ popupPosition WRITE setPopupPosition NOTIFY settingsChanged)
0058 
0059     /**
0060      * The default timeout for notification popups that do not have an explicit timeout set,
0061      * in milliseconds. Default is 5000ms (5 seconds).
0062      */
0063     Q_PROPERTY(int popupTimeout READ popupTimeout WRITE setPopupTimeout RESET resetPopupTimeout NOTIFY settingsChanged)
0064 
0065     /**
0066      * Whether to show application jobs in task manager
0067      */
0068     Q_PROPERTY(bool jobsInTaskManager READ jobsInTaskManager WRITE setJobsInTaskManager /*RESET resetJobsInTaskManager*/ NOTIFY settingsChanged)
0069     /**
0070      * Whether to show application jobs as notifications
0071      */
0072     Q_PROPERTY(bool jobsInNotifications READ jobsInNotifications WRITE setJobsInNotifications /*RESET resetJobsPopup*/ NOTIFY settingsChanged)
0073     /**
0074      * Whether application jobs stay visible for the whole duration of the job
0075      */
0076     Q_PROPERTY(bool permanentJobPopups READ permanentJobPopups WRITE setPermanentJobPopups /*RESET resetAutoHideJobsPopup*/ NOTIFY settingsChanged)
0077 
0078     /**
0079      * Whether to show notification badges (numbers in circles) in task manager
0080      */
0081     Q_PROPERTY(bool badgesInTaskManager READ badgesInTaskManager WRITE setBadgesInTaskManager NOTIFY settingsChanged)
0082 
0083     /**
0084      * A list of desktop entries of applications that have been seen sending a notification.
0085      */
0086     Q_PROPERTY(QStringList knownApplications READ knownApplications NOTIFY knownApplicationsChanged)
0087 
0088     /**
0089      * A list of desktop entries of applications for which no popups should be shown.
0090      */
0091     Q_PROPERTY(QStringList popupBlacklistedApplications READ popupBlacklistedApplications NOTIFY settingsChanged)
0092     /**
0093      * A list of notifyrc names of services for which no popups should be shown.
0094      */
0095     Q_PROPERTY(QStringList popupBlacklistedServices READ popupBlacklistedServices NOTIFY settingsChanged)
0096 
0097     /**
0098      * A list of desktop entries of applications for which a popup should be shown even in do not disturb mode.
0099      */
0100     Q_PROPERTY(QStringList doNotDisturbPopupWhitelistedApplications READ doNotDisturbPopupWhitelistedApplications NOTIFY settingsChanged)
0101     /**
0102      * A list of notifyrc names of services for which a popup should be shown even in do not disturb mode.
0103      */
0104     Q_PROPERTY(QStringList doNotDisturbPopupWhitelistedServices READ doNotDisturbPopupWhitelistedServices NOTIFY settingsChanged)
0105 
0106     /**
0107      * A list of desktop entries of applications which shouldn't be shown in the history.
0108      */
0109     Q_PROPERTY(QStringList historyBlacklistedApplications READ historyBlacklistedApplications NOTIFY settingsChanged)
0110     /**
0111      * A list of notifyrc names of services which shouldn't be shown in the history.
0112      */
0113     Q_PROPERTY(QStringList historyBlacklistedServices READ historyBlacklistedServices NOTIFY settingsChanged)
0114 
0115     /**
0116      * A list of desktop entries of applications which shouldn't show badges in task manager.
0117      */
0118     Q_PROPERTY(QStringList badgeBlacklistedApplications READ badgeBlacklistedApplications NOTIFY settingsChanged)
0119 
0120     /**
0121      * The date until which do not disturb mode is enabled.
0122      *
0123      * When invalid or in the past, do not disturb mode should be considered disabled.
0124      * Do not disturb mode is considered active when this property points to a date
0125      * in the future OR notificationsInhibitedByApplication is true.
0126      */
0127     Q_PROPERTY(QDateTime notificationsInhibitedUntil READ notificationsInhibitedUntil WRITE setNotificationsInhibitedUntil RESET
0128                    resetNotificationsInhibitedUntil NOTIFY settingsChanged)
0129 
0130     /**
0131      * Whether an application currently requested do not disturb mode.
0132      *
0133      * Do not disturb mode is considered active when this property is true OR
0134      * notificationsInhibitedUntil points to a date in the future.
0135      *
0136      * @sa revokeApplicationInhibitions
0137      */
0138     Q_PROPERTY(bool notificationsInhibitedByApplication READ notificationsInhibitedByApplication NOTIFY notificationsInhibitedByApplicationChanged)
0139 
0140     Q_PROPERTY(QStringList notificationInhibitionApplications READ notificationInhibitionApplications NOTIFY notificationInhibitionApplicationsChanged)
0141 
0142     Q_PROPERTY(QStringList notificationInhibitionReasons READ notificationInhibitionReasons NOTIFY notificationInhibitionApplicationsChanged)
0143 
0144     /**
0145      * Whether to enable do not disturb mode when screens are mirrored/overlapping
0146      *
0147      * @since 5.17
0148      */
0149     Q_PROPERTY(bool inhibitNotificationsWhenScreensMirrored READ inhibitNotificationsWhenScreensMirrored WRITE setInhibitNotificationsWhenScreensMirrored NOTIFY
0150                    settingsChanged)
0151 
0152     /**
0153      * Whether there currently are mirrored/overlapping screens
0154      *
0155      * This property is only updated when @c inhibitNotificationsWhenScreensMirrored
0156      * is set to true, otherwise it is always false.
0157      * You can assign false to this property if you want to temporarily revoke automatic do not disturb
0158      * mode when screens are mirrored until the screen configuration changes.
0159      *
0160      * @since 5.17
0161      */
0162     Q_PROPERTY(bool screensMirrored READ screensMirrored WRITE setScreensMirrored NOTIFY screensMirroredChanged)
0163 
0164     /**
0165      * Whether to enable do not disturb mode while screen sharing
0166      *
0167      * @since 5.22
0168      */
0169     Q_PROPERTY(bool inhibitNotificationsWhenScreenSharing READ inhibitNotificationsWhenScreenSharing WRITE setInhibitNotificationsWhenScreenSharing NOTIFY
0170                    settingsChanged)
0171 
0172     /**
0173      * Whether notification sounds should be disabled
0174      *
0175      * This does not reflect the actual mute state of the Notification Sounds
0176      * stream but only remembers what value was assigned to this property.
0177      *
0178      * This way you can tell whether to unmute notification sounds or not, in case
0179      * the user had them explicitly muted previously.
0180      *
0181      * @note This does not actually mute or unmute the actual sound stream,
0182      * you need to do this yourself using e.g. PulseAudio.
0183      */
0184     Q_PROPERTY(bool notificationSoundsInhibited READ notificationSoundsInhibited WRITE setNotificationSoundsInhibited NOTIFY settingsChanged)
0185 
0186     /**
0187      * Whether to update the properties immediately when they are changed on disk
0188      *
0189      * This can be undesirable for a settings dialog where outside changes
0190      * should not suddenly cause the UI to change.
0191      *
0192      * Default is true.
0193      */
0194     Q_PROPERTY(bool live READ live WRITE setLive NOTIFY liveChanged)
0195 
0196     /**
0197      * Whether the settings have changed and need to be saved
0198      *
0199      * @sa save()
0200      */
0201     Q_PROPERTY(bool dirty READ dirty NOTIFY dirtyChanged)
0202 
0203 public:
0204     explicit Settings(QObject *parent = nullptr);
0205     /**
0206      * @deprecated
0207      */
0208     Settings(const KSharedConfig::Ptr &config, QObject *parent = nullptr);
0209     ~Settings() override;
0210 
0211     enum PopupPosition {
0212         CloseToWidget = 0,
0213         TopLeft,
0214         TopCenter,
0215         TopRight,
0216         BottomLeft,
0217         BottomCenter,
0218         BottomRight,
0219     };
0220     Q_ENUM(PopupPosition)
0221 
0222     enum NotificationBehavior {
0223         ShowPopups = 1 << 1,
0224         ShowPopupsInDoNotDisturbMode = 1 << 2,
0225         ShowInHistory = 1 << 3,
0226         ShowBadges = 1 << 4,
0227     };
0228     Q_ENUM(NotificationBehavior)
0229     Q_DECLARE_FLAGS(NotificationBehaviors, NotificationBehavior)
0230     Q_FLAG(NotificationBehaviors)
0231 
0232     Q_INVOKABLE NotificationBehaviors applicationBehavior(const QString &desktopEntry) const;
0233     Q_INVOKABLE void setApplicationBehavior(const QString &desktopEntry, NotificationBehaviors behaviors);
0234 
0235     Q_INVOKABLE NotificationBehaviors serviceBehavior(const QString &desktopEntry) const;
0236     Q_INVOKABLE void setServiceBehavior(const QString &desktopEntry, NotificationBehaviors behaviors);
0237 
0238     Q_INVOKABLE void registerKnownApplication(const QString &desktopEntry);
0239     Q_INVOKABLE void forgetKnownApplication(const QString &desktopEntry);
0240 
0241     Q_INVOKABLE void load();
0242     Q_INVOKABLE void save();
0243     Q_INVOKABLE void defaults();
0244 
0245     bool live() const;
0246     void setLive(bool live);
0247 
0248     bool dirty() const;
0249 
0250     bool criticalPopupsInDoNotDisturbMode() const;
0251     void setCriticalPopupsInDoNotDisturbMode(bool enable);
0252 
0253     bool keepNormalAlwaysOnTop() const;
0254     void setKeepNormalAlwaysOnTop(bool enable);
0255 
0256     bool lowPriorityPopups() const;
0257     void setLowPriorityPopups(bool enable);
0258 
0259     bool lowPriorityHistory() const;
0260     void setLowPriorityHistory(bool enable);
0261 
0262     PopupPosition popupPosition() const;
0263     void setPopupPosition(PopupPosition popupPosition);
0264 
0265     int popupTimeout() const;
0266     void setPopupTimeout(int popupTimeout);
0267     void resetPopupTimeout();
0268 
0269     bool jobsInTaskManager() const;
0270     void setJobsInTaskManager(bool enable);
0271 
0272     bool jobsInNotifications() const;
0273     void setJobsInNotifications(bool enable);
0274 
0275     bool permanentJobPopups() const;
0276     void setPermanentJobPopups(bool enable);
0277 
0278     bool badgesInTaskManager() const;
0279     void setBadgesInTaskManager(bool enable);
0280 
0281     QStringList knownApplications() const;
0282 
0283     QStringList popupBlacklistedApplications() const;
0284     QStringList popupBlacklistedServices() const;
0285 
0286     QStringList doNotDisturbPopupWhitelistedApplications() const;
0287     QStringList doNotDisturbPopupWhitelistedServices() const;
0288 
0289     QStringList historyBlacklistedApplications() const;
0290     QStringList historyBlacklistedServices() const;
0291 
0292     QStringList badgeBlacklistedApplications() const;
0293 
0294     QDateTime notificationsInhibitedUntil() const;
0295     void setNotificationsInhibitedUntil(const QDateTime &time);
0296     void resetNotificationsInhibitedUntil();
0297 
0298     bool notificationsInhibitedByApplication() const;
0299     QStringList notificationInhibitionApplications() const;
0300     QStringList notificationInhibitionReasons() const;
0301 
0302     bool inhibitNotificationsWhenScreensMirrored() const;
0303     void setInhibitNotificationsWhenScreensMirrored(bool mirrored);
0304 
0305     bool screensMirrored() const;
0306     void setScreensMirrored(bool enable);
0307 
0308     bool inhibitNotificationsWhenScreenSharing() const;
0309     void setInhibitNotificationsWhenScreenSharing(bool inhibit);
0310 
0311     bool notificationSoundsInhibited() const;
0312     void setNotificationSoundsInhibited(bool inhibited);
0313 
0314     /**
0315      * Revoke application notification inhibitions.
0316      *
0317      * @note Applications are not notified of the fact that their
0318      * inhibition might have been taken away.
0319      */
0320     Q_INVOKABLE void revokeApplicationInhibitions();
0321 
0322 Q_SIGNALS:
0323     void settingsChanged();
0324 
0325     void liveChanged();
0326     void dirtyChanged();
0327 
0328     void knownApplicationsChanged();
0329 
0330     void notificationsInhibitedByApplicationChanged(bool notificationsInhibitedByApplication);
0331     void notificationInhibitionApplicationsChanged();
0332 
0333     void screensMirroredChanged();
0334 
0335 private:
0336     class Private;
0337     QScopedPointer<Private> d;
0338 };
0339 
0340 } // namespace NotificationManager
0341 
0342 Q_DECLARE_OPERATORS_FOR_FLAGS(NotificationManager::Settings::NotificationBehaviors)