Updated Spanish translation
[evolution.git] / e-util / test-source-selector.c
blobc3782cbdd44175e8aea2336f83d30713063531d7
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-source-selector.c - Test program for the ESourceSelector widget.
4 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <e-util/e-util.h>
21 #define OPENED_KEY "sources-opened-key"
22 #define SOURCE_TYPE_KEY "sources-source-type-key"
23 #define EXTENSION_NAME_KEY "sources-extension-name-key"
24 #define TOOLTIP_ENTRY_KEY "sources-tooltip-entry-key"
26 static void
27 dump_selection (ESourceSelector *selector,
28 const gchar *extension_name)
30 GList *list, *link;
32 list = e_source_selector_get_selection (selector);
34 g_print ("Current selection at %s:\n", extension_name);
36 if (list == NULL)
37 g_print ("\t(None)\n");
39 for (link = list; link != NULL; link = g_list_next (link->next)) {
40 ESource *source = E_SOURCE (link->data);
41 ESourceBackend *extension;
43 extension = e_source_get_extension (source, extension_name);
45 g_print (
46 "\tSource %s (backend %s)\n",
47 e_source_get_display_name (source),
48 e_source_backend_get_backend_name (extension));
51 g_list_free_full (list, (GDestroyNotify) g_object_unref);
54 static void
55 selection_changed_callback (ESourceSelector *selector)
57 const gchar *extension_name;
59 g_print ("Selection changed!\n");
61 extension_name = g_object_get_data (
62 G_OBJECT (selector), EXTENSION_NAME_KEY);
63 dump_selection (selector, extension_name);
66 static void
67 enable_widget_if_opened_cb (ESourceSelector *selector,
68 GtkWidget *widget)
70 GHashTable *opened_sources;
71 ESource *source;
72 gboolean sensitive = FALSE;
74 opened_sources = g_object_get_data (G_OBJECT (selector), OPENED_KEY);
75 g_return_if_fail (opened_sources != NULL);
77 source = e_source_selector_ref_primary_selection (selector);
78 if (source != NULL)
79 sensitive = g_hash_table_contains (opened_sources, source);
80 gtk_widget_set_sensitive (widget, sensitive);
81 g_clear_object (&source);
84 static void
85 disable_widget_if_opened_cb (ESourceSelector *selector,
86 GtkWidget *widget)
88 GHashTable *opened_sources;
89 ESource *source;
90 gboolean sensitive = FALSE;
92 opened_sources = g_object_get_data (G_OBJECT (selector), OPENED_KEY);
93 g_return_if_fail (opened_sources != NULL);
95 source = e_source_selector_ref_primary_selection (selector);
96 if (source != NULL)
97 sensitive = !g_hash_table_contains (opened_sources, source);
98 gtk_widget_set_sensitive (widget, sensitive);
99 g_clear_object (&source);
102 static void
103 enable_widget_if_any_selected (ESourceSelector *selector,
104 GtkWidget *widget)
106 ESource *source;
107 gboolean sensitive;
109 source = e_source_selector_ref_primary_selection (selector);
110 sensitive = (source != NULL);
111 gtk_widget_set_sensitive (widget, sensitive);
112 g_clear_object (&source);
115 static void
116 open_selected_clicked_cb (GtkWidget *button,
117 ESourceSelector *selector)
119 GHashTable *opened_sources;
120 ESource *source;
122 opened_sources = g_object_get_data (G_OBJECT (selector), OPENED_KEY);
123 g_return_if_fail (opened_sources != NULL);
125 source = e_source_selector_ref_primary_selection (selector);
126 if (source == NULL)
127 return;
129 if (!g_hash_table_contains (opened_sources, source)) {
130 EClient *client;
131 ECalClientSourceType source_type;
132 gpointer data;
133 GError *local_error = NULL;
135 data = g_object_get_data (G_OBJECT (selector), SOURCE_TYPE_KEY);
136 source_type = GPOINTER_TO_UINT (data);
138 if (source_type == E_CAL_CLIENT_SOURCE_TYPE_LAST)
139 client = e_book_client_connect_sync (
140 source, (guint32) -1, NULL, &local_error);
141 else
142 client = e_cal_client_connect_sync (
143 source, source_type, (guint32) -1, NULL, &local_error);
145 if (client != NULL) {
146 g_hash_table_insert (
147 opened_sources,
148 g_object_ref (source),
149 g_object_ref (client));
150 g_signal_emit_by_name (
151 selector, "primary-selection-changed", 0);
152 g_object_unref (client);
155 if (local_error != NULL) {
156 g_warning (
157 "Failed to open '%s': %s",
158 e_source_get_display_name (source),
159 local_error->message);
160 g_error_free (local_error);
164 g_object_unref (source);
167 static void
168 close_selected_clicked_cb (GtkWidget *button,
169 ESourceSelector *selector)
171 GHashTable *opened_sources;
172 ESource *source;
174 opened_sources = g_object_get_data (G_OBJECT (selector), OPENED_KEY);
175 g_return_if_fail (opened_sources != NULL);
177 source = e_source_selector_ref_primary_selection (selector);
178 if (source == NULL)
179 return;
181 if (g_hash_table_remove (opened_sources, source))
182 g_signal_emit_by_name (
183 selector, "primary-selection-changed", 0);
185 g_object_unref (source);
188 static void
189 flip_busy_clicked_cb (GtkWidget *button,
190 ESourceSelector *selector)
192 ESource *source;
194 source = e_source_selector_ref_primary_selection (selector);
195 if (source)
196 e_source_selector_set_source_is_busy (selector, source,
197 !e_source_selector_get_source_is_busy (selector, source));
199 g_clear_object (&source);
202 static void
203 set_tooltip_clicked_cb (GtkWidget *button,
204 ESourceSelector *selector)
206 ESource *source;
207 GtkEntry *entry;
209 entry = g_object_get_data (G_OBJECT (button), TOOLTIP_ENTRY_KEY);
210 g_return_if_fail (entry != NULL);
212 source = e_source_selector_ref_primary_selection (selector);
213 if (source)
214 e_source_selector_set_source_tooltip (selector, source,
215 gtk_entry_get_text (entry));
217 g_clear_object (&source);
220 static GtkWidget *
221 create_page (ESourceRegistry *registry,
222 const gchar *extension_name,
223 ECalClientSourceType source_type)
225 GtkWidget *widget, *subwindow, *selector, *button_box, *entry;
226 GtkGrid *grid;
227 GHashTable *opened_sources;
229 grid = GTK_GRID (gtk_grid_new ());
231 subwindow = gtk_scrolled_window_new (NULL, NULL);
232 g_object_set (
233 G_OBJECT (subwindow),
234 "halign", GTK_ALIGN_FILL,
235 "hexpand", TRUE,
236 "valign", GTK_ALIGN_FILL,
237 "vexpand", TRUE,
238 NULL);
240 selector = e_source_selector_new (registry, extension_name);
241 g_object_set (
242 G_OBJECT (selector),
243 "halign", GTK_ALIGN_FILL,
244 "hexpand", TRUE,
245 "valign", GTK_ALIGN_FILL,
246 "vexpand", TRUE,
247 "show-toggles", FALSE,
248 "show-colors", source_type != E_CAL_CLIENT_SOURCE_TYPE_LAST,
249 NULL);
250 gtk_container_add (GTK_CONTAINER (subwindow), selector);
252 gtk_grid_attach (grid, subwindow, 0, 0, 1, 5);
254 button_box = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
255 g_object_set (
256 G_OBJECT (button_box),
257 "halign", GTK_ALIGN_START,
258 "hexpand", FALSE,
259 "valign", GTK_ALIGN_START,
260 "vexpand", FALSE,
261 NULL);
262 gtk_grid_attach (grid, button_box, 1, 0, 1, 1);
264 widget = gtk_button_new_with_label ("Open selected");
265 gtk_container_add (GTK_CONTAINER (button_box), widget);
267 g_signal_connect (
268 widget, "clicked",
269 G_CALLBACK (open_selected_clicked_cb), selector);
270 g_signal_connect (
271 selector, "primary-selection-changed",
272 G_CALLBACK (disable_widget_if_opened_cb), widget);
274 widget = gtk_button_new_with_label ("Close selected");
275 gtk_container_add (GTK_CONTAINER (button_box), widget);
277 g_signal_connect (
278 widget, "clicked",
279 G_CALLBACK (close_selected_clicked_cb), selector);
280 g_signal_connect (
281 selector, "primary-selection-changed",
282 G_CALLBACK (enable_widget_if_opened_cb), widget);
284 widget = gtk_button_new_with_label ("Flip busy status");
285 gtk_widget_set_margin_top (widget, 10);
286 gtk_container_add (GTK_CONTAINER (button_box), widget);
288 g_signal_connect (
289 widget, "clicked",
290 G_CALLBACK (flip_busy_clicked_cb), selector);
291 g_signal_connect (
292 selector, "primary-selection-changed",
293 G_CALLBACK (enable_widget_if_any_selected), widget);
295 entry = gtk_entry_new ();
296 gtk_container_add (GTK_CONTAINER (button_box), entry);
298 widget = gtk_button_new_with_label ("Set Tooltip");
299 g_object_set_data (G_OBJECT (widget), TOOLTIP_ENTRY_KEY, entry);
300 gtk_container_add (GTK_CONTAINER (button_box), widget);
302 g_signal_connect (
303 widget, "clicked",
304 G_CALLBACK (set_tooltip_clicked_cb), selector);
305 g_signal_connect (
306 selector, "primary-selection-changed",
307 G_CALLBACK (enable_widget_if_any_selected), widget);
309 widget = gtk_label_new ("");
310 g_object_set (
311 G_OBJECT (widget),
312 "halign", GTK_ALIGN_FILL,
313 "hexpand", FALSE,
314 "valign", GTK_ALIGN_FILL,
315 "vexpand", TRUE,
316 NULL);
317 gtk_grid_attach (grid, widget, 1, 1, 1, 1);
319 widget = gtk_check_button_new_with_label ("Show colors");
320 g_object_set (
321 G_OBJECT (widget),
322 "halign", GTK_ALIGN_START,
323 "hexpand", FALSE,
324 "valign", GTK_ALIGN_END,
325 "vexpand", FALSE,
326 NULL);
327 gtk_grid_attach (grid, widget, 1, 2, 1, 1);
329 e_binding_bind_property (
330 selector, "show-colors",
331 widget, "active",
332 G_BINDING_BIDIRECTIONAL |
333 G_BINDING_SYNC_CREATE);
335 widget = gtk_check_button_new_with_label ("Show icons");
336 g_object_set (
337 G_OBJECT (widget),
338 "halign", GTK_ALIGN_START,
339 "hexpand", FALSE,
340 "valign", GTK_ALIGN_END,
341 "vexpand", FALSE,
342 NULL);
343 gtk_grid_attach (grid, widget, 1, 3, 1, 1);
345 e_binding_bind_property (
346 selector, "show-icons",
347 widget, "active",
348 G_BINDING_BIDIRECTIONAL |
349 G_BINDING_SYNC_CREATE);
351 widget = gtk_check_button_new_with_label ("Show toggles");
352 g_object_set (
353 G_OBJECT (widget),
354 "halign", GTK_ALIGN_START,
355 "hexpand", FALSE,
356 "valign", GTK_ALIGN_END,
357 "vexpand", FALSE,
358 NULL);
359 gtk_grid_attach (grid, widget, 1, 4, 1, 1);
361 e_binding_bind_property (
362 selector, "show-toggles",
363 widget, "active",
364 G_BINDING_BIDIRECTIONAL |
365 G_BINDING_SYNC_CREATE);
367 opened_sources = g_hash_table_new_full (
368 (GHashFunc) g_direct_hash,
369 (GEqualFunc) g_direct_equal,
370 (GDestroyNotify) g_object_unref,
371 (GDestroyNotify) g_object_unref);
372 g_object_set_data_full (
373 G_OBJECT (selector),
374 OPENED_KEY,
375 opened_sources,
376 (GDestroyNotify) g_hash_table_unref);
377 g_object_set_data (
378 G_OBJECT (selector),
379 SOURCE_TYPE_KEY,
380 GUINT_TO_POINTER (source_type));
381 g_object_set_data_full (
382 G_OBJECT (selector),
383 EXTENSION_NAME_KEY,
384 g_strdup (extension_name),
385 (GDestroyNotify) g_free);
387 /* update buttons */
388 g_signal_emit_by_name (selector, "primary-selection-changed", 0);
390 g_signal_connect (
391 selector, "selection-changed",
392 G_CALLBACK (selection_changed_callback), NULL);
394 return GTK_WIDGET (grid);
397 static gint
398 on_idle_create_widget (ESourceRegistry *registry)
400 GtkWidget *window, *notebook;
402 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
403 gtk_window_set_default_size (GTK_WINDOW (window), 300, 400);
405 g_signal_connect (
406 window, "delete-event",
407 G_CALLBACK (gtk_main_quit), NULL);
409 notebook = gtk_notebook_new ();
410 gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
411 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (notebook));
413 gtk_notebook_append_page (
414 GTK_NOTEBOOK (notebook),
415 create_page (
416 registry,
417 E_SOURCE_EXTENSION_CALENDAR,
418 E_CAL_CLIENT_SOURCE_TYPE_EVENTS),
419 gtk_label_new ("Calendars"));
421 gtk_notebook_append_page (
422 GTK_NOTEBOOK (notebook),
423 create_page (
424 registry,
425 E_SOURCE_EXTENSION_MEMO_LIST,
426 E_CAL_CLIENT_SOURCE_TYPE_MEMOS),
427 gtk_label_new ("Memos"));
429 gtk_notebook_append_page (
430 GTK_NOTEBOOK (notebook),
431 create_page (
432 registry,
433 E_SOURCE_EXTENSION_TASK_LIST,
434 E_CAL_CLIENT_SOURCE_TYPE_TASKS),
435 gtk_label_new ("Tasks"));
437 gtk_notebook_append_page (
438 GTK_NOTEBOOK (notebook),
439 create_page (
440 registry,
441 E_SOURCE_EXTENSION_ADDRESS_BOOK,
442 E_CAL_CLIENT_SOURCE_TYPE_LAST),
443 gtk_label_new ("Books"));
445 gtk_widget_show_all (window);
447 return FALSE;
450 gint
451 main (gint argc,
452 gchar **argv)
454 ESourceRegistry *registry;
455 GError *local_error = NULL;
457 gtk_init (&argc, &argv);
459 registry = e_source_registry_new_sync (NULL, &local_error);
461 if (local_error != NULL) {
462 g_error (
463 "Failed to load ESource registry: %s",
464 local_error->message);
465 g_return_val_if_reached (-1);
468 g_idle_add ((GSourceFunc) on_idle_create_widget, registry);
470 gtk_main ();
472 g_object_unref (registry);
473 e_util_cleanup_settings ();
475 return 0;