33 #include "qofundo-p.h"
46 struct QofUndoEntity_t
57 struct QofUndoOperation_t
75 void (*string_setter) (
QofEntity *, gchar *);
80 void (*boolean_setter) (
QofEntity *, gboolean);
83 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_STRING))
86 (void (*)(
QofEntity *, gchar *)) param->param_setfcn;
89 param->param_setfcn (ent, value);
92 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_GUID))
94 cm_guid = g_new (
GUID, 1);
99 if (guid_setter != NULL)
101 guid_setter (ent, cm_guid);
105 if ((0 ==
safe_strcmp (param->param_type, QOF_TYPE_NUMERIC)) ||
106 (
safe_strcmp (param->param_type, QOF_TYPE_DEBCRED) == 0))
111 if (numeric_setter != NULL)
113 numeric_setter (ent, cli_numeric);
116 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_BOOLEAN))
124 (void (*)(
QofEntity *, gboolean)) param->param_setfcn;
125 if (boolean_setter != NULL)
127 boolean_setter (ent, cli_bool);
130 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_INT32))
133 cli_i32 = (gint32) strtol (value, &tail, 0);
137 (void (*)(
QofEntity *, gint32)) param->param_setfcn;
138 if (i32_setter != NULL)
140 i32_setter (ent, cli_i32);
145 PERR (
" Cannot convert %s into a number: "
146 "an overflow has been detected.", value);
149 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_INT64))
152 cli_i64 = (gint64) strtol (value, &tail, 0);
156 (void (*)(
QofEntity *, gint64)) param->param_setfcn;
157 if (i64_setter != NULL)
159 i64_setter (ent, cli_i64);
164 PERR (
" Cannot convert %s into a number: "
165 "an overflow has been detected.", value);
168 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_TIME))
176 if ((time_setter != NULL) && qof_time_is_valid (cli_time))
178 time_setter (ent, cli_time);
181 #ifndef QOF_DISABLE_DEPRECATED
192 cli_time_t = mktime (&cli_time);
193 timespecFromTime_t (&cli_date, cli_time_t);
194 if (date_setter != NULL)
196 date_setter (ent, cli_date);
200 if (0 ==
safe_strcmp (param->param_type, QOF_TYPE_CHAR))
202 param->param_setfcn (ent, value);
211 set_param (ent, param, value);
216 undo_from_kvp_helper (
const gchar * path,
KvpValue * content,
219 QofUndoEntity *undo_entity;
221 undo_entity = (QofUndoEntity *) data;
222 undo_entity->path = g_strdup (path);
229 QofUndoEntity *undo_entity;
233 undo_entity = g_new0 (QofUndoEntity, 1);
235 undo_entity->param = param;
236 undo_entity->how = UNDO_MODIFY;
237 undo_entity->type = ent->e_type;
240 if (0 == (
safe_strcmp (param->param_type, QOF_TYPE_KVP)))
251 qof_reinstate_entity (QofUndoEntity * undo_entity,
QofBook * book)
257 undo_param = undo_entity->param;
260 PINFO (
" reinstate:%s", undo_entity->type);
267 PINFO (
" undoing %s %s", undo_param->param_name, undo_entity->value);
268 set_param (ent, undo_param, undo_entity->value);
272 qof_recreate_entity (QofUndoEntity * undo_entity,
QofBook * book)
279 guid = undo_entity->guid;
280 type = undo_entity->type;
281 g_return_if_fail (guid || type);
288 qof_dump_entity (QofUndoEntity * undo_entity,
QofBook * book)
295 type = undo_entity->type;
296 guid = undo_entity->guid;
297 g_return_if_fail (type || book);
306 QofUndoOperation *undo_operation;
307 QofUndoEntity *undo_entity;
313 length = g_list_length (book_undo->undo_list);
314 if (book_undo->index_position > 1)
315 book_undo->index_position--;
317 book_undo->index_position = 0;
320 *) (g_list_nth (book_undo->undo_list,
321 book_undo->index_position))->data;
322 g_return_if_fail (undo_operation);
323 ent_list = undo_operation->entity_list;
324 while (ent_list != NULL)
326 undo_entity = (QofUndoEntity *) ent_list->data;
329 switch (undo_entity->how)
333 qof_reinstate_entity (undo_entity, book);
338 qof_recreate_entity (undo_entity, book);
343 qof_dump_entity (undo_entity, book);
351 ent_list = g_list_next (ent_list);
358 QofUndoOperation *undo_operation;
359 QofUndoEntity *undo_entity;
367 *) (g_list_nth (book_undo->undo_list,
368 book_undo->index_position))->data;
371 ent_list = undo_operation->entity_list;
372 while (ent_list != NULL)
374 undo_entity = (QofUndoEntity *) ent_list->data;
377 switch (undo_entity->how)
381 qof_reinstate_entity (undo_entity, book);
386 qof_dump_entity (undo_entity, book);
391 qof_recreate_entity (undo_entity, book);
399 ent_list = g_list_next (ent_list);
401 length = g_list_length (book_undo->undo_list);
402 if (book_undo->index_position < length)
403 book_undo->index_position++;
405 book_undo->index_position = length;
411 QofUndoOperation *operation;
417 while (book_undo != NULL)
419 operation = (QofUndoOperation *) book_undo->undo_list->data;
420 if(operation->entity_list)
421 g_list_free (operation->entity_list);
422 book_undo->undo_list = g_list_next (book_undo->undo_list);
424 book_undo->index_position = 0;
425 g_free (book_undo->undo_label);
435 length = g_list_length (book_undo->undo_list);
436 if ((book_undo->index_position == 0) || (length == 0))
448 length = g_list_length (book_undo->undo_list);
449 if ((book_undo->index_position == length) || (length == 0))
455 qof_undo_new_operation (
QofBook * book, gchar * label)
457 QofUndoOperation *undo_operation;
460 undo_operation = NULL;
462 undo_operation = g_new0 (QofUndoOperation, 1);
463 undo_operation->label = label;
465 undo_operation->entity_list = NULL;
466 g_list_foreach (book_undo->undo_cache,
467 qof_undo_new_entry, undo_operation);
468 return undo_operation;
472 qof_undo_new_entry (gpointer cache, gpointer operation)
474 QofUndoOperation *undo_operation;
475 QofUndoEntity *undo_entity;
477 g_return_if_fail (operation || cache);
478 undo_operation = (QofUndoOperation *) operation;
479 undo_entity = (QofUndoEntity *) cache;
480 g_return_if_fail (undo_operation || undo_entity);
481 undo_operation->entity_list =
482 g_list_prepend (undo_operation->entity_list, undo_entity);
488 QofUndoEntity *undo_entity;
494 book = instance->
book;
496 undo_entity = g_new0 (QofUndoEntity, 1);
498 undo_entity->how = UNDO_DELETE;
500 undo_entity->type = instance->
entity.e_type;
501 book_undo->undo_cache =
502 g_list_prepend (book_undo->undo_cache, undo_entity);
506 undo_get_entity (
QofParam * param, gpointer data)
511 QofUndoEntity *undo_entity;
514 book = instance->
book;
516 g_return_if_fail (instance || param);
517 undo_entity = qof_prepare_undo (&instance->
entity, param);
518 book_undo->undo_cache =
519 g_list_prepend (book_undo->undo_cache, undo_entity);
525 QofUndoEntity *undo_entity;
532 book = instance->
book;
535 type = instance->
entity.e_type;
537 undo_entity = g_new0 (QofUndoEntity, 1);
539 undo_entity->how = UNDO_CREATE;
541 undo_entity->type = type;
542 book_undo->undo_cache =
543 g_list_prepend (book_undo->undo_cache, undo_entity);
551 QofUndoEntity *undo_entity;
553 if (!instance || !param)
555 book = instance->
book;
558 undo_entity = qof_prepare_undo (&instance->
entity, param);
559 book_undo->undo_cache =
560 g_list_prepend (book_undo->undo_cache, undo_entity);
562 if (book_undo->index_position == 0)
564 book_undo->undo_list = g_list_prepend (book_undo->undo_list,
565 qof_undo_new_operation (book,
"initial"));
566 book_undo->index_position++;
573 QofUndoEntity *undo_entity;
577 if (!instance || !param)
579 book = instance->
book;
581 undo_entity = qof_prepare_undo (&instance->
entity, param);
582 book_undo->undo_cache =
583 g_list_prepend (book_undo->undo_cache, undo_entity);
592 if (book_undo->undo_operation_open && book_undo->undo_cache)
594 g_list_free (book_undo->undo_cache);
595 book_undo->undo_operation_open = FALSE;
596 if (book_undo->undo_label)
597 g_free (book_undo->undo_label);
599 book_undo->undo_label = g_strdup (label);
600 book_undo->undo_operation_open = TRUE;
609 book_undo->undo_list = g_list_prepend (book_undo->undo_list,
610 qof_undo_new_operation (book, book_undo->undo_label));
611 book_undo->index_position++;
612 g_list_free (book_undo->undo_cache);
613 book_undo->undo_operation_open = FALSE;
619 QofUndoOperation *undo_operation;
624 (QofUndoOperation *) g_list_last (book_undo->undo_list);
625 return undo_operation->qt;
634 return g_list_length (book_undo->undo_list);