diff options
author | Lars Uebernickel <lars.uebernickel@canonical.com> | 2012-06-26 00:18:18 +0200 |
---|---|---|
committer | Lars Uebernickel <lars.uebernickel@canonical.com> | 2012-06-26 00:18:18 +0200 |
commit | 106146172620ce24dc34575a3ad5237251a80407 (patch) | |
tree | a1d8b762fcb70f46208e69dae7b2d4c1c7ba8e45 /src | |
parent | 82c080337fc3f420043dab2784f598472c7cfda0 (diff) | |
download | ayatana-indicator-messages-106146172620ce24dc34575a3ad5237251a80407.tar.gz ayatana-indicator-messages-106146172620ce24dc34575a3ad5237251a80407.tar.bz2 ayatana-indicator-messages-106146172620ce24dc34575a3ad5237251a80407.zip |
messages-service.c: move gsettings strv handling into gsettingsstrv.[ch]
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/gsettingsstrv.c | 106 | ||||
-rw-r--r-- | src/gsettingsstrv.h | 33 | ||||
-rw-r--r-- | src/messages-service.c | 51 |
4 files changed, 145 insertions, 49 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 95a6258..79fdee6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,7 +46,9 @@ indicator_messages_service_SOURCES = \ app-section.h \ dbus-data.h \ gactionmuxer.c \ - gactionmuxer.h + gactionmuxer.h \ + gsettingsstrv.c \ + gsettingsstrv.h indicator_messages_service_CFLAGS = \ $(APPLET_CFLAGS) \ diff --git a/src/gsettingsstrv.c b/src/gsettingsstrv.c new file mode 100644 index 0000000..cf889fa --- /dev/null +++ b/src/gsettingsstrv.c @@ -0,0 +1,106 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Lars Uebernickel <lars.uebernickel@canonical.com> + */ + +#include "gsettingsstrv.h" + +/** + * g_settings_strv_append_unique: + * @settings: a #GSettings object + * @key: the key at which @settings contains a string array + * @item: the string to append + * + * Appends @item to the string array at @key if that string array doesn't + * contain @item yet. + * + * Returns: TRUE if @item was added to the list, FALSE if it already existed. + */ +gboolean +g_settings_strv_append_unique (GSettings *settings, + const gchar *key, + const gchar *item) +{ + gchar **strv; + gchar **it; + gboolean add = TRUE; + + g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE); + g_return_val_if_fail (key != NULL, FALSE); + g_return_val_if_fail (item != NULL, FALSE); + + strv = g_settings_get_strv (settings, key); + + for (it = strv; *it; it++) + { + if (g_str_equal (*it, item)) + { + add = FALSE; + break; + } + } + + if (add) + { + GVariantBuilder builder; + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); + + for (it = strv; *it; it++) + g_variant_builder_add (&builder, "s", *it); + g_variant_builder_add (&builder, "s", item); + + g_settings_set_value (settings, key, g_variant_builder_end (&builder)); + } + + g_strfreev (strv); + return add; +} + +/** + * g_settings_strv_remove: + * @settings: a #GSettings object + * @key: the key at which @settings contains a string array + * @item: the string to remove + * + * Removes all occurences of @item in @key. + */ +void +g_settings_strv_remove (GSettings *settings, + const gchar *key, + const gchar *item) +{ + gchar **strv; + gchar **it; + GVariantBuilder builder; + + g_return_if_fail (G_IS_SETTINGS (settings)); + g_return_if_fail (key != NULL); + g_return_if_fail (item != NULL); + + strv = g_settings_get_strv (settings, key); + + g_variant_builder_init (&builder, (GVariantType *)"as"); + for (it = strv; *it; it++) + { + if (!g_str_equal (*it, item)) + g_variant_builder_add (&builder, "s", *it); + } + g_settings_set_value (settings, key, g_variant_builder_end (&builder)); + + g_strfreev (strv); +} diff --git a/src/gsettingsstrv.h b/src/gsettingsstrv.h new file mode 100644 index 0000000..accebda --- /dev/null +++ b/src/gsettingsstrv.h @@ -0,0 +1,33 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Lars Uebernickel <lars.uebernickel@canonical.com> + */ + +#ifndef __G_SETTINGS_STRV_H__ +#define __G_SETTINGS_STRV_H__ + +#include <gio/gio.h> + +gboolean g_settings_strv_append_unique (GSettings *settings, + const gchar *key, + const gchar *item); + +void g_settings_strv_remove (GSettings *settings, + const gchar *key, + const gchar *item); + +#endif diff --git a/src/messages-service.c b/src/messages-service.c index 33ad237..f07ea68 100644 --- a/src/messages-service.c +++ b/src/messages-service.c @@ -31,6 +31,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>. #include "dbus-data.h" #include "messages-service-dbus.h" #include "gactionmuxer.h" +#include "gsettingsstrv.h" static GHashTable *applications; @@ -237,22 +238,6 @@ clear_action_handler (MessageServiceDbus *msd, g_simple_action_set_enabled (action, attention); } -static gint -g_strv_find (gchar **str_array, - const gchar *key) -{ - gchar **it; - - g_return_val_if_fail (str_array != NULL, -1); - - for (it = str_array; *it; it++) { - if (!g_strcmp0 (key, *it)) - return it - str_array; - } - - return -1; -} - static void register_application (MessageServiceDbus *msd, const gchar *sender, @@ -261,7 +246,6 @@ register_application (MessageServiceDbus *msd, gpointer user_data) { AppSection *section; - gchar **applications; GDBusConnection *bus; section = add_application (desktop_id); @@ -272,22 +256,7 @@ register_application (MessageServiceDbus *msd, app_section_set_object_path (section, bus, sender, menu_path); - /* remember this application in the settings key */ - applications = g_settings_get_strv (settings, "applications"); - if (g_strv_find (applications, desktop_id) < 0) { - GVariantBuilder builder; - gchar **app; - - g_variant_builder_init (&builder, (GVariantType *)"as"); - for (app = applications; *app; app++) - g_variant_builder_add (&builder, "s", *app); - g_variant_builder_add (&builder, "s", desktop_id); - - g_settings_set_value (settings, "applications", - g_variant_builder_end (&builder)); - } - - g_strfreev (applications); + g_settings_strv_append_unique (settings, "applications", desktop_id); } static void @@ -295,22 +264,8 @@ unregister_application (MessageServiceDbus *msd, const gchar *desktop_id, gpointer user_data) { - gchar **applications = g_settings_get_strv (settings, "applications"); - gchar **app; - GVariantBuilder builder; - - g_variant_builder_init (&builder, (GVariantType *)"as"); - for (app = applications; *app; app++) { - if (g_strcmp0 (desktop_id, *app)) - g_variant_builder_add (&builder, "s", *app); - } - - g_settings_set_value (settings, "applications", - g_variant_builder_end (&builder)); - remove_application (desktop_id); - - g_strfreev (applications); + g_settings_strv_remove (settings, "applications", desktop_id); } static void |