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
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"
27 dump_selection (ESourceSelector
*selector
,
28 const gchar
*extension_name
)
32 list
= e_source_selector_get_selection (selector
);
34 g_print ("Current selection at %s:\n", extension_name
);
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
);
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
);
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
);
67 enable_widget_if_opened_cb (ESourceSelector
*selector
,
70 GHashTable
*opened_sources
;
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
);
79 sensitive
= g_hash_table_contains (opened_sources
, source
);
80 gtk_widget_set_sensitive (widget
, sensitive
);
81 g_clear_object (&source
);
85 disable_widget_if_opened_cb (ESourceSelector
*selector
,
88 GHashTable
*opened_sources
;
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
);
97 sensitive
= !g_hash_table_contains (opened_sources
, source
);
98 gtk_widget_set_sensitive (widget
, sensitive
);
99 g_clear_object (&source
);
103 enable_widget_if_any_selected (ESourceSelector
*selector
,
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
);
116 open_selected_clicked_cb (GtkWidget
*button
,
117 ESourceSelector
*selector
)
119 GHashTable
*opened_sources
;
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
);
129 if (!g_hash_table_contains (opened_sources
, source
)) {
131 ECalClientSourceType source_type
;
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
);
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 (
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
) {
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
);
168 close_selected_clicked_cb (GtkWidget
*button
,
169 ESourceSelector
*selector
)
171 GHashTable
*opened_sources
;
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
);
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
);
189 flip_busy_clicked_cb (GtkWidget
*button
,
190 ESourceSelector
*selector
)
194 source
= e_source_selector_ref_primary_selection (selector
);
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
);
203 set_tooltip_clicked_cb (GtkWidget
*button
,
204 ESourceSelector
*selector
)
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
);
214 e_source_selector_set_source_tooltip (selector
, source
,
215 gtk_entry_get_text (entry
));
217 g_clear_object (&source
);
221 create_page (ESourceRegistry
*registry
,
222 const gchar
*extension_name
,
223 ECalClientSourceType source_type
)
225 GtkWidget
*widget
, *subwindow
, *selector
, *button_box
, *entry
;
227 GHashTable
*opened_sources
;
229 grid
= GTK_GRID (gtk_grid_new ());
231 subwindow
= gtk_scrolled_window_new (NULL
, NULL
);
233 G_OBJECT (subwindow
),
234 "halign", GTK_ALIGN_FILL
,
236 "valign", GTK_ALIGN_FILL
,
240 selector
= e_source_selector_new (registry
, extension_name
);
243 "halign", GTK_ALIGN_FILL
,
245 "valign", GTK_ALIGN_FILL
,
247 "show-toggles", FALSE
,
248 "show-colors", source_type
!= E_CAL_CLIENT_SOURCE_TYPE_LAST
,
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
);
256 G_OBJECT (button_box
),
257 "halign", GTK_ALIGN_START
,
259 "valign", GTK_ALIGN_START
,
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
);
269 G_CALLBACK (open_selected_clicked_cb
), selector
);
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
);
279 G_CALLBACK (close_selected_clicked_cb
), selector
);
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
);
290 G_CALLBACK (flip_busy_clicked_cb
), selector
);
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
);
304 G_CALLBACK (set_tooltip_clicked_cb
), selector
);
306 selector
, "primary-selection-changed",
307 G_CALLBACK (enable_widget_if_any_selected
), widget
);
309 widget
= gtk_label_new ("");
312 "halign", GTK_ALIGN_FILL
,
314 "valign", GTK_ALIGN_FILL
,
317 gtk_grid_attach (grid
, widget
, 1, 1, 1, 1);
319 widget
= gtk_check_button_new_with_label ("Show colors");
322 "halign", GTK_ALIGN_START
,
324 "valign", GTK_ALIGN_END
,
327 gtk_grid_attach (grid
, widget
, 1, 2, 1, 1);
329 e_binding_bind_property (
330 selector
, "show-colors",
332 G_BINDING_BIDIRECTIONAL
|
333 G_BINDING_SYNC_CREATE
);
335 widget
= gtk_check_button_new_with_label ("Show icons");
338 "halign", GTK_ALIGN_START
,
340 "valign", GTK_ALIGN_END
,
343 gtk_grid_attach (grid
, widget
, 1, 3, 1, 1);
345 e_binding_bind_property (
346 selector
, "show-icons",
348 G_BINDING_BIDIRECTIONAL
|
349 G_BINDING_SYNC_CREATE
);
351 widget
= gtk_check_button_new_with_label ("Show toggles");
354 "halign", GTK_ALIGN_START
,
356 "valign", GTK_ALIGN_END
,
359 gtk_grid_attach (grid
, widget
, 1, 4, 1, 1);
361 e_binding_bind_property (
362 selector
, "show-toggles",
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 (
376 (GDestroyNotify
) g_hash_table_unref
);
380 GUINT_TO_POINTER (source_type
));
381 g_object_set_data_full (
384 g_strdup (extension_name
),
385 (GDestroyNotify
) g_free
);
388 g_signal_emit_by_name (selector
, "primary-selection-changed", 0);
391 selector
, "selection-changed",
392 G_CALLBACK (selection_changed_callback
), NULL
);
394 return GTK_WIDGET (grid
);
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);
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
),
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
),
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
),
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
),
441 E_SOURCE_EXTENSION_ADDRESS_BOOK
,
442 E_CAL_CLIENT_SOURCE_TYPE_LAST
),
443 gtk_label_new ("Books"));
445 gtk_widget_show_all (window
);
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
) {
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
);
472 g_object_unref (registry
);
473 e_util_cleanup_settings ();