From 356e62e832cf5daf9bc50dccd3a9a38f773492a0 Mon Sep 17 00:00:00 2001 From: Jason Conti Date: Sat, 31 Aug 2019 12:01:14 -0400 Subject: * Add our own do-not-disturb setting * Remove the mate specific code from indicator-notifications-settings * When do-not-disturb is set the indicator will try to set do-not-disturb on all supported desktops (currently only mate) --- ...chpad.indicator.notifications.gschema.xml.in.in | 5 ++ src/indicator-notifications-settings.c | 67 +++------------------ src/indicator-notifications.c | 69 ++++++++++++++++++++++ src/settings.h | 4 ++ 4 files changed, 87 insertions(+), 58 deletions(-) diff --git a/data/net.launchpad.indicator.notifications.gschema.xml.in.in b/data/net.launchpad.indicator.notifications.gschema.xml.in.in index d04251d..d31c182 100644 --- a/data/net.launchpad.indicator.notifications.gschema.xml.in.in +++ b/data/net.launchpad.indicator.notifications.gschema.xml.in.in @@ -15,6 +15,11 @@ Clear notifications on middle click Normally when middle clicking the notification icon, the unread status will be toggled if the queue is not empty. With this option enabled, the notification queue will be cleared instead. + + false + Enable do-not-disturb mode + On supported desktops enables do-not-disturb mode on the notification daemon. + false Hide the indicator diff --git a/src/indicator-notifications-settings.c b/src/indicator-notifications-settings.c index 273859d..806c344 100644 --- a/src/indicator-notifications-settings.c +++ b/src/indicator-notifications-settings.c @@ -13,9 +13,6 @@ #define SCHEMA_KEY "schema-key" -#define MATE_SCHEMA "org.mate.NotificationDaemon" -#define MATE_KEY_DND "do-not-disturb" - #define COLUMN_APPNAME 0 typedef struct @@ -54,11 +51,6 @@ static gboolean foreach_check_duplicates(GtkTreeModel *model, GtkTreePath *path, static gboolean foreach_build_array(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data); -/* Mate Functions */ -static gboolean has_mate_dnd(); -static gboolean get_mate_dnd(); -static void mate_dnd_button_toggled_cb(GtkToggleButton *button, gpointer user_data); - /* Callbacks */ static void blacklist_add_clicked_cb(GtkButton *button, gpointer user_data); static void blacklist_remove_clicked_cb(GtkButton *button, gpointer user_data); @@ -155,47 +147,6 @@ foreach_build_array(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, g return FALSE; } -static gboolean -has_mate_dnd() -{ - /* Check if we can access the mate do-not-disturb key */ - GSettingsSchemaSource *source = g_settings_schema_source_get_default(); - if (source == NULL) { - return FALSE; - } - - /* Lookup the schema */ - GSettingsSchema *schema = g_settings_schema_source_lookup(source, MATE_SCHEMA, FALSE); - - /* Couldn't find the schema */ - if (schema == NULL) { - return FALSE; - } - - /* Found the schema, make sure we have the do-not-disturb key */ - gboolean result = g_settings_schema_has_key(schema, MATE_KEY_DND); - g_settings_schema_unref(schema); - - return result; -} - -static gboolean -get_mate_dnd() -{ - GSettings *settings = g_settings_new(MATE_SCHEMA); - gboolean result = g_settings_get_boolean(settings, MATE_KEY_DND); - g_object_unref(settings); - return result; -} - -static void -mate_dnd_button_toggled_cb(GtkToggleButton *button, gpointer user_data) -{ - GSettings *settings = g_settings_new(MATE_SCHEMA); - g_settings_set_boolean(settings, MATE_KEY_DND, gtk_toggle_button_get_active(button)); - g_object_unref(settings); -} - static void blacklist_add_clicked_cb(GtkButton *button, gpointer user_data) { @@ -279,7 +230,7 @@ indicator_notifications_settings_activate(GApplication *app) GtkWidget *vbox; GtkWidget *button_cmc; GtkWidget *button_hide_ind; - GtkWidget *button_mate_dnd; + GtkWidget *button_dnd; GtkWidget *spin; GtkWidget *spin_label; GtkWidget *blacklist_label; @@ -341,14 +292,14 @@ indicator_notifications_settings_activate(GApplication *app) gtk_box_pack_start(GTK_BOX(vbox), button_hide_ind, FALSE, FALSE, 4); gtk_widget_show(button_hide_ind); - /* mate do-not-disturb */ - if (has_mate_dnd()) { - button_mate_dnd = gtk_check_button_new_with_label(_("Mate Desktop Do Not Disturb")); - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_mate_dnd), get_mate_dnd()); - g_signal_connect(button_mate_dnd, "toggled", G_CALLBACK(mate_dnd_button_toggled_cb), NULL); - gtk_box_pack_start(GTK_BOX(vbox), button_mate_dnd, FALSE, FALSE, 4); - gtk_widget_show(button_mate_dnd); - } + /* do-not-disturb */ + button_dnd = gtk_check_button_new_with_label(_("Do not disturb")); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button_dnd), + g_settings_get_boolean(self->settings, NOTIFICATIONS_KEY_DND)); + g_object_set_data(G_OBJECT(button_dnd), SCHEMA_KEY, NOTIFICATIONS_KEY_DND); + g_signal_connect(button_dnd, "toggled", G_CALLBACK(button_toggled_cb), self->settings); + gtk_box_pack_start(GTK_BOX(vbox), button_dnd, FALSE, FALSE, 4); + gtk_widget_show(button_dnd); /* max-items */ /* FIXME: indicator does not change max items until restart... */ diff --git a/src/indicator-notifications.c b/src/indicator-notifications.c index f7d45f6..d4e5eed 100644 --- a/src/indicator-notifications.c +++ b/src/indicator-notifications.c @@ -61,6 +61,7 @@ struct _IndicatorNotificationsPrivate { GList *hidden_items; gboolean clear_on_middle_click; + gboolean do_not_disturb; gboolean have_unread; gboolean hide_indicator; @@ -121,6 +122,8 @@ static void update_indicator_visibility(IndicatorNotifications *self); static void load_blacklist_hints(IndicatorNotifications *self); static void save_blacklist_hints(IndicatorNotifications *self); static void update_blacklist_hints(IndicatorNotifications *self, Notification *notification); +static void update_do_not_disturb(IndicatorNotifications *self); +static void settings_try_set_boolean(const gchar *schema, const gchar *key, gboolean value); /* Callbacks */ static void clear_item_activated_cb(GtkMenuItem *menuitem, gpointer user_data); @@ -207,9 +210,11 @@ indicator_notifications_init(IndicatorNotifications *self) /* Connect to GSettings */ self->priv->settings = g_settings_new(NOTIFICATIONS_SCHEMA); self->priv->clear_on_middle_click = g_settings_get_boolean(self->priv->settings, NOTIFICATIONS_KEY_CLEAR_MC); + self->priv->do_not_disturb = g_settings_get_boolean(self->priv->settings, NOTIFICATIONS_KEY_DND); self->priv->hide_indicator = g_settings_get_boolean(self->priv->settings, NOTIFICATIONS_KEY_HIDE_INDICATOR); self->priv->max_items = g_settings_get_int(self->priv->settings, NOTIFICATIONS_KEY_MAX_ITEMS); update_blacklist(self); + update_do_not_disturb(self); g_signal_connect(self->priv->settings, "changed", G_CALLBACK(setting_changed_cb), self); /* Set up blacklist hints */ @@ -599,6 +604,66 @@ update_blacklist_hints(IndicatorNotifications *self, Notification *notification) save_blacklist_hints(self); } +/** + * update_do_not_disturb: + * @self: the indicator object + * + * Updates the icon with the do-not-disturb version and sets do-not-disturb options + * on external notification daemons that are supported. + **/ +static void +update_do_not_disturb(IndicatorNotifications *self) +{ + g_return_if_fail(IS_INDICATOR_NOTIFICATIONS(self)); + + /* TODO: update icons here */ + + /* Mate do-not-disturb support */ + settings_try_set_boolean(MATE_SCHEMA, MATE_KEY_DND, self->priv->do_not_disturb); +} + +/** + * settings_try_set_boolean: + * @schema: the GSettings schema + * @key: the GSettings key + * @value: the boolean value + * + * Checks to see if the schema and key exist before setting the value. + */ +static void +settings_try_set_boolean(const gchar *schema, const gchar *key, gboolean value) +{ + /* Check if we can access the schema */ + GSettingsSchemaSource *source = g_settings_schema_source_get_default(); + if (source == NULL) { + return; + } + + /* Lookup the schema */ + GSettingsSchema *source_schema = g_settings_schema_source_lookup(source, schema, FALSE); + + /* Couldn't find the schema */ + if (source_schema == NULL) { + return; + } + + /* Found the schema, make sure we have the key */ + if (g_settings_schema_has_key(source_schema, key)) { + /* Make sure the key is of boolean type */ + GSettingsSchemaKey *source_key = g_settings_schema_get_key(source_schema, key); + + if (g_variant_type_equal(g_settings_schema_key_get_value_type(source_key), G_VARIANT_TYPE_BOOLEAN)) { + /* Set the value */ + GSettings *settings = g_settings_new(schema); + g_settings_set_boolean(settings, key, value); + g_object_unref(settings); + } + + g_settings_schema_key_unref(source_key); + } + g_settings_schema_unref(source_schema); +} + /** * clear_item_activated_cb: * @menuitem: the clear menuitem @@ -663,6 +728,10 @@ setting_changed_cb(GSettings *settings, gchar *key, gpointer user_data) self->priv->hide_indicator = g_settings_get_boolean(settings, NOTIFICATIONS_KEY_HIDE_INDICATOR); update_indicator_visibility(self); } + else if(g_strcmp0(key, NOTIFICATIONS_KEY_DND) == 0) { + self->priv->do_not_disturb = g_settings_get_boolean(settings, NOTIFICATIONS_KEY_DND); + update_do_not_disturb(self); + } else if(g_strcmp0(key, NOTIFICATIONS_KEY_CLEAR_MC) == 0) { self->priv->clear_on_middle_click = g_settings_get_boolean(self->priv->settings, NOTIFICATIONS_KEY_CLEAR_MC); } diff --git a/src/settings.h b/src/settings.h index cb6c20e..8d12a8b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -9,7 +9,11 @@ #define NOTIFICATIONS_KEY_BLACKLIST "blacklist" #define NOTIFICATIONS_KEY_BLACKLIST_HINTS "blacklist-hints" #define NOTIFICATIONS_KEY_CLEAR_MC "clear-on-middle-click" +#define NOTIFICATIONS_KEY_DND "do-not-disturb" #define NOTIFICATIONS_KEY_HIDE_INDICATOR "hide-indicator" #define NOTIFICATIONS_KEY_MAX_ITEMS "max-items" +#define MATE_SCHEMA "org.mate.NotificationDaemon" +#define MATE_KEY_DND "do-not-disturb" + #endif -- cgit v1.2.3