1 /* bjb-settings.c
2 * Copyright (C) Pierre-Yves LUYTEN 2011 <py@luyten.fr>
3 *
4 * bijiben is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * bijiben is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.*/
16
17 #include "config.h"
18
19 #include <glib/gi18n.h>
20 #include <gtk/gtk.h>
21
22 #include "bjb-application.h"
23 #include "bjb-settings.h"
24 #include "bjb-settings-dialog.h"
25
26
27 struct _BjbSettings
28 {
29 GSettings parent_instance;
30
31 /* Note Appearance settings */
32 gboolean use_system_font;
33 gchar *font;
34 gchar *color;
35
36 /* Default Provider */
37 gchar *primary;
38
39 /* org.gnome.desktop */
40 GSettings *system;
41 };
42
43
44 enum
45 {
46 PROP_0,
47 PROP_USE_SYSTEM_FONT,
48 PROP_FONT,
49 PROP_COLOR,
50 PROP_PRIMARY,
51 N_PROPERTIES
52 };
53
54
55 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
56
G_DEFINE_TYPE(BjbSettings,bjb_settings,G_TYPE_SETTINGS)57 G_DEFINE_TYPE (BjbSettings, bjb_settings, G_TYPE_SETTINGS)
58
59 static void
60 bjb_settings_init (BjbSettings *self)
61 {
62 }
63
64 static void
bjb_settings_finalize(GObject * object)65 bjb_settings_finalize (GObject *object)
66 {
67 BjbSettings *self;
68
69 self = BJB_SETTINGS (object);
70 g_object_unref (self->system);
71
72 g_free (self->font);
73 g_free (self->color);
74 g_free (self->primary);
75
76 G_OBJECT_CLASS (bjb_settings_parent_class)->finalize (object);
77 }
78
79 static void
bjb_settings_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)80 bjb_settings_get_property (GObject *object,
81 guint prop_id,
82 GValue *value,
83 GParamSpec *pspec)
84 {
85 BjbSettings *self = BJB_SETTINGS (object);
86
87 switch (prop_id)
88 {
89 case PROP_USE_SYSTEM_FONT:
90 g_value_set_boolean (value, self->use_system_font);
91 break;
92
93 case PROP_FONT:
94 g_value_set_string (value, self->font);
95 break;
96
97 case PROP_COLOR:
98 g_value_set_string (value, self->color);
99 break;
100
101 case PROP_PRIMARY:
102 g_value_set_string (value, self->primary);
103 break;
104
105 default:
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 break;
108 }
109 }
110
111 static void
bjb_settings_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)112 bjb_settings_set_property (GObject *object,
113 guint prop_id,
114 const GValue *value,
115 GParamSpec *pspec)
116 {
117 BjbSettings *self = BJB_SETTINGS (object);
118 GSettings *settings = G_SETTINGS (object);
119
120 switch (prop_id)
121 {
122 case PROP_USE_SYSTEM_FONT:
123 self->use_system_font = g_value_get_boolean (value);
124 g_settings_set_boolean (settings, "use-system-font", self->use_system_font);
125 break;
126
127 case PROP_FONT:
128 g_free (self->font);
129 self->font = g_value_dup_string (value);
130 g_settings_set_string (settings, "font", self->font);
131 break;
132
133 case PROP_COLOR:
134 g_free (self->color);
135 self->color = g_value_dup_string (value);
136 g_settings_set_string (settings, "color", self->color);
137 break;
138
139 case PROP_PRIMARY:
140 g_free (self->primary);
141 self->primary = g_value_dup_string (value);
142 g_settings_set_string (settings, "default-location", self->primary);
143 break;
144
145 default:
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 break;
148 }
149 }
150
151
152 static void
bjb_settings_constructed(GObject * object)153 bjb_settings_constructed (GObject *object)
154 {
155 BjbSettings *self;
156 GSettings *settings;
157
158 G_OBJECT_CLASS (bjb_settings_parent_class)->constructed (object);
159
160 self = BJB_SETTINGS (object);
161 settings = G_SETTINGS (object);
162 self->system = g_settings_new ("org.gnome.desktop.interface");
163
164 self->use_system_font = g_settings_get_boolean (settings, "use-system-font");
165 self->font = g_settings_get_string (settings, "font");
166 self->color = g_settings_get_string (settings, "color");
167 self->primary = g_settings_get_string (settings, "default-location");
168 }
169
170
171 static void
bjb_settings_class_init(BjbSettingsClass * klass)172 bjb_settings_class_init (BjbSettingsClass *klass)
173 {
174 GObjectClass* object_class = G_OBJECT_CLASS (klass);
175
176 object_class->constructed = bjb_settings_constructed;
177 object_class->finalize = bjb_settings_finalize;
178 object_class->get_property = bjb_settings_get_property;
179 object_class->set_property = bjb_settings_set_property;
180
181 properties[PROP_USE_SYSTEM_FONT] = g_param_spec_boolean (
182 "use-system-font",
183 "Use system font",
184 "Default System Font for Notes",
185 TRUE,
186 G_PARAM_READWRITE |
187 G_PARAM_STATIC_STRINGS);
188
189
190 properties[PROP_FONT] = g_param_spec_string (
191 "font",
192 "Notes Font",
193 "Font for Notes",
194 NULL,
195 G_PARAM_READWRITE |
196 G_PARAM_STATIC_STRINGS);
197
198
199 properties[PROP_COLOR] = g_param_spec_string (
200 "color",
201 "New Notes Color",
202 "Default Color for New Notes",
203 NULL,
204 G_PARAM_READWRITE |
205 G_PARAM_STATIC_STRINGS);
206
207
208 properties[PROP_PRIMARY] = g_param_spec_string (
209 "default-location",
210 "Primary Location",
211 "Default Provider for New Notes",
212 NULL,
213 G_PARAM_READWRITE |
214 G_PARAM_STATIC_STRINGS);
215
216
217 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
218 }
219
220
221
222
223 BjbSettings *
bjb_settings_new(void)224 bjb_settings_new (void)
225 {
226 return g_object_new (BJB_TYPE_SETTINGS, "schema-id", "org.gnome.Notes", NULL);
227 }
228
229
230 gboolean
bjb_settings_use_system_font(BjbSettings * self)231 bjb_settings_use_system_font (BjbSettings *self)
232 {
233 return self->use_system_font;
234 }
235
236
237 void
bjb_settings_set_use_system_font(BjbSettings * self,gboolean value)238 bjb_settings_set_use_system_font (BjbSettings *self, gboolean value)
239 {
240 self->use_system_font = value;
241 }
242
243
244 const gchar *
bjb_settings_get_default_font(BjbSettings * self)245 bjb_settings_get_default_font (BjbSettings *self)
246 {
247 return self->font;
248 }
249
250
251 const gchar *
bjb_settings_get_default_color(BjbSettings * self)252 bjb_settings_get_default_color (BjbSettings *self)
253 {
254 return self->color;
255 }
256
257
258 const gchar *
bjb_settings_get_default_location(BjbSettings * self)259 bjb_settings_get_default_location (BjbSettings *self)
260 {
261 return self->primary;
262 }
263
264
265 gchar *
bjb_settings_get_system_font(BjbSettings * self)266 bjb_settings_get_system_font (BjbSettings *self)
267 {
268 return g_settings_get_string (self->system,
269 "document-font-name");
270 }
271
272 BjbTextSizeType
bjb_settings_get_text_size(BjbSettings * self)273 bjb_settings_get_text_size (BjbSettings *self)
274 {
275 return g_settings_get_enum (G_SETTINGS (self), "text-size");
276 }
277
278 void
show_bijiben_settings_window(GtkWidget * parent_window)279 show_bijiben_settings_window (GtkWidget *parent_window)
280 {
281 GtkDialog *dialog;
282
283 dialog = bjb_settings_dialog_new ();
284 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window));
285
286 /* result = */ gtk_dialog_run (dialog);
287 gtk_widget_destroy (GTK_WIDGET (dialog));
288 }
289