1 /*
2  * This file is part of GNOME LaTeX.
3  *
4  * Copyright (C) 2020 - Sébastien Wilmet <swilmet@gnome.org>
5  *
6  * GNOME LaTeX is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GNOME LaTeX is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GNOME LaTeX.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "latexila-settings.h"
21 
22 /**
23  * SECTION:settings
24  * @title: LatexilaSettings
25  * @short_description: Central access to #GSettings objects
26  *
27  * #LatexilaSettings is a singleton class to have a central access to #GSettings
28  * objects. The central access permits to share the same #GSettings objects
29  * between different parts of the application, which normally increases the
30  * performances when synchronizing the values (but this hasn't been verified).
31  */
32 
33 struct _LatexilaSettingsPrivate
34 {
35 	GSettings *settings_editor;
36 };
37 
38 /* LatexilaSettings is a singleton. */
39 static LatexilaSettings *singleton = NULL;
40 
G_DEFINE_TYPE_WITH_PRIVATE(LatexilaSettings,latexila_settings,G_TYPE_OBJECT)41 G_DEFINE_TYPE_WITH_PRIVATE (LatexilaSettings, latexila_settings, G_TYPE_OBJECT)
42 
43 static void
44 latexila_settings_dispose (GObject *object)
45 {
46 	LatexilaSettings *self = LATEXILA_SETTINGS (object);
47 
48 	g_clear_object (&self->priv->settings_editor);
49 
50 	G_OBJECT_CLASS (latexila_settings_parent_class)->dispose (object);
51 }
52 
53 static void
latexila_settings_finalize(GObject * object)54 latexila_settings_finalize (GObject *object)
55 {
56 	if (singleton == LATEXILA_SETTINGS (object))
57 	{
58 		singleton = NULL;
59 	}
60 
61 	G_OBJECT_CLASS (latexila_settings_parent_class)->finalize (object);
62 }
63 
64 static void
latexila_settings_class_init(LatexilaSettingsClass * klass)65 latexila_settings_class_init (LatexilaSettingsClass *klass)
66 {
67 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
68 
69 	object_class->dispose = latexila_settings_dispose;
70 	object_class->finalize = latexila_settings_finalize;
71 }
72 
73 static void
latexila_settings_init(LatexilaSettings * self)74 latexila_settings_init (LatexilaSettings *self)
75 {
76 	self->priv = latexila_settings_get_instance_private (self);
77 
78 	self->priv->settings_editor = g_settings_new ("org.gnome.gnome-latex.preferences.editor");
79 }
80 
81 /**
82  * latexila_settings_get_singleton:
83  *
84  * Returns: (transfer none): the #LatexilaSettings singleton instance.
85  */
86 LatexilaSettings *
latexila_settings_get_singleton(void)87 latexila_settings_get_singleton (void)
88 {
89 	if (singleton == NULL)
90 	{
91 		singleton = g_object_new (LATEXILA_TYPE_SETTINGS, NULL);
92 	}
93 
94 	return singleton;
95 }
96 
97 void
_latexila_settings_unref_singleton(void)98 _latexila_settings_unref_singleton (void)
99 {
100 	if (singleton != NULL)
101 	{
102 		g_object_unref (singleton);
103 	}
104 
105 	/* singleton is not set to NULL here, it is set to NULL in
106 	 * latexila_settings_finalize() (i.e. when we are sure that the ref
107 	 * count reaches 0).
108 	 */
109 }
110 
111 /**
112  * latexila_settings_peek_editor_settings:
113  * @self: the #LatexilaSettings instance.
114  *
115  * Returns: (transfer none): the #GSettings for
116  * `"org.gnome.gnome-latex.preferences.editor"`.
117  */
118 GSettings *
latexila_settings_peek_editor_settings(LatexilaSettings * self)119 latexila_settings_peek_editor_settings (LatexilaSettings *self)
120 {
121 	g_return_val_if_fail (LATEXILA_IS_SETTINGS (self), NULL);
122 	return self->priv->settings_editor;
123 }
124