No issues found
1 /*
2 * e-book-source-config.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16 *
17 */
18
19 #include "e-book-source-config.h"
20
21 #include <config.h>
22 #include <glib/gi18n-lib.h>
23
24 #define E_BOOK_SOURCE_CONFIG_GET_PRIVATE(obj) \
25 (G_TYPE_INSTANCE_GET_PRIVATE \
26 ((obj), E_TYPE_BOOK_SOURCE_CONFIG, EBookSourceConfigPrivate))
27
28 struct _EBookSourceConfigPrivate {
29 GtkWidget *default_button;
30 GtkWidget *autocomplete_button;
31 };
32
33 G_DEFINE_TYPE (
34 EBookSourceConfig,
35 e_book_source_config,
36 E_TYPE_SOURCE_CONFIG)
37
38 static ESource *
39 book_source_config_ref_default (ESourceConfig *config)
40 {
41 ESourceRegistry *registry;
42
43 registry = e_source_config_get_registry (config);
44
45 return e_source_registry_ref_default_address_book (registry);
46 }
47
48 static void
49 book_source_config_set_default (ESourceConfig *config,
50 ESource *source)
51 {
52 ESourceRegistry *registry;
53
54 registry = e_source_config_get_registry (config);
55
56 e_source_registry_set_default_address_book (registry, source);
57 }
58
59 static void
60 book_source_config_dispose (GObject *object)
61 {
62 EBookSourceConfigPrivate *priv;
63
64 priv = E_BOOK_SOURCE_CONFIG_GET_PRIVATE (object);
65
66 if (priv->default_button != NULL) {
67 g_object_unref (priv->default_button);
68 priv->default_button = NULL;
69 }
70
71 if (priv->autocomplete_button != NULL) {
72 g_object_unref (priv->autocomplete_button);
73 priv->autocomplete_button = NULL;
74 }
75
76 /* Chain up to parent's dispose() method. */
77 G_OBJECT_CLASS (e_book_source_config_parent_class)->dispose (object);
78 }
79
80 static void
81 book_source_config_constructed (GObject *object)
82 {
83 EBookSourceConfigPrivate *priv;
84 ESource *default_source;
85 ESource *original_source;
86 ESourceConfig *config;
87 GObjectClass *class;
88 GtkWidget *widget;
89 const gchar *label;
90
91 /* Chain up to parent's constructed() method. */
92 class = G_OBJECT_CLASS (e_book_source_config_parent_class);
93 class->constructed (object);
94
95 config = E_SOURCE_CONFIG (object);
96 priv = E_BOOK_SOURCE_CONFIG_GET_PRIVATE (object);
97
98 label = _("Mark as default address book");
99 widget = gtk_check_button_new_with_label (label);
100 priv->default_button = g_object_ref_sink (widget);
101 gtk_widget_show (widget);
102
103 label = _("Autocomplete with this address book");
104 widget = gtk_check_button_new_with_label (label);
105 priv->autocomplete_button = g_object_ref_sink (widget);
106 gtk_widget_show (widget);
107
108 default_source = book_source_config_ref_default (config);
109 original_source = e_source_config_get_original_source (config);
110
111 if (original_source != NULL) {
112 gboolean active;
113
114 active = e_source_equal (original_source, default_source);
115 g_object_set (priv->default_button, "active", active, NULL);
116 }
117
118 g_object_unref (default_source);
119
120 e_source_config_insert_widget (
121 config, NULL, NULL, priv->default_button);
122
123 e_source_config_insert_widget (
124 config, NULL, NULL, priv->autocomplete_button);
125 }
126
127 static const gchar *
128 book_source_config_get_backend_extension_name (ESourceConfig *config)
129 {
130 return E_SOURCE_EXTENSION_ADDRESS_BOOK;
131 }
132
133 static GList *
134 book_source_config_list_eligible_collections (ESourceConfig *config)
135 {
136 GQueue trash = G_QUEUE_INIT;
137 GList *list, *link;
138
139 /* Chain up to parent's list_eligible_collections() method. */
140 list = E_SOURCE_CONFIG_CLASS (e_book_source_config_parent_class)->
141 list_eligible_collections (config);
142
143 for (link = list; link != NULL; link = g_list_next (link)) {
144 ESource *source = E_SOURCE (link->data);
145 ESourceCollection *extension;
146 const gchar *extension_name;
147
148 extension_name = E_SOURCE_EXTENSION_COLLECTION;
149 extension = e_source_get_extension (source, extension_name);
150
151 if (!e_source_collection_get_contacts_enabled (extension))
152 g_queue_push_tail (&trash, link);
153 }
154
155 /* Remove ineligible collections from the list. */
156 while ((link = g_queue_pop_head (&trash)) != NULL) {
157 g_object_unref (link->data);
158 list = g_list_delete_link (list, link);
159 }
160
161 return list;
162 }
163
164 static void
165 book_source_config_init_candidate (ESourceConfig *config,
166 ESource *scratch_source)
167 {
168 EBookSourceConfigPrivate *priv;
169 ESourceConfigClass *class;
170 ESourceExtension *extension;
171 const gchar *extension_name;
172
173 /* Chain up to parent's init_candidate() method. */
174 class = E_SOURCE_CONFIG_CLASS (e_book_source_config_parent_class);
175 class->init_candidate (config, scratch_source);
176
177 priv = E_BOOK_SOURCE_CONFIG_GET_PRIVATE (config);
178
179 extension_name = E_SOURCE_EXTENSION_AUTOCOMPLETE;
180 extension = e_source_get_extension (scratch_source, extension_name);
181
182 g_object_bind_property (
183 extension, "include-me",
184 priv->autocomplete_button, "active",
185 G_BINDING_BIDIRECTIONAL |
186 G_BINDING_SYNC_CREATE);
187 }
188
189 static void
190 book_source_config_commit_changes (ESourceConfig *config,
191 ESource *scratch_source)
192 {
193 EBookSourceConfigPrivate *priv;
194 ESourceConfigClass *class;
195 ESource *default_source;
196 GtkToggleButton *toggle_button;
197
198 priv = E_BOOK_SOURCE_CONFIG_GET_PRIVATE (config);
199 toggle_button = GTK_TOGGLE_BUTTON (priv->default_button);
200
201 /* Chain up to parent's commit_changes() method. */
202 class = E_SOURCE_CONFIG_CLASS (e_book_source_config_parent_class);
203 class->commit_changes (config, scratch_source);
204
205 default_source = book_source_config_ref_default (config);
206
207 /* The default setting is a little tricky to get right. If
208 * the toggle button is active, this ESource is now the default.
209 * That much is simple. But if the toggle button is NOT active,
210 * then we have to inspect the old default. If this ESource WAS
211 * the default, reset the default to 'system'. If this ESource
212 * WAS NOT the old default, leave it alone. */
213 if (gtk_toggle_button_get_active (toggle_button))
214 book_source_config_set_default (config, scratch_source);
215 else if (e_source_equal (scratch_source, default_source))
216 book_source_config_set_default (config, NULL);
217
218 g_object_unref (default_source);
219 }
220
221 static void
222 e_book_source_config_class_init (EBookSourceConfigClass *class)
223 {
224 GObjectClass *object_class;
225 ESourceConfigClass *source_config_class;
226
227 g_type_class_add_private (class, sizeof (EBookSourceConfigPrivate));
228
229 object_class = G_OBJECT_CLASS (class);
230 object_class->dispose = book_source_config_dispose;
231 object_class->constructed = book_source_config_constructed;
232
233 source_config_class = E_SOURCE_CONFIG_CLASS (class);
234 source_config_class->get_backend_extension_name =
235 book_source_config_get_backend_extension_name;
236 source_config_class->list_eligible_collections =
237 book_source_config_list_eligible_collections;
238 source_config_class->init_candidate = book_source_config_init_candidate;
239 source_config_class->commit_changes = book_source_config_commit_changes;
240 }
241
242 static void
243 e_book_source_config_init (EBookSourceConfig *config)
244 {
245 config->priv = E_BOOK_SOURCE_CONFIG_GET_PRIVATE (config);
246 }
247
248 GtkWidget *
249 e_book_source_config_new (ESourceRegistry *registry,
250 ESource *original_source)
251 {
252 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
253
254 if (original_source != NULL)
255 g_return_val_if_fail (E_IS_SOURCE (original_source), NULL);
256
257 return g_object_new (
258 E_TYPE_BOOK_SOURCE_CONFIG, "registry", registry,
259 "original-source", original_source, NULL);
260 }
261
262 void
263 e_book_source_config_add_offline_toggle (EBookSourceConfig *config,
264 ESource *scratch_source)
265 {
266 GtkWidget *widget;
267 ESourceExtension *extension;
268 const gchar *extension_name;
269
270 g_return_if_fail (E_IS_BOOK_SOURCE_CONFIG (config));
271 g_return_if_fail (E_IS_SOURCE (scratch_source));
272
273 extension_name = E_SOURCE_EXTENSION_OFFLINE;
274 extension = e_source_get_extension (scratch_source, extension_name);
275
276 widget = gtk_check_button_new_with_label (
277 _("Copy book content locally for offline operation"));
278 e_source_config_insert_widget (
279 E_SOURCE_CONFIG (config), scratch_source, NULL, widget);
280 gtk_widget_show (widget);
281
282 g_object_bind_property (
283 extension, "stay-synchronized",
284 widget, "active",
285 G_BINDING_BIDIRECTIONAL |
286 G_BINDING_SYNC_CREATE);
287 }