From fef1531749b5e86922dee48c2c95b53d830d7543 Mon Sep 17 00:00:00 2001 From: Evangelos Ribeiro Tzaras Date: Fri, 16 Apr 2021 03:54:17 +0200 Subject: [PATCH] application: Add CallsSettings class This makes it easy to access application wide settings. --- data/meson.build | 11 ++ data/sm.puri.Calls.gschema.xml | 12 +++ debian/gnome-calls.install | 1 + src/calls-application.c | 23 ++++ src/calls-application.h | 5 +- src/calls-settings.c | 186 +++++++++++++++++++++++++++++++++ src/calls-settings.h | 41 ++++++++ src/meson.build | 1 + 8 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 data/sm.puri.Calls.gschema.xml create mode 100644 src/calls-settings.c create mode 100644 src/calls-settings.h diff --git a/data/meson.build b/data/meson.build index 0ec922d..c18aafe 100644 --- a/data/meson.build +++ b/data/meson.build @@ -67,3 +67,14 @@ install_data( 'apps' ) ) + +install_data('sm.puri.Calls.gschema.xml', + install_dir: join_paths(get_option('datadir'), 'glib-2.0/schemas') +) + +compile_schemas = find_program('glib-compile-schemas', required: false) +if compile_schemas.found() + test('Validate schema file', compile_schemas, + args: ['--strict', '--dry-run', meson.current_source_dir()] + ) +endif diff --git a/data/sm.puri.Calls.gschema.xml b/data/sm.puri.Calls.gschema.xml new file mode 100644 index 0000000..806be34 --- /dev/null +++ b/data/sm.puri.Calls.gschema.xml @@ -0,0 +1,12 @@ + + + + + + true + Whether calls should automatically use the default origin + Whether calls should automatically use the default origin + + + + diff --git a/debian/gnome-calls.install b/debian/gnome-calls.install index 821e0c7..f3d2dc9 100644 --- a/debian/gnome-calls.install +++ b/debian/gnome-calls.install @@ -2,6 +2,7 @@ /usr/bin /usr/lib /usr/share/applications +/usr/share/glib-2.0 /usr/share/icons /usr/share/locale /usr/share/metainfo diff --git a/src/calls-application.c b/src/calls-application.c index d4496a7..cca8f0a 100644 --- a/src/calls-application.c +++ b/src/calls-application.c @@ -35,6 +35,7 @@ #include "calls-call-window.h" #include "calls-main-window.h" #include "calls-manager.h" +#include "calls-settings.h" #include "calls-application.h" #include "version.h" @@ -63,6 +64,7 @@ struct _CallsApplication CallsRecordStore *record_store; CallsMainWindow *main_window; CallsCallWindow *call_window; + CallsSettings *settings; char *uri; }; @@ -374,6 +376,9 @@ start_proper (CallsApplication *self) self->call_window = calls_call_window_new (gtk_app); g_assert (self->call_window != NULL); + self->settings = calls_settings_new (); + g_assert (self->settings != NULL); + g_signal_connect (self->call_window, "notify::visible", G_CALLBACK (notify_window_visible_cb), @@ -522,6 +527,7 @@ finalize (GObject *object) g_clear_object (&self->record_store); g_clear_object (&self->ringer); g_clear_object (&self->notifier); + g_clear_object (&self->settings); g_free (self->uri); G_OBJECT_CLASS (calls_application_parent_class)->finalize (object); @@ -593,3 +599,20 @@ calls_application_new (void) "register-session", TRUE, NULL); } + +gboolean +calls_application_get_use_default_origins_setting (CallsApplication *self) +{ + g_return_val_if_fail (CALLS_IS_APPLICATION (self), FALSE); + + return calls_settings_get_use_default_origins (self->settings); +} + +void +calls_application_set_use_default_origins_setting (CallsApplication *self, + gboolean enabled) +{ + g_return_if_fail (CALLS_IS_APPLICATION (self)); + + calls_settings_set_use_default_origins (self->settings, enabled); +} diff --git a/src/calls-application.h b/src/calls-application.h index 08c3c56..361fb74 100644 --- a/src/calls-application.h +++ b/src/calls-application.h @@ -33,6 +33,9 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsApplication, calls_application, CALLS, APPLICATION, GtkApplication) -CallsApplication *calls_application_new (void); +CallsApplication *calls_application_new (void); +gboolean calls_application_get_use_default_origins_setting (CallsApplication *self); +void calls_application_set_use_default_origins_setting (CallsApplication *self, + gboolean enabled); G_END_DECLS diff --git a/src/calls-settings.c b/src/calls-settings.c new file mode 100644 index 0000000..c6aef96 --- /dev/null +++ b/src/calls-settings.c @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2021 Purism SPC + * + * This file is part of Calls. + * + * Calls is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calls is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY 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 Calls. If not, see . + * + * Author: Evangelos Ribeiro Tzaras + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#define G_LOG_DOMAIN "CallsSettings" + +#include "calls-settings.h" + +#include + +/** + * SECTION: calls-settings + * @title: CallsSettings + * @short_description: The application settings + * + * Manage application settings + */ + +enum { + PROP_0, + PROP_AUTO_USE_DEFAULT_ORIGINS, + PROP_LAST_PROP +}; +static GParamSpec *props[PROP_LAST_PROP]; + +struct _CallsSettings { + GObject parent_instance; + + GSettings *settings; +}; + +G_DEFINE_TYPE (CallsSettings, calls_settings, G_TYPE_OBJECT) + + +static void +calls_settings_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + CallsSettings *self = CALLS_SETTINGS (object); + + switch (prop_id) { + case PROP_AUTO_USE_DEFAULT_ORIGINS: + calls_settings_set_use_default_origins (self, g_value_get_boolean (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +static void +calls_settings_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + CallsSettings *self = CALLS_SETTINGS (object); + + switch (prop_id) { + case PROP_AUTO_USE_DEFAULT_ORIGINS: + g_value_set_boolean (value, calls_settings_get_use_default_origins (self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + + +static void +calls_settings_constructed (GObject *object) +{ + CallsSettings *self = CALLS_SETTINGS (object); + + g_settings_bind (self->settings, "auto-use-default-origins", + self, "auto-use-default-origins", G_SETTINGS_BIND_DEFAULT); + + G_OBJECT_CLASS (calls_settings_parent_class)->constructed (object); +} + + +static void +calls_settings_finalize (GObject *object) +{ + CallsSettings *self = CALLS_SETTINGS (object); + + g_object_unref (self->settings); + + G_OBJECT_CLASS (calls_settings_parent_class)->finalize (object); +} + + +static void +calls_settings_class_init (CallsSettingsClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->set_property = calls_settings_set_property; + object_class->get_property = calls_settings_get_property; + object_class->constructed = calls_settings_constructed; + object_class->finalize = calls_settings_finalize; + + props[PROP_AUTO_USE_DEFAULT_ORIGINS] = + g_param_spec_boolean ("auto-use-default-origins", + "auto use default origins", + "Automatically use default origins", + TRUE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, PROP_LAST_PROP, props); +} + +static void +calls_settings_init (CallsSettings *self) +{ + self->settings = g_settings_new ("sm.puri.Calls"); +} + +/** + * calls_settings_new: + * + * Returns: (transfer full): A #CallsSettings. + */ +CallsSettings * +calls_settings_new (void) +{ + return g_object_new (CALLS_TYPE_SETTINGS, NULL); +} + +/** + * calls_settings_get_use_default_origins: + * @self: A #CallsSettings + * + * Whether to prompt the user when there multiple origigins or fall back to defaults + * + * Returns: %TRUE if using defaults, %FALSE when the user should be prompted + */ +gboolean +calls_settings_get_use_default_origins (CallsSettings *self) +{ + g_return_val_if_fail (CALLS_IS_SETTINGS (self), FALSE); + + return g_settings_get_boolean (G_SETTINGS (self->settings), "auto-use-default-origins"); +} + +/** + * calls_settings_set_use_default_origins: + * @self: A #CallsSettings + * @enable: %TRUE to use default origins, %FALSE otherwise + * + * Sets whether to prompt the user when there multiple origigins or fall back to defaults + */ +void +calls_settings_set_use_default_origins (CallsSettings *self, + gboolean enable) +{ + g_return_if_fail (CALLS_IS_SETTINGS (self)); + + g_debug ("%sabling the use of default origins", enable ? "En" : "Dis"); + g_settings_set_boolean (G_SETTINGS (self->settings), "auto-use-default-origins", enable); +} diff --git a/src/calls-settings.h b/src/calls-settings.h new file mode 100644 index 0000000..6ec602f --- /dev/null +++ b/src/calls-settings.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2021 Purism SPC + * + * This file is part of Calls. + * + * Calls is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calls is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY 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 Calls. If not, see . + * + * Author: Evangelos Ribeiro Tzaras + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +#define CALLS_TYPE_SETTINGS (calls_settings_get_type ()) + +G_DECLARE_FINAL_TYPE (CallsSettings, calls_settings, CALLS, SETTINGS, GObject) + +CallsSettings *calls_settings_new (void); +gboolean calls_settings_get_use_default_origins (CallsSettings *self); +void calls_settings_set_use_default_origins (CallsSettings *self, + gboolean enable); + +G_END_DECLS + diff --git a/src/meson.build b/src/meson.build index cc21f61..3a64511 100644 --- a/src/meson.build +++ b/src/meson.build @@ -113,6 +113,7 @@ calls_sources = files(['calls-message-source.c', 'calls-message-source.h', 'calls-credentials.c', 'calls-credentials.h', 'calls-account.c', 'calls-account.h', 'calls-account-provider.c', 'calls-account-provider.h', + 'calls-settings.c', 'calls-settings.h', ]) + wayland_sources + calls_generated_sources calls_config_data = config_data