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