• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List

opensync/opensync_change.c

00001 /*
00002  * libopensync - A synchronization framework
00003  * Copyright (C) 2004-2005  Armin Bauer <armin.bauer@opensync.org>
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00018  * 
00019  */
00020  
00021 #include "opensync.h"
00022 #include "opensync_internals.h"
00023 
00031 
00036 void osync_change_ref(OSyncChange *change)
00037 {
00038         g_assert(change);
00039         change->refcount++;
00040 }
00041 
00046 void osync_change_decref(OSyncChange *change)
00047 {
00048         g_assert(change);
00049         change->refcount--;
00050         if (change->refcount >= 0)
00051                 osync_change_free(change);
00052 }
00053 
00059 const char *osync_change_get_sourceobjtype(OSyncChange *change)
00060 {
00061         g_assert(change);
00062         return change->sourceobjtype;
00063 }
00064 
00070 OSyncObjFormat *osync_change_get_initial_objformat(OSyncChange *change)
00071 {
00072         g_assert(change);
00073         if (change->initial_format)
00074                 return change->initial_format;
00075         
00076         if (!change->initial_format)
00077                 return NULL;
00078         
00079         osync_assert_msg(change->conv_env, "The conv env of the change must be set by calling member_set or conv_env_set");
00080         change->initial_format = osync_conv_find_objformat(change->conv_env, change->initial_format_name);
00081         return change->initial_format;
00082 }
00083 
00093 
00099 OSyncChange *osync_change_new(void)
00100 {
00101         osync_trace(TRACE_ENTRY, "%s()", __func__);
00102         
00103         OSyncChange *change = g_malloc0(sizeof(OSyncChange));
00104         change->refcount = 1;
00105         
00106         osync_trace(TRACE_EXIT, "%s: %p", __func__, change);
00107         return change;
00108 }
00109 
00115 void osync_change_free(OSyncChange *change)
00116 {
00117         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, change);
00118         g_assert(change);
00119         
00120         //FIXME cleanly release the change!
00121         g_free(change);
00122 
00123         osync_trace(TRACE_EXIT, "%s", __func__);
00124 }
00125 
00134 void osync_change_free_data(OSyncChange *change)
00135 {
00136         g_assert(change);
00137         g_assert(osync_change_get_objformat(change));
00138         if (!osync_change_get_objformat(change)->destroy_func)
00139                 osync_debug("OSCONV", 1, "Memory leak: can't free data of type %s", osync_change_get_objformat(change)->name);
00140         else {
00141                 osync_debug("OSCONV", 4, "Freeing data of type %s", osync_change_get_objformat(change)->name);
00142                 osync_change_get_objformat(change)->destroy_func(change->data, change->size);
00143         }
00144         change->data = NULL;
00145         change->size = 0;
00146         //FIXME Set format to NULL here?
00147 }
00148 
00156 void osync_change_reset(OSyncChange *change)
00157 {
00158         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, change);
00159         
00160         if (change->hash)
00161                 g_free(change->hash);
00162         change->hash = NULL;
00163         //FIXME Release data
00164         change->data = NULL;
00165         change->size = 0;
00166         change->has_data = FALSE;
00167         change->changetype = CHANGE_UNKNOWN;
00168         //change->sourceobjtype = NULL;
00169         //change->destobjtype = NULL;
00170         
00171         osync_trace(TRACE_EXIT, "%s", __func__);
00172 }
00173 
00182 osync_bool osync_change_save(OSyncChange *change, osync_bool save_format, OSyncError **error)
00183 {
00184         if (!change->changes_db)
00185                 change->changes_db = change->member->group->changes_db;
00186         return osync_db_save_change(change, save_format, error);
00187 }
00188 
00196 osync_bool osync_change_delete(OSyncChange *change, OSyncError **error)
00197 {
00198         return osync_db_delete_change(change, error);
00199 }
00200 
00213 osync_bool osync_changes_load(OSyncGroup *group, OSyncChange ***changes, OSyncError **error)
00214 {
00215         return osync_db_open_changes(group, changes, error);
00216 }
00217 
00223 void osync_changes_close(OSyncGroup *group)
00224 {
00225         osync_db_close_changes(group);
00226 }
00227 
00234 OSyncMember *osync_change_get_member(OSyncChange *change)
00235 {
00236         g_assert(change);
00237         return change->member;
00238 }
00239 
00246 void osync_change_set_member(OSyncChange *change, OSyncMember *member)
00247 {
00248         g_assert(change);
00249         change->member = member;
00250         change->conv_env = member->group->conv_env;
00251 }
00252 
00259 void osync_change_set_conv_env(OSyncChange *change, OSyncFormatEnv *env)
00260 {
00261         g_assert(change);
00262         change->conv_env = env;
00263 }
00264 
00271 OSyncObjType *osync_change_get_objtype(OSyncChange *change)
00272 {
00273         g_assert(change);
00274         
00275         if (change->objtype)
00276                 return change->objtype;
00277         
00278         if (!change->objtype_name) {
00279                 OSyncObjFormat *format = osync_change_get_objformat(change);
00280                 if (!format)
00281                         return NULL;
00282                 change->objtype = format->objtype;
00283                 return format->objtype;
00284         }
00285         
00286         osync_assert_msg(change->conv_env, "The conv env of the change must be set by calling member_set or conv_env_set");
00287         change->objtype = osync_conv_find_objtype(change->conv_env, change->objtype_name);
00288         return change->objtype;
00289 }
00290 
00297 void osync_change_set_objtype(OSyncChange *change, OSyncObjType *type)
00298 {
00299         g_assert(change);
00300         change->objtype = type;
00301 }
00302 
00309 void osync_change_set_objtype_string(OSyncChange *change, const char *name)
00310 {
00311         osync_trace(TRACE_ENTRY, "%s(%p, %s)", __func__, change, name);
00312         
00313         g_assert(change);
00314         if (change->objtype_name)
00315                 g_free(change->objtype_name);
00316         change->objtype_name = g_strdup(name);
00317         //Invalidate the previous object type
00318         change->objtype = NULL;
00319         
00320         osync_trace(TRACE_EXIT, "%s", __func__);
00321 }
00322 
00329 OSyncObjFormat *osync_change_get_objformat(OSyncChange *change)
00330 {
00331         osync_trace(TRACE_ENTRY, "%s(%p)", __func__, change);
00332         g_assert(change);
00333         
00334         if (change->format) {
00335                 osync_trace(TRACE_EXIT, "%s: %p", __func__, change->format);
00336                 return change->format;
00337         }
00338         
00339         if (!change->format_name) {
00340                 osync_trace(TRACE_EXIT, "%s: No name yet", __func__);
00341                 return NULL;
00342         }
00343         
00344         osync_assert_msg(change->conv_env, "The conv env of the change must be set by calling member_set or conv_env_set");
00345         change->format = osync_conv_find_objformat(change->conv_env, change->format_name);
00346         
00347         osync_trace(TRACE_EXIT, "%s: %p", __func__, change->format);
00348         return change->format;
00349 }
00350 
00357 void osync_change_set_objformat(OSyncChange *change, OSyncObjFormat *objformat)
00358 {
00359         g_assert(change);
00360         change->format = objformat;
00361         if (objformat)
00362                 change->objtype = objformat->objtype;
00363         else
00364                 change->objtype = NULL;
00365 }
00366 
00373 void osync_change_set_objformat_string(OSyncChange *change, const char *name)
00374 {
00375         osync_trace(TRACE_ENTRY, "%s(%p, %s)", __func__, change, name);
00376         
00377         g_assert(change);
00378         if (change->format_name)
00379                 g_free(change->format_name);
00380         change->format_name = g_strdup(name);
00381         //Invalidate the previous format
00382         change->format = NULL;
00383         
00384         osync_trace(TRACE_EXIT, "%s", __func__);
00385 }
00386 
00393 OSyncChangeType osync_change_get_changetype(OSyncChange *change)
00394 {
00395         if (!change)
00396                 return CHANGE_UNKNOWN;
00397         
00398         return change->changetype;
00399 }
00400 
00407 void osync_change_set_changetype(OSyncChange *change, OSyncChangeType type)
00408 {
00409         g_assert(change);
00410         change->changetype = type;
00411 }
00412 
00419 void osync_change_set_hash(OSyncChange *change, const char *hash)
00420 {
00421         g_assert(change);
00422         if (change->hash)
00423                 g_free(change->hash);
00424         change->hash = g_strdup(hash);
00425 }
00426 
00433 const char *osync_change_get_hash(OSyncChange *change)
00434 {
00435         g_assert(change);
00436         return change->hash;
00437 }
00438 
00445 void osync_change_set_uid(OSyncChange *change, const char *uid)
00446 {
00447         g_assert(change);
00448         if (change->uid)
00449                 g_free(change->uid);
00450         change->uid = g_strdup(uid);
00451 }
00452 
00459 const char *osync_change_get_uid(OSyncChange *change)
00460 {
00461         g_assert(change);
00462         return change->uid;
00463 }
00464 
00473 void osync_change_set_data(OSyncChange *change, char *data, int size, osync_bool has_data)
00474 {
00475         change->data = data;
00476         change->size = size;
00477         change->has_data = has_data;
00478 }
00479 
00486 osync_bool osync_change_has_data(OSyncChange *change)
00487 {
00488         g_assert(change);
00489         return change->has_data;
00490 }
00491 
00498 char *osync_change_get_data(OSyncChange *change)
00499 {
00500         g_assert(change);
00501         return change->data;
00502 }
00503 
00510 int osync_change_get_datasize(OSyncChange *change)
00511 {
00512         g_assert(change);
00513         return change->size;
00514 }
00515 
00522 long long int osync_change_get_mappingid(OSyncChange *change)
00523 {
00524         g_assert(change);
00525         return change->mappingid;
00526 }
00527 
00534 void osync_change_set_mappingid(OSyncChange *change, long long int mappingid)
00535 {
00536         g_assert(change);
00537         change->mappingid = mappingid;
00538 }
00539 
00546 void *osync_change_get_engine_data(OSyncChange *change)
00547 {
00548         g_assert(change);
00549         return change->engine_data;
00550 }
00551 
00558 void osync_change_set_engine_data(OSyncChange *change, void *engine_data)
00559 {
00560         g_assert(change);
00561         change->engine_data = engine_data;
00562 }
00563 
00570 long long int osync_change_get_id(OSyncChange *change)
00571 {
00572         g_assert(change);
00573         return change->id;
00574 }
00575 
00587 void osync_change_update(OSyncChange *source, OSyncChange *target)
00588 {
00589         osync_trace(TRACE_ENTRY, "osync_change_update(%p, %p)", source, target);
00590         //FIXME free stuff
00591         g_assert(source);
00592         g_assert(target);
00593         if (!target->uid)
00594                 target->uid = g_strdup(source->uid);
00595         target->hash = g_strdup(source->hash);
00596         
00597         OSyncError *error = NULL;
00598         if (!osync_change_copy_data(source, target, &error)) {
00599                 osync_trace(TRACE_INTERNAL, "unable to copy change: %s", osync_error_print(&error));
00600                 osync_error_free(&error);
00601         }
00602         
00603         target->has_data = source->has_data;
00604         target->changetype = source->changetype;
00605         if (source->format)
00606                 target->format = osync_change_get_objformat(source);
00607         if (source->objtype) {
00608                 target->objtype = osync_change_get_objtype(source);
00609                 target->sourceobjtype = g_strdup(osync_change_get_objtype(source)->name);
00610         }
00611         
00612         target->changes_db = source->changes_db;
00613         
00614         osync_trace(TRACE_EXIT, "osync_change_update");
00615 }
00616 

Generated on Fri Sep 17 2010 for OpenSync by  doxygen 1.7.1