OpenSync  0.22
opensync_change.c
1 /*
2  * libopensync - A synchronization framework
3  * Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "opensync.h"
22 #include "opensync_internals.h"
23 
31 
37 {
38  g_assert(change);
39  change->refcount++;
40 }
41 
47 {
48  g_assert(change);
49  change->refcount--;
50  if (change->refcount >= 0)
51  osync_change_free(change);
52 }
53 
60 {
61  g_assert(change);
62  return change->sourceobjtype;
63 }
64 
71 {
72  g_assert(change);
73  if (change->initial_format)
74  return change->initial_format;
75 
76  if (!change->initial_format)
77  return NULL;
78 
79  osync_assert_msg(change->conv_env, "The conv env of the change must be set by calling member_set or conv_env_set");
81  return change->initial_format;
82 }
83 
93 
100 {
101  osync_trace(TRACE_ENTRY, "%s()", __func__);
102 
103  OSyncChange *change = g_malloc0(sizeof(OSyncChange));
104  change->refcount = 1;
105 
106  osync_trace(TRACE_EXIT, "%s: %p", __func__, change);
107  return change;
108 }
109 
116 {
117  osync_trace(TRACE_ENTRY, "%s(%p)", __func__, change);
118  g_assert(change);
119 
120  //FIXME cleanly release the change!
121  g_free(change);
122 
123  osync_trace(TRACE_EXIT, "%s", __func__);
124 }
125 
135 {
136  g_assert(change);
137  g_assert(osync_change_get_objformat(change));
138  if (!osync_change_get_objformat(change)->destroy_func)
139  osync_debug("OSCONV", 1, "Memory leak: can't free data of type %s", osync_change_get_objformat(change)->name);
140  else {
141  osync_debug("OSCONV", 4, "Freeing data of type %s", osync_change_get_objformat(change)->name);
142  osync_change_get_objformat(change)->destroy_func(change->data, change->size);
143  }
144  change->data = NULL;
145  change->size = 0;
146  //FIXME Set format to NULL here?
147 }
148 
157 {
158  osync_trace(TRACE_ENTRY, "%s(%p)", __func__, change);
159 
160  if (change->hash)
161  g_free(change->hash);
162  change->hash = NULL;
163  //FIXME Release data
164  change->data = NULL;
165  change->size = 0;
166  change->has_data = FALSE;
167  change->changetype = CHANGE_UNKNOWN;
168  //change->sourceobjtype = NULL;
169  //change->destobjtype = NULL;
170 
171  osync_trace(TRACE_EXIT, "%s", __func__);
172 }
173 
182 osync_bool osync_change_save(OSyncChange *change, osync_bool save_format, OSyncError **error)
183 {
184  if (!change->changes_db)
185  change->changes_db = change->member->group->changes_db;
186  return osync_db_save_change(change, save_format, error);
187 }
188 
196 osync_bool osync_change_delete(OSyncChange *change, OSyncError **error)
197 {
198  return osync_db_delete_change(change, error);
199 }
200 
213 osync_bool osync_changes_load(OSyncGroup *group, OSyncChange ***changes, OSyncError **error)
214 {
215  return osync_db_open_changes(group, changes, error);
216 }
217 
224 {
225  osync_db_close_changes(group);
226 }
227 
235 {
236  g_assert(change);
237  return change->member;
238 }
239 
247 {
248  g_assert(change);
249  change->member = member;
250  change->conv_env = member->group->conv_env;
251 }
252 
260 {
261  g_assert(change);
262  change->conv_env = env;
263 }
264 
272 {
273  g_assert(change);
274 
275  if (change->objtype)
276  return change->objtype;
277 
278  if (!change->objtype_name) {
279  OSyncObjFormat *format = osync_change_get_objformat(change);
280  if (!format)
281  return NULL;
282  change->objtype = format->objtype;
283  return format->objtype;
284  }
285 
286  osync_assert_msg(change->conv_env, "The conv env of the change must be set by calling member_set or conv_env_set");
287  change->objtype = osync_conv_find_objtype(change->conv_env, change->objtype_name);
288  return change->objtype;
289 }
290 
298 {
299  g_assert(change);
300  change->objtype = type;
301 }
302 
310 {
311  osync_trace(TRACE_ENTRY, "%s(%p, %s)", __func__, change, name);
312 
313  g_assert(change);
314  if (change->objtype_name)
315  g_free(change->objtype_name);
316  change->objtype_name = g_strdup(name);
317  //Invalidate the previous object type
318  change->objtype = NULL;
319 
320  osync_trace(TRACE_EXIT, "%s", __func__);
321 }
322 
330 {
331  osync_trace(TRACE_ENTRY, "%s(%p)", __func__, change);
332  g_assert(change);
333 
334  if (change->format) {
335  osync_trace(TRACE_EXIT, "%s: %p", __func__, change->format);
336  return change->format;
337  }
338 
339  if (!change->format_name) {
340  osync_trace(TRACE_EXIT, "%s: No name yet", __func__);
341  return NULL;
342  }
343 
344  osync_assert_msg(change->conv_env, "The conv env of the change must be set by calling member_set or conv_env_set");
345  change->format = osync_conv_find_objformat(change->conv_env, change->format_name);
346 
347  osync_trace(TRACE_EXIT, "%s: %p", __func__, change->format);
348  return change->format;
349 }
350 
358 {
359  g_assert(change);
360  change->format = objformat;
361  if (objformat)
362  change->objtype = objformat->objtype;
363  else
364  change->objtype = NULL;
365 }
366 
374 {
375  osync_trace(TRACE_ENTRY, "%s(%p, %s)", __func__, change, name);
376 
377  g_assert(change);
378  if (change->format_name)
379  g_free(change->format_name);
380  change->format_name = g_strdup(name);
381  //Invalidate the previous format
382  change->format = NULL;
383 
384  osync_trace(TRACE_EXIT, "%s", __func__);
385 }
386 
394 {
395  if (!change)
396  return CHANGE_UNKNOWN;
397 
398  return change->changetype;
399 }
400 
408 {
409  g_assert(change);
410  change->changetype = type;
411 }
412 
419 void osync_change_set_hash(OSyncChange *change, const char *hash)
420 {
421  g_assert(change);
422  if (change->hash)
423  g_free(change->hash);
424  change->hash = g_strdup(hash);
425 }
426 
433 const char *osync_change_get_hash(OSyncChange *change)
434 {
435  g_assert(change);
436  return change->hash;
437 }
438 
445 void osync_change_set_uid(OSyncChange *change, const char *uid)
446 {
447  g_assert(change);
448  if (change->uid)
449  g_free(change->uid);
450  change->uid = g_strdup(uid);
451 }
452 
459 const char *osync_change_get_uid(OSyncChange *change)
460 {
461  g_assert(change);
462  return change->uid;
463 }
464 
473 void osync_change_set_data(OSyncChange *change, char *data, int size, osync_bool has_data)
474 {
475  change->data = data;
476  change->size = size;
477  change->has_data = has_data;
478 }
479 
487 {
488  g_assert(change);
489  return change->has_data;
490 }
491 
499 {
500  g_assert(change);
501  return change->data;
502 }
503 
511 {
512  g_assert(change);
513  return change->size;
514 }
515 
523 {
524  g_assert(change);
525  return change->mappingid;
526 }
527 
534 void osync_change_set_mappingid(OSyncChange *change, long long int mappingid)
535 {
536  g_assert(change);
537  change->mappingid = mappingid;
538 }
539 
547 {
548  g_assert(change);
549  return change->engine_data;
550 }
551 
558 void osync_change_set_engine_data(OSyncChange *change, void *engine_data)
559 {
560  g_assert(change);
561  change->engine_data = engine_data;
562 }
563 
570 long long int osync_change_get_id(OSyncChange *change)
571 {
572  g_assert(change);
573  return change->id;
574 }
575 
588 {
589  osync_trace(TRACE_ENTRY, "osync_change_update(%p, %p)", source, target);
590  //FIXME free stuff
591  g_assert(source);
592  g_assert(target);
593  if (!target->uid)
594  target->uid = g_strdup(source->uid);
595  target->hash = g_strdup(source->hash);
596 
597  OSyncError *error = NULL;
598  if (!osync_change_copy_data(source, target, &error)) {
599  osync_trace(TRACE_INTERNAL, "unable to copy change: %s", osync_error_print(&error));
600  osync_error_free(&error);
601  }
602 
603  target->has_data = source->has_data;
604  target->changetype = source->changetype;
605  if (source->format)
606  target->format = osync_change_get_objformat(source);
607  if (source->objtype) {
608  target->objtype = osync_change_get_objtype(source);
609  target->sourceobjtype = g_strdup(osync_change_get_objtype(source)->name);
610  }
611 
612  target->changes_db = source->changes_db;
613 
614  osync_trace(TRACE_EXIT, "osync_change_update");
615 }
616 
void osync_change_update(OSyncChange *source, OSyncChange *target)
Updated one change from another change.
OSyncChangeType osync_change_get_changetype(OSyncChange *change)
Gets the changetype of a change.
void osync_change_set_objformat(OSyncChange *change, OSyncObjFormat *objformat)
Sets the object format of a change.
void osync_change_free(OSyncChange *change)
Frees a change.
void osync_change_set_mappingid(OSyncChange *change, long long int mappingid)
Sets the mappingid of a change.
void osync_change_set_data(OSyncChange *change, char *data, int size, osync_bool has_data)
Sets the data of a change.
int osync_change_get_datasize(OSyncChange *change)
Gets the size of the data of a change.
void osync_changes_close(OSyncGroup *group)
Closes the change database.
Represent an error.
void osync_change_ref(OSyncChange *change)
void osync_change_free_data(OSyncChange *change)
Frees the data of a change.
void osync_change_set_uid(OSyncChange *change, const char *uid)
Sets the uid of a change.
OSyncObjFormat * osync_conv_find_objformat(OSyncFormatEnv *env, const char *name)
Finds the object format with the given name.
OSyncObjFormat * osync_change_get_initial_objformat(OSyncChange *change)
long long int osync_change_get_mappingid(OSyncChange *change)
Gets the mappingid of a change.
OSyncObjType * objtype
void osync_change_decref(OSyncChange *change)
Represent a group of members that should be synchronized.
OSyncChange * osync_change_new(void)
Spawns a new change object.
void osync_change_set_engine_data(OSyncChange *change, void *engine_data)
Sets the data of the engine.
OSyncFormatEnv * conv_env
osync_bool osync_changes_load(OSyncGroup *group, OSyncChange ***changes, OSyncError **error)
This will load the changes from the database.
const char * osync_change_get_uid(OSyncChange *change)
Gets the uid of a change.
void osync_error_free(OSyncError **error)
Frees the error so it can be reused.
osync_bool osync_change_has_data(OSyncChange *change)
Returns wether the complete data already has been set.
A member of a group which represent a single device.
OSyncObjFormat * osync_change_get_objformat(OSyncChange *change)
Gets the object format of a change.
osync_bool osync_change_copy_data(OSyncChange *source, OSyncChange *target, OSyncError **error)
Copies the data from one change to another change.
void osync_change_set_changetype(OSyncChange *change, OSyncChangeType type)
Sets the changetype of a change.
OSyncObjType * osync_conv_find_objtype(OSyncFormatEnv *env, const char *name)
Finds the object type with the given name.
const char * osync_change_get_hash(OSyncChange *change)
Gets the hash of a change.
void osync_debug(const char *subpart, int level, const char *message,...)
Used for debugging.
long long int mappingid
OSyncObjFormat * format
long long int osync_change_get_id(OSyncChange *change)
Gets the id of the change which is always unique.
void osync_change_set_conv_env(OSyncChange *change, OSyncFormatEnv *env)
Sets the conversion environment of a change.
void osync_change_set_objformat_string(OSyncChange *change, const char *name)
Sets the object format of a change from the name.
OSyncMember * osync_change_get_member(OSyncChange *change)
Gets the member which reported a change.
A change object.
void osync_change_reset(OSyncChange *change)
Resets a change.
void osync_change_set_hash(OSyncChange *change, const char *hash)
Sets the hash of a change that is used to decide wether a change is new, modifed etc.
void osync_change_set_objtype(OSyncChange *change, OSyncObjType *type)
Sets the object type of a change.
void osync_change_set_member(OSyncChange *change, OSyncMember *member)
Sets the member of a change.
const char * osync_change_get_sourceobjtype(OSyncChange *change)
const char * osync_error_print(OSyncError **error)
Returns the message of the error.
void osync_trace(OSyncTraceType type, const char *message,...)
Used for tracing the application.
OSyncObjType * osync_change_get_objtype(OSyncChange *change)
Gets the object type of a change.
Represent a abstract object type (like "contact")
OSyncObjFormat * initial_format
osync_bool osync_change_delete(OSyncChange *change, OSyncError **error)
This will delete a change from the database.
void osync_change_set_objtype_string(OSyncChange *change, const char *name)
Sets the object type of a change from the name.
Represent a format for a object type.
OSyncChangeType
The changetypes of a change object.
Definition: opensync.h:28
The environment used for conversions.
void * osync_change_get_engine_data(OSyncChange *change)
Gets data that can be used privately by the engine.
OSyncChangeType changetype
char * osync_change_get_data(OSyncChange *change)
Gets the data of a change.
osync_bool osync_change_save(OSyncChange *change, osync_bool save_format, OSyncError **error)
This will save a change into the database.