Adapt libecal to the new ESource API.
[platform/upstream/evolution-data-server.git] / tests / libecal / client / client-test-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 #include <stdio.h>
4 #include <libedataserver/e-source-registry.h>
5 #include <libedataserver/e-source-calendar.h>
6
7 #include <libedataserver/e-gdbus-templates.h>
8
9 #include "client-test-utils.h"
10
11 void
12 print_ecomp (ECalComponent *ecalcomp)
13 {
14         const gchar *uid = NULL;
15         ECalComponentText summary = { 0 };
16
17         g_return_if_fail (ecalcomp != NULL);
18
19         e_cal_component_get_uid (ecalcomp, &uid);
20         e_cal_component_get_summary (ecalcomp, &summary);
21
22         g_print ("   Component: %s\n", uid ? uid : "no-uid");
23         g_print ("   Summary: %s\n", summary.value ? summary.value : "NULL");
24         g_print ("\n");
25 }
26
27 void
28 print_icomp (icalcomponent *icalcomp)
29 {
30         ECalComponent *ecomp;
31
32         g_return_if_fail (icalcomp != NULL);
33
34         ecomp = e_cal_component_new ();
35         icalcomp = icalcomponent_new_clone (icalcomp);
36
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);
41                 g_print ("\n");
42                 return;
43         }
44
45         print_ecomp (ecomp);
46
47         g_object_unref (ecomp);
48 }
49
50 void
51 report_error (const gchar *operation,
52               GError **error)
53 {
54         g_return_if_fail (operation != NULL);
55
56         g_printerr ("Failed to %s: %s\n", operation, (error && *error) ? (*error)->message : "Unknown error");
57
58         g_clear_error (error);
59 }
60
61 void
62 main_initialize (void)
63 {
64         static gboolean initialized = FALSE;
65
66         if (initialized)
67                 return;
68
69         g_type_init ();
70         e_gdbus_templates_init_main_thread ();
71
72         initialized = TRUE;
73 }
74
75 struct IdleData {
76         GThreadFunc func;
77         gpointer data;
78         gboolean run_in_thread; /* FALSE to run in idle callback */
79 };
80
81 static gboolean
82 idle_cb (gpointer data)
83 {
84         struct IdleData *idle = data;
85
86         g_return_val_if_fail (idle != NULL, FALSE);
87         g_return_val_if_fail (idle->func != NULL, FALSE);
88
89         if (idle->run_in_thread) {
90                 GError *error = NULL;
91
92                 g_thread_create (idle->func, idle->data, FALSE, &error);
93
94                 if (error) {
95                         report_error ("create thread", &error);
96                         stop_main_loop (1);
97                 }
98         } else {
99                 idle->func (idle->data);
100         }
101
102         g_free (idle);
103
104         return FALSE;
105 }
106
107 static GMainLoop *loop = NULL;
108 static gint main_stop_result = 0;
109
110 static void
111 do_start (GThreadFunc func,
112           gpointer data)
113 {
114         main_initialize ();
115
116         g_return_if_fail (loop == NULL);
117
118         loop = g_main_loop_new (NULL, FALSE);
119
120         if (func)
121                 func (data);
122
123         g_main_loop_run (loop);
124
125         g_main_loop_unref (loop);
126         loop = NULL;
127 }
128
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 (). */
132 void
133 start_main_loop (GThreadFunc func,
134                  gpointer data)
135 {
136         g_return_if_fail (loop == NULL);
137
138         do_start (func, data);
139 }
140
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 (). */
144 void
145 start_in_thread_with_main_loop (GThreadFunc func,
146                                 gpointer data)
147 {
148         struct IdleData *idle;
149
150         g_return_if_fail (func != NULL);
151         g_return_if_fail (loop == NULL);
152
153         main_initialize ();
154
155         idle = g_new0 (struct IdleData, 1);
156         idle->func = func;
157         idle->data = data;
158         idle->run_in_thread = TRUE;
159
160         g_idle_add (idle_cb, idle);
161
162         do_start (NULL, NULL);
163 }
164
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 (). */
168 void
169 start_in_idle_with_main_loop (GThreadFunc func,
170                               gpointer data)
171 {
172         struct IdleData *idle;
173
174         g_return_if_fail (func != NULL);
175         g_return_if_fail (loop == NULL);
176
177         main_initialize ();
178
179         idle = g_new0 (struct IdleData, 1);
180         idle->func = func;
181         idle->data = data;
182         idle->run_in_thread = FALSE;
183
184         g_idle_add (idle_cb, idle);
185
186         do_start (NULL, NULL);
187 }
188
189 /* Stops main-loop previously run by start_main_loop,
190  * start_in_thread_with_main_loop or start_in_idle_with_main_loop.
191 */
192 void
193 stop_main_loop (gint stop_result)
194 {
195         g_return_if_fail (loop != NULL);
196
197         main_stop_result = stop_result;
198         g_main_loop_quit (loop);
199 }
200
201 /* returns value used in stop_main_loop() */
202 gint
203 get_main_loop_stop_result (void)
204 {
205         return main_stop_result;
206 }
207
208 void
209 foreach_configured_source (ESourceRegistry *registry,
210                            ECalClientSourceType source_type,
211                            void (*func) (ESource *source,
212                            ECalClientSourceType source_type))
213 {
214         gpointer foreach_async_data;
215         ESource *source = NULL;
216
217         g_return_if_fail (func != NULL);
218
219         main_initialize ();
220
221         foreach_async_data = foreach_configured_source_async_start (registry, source_type, &source);
222         if (!foreach_async_data)
223                 return;
224
225         do {
226                 func (source, source_type);
227         } while (foreach_configured_source_async_next (&foreach_async_data, &source));
228 }
229
230 struct ForeachConfiguredData {
231         ECalClientSourceType source_type;
232         GList *list;
233 };
234
235 gpointer
236 foreach_configured_source_async_start (ESourceRegistry *registry,
237                                        ECalClientSourceType source_type,
238                                        ESource **source)
239 {
240         struct ForeachConfiguredData *async_data;
241         const gchar *extension_name;
242         GList *list;
243
244         g_return_val_if_fail (source != NULL, NULL);
245
246         main_initialize ();
247
248         switch (source_type) {
249                 case E_CAL_CLIENT_SOURCE_TYPE_EVENTS:
250                         extension_name = E_SOURCE_EXTENSION_CALENDAR;
251                         break;
252                 case E_CAL_CLIENT_SOURCE_TYPE_TASKS:
253                         extension_name = E_SOURCE_EXTENSION_TASK_LIST;
254                         break;
255                 case E_CAL_CLIENT_SOURCE_TYPE_MEMOS:
256                         extension_name = E_SOURCE_EXTENSION_MEMO_LIST;
257                         break;
258                 default:
259                         g_assert_not_reached ();
260         }
261
262         list = e_source_registry_list_sources (registry, extension_name);
263
264         async_data = g_new0 (struct ForeachConfiguredData, 1);
265         async_data->source_type = source_type;
266         async_data->list = list;
267
268         *source = async_data->list->data;
269
270         return async_data;
271 }
272
273 gboolean
274 foreach_configured_source_async_next (gpointer *foreach_async_data,
275                                       ESource **source)
276 {
277         struct ForeachConfiguredData *async_data;
278
279         g_return_val_if_fail (foreach_async_data != NULL, FALSE);
280         g_return_val_if_fail (source != NULL, FALSE);
281
282         async_data = *foreach_async_data;
283         g_return_val_if_fail (async_data != NULL, FALSE);
284
285         if (async_data->list) {
286                 g_object_unref (async_data->list->data);
287                 async_data->list = async_data->list->next;
288         }
289         if (async_data->list) {
290                 *source = async_data->list->data;
291                 return TRUE;
292         }
293
294         g_free (async_data);
295
296         *foreach_async_data = NULL;
297
298         return FALSE;
299 }
300
301 ECalClientSourceType
302 foreach_configured_source_async_get_source_type (gpointer foreach_async_data)
303 {
304         struct ForeachConfiguredData *async_data = foreach_async_data;
305
306         g_return_val_if_fail (foreach_async_data != NULL, E_CAL_CLIENT_SOURCE_TYPE_LAST);
307
308         return async_data->source_type;
309 }
310
311 ECalClient *
312 new_temp_client (ECalClientSourceType source_type,
313                  gchar **uri)
314 {
315 #if 0  /* ACCOUNT_MGMT */
316         ECalClient *cal_client;
317         ESource *source;
318         gchar *abs_uri, *filename;
319         GError *error = NULL;
320
321         filename = g_build_filename (g_get_tmp_dir (), "e-cal-client-test-XXXXXX/", NULL);
322         abs_uri = g_strconcat ("local:", filename, NULL);
323         g_free (filename);
324
325         source = e_source_new_with_absolute_uri ("Test cal", abs_uri);
326         if (uri)
327                 *uri = abs_uri;
328         else
329                 g_free (abs_uri);
330
331         g_return_val_if_fail (source != NULL, NULL);
332
333         cal_client = e_cal_client_new (source, source_type, &error);
334         g_object_unref (source);
335
336         if (error)
337                 report_error ("new temp client", &error);
338
339         return cal_client;
340 #endif /* ACCOUNT_MGMT */
341
342         return NULL;
343 }