1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 #include <libedataserver/e-source-registry.h>
5 #include <libedataserver/e-source-calendar.h>
7 #include <libedataserver/e-gdbus-templates.h>
9 #include "client-test-utils.h"
12 print_ecomp (ECalComponent *ecalcomp)
14 const gchar *uid = NULL;
15 ECalComponentText summary = { 0 };
17 g_return_if_fail (ecalcomp != NULL);
19 e_cal_component_get_uid (ecalcomp, &uid);
20 e_cal_component_get_summary (ecalcomp, &summary);
22 g_print (" Component: %s\n", uid ? uid : "no-uid");
23 g_print (" Summary: %s\n", summary.value ? summary.value : "NULL");
28 print_icomp (icalcomponent *icalcomp)
32 g_return_if_fail (icalcomp != NULL);
34 ecomp = e_cal_component_new ();
35 icalcomp = icalcomponent_new_clone (icalcomp);
37 if (!e_cal_component_set_icalcomponent (ecomp, icalcomp)) {
38 icalcomponent_free (icalcomp);
39 g_object_unref (ecomp);
40 g_printerr ("%s: Failed to assing icalcomp to ECalComponent\n", G_STRFUNC);
47 g_object_unref (ecomp);
51 report_error (const gchar *operation,
54 g_return_if_fail (operation != NULL);
56 g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
58 g_clear_error (error);
62 main_initialize (void)
64 static gboolean initialized = FALSE;
70 e_gdbus_templates_init_main_thread ();
78 gboolean run_in_thread; /* FALSE to run in idle callback */
82 idle_cb (gpointer data)
84 struct IdleData *idle = data;
86 g_return_val_if_fail (idle != NULL, FALSE);
87 g_return_val_if_fail (idle->func != NULL, FALSE);
89 if (idle->run_in_thread) {
92 g_thread_create (idle->func, idle->data, FALSE, &error);
95 report_error ("create thread", &error);
99 idle->func (idle->data);
107 static GMainLoop *loop = NULL;
108 static gint main_stop_result = 0;
111 do_start (GThreadFunc func,
116 g_return_if_fail (loop == NULL);
118 loop = g_main_loop_new (NULL, FALSE);
123 g_main_loop_run (loop);
125 g_main_loop_unref (loop);
129 /* Starts new main-loop, but just before that calls 'func'.
130 * Main-loop is kept running, and this function blocks,
131 * until call of stop_main_loop (). */
133 start_main_loop (GThreadFunc func,
136 g_return_if_fail (loop == NULL);
138 do_start (func, data);
141 /* Starts new main-loop and then invokes func in a new thread.
142 * Main-loop is kept running, and this function blocks,
143 * until call of stop_main_loop (). */
145 start_in_thread_with_main_loop (GThreadFunc func,
148 struct IdleData *idle;
150 g_return_if_fail (func != NULL);
151 g_return_if_fail (loop == NULL);
155 idle = g_new0 (struct IdleData, 1);
158 idle->run_in_thread = TRUE;
160 g_idle_add (idle_cb, idle);
162 do_start (NULL, NULL);
165 /* Starts new main-loop and then invokes func in an idle callback.
166 * Main-loop is kept running, and this function blocks,
167 * until call of stop_main_loop (). */
169 start_in_idle_with_main_loop (GThreadFunc func,
172 struct IdleData *idle;
174 g_return_if_fail (func != NULL);
175 g_return_if_fail (loop == NULL);
179 idle = g_new0 (struct IdleData, 1);
182 idle->run_in_thread = FALSE;
184 g_idle_add (idle_cb, idle);
186 do_start (NULL, NULL);
189 /* Stops main-loop previously run by start_main_loop,
190 * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
193 stop_main_loop (gint stop_result)
195 g_return_if_fail (loop != NULL);
197 main_stop_result = stop_result;
198 g_main_loop_quit (loop);
201 /* returns value used in stop_main_loop() */
203 get_main_loop_stop_result (void)
205 return main_stop_result;
209 foreach_configured_source (ESourceRegistry *registry,
210 ECalClientSourceType source_type,
211 void (*func) (ESource *source,
212 ECalClientSourceType source_type))
214 gpointer foreach_async_data;
215 ESource *source = NULL;
217 g_return_if_fail (func != NULL);
221 foreach_async_data = foreach_configured_source_async_start (registry, source_type, &source);
222 if (!foreach_async_data)
226 func (source, source_type);
227 } while (foreach_configured_source_async_next (&foreach_async_data, &source));
230 struct ForeachConfiguredData {
231 ECalClientSourceType source_type;
236 foreach_configured_source_async_start (ESourceRegistry *registry,
237 ECalClientSourceType source_type,
240 struct ForeachConfiguredData *async_data;
241 const gchar *extension_name;
244 g_return_val_if_fail (source != NULL, NULL);
248 switch (source_type) {
249 case E_CAL_CLIENT_SOURCE_TYPE_EVENTS:
250 extension_name = E_SOURCE_EXTENSION_CALENDAR;
252 case E_CAL_CLIENT_SOURCE_TYPE_TASKS:
253 extension_name = E_SOURCE_EXTENSION_TASK_LIST;
255 case E_CAL_CLIENT_SOURCE_TYPE_MEMOS:
256 extension_name = E_SOURCE_EXTENSION_MEMO_LIST;
259 g_assert_not_reached ();
262 list = e_source_registry_list_sources (registry, extension_name);
264 async_data = g_new0 (struct ForeachConfiguredData, 1);
265 async_data->source_type = source_type;
266 async_data->list = list;
268 *source = async_data->list->data;
274 foreach_configured_source_async_next (gpointer *foreach_async_data,
277 struct ForeachConfiguredData *async_data;
279 g_return_val_if_fail (foreach_async_data != NULL, FALSE);
280 g_return_val_if_fail (source != NULL, FALSE);
282 async_data = *foreach_async_data;
283 g_return_val_if_fail (async_data != NULL, FALSE);
285 if (async_data->list) {
286 g_object_unref (async_data->list->data);
287 async_data->list = async_data->list->next;
289 if (async_data->list) {
290 *source = async_data->list->data;
296 *foreach_async_data = NULL;
302 foreach_configured_source_async_get_source_type (gpointer foreach_async_data)
304 struct ForeachConfiguredData *async_data = foreach_async_data;
306 g_return_val_if_fail (foreach_async_data != NULL, E_CAL_CLIENT_SOURCE_TYPE_LAST);
308 return async_data->source_type;
312 new_temp_client (ECalClientSourceType source_type,
315 #if 0 /* ACCOUNT_MGMT */
316 ECalClient *cal_client;
318 gchar *abs_uri, *filename;
319 GError *error = NULL;
321 filename = g_build_filename (g_get_tmp_dir (), "e-cal-client-test-XXXXXX/", NULL);
322 abs_uri = g_strconcat ("local:", filename, NULL);
325 source = e_source_new_with_absolute_uri ("Test cal", abs_uri);
331 g_return_val_if_fail (source != NULL, NULL);
333 cal_client = e_cal_client_new (source, source_type, &error);
334 g_object_unref (source);
337 report_error ("new temp client", &error);
340 #endif /* ACCOUNT_MGMT */